import { Controller, Get, Param, Query, Req, UseGuards } from '@nestjs/common'; import type { Request } from 'express'; import { WecomPluginGuard } from './wecom-plugin.guard'; import { WecomPluginQueryService } from './wecom-plugin-query.service'; import { WECOM_PLUGIN_OPENAPI } from './wecom-plugin.openapi'; @Controller('wecom/plugin') @UseGuards(WecomPluginGuard) export class WecomPluginController { constructor(private readonly query: WecomPluginQueryService) {} @Get() info() { return this.query.info(); } @Get('openapi.json') openapi() { return WECOM_PLUGIN_OPENAPI; } @Get('orders') orders( @Req() req: Request, @Query('q') q?: string, @Query('page') page?: string, @Query('pageSize') pageSize?: string, ) { return this.query.queryOrders(pluginCaller(req), q, page, pageSize); } @Get('users') users( @Req() req: Request, @Query('q') q?: string, @Query('page') page?: string, @Query('pageSize') pageSize?: string, ) { return this.query.queryUsers(pluginCaller(req), q, page, pageSize); } @Get('stores') stores( @Req() req: Request, @Query('q') q?: string, @Query('page') page?: string, @Query('pageSize') pageSize?: string, ) { return this.query.queryStores(pluginCaller(req), q, page, pageSize); } @Get('redeems') redeems( @Req() req: Request, @Query('q') q?: string, @Query('page') page?: string, @Query('pageSize') pageSize?: string, ) { return this.query.queryRedeems(pluginCaller(req), q, page, pageSize); } @Get('promo-codes') promoCodes( @Req() req: Request, @Query('q') q?: string, @Query('page') page?: string, @Query('pageSize') pageSize?: string, ) { return this.query.queryPromoCodes(pluginCaller(req), q, page, pageSize); } @Get('promo-codes/:code/stats') promoStats(@Req() req: Request, @Param('code') code: string) { return this.query.queryPromoCodeStats(pluginCaller(req), code); } @Get('metrics') metrics(@Req() req: Request, @Query('kind') kind?: string) { return this.query.queryMetrics(pluginCaller(req), kind); } } function pluginCaller(req: Request): string { const raw = req.headers['x-wecom-userid'] ?? req.headers['userid']; const v = Array.isArray(raw) ? raw[0] : raw; return String(v || 'plugin').trim() || 'plugin'; }