Make slug optional on create and auto-generate from title when omitted

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-05-24 15:58:56 +03:30
co-authored by Cursor
parent aa990c3204
commit 1522ff62f5
9 changed files with 118 additions and 23 deletions
+16 -4
View File
@@ -8,6 +8,7 @@ import {
import { InjectRepository } from '@nestjs/typeorm';
import { DataSource, Repository } from 'typeorm';
import { deleteBusinessCascade } from '../../common/utils/business-cascade.util';
import { resolveUniqueSlug, normalizeSlugInput } from '../../common/utils/slug.util';
import { BusinessImagesService } from './business-images.service';
import { BusinessCategoryEntity } from '../../entities/business-category.entity';
import { BusinessEntity } from '../../entities/business.entity';
@@ -79,16 +80,27 @@ export class BusinessesService {
}
async create(payload: CreateBusinessDto): Promise<BusinessEntity> {
const slugTaken = await this.repo.findOne({ where: { slug: payload.slug } });
if (slugTaken) {
throw new ConflictException('Slug already exists');
const explicitSlug = normalizeSlugInput(payload.slug);
if (explicitSlug) {
const taken = await this.repo.findOne({ where: { slug: explicitSlug } });
if (taken) {
throw new ConflictException('Slug already exists');
}
}
const slug = await resolveUniqueSlug(
payload.title,
async (candidate) =>
Boolean(await this.repo.findOne({ where: { slug: candidate } })),
payload.slug,
'business',
);
await this.ensureCategoryExists(payload.categoryId);
const entity = this.repo.create({
title: payload.title,
slug: payload.slug,
slug,
categoryId: payload.categoryId,
description: payload.description ?? null,
contactPhone: payload.contactPhone ?? null,
@@ -16,13 +16,14 @@ export class CreateBusinessDto {
@MaxLength(150)
title!: string;
@ApiProperty({ example: 'shine-carwash' })
@ApiPropertyOptional({ example: 'shine-carwash' })
@IsOptional()
@IsString()
@MaxLength(120)
@Matches(/^[a-z0-9]+(?:-[a-z0-9]+)*$/, {
message: 'slug must be lowercase words separated by hyphens',
})
slug!: string;
slug?: string;
@ApiProperty({ format: 'uuid' })
@IsUUID()