Make slug optional on create and auto-generate from title when omitted
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user