Skip to content

Commit 28bc005

Browse files
committed
Added bug reporting table and endpoints
1 parent f3901b8 commit 28bc005

5 files changed

Lines changed: 261 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { Knex } from "knex";
2+
3+
4+
export async function up(knex: Knex): Promise<void> {
5+
await knex.schema.createTable("bug_reports", (table) => {
6+
table.uuid("id").primary().notNullable();
7+
8+
table.string("title").notNullable();
9+
10+
table.text("description").nullable();
11+
12+
table
13+
.enu("severity", ["low", "medium", "high"])
14+
.notNullable();
15+
16+
table
17+
.enu("status", ["unseen", "in progress", "resolved"])
18+
.notNullable()
19+
.defaultTo("unseen");
20+
21+
22+
table.bigInteger("date_reported").unsigned().notNullable();
23+
table.bigInteger("date_updated").unsigned().notNullable();
24+
});
25+
}
26+
27+
28+
export async function down(knex: Knex): Promise<void> {
29+
await knex.schema.dropTableIfExists("bug_reports");
30+
}
31+

‎src/app.module.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import { ApiKeyModule } from "modules/api-key/api-key.module";
4545
import { ApplicantScoreModule } from "modules/applicant-score/applicant-score.module";
4646
import { ScheduleModule } from "@nestjs/schedule";
4747
import { DistributedLockModule } from "common/gcp/scheduler";
48+
import { BugReportModule } from "modules/bug-reporting/bug-report.module"
4849

4950
@Module({
5051
imports: [
@@ -117,6 +118,7 @@ import { DistributedLockModule } from "common/gcp/scheduler";
117118
DriveModule,
118119
OrganizerApplicationModule,
119120
ApiKeyModule,
121+
BugReportModule,
120122

121123
// WebSocket
122124
SocketModule,

‎src/entities/bug-report.entity.ts‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { ApiProperty, PickType } from "@nestjs/swagger";
2+
import { Column, ID, Table } from "common/objection";
3+
import { Entity } from "entities/base.entity";
4+
import { IsDate, IsEnum, IsNumber, IsOptional, IsString, IsUUID } from "class-validator";
5+
import { Type } from "class-transformer";
6+
7+
export enum BugSeverity {
8+
LOW = "low",
9+
MEDIUM = "medium",
10+
HIGH = "high",
11+
}
12+
13+
export enum BugStatus {
14+
UNSEEN = "unseen",
15+
IN_PROGRESS = "in progress",
16+
RESOLVED = "resolved",
17+
}
18+
19+
@Table({
20+
name: "bug_reports",
21+
})
22+
export class BugReport extends Entity {
23+
@ApiProperty()
24+
@IsUUID()
25+
@ID({ type: "string" })
26+
id: string;
27+
28+
@ApiProperty()
29+
@IsString()
30+
@Column({ type: "string" })
31+
title: string;
32+
33+
@ApiProperty({ type: "string", required: false, nullable: true })
34+
@IsOptional()
35+
@IsString()
36+
@Column({ type: "string", required: false, nullable: true })
37+
description?: string;
38+
39+
@ApiProperty({ enum: BugSeverity })
40+
@IsEnum(BugSeverity)
41+
@Column({ type: "string" })
42+
severity: BugSeverity;
43+
44+
@ApiProperty({ enum: BugStatus, required: false, default: BugStatus.UNSEEN })
45+
@IsOptional()
46+
@IsEnum(BugStatus)
47+
@Column({ type: "string", required: false })
48+
status: BugStatus;
49+
50+
@ApiProperty()
51+
@IsNumber()
52+
@Type(() => Number)
53+
@Column({ type: "integer" })
54+
dateReported?: number;
55+
56+
@ApiProperty()
57+
@IsNumber()
58+
@Type(() => Number)
59+
@Column({ type: "integer" })
60+
dateUpdated?: number;
61+
}
62+
63+
export class BugReportEntity extends PickType(BugReport, [
64+
"id",
65+
"title",
66+
"description",
67+
"severity",
68+
"status",
69+
"dateReported",
70+
"dateUpdated",
71+
] as const) {}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Module } from "@nestjs/common";
2+
import { ObjectionModule } from "common/objection";
3+
import { BugReport } from "entities/bug-report.entity";
4+
import { BugReportController } from "./bug-report.controller";
5+
6+
@Module({
7+
imports: [ObjectionModule.forFeature([BugReport])],
8+
controllers: [BugReportController],
9+
})
10+
export class BugReportModule {}

0 commit comments

Comments
 (0)