import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common'; import { GenerationService } from '../generation/generation.service'; import { GenerationQueue } from '../generation/generation.queue'; import { MerchantsService, UpsertMerchantDto } from './merchants.service'; @Controller('v1/merchants') export class MerchantsController { constructor( private readonly merchants: MerchantsService, private readonly generation: GenerationService, private readonly queue: GenerationQueue, ) {} @Get() list() { return this.merchants.list(); } @Get(':id') get(@Param('id') id: string) { return this.merchants.findWithTaxonomy(id); } /** o2o-site-AEO 사이트 발행 웹훅: 업체 등록 + 키워드 생성 예약 */ @Post('publish') async publish(@Body() dto: UpsertMerchantDto & { generate?: boolean; sync?: boolean }) { const merchant = await this.merchants.upsert(dto); if (dto.generate === false) return { merchant, generation: 'skipped' }; if (dto.sync) { const stats = await this.generation.runForMerchant(merchant.id, 'published'); return { merchant, generation: stats }; } const jobId = await this.queue.enqueue(merchant.id, 'published'); return { merchant, generation: { queued: true, jobId } }; } /** 수동 재생성 */ @Post(':id/generate') async generate( @Param('id') id: string, @Query('sync') sync?: string, @Query('count') count?: string, ) { const target = count ? Math.min(Math.max(1, Number(count)), 500) : undefined; if (sync === 'true' || sync === '1') { return this.generation.runForMerchant(id, 'manual', target); } const m = await this.merchants.findWithTaxonomy(id); const jobId = await this.queue.enqueue(m.id, 'manual'); return { queued: true, jobId }; } }