From 4ce7676a7757d11d3f0f5c0f7358ded389826ebe Mon Sep 17 00:00:00 2001 From: Philipinho <16838612+Philipinho@users.noreply.github.com> Date: Fri, 29 Mar 2024 17:44:25 +0000 Subject: [PATCH] space slug --- .../src/core/space/services/space.service.ts | 19 ++++++++---- .../src/kysely/repos/space/space.repo.ts | 29 ++++++++++++------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/apps/server/src/core/space/services/space.service.ts b/apps/server/src/core/space/services/space.service.ts index 4f7d5162..d83c07a4 100644 --- a/apps/server/src/core/space/services/space.service.ts +++ b/apps/server/src/core/space/services/space.service.ts @@ -1,4 +1,8 @@ -import { Injectable, NotFoundException } from '@nestjs/common'; +import { + BadRequestException, + Injectable, + NotFoundException, +} from '@nestjs/common'; import { CreateSpaceDto } from '../dto/create-space.dto'; import { PaginationOptions } from '../../../helpers/pagination/pagination-options'; import { PaginationMetaDto } from '../../../helpers/pagination/pagination-meta-dto'; @@ -6,7 +10,6 @@ import { PaginatedResult } from '../../../helpers/pagination/paginated-result'; import slugify from 'slugify'; import { SpaceRepo } from '@docmost/db/repos/space/space.repo'; import { KyselyTransaction } from '@docmost/db/types/kysely.types'; -import { getRandomInt } from '../../../helpers/utils'; import { Space } from '@docmost/db/types/entity.types'; @Injectable() @@ -19,11 +22,15 @@ export class SpaceService { createSpaceDto: CreateSpaceDto, trx?: KyselyTransaction, ): Promise { - // until we allow slug in dto - let slug = slugify(createSpaceDto.name.toLowerCase()); - const slugExists = await this.spaceRepo.slugExists(slug, workspaceId); + const slug = slugify( + createSpaceDto?.slug?.toLowerCase() ?? createSpaceDto.name.toLowerCase(), + ); + + const slugExists = await this.spaceRepo.slugExists(slug, workspaceId, trx); if (slugExists) { - slug = `${slug}-${getRandomInt()}`; + throw new BadRequestException( + 'Slug exist. Please use a unique space slug', + ); } return await this.spaceRepo.insertSpace( diff --git a/apps/server/src/kysely/repos/space/space.repo.ts b/apps/server/src/kysely/repos/space/space.repo.ts index f7eb2981..35938886 100644 --- a/apps/server/src/kysely/repos/space/space.repo.ts +++ b/apps/server/src/kysely/repos/space/space.repo.ts @@ -32,16 +32,25 @@ export class SpaceRepo { .executeTakeFirst(); } - async slugExists(slug: string, workspaceId: string): Promise { - let { count } = await this.db - .selectFrom('spaces') - .select((eb) => eb.fn.count('id').as('count')) - .where(sql`LOWER(slug)`, '=', sql`LOWER(${slug})`) - .where('workspaceId', '=', workspaceId) - .executeTakeFirst(); - count = count as number; - - return !!count; + async slugExists( + slug: string, + workspaceId: string, + trx?: KyselyTransaction, + ): Promise { + return executeTx( + this.db, + async (trx) => { + let { count } = await trx + .selectFrom('spaces') + .select((eb) => eb.fn.count('id').as('count')) + .where(sql`LOWER(slug)`, '=', sql`LOWER(${slug})`) + .where('workspaceId', '=', workspaceId) + .executeTakeFirst(); + count = count as number; + return count == 0 ? false : true; + }, + trx, + ); } async updateSpace(