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