Selaa lähdekoodia

refactor: remove AdminSecretGuard and backfillAdId endpoint from AdsController; update adId field to be non-nullable in Ads model

Dave 1 kuukausi sitten
vanhempi
säilyke
700f8ff5d1

+ 0 - 26
apps/box-mgnt-api/src/mgnt-backend/feature/ads/admin-secret.guard.ts

@@ -1,26 +0,0 @@
-import {
-  CanActivate,
-  ExecutionContext,
-  ForbiddenException,
-  Injectable,
-} from '@nestjs/common';
-import type { Request } from 'express';
-
-@Injectable()
-export class AdminSecretGuard implements CanActivate {
-  canActivate(ctx: ExecutionContext): boolean {
-    const req = ctx.switchToHttp().getRequest<Request>();
-    const secretHeader = req.header('x-admin-secret') ?? '';
-
-    // Put this in env (recommended): ADS_ADMIN_SECRET=...
-    const expected = 'davennnbbbwww';
-
-    if (!expected) {
-      throw new ForbiddenException('Admin secret not configured');
-    }
-    if (secretHeader !== expected) {
-      throw new ForbiddenException('Invalid admin secret');
-    }
-    return true;
-  }
-}

+ 0 - 15
apps/box-mgnt-api/src/mgnt-backend/feature/ads/ads.controller.ts

@@ -23,27 +23,12 @@ import type { FastifyRequest } from 'fastify';
 import { CreateAdsDto, ListAdsDto, UpdateAdsDto, AdsDto } from './ads.dto';
 import { AdsService } from './ads.service';
 import { MongoIdParamDto } from '../common/mongo-id.dto';
-import { AdminSecretGuard } from './admin-secret.guard';
-import { Public } from '../../core/auth/decorators/public.decorator';
 
 @ApiTags('营销管理 - 广告')
 @Controller('ads')
 export class AdsController {
   constructor(private readonly service: AdsService) {}
 
-  @Post('backfill-adid')
-  @Public()
-  @UseGuards(AdminSecretGuard)
-  @ApiOperation({ summary: '[TEMP] Backfill Ads.adId for existing docs' })
-  @ApiQuery({ name: 'batchSize', required: false, description: 'Default 200' })
-  async backfillAdId(@Query('batchSize') batchSize?: string) {
-    const size = Number(batchSize ?? 200);
-    const safeSize =
-      Number.isFinite(size) && size > 0 && size <= 1000 ? size : 200;
-
-    return this.service.backfillAdIds(safeSize);
-  }
-
   @Post('list')
   @ApiOperation({ summary: 'List ads (pagination + filters)' })
   @ApiBody({ type: ListAdsDto })

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

@@ -157,45 +157,6 @@ export class AdsService {
     };
   }
 
-  async backfillAdIds(batchSize = 200): Promise<{
-    updated: number;
-    remaining: number;
-  }> {
-    let updated = 0;
-    let cursorId: string | undefined = undefined;
-
-    while (true) {
-      const rows = await this.mongoPrismaService.ads.findMany({
-        where: {
-          adId: null,
-          ...(cursorId ? { id: { gt: cursorId } } : {}),
-        },
-        select: { id: true },
-        orderBy: { id: 'asc' },
-        take: batchSize,
-      });
-
-      if (rows.length === 0) break;
-
-      for (const r of rows) {
-        const adId = await this.allocateAdId();
-        await this.mongoPrismaService.ads.update({
-          where: { id: r.id },
-          data: { adId },
-        });
-
-        updated++;
-        cursorId = r.id;
-      }
-    }
-
-    const remaining = await this.mongoPrismaService.ads.count({
-      where: { adId: null },
-    });
-
-    return { updated, remaining };
-  }
-
   async create(dto: CreateAdsDto) {
     this.ensureTimeRange(dto.startDt, dto.expiryDt);
 

+ 1 - 1
prisma/mongo/schema/ads.prisma

@@ -1,6 +1,6 @@
 model Ads {
   id           String     @id @map("_id") @default(auto()) @db.ObjectId
-  adId         Int?       @unique
+  adId         Int        @unique
   adType       AdType     // Redis key & module type
   advertiser   String     // 广告商 (业务上限制 max 20 字符)
   title        String     // 标题 (业务上限制 max 20 字符)