Эх сурвалжийг харах

feat(channel): add duplicate channelId validation in create and update methods

Dave 2 сар өмнө
parent
commit
ec0c18ec6f

+ 0 - 16
apps/box-mgnt-api/src/mgnt-backend/feature/ads/ads.dto.ts

@@ -24,13 +24,6 @@ export class AdsDto {
   id: string;
 
   @ApiProperty({
-    description: '渠道ID (Mongo ObjectId)',
-    example: '664f9b5b8e4ff3f4c0c00001',
-  })
-  @IsMongoId()
-  channelId: string;
-
-  @ApiProperty({
     description: '广告模块 (banner/startup/轮播等)',
     example: 'banner',
   })
@@ -120,10 +113,6 @@ export class AdsDto {
 
 // ---- Create ----
 export class CreateAdsDto {
-  @ApiProperty({ description: '渠道ID (ObjectId)' })
-  @IsMongoId()
-  channelId: string;
-
   @ApiProperty({ description: '广告模块 (banner/startup/轮播等)' })
   @IsString()
   @Length(1, 50)
@@ -219,11 +208,6 @@ export class ListAdsDto extends PageListDto {
   @Transform(({ value }) => (typeof value === 'string' ? value.trim() : value))
   adsModuleId?: string;
 
-  @ApiPropertyOptional({ description: '渠道ID (ObjectId)' })
-  @IsOptional()
-  @IsMongoId()
-  channelId?: string;
-
   @ApiPropertyOptional({
     enum: CommonStatus,
     description: '状态: 0=禁用, 1=启用',

+ 0 - 2
apps/box-mgnt-api/src/mgnt-backend/feature/ads/ads.service.ts

@@ -63,7 +63,6 @@ export class AdsService {
   }
 
   async create(dto: CreateAdsDto) {
-    await this.assertChannelExists(dto.channelId);
     this.ensureTimeRange(dto.startDt, dto.expiryDt);
 
     const now = this.now();
@@ -93,7 +92,6 @@ export class AdsService {
   }
 
   async update(dto: UpdateAdsDto) {
-    await this.assertChannelExists(dto.channelId);
     this.ensureTimeRange(dto.startDt, dto.expiryDt);
 
     const now = this.now();

+ 26 - 0
apps/box-mgnt-api/src/mgnt-backend/feature/channel/channel.service.ts

@@ -52,6 +52,17 @@ export class ChannelService {
       );
     }
 
+    // Check for duplicate channelId
+    const existingChannelId = await this.mongoPrismaService.channel.findFirst({
+      where: { channelId: dto.channelId },
+    });
+
+    if (existingChannelId) {
+      throw new BadRequestException(
+        `Channel with channelId "${dto.channelId}" already exists`,
+      );
+    }
+
     const now = this.now();
 
     const channel = await this.mongoPrismaService.channel.create({
@@ -96,12 +107,27 @@ export class ChannelService {
       );
     }
 
+    // check for duplicate channelId (excluding current channel)
+    const duplicateChannelId = await this.mongoPrismaService.channel.findFirst({
+      where: {
+        channelId: dto.channelId,
+        id: { not: dto.id },
+      },
+    });
+
+    if (duplicateChannelId) {
+      throw new BadRequestException(
+        `Channel with channelId "${dto.channelId}" already exists`,
+      );
+    }
+
     const now = this.now();
 
     try {
       const channel = await this.mongoPrismaService.channel.update({
         where: { id: dto.id },
         data: {
+          channelId: dto.channelId,
           name: dto.name,
           landingUrl: dto.landingUrl,
           videoCdn: this.trimOptional(dto.videoCdn) ?? null,