All files / src/features/export export.controller.ts

100% Statements 12/12
100% Branches 4/4
100% Functions 3/3
100% Lines 12/12

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                  12x   12x                 2x   2x         2x             2x   2x 2x 1x         1x   1x 1x        
import { Controller, Get, Res, Logger, Query, Param, UseGuards } from '@nestjs/common'
import { ExportService } from './export.service'
import type { Response } from 'express'
import { AuthGuard } from '@nestjs/passport'
import { RolesGuard } from '../auth/guards/roles.guard'
import { Roles } from '../auth/decorators/roles.decorator'
 
@Controller('export')
export class ExportController {
    private readonly logger = new Logger(ExportController.name)
 
    constructor(private readonly exportService: ExportService) {}
 
    @Get('receipts/zip')
    @UseGuards(AuthGuard('jwt'), RolesGuard)
    @Roles('ADMIN', 'STAFF')
    async downloadAllReceipts(
        @Res({ passthrough: true }) res: Response,
        @Query('eventId') eventId?: string,
    ) {
        this.logger.log(`Received request for Global Receipt Export. Event: ${eventId || 'ALL'}`)
 
        res.set({
            'Content-Type': 'application/zip',
            'Content-Disposition': `attachment; filename="receipts-${eventId || 'global'}.zip"`,
        })
 
        return this.exportService.exportReceipts(eventId)
    }
 
    @Get('receipts/:id')
    @UseGuards(AuthGuard('jwt'), RolesGuard)
    @Roles('ADMIN', 'STAFF')
    async downloadSingleReceipt(@Param('id') id: string, @Res() res: Response) {
        this.logger.log(`Received request for single receipt download: ${id}`)
 
        try {
            const buffer = await this.exportService.getReceipt(id)
            res.set({
                'Content-Type': 'application/pdf',
                'Content-Disposition': `attachment; filename="receipt-${id}.pdf"`,
                'Content-Length': buffer.length,
            })
            res.end(buffer)
        } catch (error) {
            this.logger.error(`Failed to download receipt ${id}`, error)
            res.status(404).send({ message: 'Receipt not found' })
        }
    }
}