import { Controller, Inject, Type } from '@nestjs/common'; import { MessagePattern } from '@nestjs/microservices'; import { ReportControllerOptions } from '../interfaces/report-capability.types'; export function createReportController(options: ReportControllerOptions): Type { const { pluginId, endpoints, handlerToken, methodPrefix = 'handle' } = options; @Controller() class ReportCapabilityController { constructor(@Inject(handlerToken) private readonly handler: any) {} @MessagePattern(`plugin.${pluginId}.report.capabilities`) async getCapabilities() { return { endpoints: endpoints.map(e => ({ endpoint: e.endpoint, label: e.label, description: e.description, outputFields: e.outputFields, filters: e.filters, })), }; } } for (const ep of endpoints) { const pattern = `plugin.${pluginId}.report.${ep.endpoint}`; const safeName = ep.endpoint.replace(/[^a-zA-Z0-9_]/g, '_'); const methodName = `handle_${safeName}`; const handlerMethod = `${methodPrefix}${ep.endpoint .replace(/[-_](.)/g, (_, c) => c.toUpperCase()) .replace(/^([a-z])/, (_, c) => c.toUpperCase())}`; Object.defineProperty(ReportCapabilityController.prototype, methodName, { value: async function (this: any, payload: any) { return this.handler[handlerMethod](payload); }, writable: true, enumerable: false, configurable: true, }); const descriptor = Object.getOwnPropertyDescriptor( ReportCapabilityController.prototype, methodName, ); MessagePattern(pattern)(ReportCapabilityController.prototype, methodName, descriptor!); } return ReportCapabilityController; }