feat: add optional slug field to UpdateSectionDto and update service to support manual slug overrides

This commit is contained in:
pradeepkumar
2026-04-11 23:05:56 +05:30
parent bebff19052
commit 91336e5ead
2 changed files with 28 additions and 9 deletions

View File

@@ -1,4 +1,18 @@
import { PartialType } from '@nestjs/swagger';
import { PartialType, ApiPropertyOptional } from '@nestjs/swagger';
import { IsOptional, IsString, Matches, MaxLength, MinLength } from 'class-validator';
import { CreateSectionDto } from './create-section.dto';
export class UpdateSectionDto extends PartialType(CreateSectionDto) {}
export class UpdateSectionDto extends PartialType(CreateSectionDto) {
@ApiPropertyOptional({
description: 'URL-friendly slug (lowercase letters, digits, hyphens). Auto-regenerated from name if omitted.',
example: 'basic-information',
})
@IsOptional()
@IsString()
@MinLength(2)
@MaxLength(100)
@Matches(/^[a-z0-9]+(?:-[a-z0-9]+)*$/, {
message: 'slug must contain only lowercase letters, digits, and hyphens (no leading/trailing/consecutive hyphens)',
})
slug?: string;
}

View File

@@ -108,22 +108,27 @@ export class ProfileSectionsService {
const data: any = { ...updateSectionDto };
// If name is being updated, update the slug too
if (updateSectionDto.name) {
const newSlug = this.generateSlug(updateSectionDto.name);
// Resolve effective slug: explicit slug from DTO wins; otherwise regenerate from name if name changed
let effectiveSlug: string | undefined;
if (updateSectionDto.slug) {
effectiveSlug = updateSectionDto.slug;
} else if (updateSectionDto.name) {
effectiveSlug = this.generateSlug(updateSectionDto.name);
}
// Check if new slug conflicts with another section
if (effectiveSlug) {
// Check if slug conflicts with another section
const existing = await this.prisma.profileSection.findFirst({
where: {
AND: [{ id: { not: id } }, { slug: newSlug }],
AND: [{ id: { not: id } }, { slug: effectiveSlug }],
},
});
if (existing) {
throw new ConflictException('A section with this name already exists');
throw new ConflictException('A section with this slug already exists');
}
data.slug = newSlug;
data.slug = effectiveSlug;
}
return this.prisma.profileSection.update({