Browse Source

feat(ads): add endpoint and service method to update ads status

Dave 1 tháng trước cách đây
mục cha
commit
a8975bf1d9

+ 16 - 1
apps/box-mgnt-api/src/mgnt-backend/feature/ads/ads.controller.ts

@@ -20,7 +20,13 @@ import {
   ApiTags,
 } from '@nestjs/swagger';
 import type { FastifyRequest } from 'fastify';
-import { CreateAdsDto, ListAdsDto, UpdateAdsDto, AdsDto } from './ads.dto';
+import {
+  CreateAdsDto,
+  ListAdsDto,
+  UpdateAdsDto,
+  AdsDto,
+  AdsStatusDto,
+} from './ads.dto';
 import { AdsService } from './ads.service';
 import { MongoIdParamDto } from '../common/mongo-id.dto';
 
@@ -131,4 +137,13 @@ export class AdsController {
   listAdsModules() {
     return this.service.listAdsModules();
   }
+
+  // post to update ads status
+  @Post('ads-status')
+  @ApiOperation({ summary: 'Update ads status (active/inactive)' })
+  @ApiBody({ type: AdsStatusDto })
+  @ApiResponse({ status: 200, type: AdsDto })
+  updateAdsStatus(@Body() dto: AdsStatusDto) {
+    return this.service.updateStatus(dto);
+  }
 }

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

@@ -283,3 +283,19 @@ export interface AdsInterfaceDto {
   createAt: bigint;
   updateAt: bigint;
 }
+
+export class AdsStatusDto {
+  @ApiProperty({
+    description: '广告ID (Mongo ObjectId)',
+    example: '664f9b5b8e4ff3f4c0c12345',
+  })
+  @IsMongoId()
+  id: string;
+  @ApiProperty({
+    description: '状态: 0=禁用, 1=启用',
+    example: CommonStatus.enabled,
+  })
+  @Type(() => Number)
+  @IsEnum(CommonStatus)
+  status: CommonStatus;
+}

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

@@ -15,6 +15,7 @@ import {
   ListAdsDto,
   UpdateAdsDto,
   AdsInterfaceDto,
+  AdsStatusDto,
 } from './ads.dto';
 import { CommonStatus } from '../common/status.enum';
 import { ImageUrlBuilderService } from './image/image-url-builder.service';
@@ -291,6 +292,32 @@ export class AdsService {
     return this.mapToDto(row);
   }
 
+  // update ads status
+  async updateStatus(dto: AdsStatusDto) {
+    try {
+      // check if ads exists
+      await this.mongoPrismaService.ads.findUniqueOrThrow({
+        where: { id: dto.id },
+      });
+      const ad = await this.mongoPrismaService.ads.update({
+        where: { id: dto.id },
+        data: {
+          status: dto.status,
+          updateAt: this.nowSeconds(),
+        },
+      });
+
+      await this.cacheSyncService.scheduleAdRefresh(ad.id, ad.adType);
+
+      return this.mapToDto(ad);
+    } catch (e) {
+      if (e instanceof PrismaClientKnownRequestError && e.code === 'P2025') {
+        throw new NotFoundException('Ads not found');
+      }
+      throw e;
+    }
+  }
+
   async list(dto: ListAdsDto) {
     const where: any = {};