All files / src/platform/reports report.controller.ts

0% Statements 0/30
0% Branches 0/10
0% Functions 0/9
0% Lines 0/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76                                                                                                                                                       
import { Controller, Get, Post, Param, Body, Req } from '@nestjs/common';
import { ReportService } from './report.service';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
 
@Controller('api/v1')
@ApiTags('Report')
export class ReportController {
  constructor(private readonly reportService: ReportService) {}
 
  private getContext(req: any) {
    const tenantId =
      req.user?.tenantId || req.headers['x-tenant-id'] || 'SYSTEM';
    const userId = req.user?.id || req.headers['x-user-id'] || 'system-user-id';
    return { tenantId, userId };
  }
 
  @Get('report-data-sources')
  @ApiOperation({ summary: 'Get sources operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getSources() {
    return this.reportService.getSources();
  }
 
  @Get('reports')
  @ApiOperation({ summary: 'List operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async list(@Req() req: any) {
    const ctx = this.getContext(req);
    return this.reportService.getReports(ctx.tenantId, ctx.userId);
  }
 
  @Post('reports')
  @ApiOperation({ summary: 'Create operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async create(@Body() body: any, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.reportService.createReport({
      ...body,
      tenantId: ctx.tenantId,
      createdBy: ctx.userId,
    });
  }
 
  @Get('reports/:id')
  @ApiOperation({ summary: 'Get one operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getOne(@Param('id') id: string, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.reportService.getReport(id, ctx.tenantId);
  }
 
  @Post('reports/:id/run')
  @ApiOperation({ summary: 'Run sync operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async runSync(@Param('id') id: string, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.reportService.executeReport(id, ctx.tenantId, ctx.userId);
  }
 
  @Post('reports/:id/queue')
  @ApiOperation({ summary: 'Run async operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async runAsync(@Param('id') id: string, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.reportService.queueReportRun(id, ctx.tenantId, ctx.userId);
  }
 
  @Get('reports/:id/runs')
  @ApiOperation({ summary: 'Get runs operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getRuns(@Param('id') id: string, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.reportService.getRuns(id, ctx.tenantId);
  }
}