mirror of
https://github.com/docmost/docmost.git
synced 2025-11-13 13:32:38 +10:00
space slug
This commit is contained in:
@ -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 { CreateSpaceDto } from '../dto/create-space.dto';
|
||||||
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
|
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
|
||||||
import { PaginationMetaDto } from '../../../helpers/pagination/pagination-meta-dto';
|
import { PaginationMetaDto } from '../../../helpers/pagination/pagination-meta-dto';
|
||||||
@ -6,7 +10,6 @@ import { PaginatedResult } from '../../../helpers/pagination/paginated-result';
|
|||||||
import slugify from 'slugify';
|
import slugify from 'slugify';
|
||||||
import { SpaceRepo } from '@docmost/db/repos/space/space.repo';
|
import { SpaceRepo } from '@docmost/db/repos/space/space.repo';
|
||||||
import { KyselyTransaction } from '@docmost/db/types/kysely.types';
|
import { KyselyTransaction } from '@docmost/db/types/kysely.types';
|
||||||
import { getRandomInt } from '../../../helpers/utils';
|
|
||||||
import { Space } from '@docmost/db/types/entity.types';
|
import { Space } from '@docmost/db/types/entity.types';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@ -19,11 +22,15 @@ export class SpaceService {
|
|||||||
createSpaceDto: CreateSpaceDto,
|
createSpaceDto: CreateSpaceDto,
|
||||||
trx?: KyselyTransaction,
|
trx?: KyselyTransaction,
|
||||||
): Promise<Space> {
|
): Promise<Space> {
|
||||||
// until we allow slug in dto
|
const slug = slugify(
|
||||||
let slug = slugify(createSpaceDto.name.toLowerCase());
|
createSpaceDto?.slug?.toLowerCase() ?? createSpaceDto.name.toLowerCase(),
|
||||||
const slugExists = await this.spaceRepo.slugExists(slug, workspaceId);
|
);
|
||||||
|
|
||||||
|
const slugExists = await this.spaceRepo.slugExists(slug, workspaceId, trx);
|
||||||
if (slugExists) {
|
if (slugExists) {
|
||||||
slug = `${slug}-${getRandomInt()}`;
|
throw new BadRequestException(
|
||||||
|
'Slug exist. Please use a unique space slug',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return await this.spaceRepo.insertSpace(
|
return await this.spaceRepo.insertSpace(
|
||||||
|
|||||||
@ -32,16 +32,25 @@ export class SpaceRepo {
|
|||||||
.executeTakeFirst();
|
.executeTakeFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
async slugExists(slug: string, workspaceId: string): Promise<boolean> {
|
async slugExists(
|
||||||
let { count } = await this.db
|
slug: string,
|
||||||
.selectFrom('spaces')
|
workspaceId: string,
|
||||||
.select((eb) => eb.fn.count('id').as('count'))
|
trx?: KyselyTransaction,
|
||||||
.where(sql`LOWER(slug)`, '=', sql`LOWER(${slug})`)
|
): Promise<boolean> {
|
||||||
.where('workspaceId', '=', workspaceId)
|
return executeTx(
|
||||||
.executeTakeFirst();
|
this.db,
|
||||||
count = count as number;
|
async (trx) => {
|
||||||
|
let { count } = await trx
|
||||||
return !!count;
|
.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(
|
async updateSpace(
|
||||||
|
|||||||
Reference in New Issue
Block a user