Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): Update slug generation method #420

Merged
merged 15 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions apps/api/src/common/slug-generator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { PrismaService } from '@/prisma/prisma.service'
import generateEntitySlug, {
generateSlugName,
incrementSlugSuffix
} from './slug-generator'
import { mockDeep } from 'jest-mock-extended'

describe('generateEntitySlug', () => {
let prisma

beforeEach(() => {
prisma = mockDeep<PrismaService>()
})

describe('generateSlugName', () => {
it('should convert name to slug format', () => {
expect(generateSlugName('Hello World')).toBe('hello-world')
expect(generateSlugName('Entity with 123')).toBe('entity-with-123')
expect(generateSlugName('Special #Name!')).toBe('special--name-')
Nil2000 marked this conversation as resolved.
Show resolved Hide resolved
})
})

describe('incrementSlugSuffix', () => {
it('should return base slug with `-0` when no suffix is found', () => {
const result = incrementSlugSuffix('', 'my-slug')
expect(result).toBe('my-slug-0')
})

it('should increment suffix when found', () => {
const result = incrementSlugSuffix('my-slug-0', 'my-slug')
expect(result).toBe('my-slug-1')
})

it('should handle complex increment cases with carryover', () => {
const result = incrementSlugSuffix('my-slug-z', 'my-slug')
expect(result).toBe('my-slug-00')
rajdip-b marked this conversation as resolved.
Show resolved Hide resolved
})
})

describe('generateEntitySlug for each entity type', () => {
it('should generate a unique slug for WORKSPACE_ROLE', async () => {
prisma.workspaceRole.findMany.mockResolvedValue([
{
slug: 'workspace-role-0'
}
])

const slug = await generateEntitySlug(
'Workspace Role',
'WORKSPACE_ROLE',
prisma
)
expect(slug).toBe('workspace-role-1')
})

it('should generate a unique slug for WORKSPACE', async () => {
prisma.workspace.findMany.mockResolvedValue([])

const slug = await generateEntitySlug('Workspace', 'WORKSPACE', prisma)
expect(slug).toBe('workspace-0')
})

it('should generate a unique slug for PROJECT', async () => {
prisma.project.findMany.mockResolvedValue([{ slug: 'project-z' }])

const slug = await generateEntitySlug('Project', 'PROJECT', prisma)
expect(slug).toBe('project-00')
})

it('should generate a unique slug for VARIABLE', async () => {
prisma.variable.findMany.mockResolvedValue([{ slug: 'variable-az' }])

const slug = await generateEntitySlug('Variable', 'VARIABLE', prisma)
expect(slug).toBe('variable-b0')
})

it('should generate a unique slug for SECRET', async () => {
prisma.secret.findMany.mockResolvedValue([{ slug: 'secret-9' }])

const slug = await generateEntitySlug('Secret', 'SECRET', prisma)
expect(slug).toBe('secret-a')
})

it('should generate a unique slug for INTEGRATION', async () => {
prisma.integration.findMany.mockResolvedValue([{ slug: 'integration-b' }])

const slug = await generateEntitySlug(
'Integration',
'INTEGRATION',
prisma
)
expect(slug).toBe('integration-c')
})

it('should generate a unique slug for ENVIRONMENT', async () => {
prisma.environment.findMany.mockResolvedValue([
{ slug: 'environment-zz' }
])

const slug = await generateEntitySlug(
'Environment',
'ENVIRONMENT',
prisma
)
expect(slug).toBe('environment-000')
})

it('should generate a unique slug for API_KEY', async () => {
prisma.apiKey.findMany.mockResolvedValue([{ slug: 'api--key-09' }])

const slug = await generateEntitySlug('Api @Key', 'API_KEY', prisma)
expect(slug).toBe('api--key-0a')
})
})
})
31 changes: 29 additions & 2 deletions apps/api/src/common/slug-generator.ts
rajdip-b marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { PrismaService } from '@/prisma/prisma.service'
import { Workspace } from '@prisma/client'

const incrementSlugSuffix = (foundSlug: string, baseSlug: string): string => {
export const incrementSlugSuffix = (
foundSlug: string,
Nil2000 marked this conversation as resolved.
Show resolved Hide resolved
baseSlug: string
): string => {
const charset = '0123456789abcdefghijklmnopqrstuvwxyz'

let suffix = ''
Expand Down Expand Up @@ -50,7 +53,7 @@ const incrementSlugSuffix = (foundSlug: string, baseSlug: string): string => {
* @param name The name of the entity.
* @returns A alphanumeric slug for the given name.
*/
const generateSlugName = (name: string): string => {
export const generateSlugName = (name: string): string => {
// Convert to lowercase
const lowerCaseName = name.trim().toLowerCase()

Expand All @@ -73,6 +76,9 @@ const getWorkspaceRoleIfSlugExists = async (
startsWith: `${slug}-`
}
},
select: {
slug: true
},
orderBy: {
slug: 'desc'
},
Expand All @@ -91,6 +97,9 @@ const getWorkspaceSlugExists = async (
startsWith: `${slug}-`
}
},
select: {
slug: true
},
orderBy: {
slug: 'desc'
},
Expand All @@ -109,6 +118,9 @@ const getProjectSlugExists = async (
startsWith: `${slug}-`
}
},
select: {
slug: true
},
orderBy: {
slug: 'desc'
},
Expand All @@ -127,6 +139,9 @@ const getVariableSlugExists = async (
startsWith: `${slug}-`
}
},
select: {
slug: true
},
orderBy: {
slug: 'desc'
},
Expand All @@ -145,6 +160,9 @@ const getSecretSlugExists = async (
startsWith: `${slug}-`
}
},
select: {
slug: true
},
orderBy: {
slug: 'desc'
},
Expand All @@ -163,6 +181,9 @@ const getIntegrationSlugExists = async (
startsWith: `${slug}-`
}
},
select: {
slug: true
},
orderBy: {
slug: 'desc'
},
Expand All @@ -181,6 +202,9 @@ const getEnvironmentSlugExists = async (
startsWith: `${slug}-`
}
},
select: {
slug: true
},
orderBy: {
slug: 'desc'
},
Expand All @@ -199,6 +223,9 @@ const getApiKeySlugExists = async (
startsWith: `${slug}-`
}
},
select: {
slug: true
},
orderBy: {
slug: 'desc'
},
Expand Down
Loading