File size: 1,161 Bytes
a381fc9
 
 
 
 
 
 
 
 
abc493d
 
 
a381fc9
abc493d
 
 
 
a381fc9
 
 
 
abc493d
 
 
a381fc9
 
 
abc493d
 
 
a381fc9
 
 
abc493d
 
a381fc9
abc493d
 
 
 
 
 
 
 
 
 
 
 
 
a381fc9
abc493d
 
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
import {
  Controller,
  Get,
  Param,
  Query,
  Res,
  UnauthorizedException,
  UseGuards,
} from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import type { Response } from 'express';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { streamPdf } from '../common/pdf-proxy.util';
import { ReportsService } from './reports.service';

@Controller('reports')
export class ReportsController {
  constructor(
    private svc: ReportsService,
    private jwt: JwtService,
  ) {}

  @Get()
  @UseGuards(JwtAuthGuard)
  findAll() {
    return this.svc.findAll();
  }

  @Get(':id')
  @UseGuards(JwtAuthGuard)
  findOne(@Param('id') id: string) {
    return this.svc.findOne(+id);
  }

  // Token verified manually from ?token= so a plain <a href> works without an
  // Authorization header.
  @Get(':id/pdf')
  async pdf(
    @Param('id') id: string,
    @Query('token') token: string,
    @Res() res: Response,
  ) {
    try {
      this.jwt.verify(token);
    } catch {
      throw new UnauthorizedException('Invalid or missing token');
    }

    const pdfPath = await this.svc.getPdfPath(+id);
    await streamPdf(pdfPath, res);
  }
}