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
@@ -7,6 +7,7 @@ import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { BusinessCategoryEntity } from '../../entities/business-category.entity';
import { ServiceTemplateEntity } from '../../entities/service-template.entity';
import { resolveUniqueSlug, normalizeSlugInput } from '../../common/utils/slug.util';
import { CreateServiceTemplateDto } from './dto/create-service-template.dto';
import { UpdateServiceTemplateDto } from './dto/update-service-template.dto';
@@ -57,16 +58,27 @@ export class ServiceTemplatesService {
}
async create(p: CreateServiceTemplateDto): Promise<ServiceTemplateEntity> {
const slugTaken = await this.repo.findOne({ where: { slug: p.slug } });
if (slugTaken) {
throw new ConflictException('Slug already exists');
const explicitSlug = normalizeSlugInput(p.slug);
if (explicitSlug) {
const taken = await this.repo.findOne({ where: { slug: explicitSlug } });
if (taken) {
throw new ConflictException('Slug already exists');
}
}
const slug = await resolveUniqueSlug(
p.title,
async (candidate) =>
Boolean(await this.repo.findOne({ where: { slug: candidate } })),
p.slug,
'service',
);
await this.ensureCategoryExists(p.categoryId);
const entity = this.repo.create({
title: p.title,
slug: p.slug,
slug,
categoryId: p.categoryId,
description: p.description ?? null,
defaultDurationMinutes: p.defaultDurationMinutes ?? 30,