1+ import {
2+ BadRequestException ,
3+ Body ,
4+ Controller ,
5+ Get ,
6+ NotFoundException ,
7+ Param ,
8+ Patch ,
9+ Post ,
10+ UseFilters ,
11+ ValidationPipe ,
12+ } from "@nestjs/common" ;
13+ import { InjectRepository , Repository } from "common/objection" ;
14+ import { ApiTags , PartialType , PickType } from "@nestjs/swagger" ;
15+ import { Role , Roles } from "common/gcp" ;
16+ import { ApiDoc } from "common/docs" ;
17+ import { DBExceptionFilter } from "common/filters" ;
18+ import { randomUUID } from "crypto" ;
19+ import { BugReport , BugReportEntity , BugStatus } from "entities/bug-report.entity" ;
20+
21+ class BugReportCreateEntity extends PickType ( BugReportEntity , [
22+ "title" ,
23+ "description" ,
24+ "severity" ,
25+ "status" ,
26+ ] as const ) { }
27+
28+ class BugReportUpdateEntity extends PartialType ( BugReportCreateEntity ) { }
29+
30+ @ApiTags ( "Bug Reports" )
31+ @Controller ( "bug-reports" )
32+ @UseFilters ( DBExceptionFilter )
33+ export class BugReportController {
34+ constructor (
35+ @InjectRepository ( BugReport )
36+ private readonly bugReportRepo : Repository < BugReport > ,
37+ ) { }
38+
39+ @Get ( "/" )
40+ @Roles ( Role . TECH )
41+ @ApiDoc ( {
42+ summary : "Get All Bug Reports" ,
43+ response : {
44+ ok : { type : [ BugReportEntity ] } ,
45+ } ,
46+ auth : Role . TEAM ,
47+ } )
48+ async getAll ( ) {
49+ return this . bugReportRepo . findAll ( ) . exec ( ) ;
50+ }
51+
52+ @Get ( ":id" )
53+ @Roles ( Role . TECH )
54+ @ApiDoc ( {
55+ summary : "Get a Bug Report" ,
56+ params : [ { name : "id" , description : "ID must be set to a bug report's ID" } ] ,
57+ response : {
58+ ok : { type : BugReportEntity } ,
59+ } ,
60+ auth : Role . TEAM ,
61+ } )
62+ async getOne ( @Param ( "id" ) id : string ) {
63+ const bug = await this . bugReportRepo . findOne ( id ) . exec ( ) ;
64+ if ( ! bug ) {
65+ throw new NotFoundException ( "Bug report not found" ) ;
66+ }
67+ return bug ;
68+ }
69+
70+ @Post ( "/" )
71+ @Roles ( Role . NONE )
72+ @ApiDoc ( {
73+ summary : "Create a Bug Report" ,
74+ request : {
75+ body : { type : BugReportCreateEntity } ,
76+ validate : true ,
77+ } ,
78+ response : {
79+ created : { type : BugReportEntity } ,
80+ } ,
81+ auth : Role . TEAM ,
82+ } )
83+ async createOne (
84+ @Body (
85+ new ValidationPipe ( {
86+ forbidNonWhitelisted : true ,
87+ whitelist : true ,
88+ transform : true ,
89+ } ) ,
90+ )
91+ data : BugReportCreateEntity ,
92+ ) {
93+ const now = Date . now ( ) ;
94+
95+ const bug : Partial < BugReport > = {
96+ ...data ,
97+ id : randomUUID ( ) ,
98+ status : data . status ?? BugStatus . UNSEEN ,
99+ dateReported : now ,
100+ dateUpdated : now ,
101+ } ;
102+
103+ return this . bugReportRepo . createOne ( bug ) . exec ( ) ;
104+ }
105+
106+ @Patch ( ":id" )
107+ @Roles ( Role . TECH )
108+ @ApiDoc ( {
109+ summary : "Update a Bug Report" ,
110+ params : [ { name : "id" , description : "ID must be set to a bug report's ID" } ] ,
111+ request : {
112+ body : { type : BugReportUpdateEntity } ,
113+ validate : true ,
114+ } ,
115+ response : {
116+ ok : { type : BugReportEntity } ,
117+ } ,
118+ auth : Role . TEAM ,
119+ } )
120+ async patchOne (
121+ @Param ( "id" ) id : string ,
122+ @Body (
123+ new ValidationPipe ( {
124+ forbidNonWhitelisted : true ,
125+ whitelist : true ,
126+ transform : true ,
127+ transformOptions : {
128+ exposeUnsetFields : false ,
129+ } ,
130+ } ) ,
131+ )
132+ data : BugReportUpdateEntity ,
133+ ) {
134+ const existing = await this . bugReportRepo . findOne ( id ) . exec ( ) ;
135+ if ( ! existing ) {
136+ throw new NotFoundException ( "Bug report not found" ) ;
137+ }
138+
139+ if ( Object . keys ( data ) . length === 0 ) {
140+ throw new BadRequestException ( "No fields provided to update" ) ;
141+ }
142+
143+ return this . bugReportRepo
144+ . patchOne ( id , { ...data , dateUpdated : Date . now ( ) } as Partial < BugReport > )
145+ . exec ( ) ;
146+ }
147+ }
0 commit comments