diff --git a/src/profile-fields/dto/update-field.dto.ts b/src/profile-fields/dto/update-field.dto.ts index 424c1bc..b2647c1 100644 --- a/src/profile-fields/dto/update-field.dto.ts +++ b/src/profile-fields/dto/update-field.dto.ts @@ -1,4 +1,10 @@ import { PartialType, OmitType } from '@nestjs/swagger'; +import { IsOptional, IsString, Matches } from 'class-validator'; import { CreateFieldDto } from './create-field.dto'; -export class UpdateFieldDto extends PartialType(OmitType(CreateFieldDto, ['sectionId'] as const)) {} +export class UpdateFieldDto extends PartialType(OmitType(CreateFieldDto, ['sectionId'] as const)) { + @IsOptional() + @IsString() + @Matches(/^[a-z0-9_]+$/, { message: 'Slug must contain only lowercase letters, numbers, and underscores' }) + slug?: string; +} diff --git a/src/profile-fields/profile-fields.service.ts b/src/profile-fields/profile-fields.service.ts index 12f6d24..1c4eb52 100644 --- a/src/profile-fields/profile-fields.service.ts +++ b/src/profile-fields/profile-fields.service.ts @@ -134,11 +134,24 @@ export class ProfileFieldsService { const data: any = { ...updateFieldDto }; - // If name is being updated, update the slug too - if (updateFieldDto.name) { - const newSlug = this.generateSlug(updateFieldDto.name); - - // Check if new slug conflicts within the section + // If slug is explicitly provided, use it; otherwise auto-generate from name + if (updateFieldDto.slug) { + const newSlug = updateFieldDto.slug; + const existing = await this.prisma.profileField.findFirst({ + where: { + AND: [ + { id: { not: id } }, + { sectionId: field.sectionId }, + { slug: newSlug }, + ], + }, + }); + if (existing) { + throw new ConflictException('A field with this slug already exists in this section'); + } + data.slug = newSlug; + } else if (updateFieldDto.name) { + const newSlug = this.generateSlug(updateFieldDto.name); const existing = await this.prisma.profileField.findFirst({ where: { AND: [ @@ -148,11 +161,9 @@ export class ProfileFieldsService { ], }, }); - if (existing) { throw new ConflictException('A field with this name already exists in this section'); } - data.slug = newSlug; }