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

fix(API): Update the functionality by which slugs are generated for entities #419

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions apps/api/src/api-key/service/api-key.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class ApiKeyService {
const apiKeyId = apiKey.id

if (!apiKey) {
throw new NotFoundException(`API key with id ${apiKeyId} not found`)
throw new NotFoundException(`API key ${apiKeySlug} not found`)
}

const updatedApiKey = await this.prisma.apiKey.update({
Expand Down Expand Up @@ -137,7 +137,7 @@ export class ApiKeyService {
}
})
} catch (error) {
throw new NotFoundException(`API key with id ${apiKeySlug} not found`)
throw new NotFoundException(`API key ${apiKeySlug} not found`)
}

this.logger.log(`User ${user.id} deleted API key ${apiKeySlug}`)
Expand All @@ -161,7 +161,7 @@ export class ApiKeyService {
})

if (!apiKey) {
throw new NotFoundException(`API key with id ${apiKeySlug} not found`)
throw new NotFoundException(`API key ${apiKeySlug} not found`)
}

return apiKey
Expand Down
15 changes: 12 additions & 3 deletions apps/api/src/common/slug-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Workspace } from '@prisma/client'
* @param name The name of the entity.
* @returns A unique slug for the given entity.
*/
const generateSlug = (name: string): string => {
const generateSlug = (name: string,counter:number): string => {
// Convert to lowercase
const lowerCaseName = name.trim().toLowerCase()

Expand All @@ -20,7 +20,7 @@ const generateSlug = (name: string): string => {

// Append the name with 5 alphanumeric characters
const slug =
alphanumericName + '-' + Math.random().toString(36).substring(2, 7)
alphanumericName + '-' + counter.toString(36)
return slug
}

Expand Down Expand Up @@ -102,46 +102,55 @@ export default async function generateEntitySlug(
| 'API_KEY',
prisma: PrismaService
): Promise<string> {
let counter=0
while (true) {
const slug = generateSlug(name)
const slug = generateSlug(name,counter)
switch (entityType) {
case 'WORKSPACE_ROLE':
if (await checkWorkspaceRoleSlugExists(slug, prisma)) {
counter++
continue
}
return slug
case 'WORKSPACE':
if (await checkWorkspaceSlugExists(slug, prisma)) {
counter++
continue
}
return slug
case 'PROJECT':
if (await checkProjectSlugExists(slug, prisma)) {
counter++
continue
}
return slug
case 'VARIABLE':
if (await checkVariableSlugExists(slug, prisma)) {
counter++
continue
}
return slug
case 'SECRET':
if (await checkSecretSlugExists(slug, prisma)) {
counter++
continue
}
return slug
case 'INTEGRATION':
if (await checkIntegrationSlugExists(slug, prisma)) {
counter++
continue
}
return slug
case 'ENVIRONMENT':
if (await checkEnvironmentSlugExists(slug, prisma)) {
counter++
continue
}
return slug
case 'API_KEY':
if (await checkApiKeySlugExists(slug, prisma)) {
counter++
continue
}
return slug
Expand Down
Loading