Skip to content

Commit 1da03e2

Browse files
committed
[add] Test cases of Award Assignment API
[fix] some Field bugs of Award models & controllers Signed-off-by: TechQuery <shiy2008@gmail.com>
1 parent b273839 commit 1da03e2

File tree

3 files changed

+67
-34
lines changed

3 files changed

+67
-34
lines changed

source/controller/Award.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
OnUndefined,
1212
Param,
1313
Post,
14-
Put
14+
Put,
15+
QueryParams
1516
} from 'routing-controllers';
1617
import { ResponseSchema } from 'routing-controllers-openapi';
1718

@@ -38,7 +39,7 @@ export class AwardController {
3839
@HttpCode(201)
3940
@ResponseSchema(Award)
4041
async createOne(
41-
@CurrentUser() currentUser: User,
42+
@CurrentUser() createdBy: User,
4243
@Param('name') name: string,
4344
@Body() award: Award
4445
) {
@@ -47,11 +48,11 @@ export class AwardController {
4748
if (!hackathon)
4849
throw new NotFoundError(`Hackathon ${name} is not found`);
4950

50-
await HackathonController.ensureAdmin(currentUser.id, name);
51+
await HackathonController.ensureAdmin(createdBy.id, name);
5152

52-
const saved = await awardStore.save({ ...award, hackathon });
53+
const saved = await awardStore.save({ ...award, createdBy, hackathon });
5354

54-
await ActivityLogController.logCreate(currentUser, 'Award', saved.id);
55+
await ActivityLogController.logCreate(createdBy, 'Award', saved.id);
5556

5657
return saved;
5758
}
@@ -67,7 +68,7 @@ export class AwardController {
6768
@Authorized()
6869
@ResponseSchema(Award)
6970
async updateOne(
70-
@CurrentUser() currentUser: User,
71+
@CurrentUser() updatedBy: User,
7172
@Param('name') name: string,
7273
@Param('id') id: number,
7374
@Body() updateData: Award
@@ -76,18 +77,19 @@ export class AwardController {
7677

7778
if (!award) throw new NotFoundError(`Award ${id} is not found`);
7879

79-
await HackathonController.ensureAdmin(currentUser.id, name);
80+
await HackathonController.ensureAdmin(updatedBy.id, name);
8081

8182
// update only allowed fields
8283
const updatedAward = {
8384
...award,
8485
...updateData,
8586
id, // make sure id is not overridden
87+
updatedBy,
8688
hackathon: award.hackathon // make sure hackathon relationship is not changed
8789
};
8890
const saved = await awardStore.save(updatedAward);
8991

90-
await ActivityLogController.logUpdate(currentUser, 'Award', saved.id);
92+
await ActivityLogController.logUpdate(updatedBy, 'Award', saved.id);
9193

9294
return saved;
9395
}
@@ -96,26 +98,27 @@ export class AwardController {
9698
@Authorized()
9799
@OnUndefined(204)
98100
async deleteOne(
99-
@CurrentUser() currentUser: User,
101+
@CurrentUser() deletedBy: User,
100102
@Param('name') name: string,
101103
@Param('id') id: number
102104
) {
103105
const award = await awardStore.findOneBy({ id });
104106

105107
if (!award) throw new NotFoundError(`Award "${id}" is not found`);
106108

107-
await HackathonController.ensureAdmin(currentUser.id, name);
109+
await HackathonController.ensureAdmin(deletedBy.id, name);
108110

111+
await awardStore.save({ ...award, deletedBy });
109112
await awardStore.softDelete(award.id);
110113

111-
await ActivityLogController.logDelete(currentUser, 'Award', award.id);
114+
await ActivityLogController.logDelete(deletedBy, 'Award', award.id);
112115
}
113116

114117
@Get()
115118
@ResponseSchema(AwardListChunk)
116119
async getList(
117120
@Param('name') name: string,
118-
{ pageSize, pageIndex }: BaseFilter
121+
@QueryParams() { pageSize, pageIndex }: BaseFilter
119122
) {
120123
const [list, count] = await awardStore.findAndCount({
121124
where: { hackathon: { name } },
@@ -212,10 +215,13 @@ export class AwardAssignmentController {
212215

213216
@Get()
214217
@ResponseSchema(AwardAssignmentListChunk)
215-
getList(@Param('id') id: number, { pageSize, pageIndex }: BaseFilter) {
218+
getList(
219+
@Param('aid') aid: number,
220+
@QueryParams() { pageSize, pageIndex }: BaseFilter
221+
) {
216222
return AwardAssignmentController.getList(
217223
'award',
218-
id,
224+
aid,
219225
pageSize,
220226
pageIndex
221227
);
@@ -226,7 +232,10 @@ export class AwardAssignmentController {
226232
export class TeamAwardAssignmentController {
227233
@Get()
228234
@ResponseSchema(AwardAssignmentListChunk)
229-
getList(@Param('id') id: number, { pageSize, pageIndex }: BaseFilter) {
235+
getList(
236+
@Param('id') id: number,
237+
@QueryParams() { pageSize, pageIndex }: BaseFilter
238+
) {
230239
return AwardAssignmentController.getList(
231240
'team',
232241
id,

source/model/Award.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
Min,
88
ValidateNested
99
} from 'class-validator';
10-
import { Column, Entity } from 'typeorm';
10+
import { Column, Entity, JoinColumn, ManyToOne, OneToOne } from 'typeorm';
1111

1212
import { ListChunk, Media } from './Base';
1313
import { HackathonBase } from './Hackathon';
@@ -63,18 +63,24 @@ export class AwardAssignment extends HackathonBase {
6363
@Type(() => Award)
6464
@Transform(({ value }) => Award.from(value))
6565
@ValidateNested()
66+
@IsOptional()
67+
@ManyToOne(() => Award)
6668
award: Award;
6769

6870
@Type(() => User)
6971
@Transform(({ value }) => User.from(value))
7072
@ValidateNested()
7173
@IsOptional()
74+
@OneToOne(() => User, { nullable: true })
75+
@JoinColumn()
7276
user?: User;
7377

7478
@Type(() => Team)
7579
@Transform(({ value }) => Team.from(value))
7680
@ValidateNested()
7781
@IsOptional()
82+
@OneToOne(() => Team, { nullable: true })
83+
@JoinColumn()
7884
team?: Team;
7985
}
8086

test/main.test.ts

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -395,24 +395,6 @@ describe('Main business logic', () => {
395395
}
396396
});
397397

398-
it('should delete an award', async () => {
399-
await client.hackathon.awardControllerDeleteOne(
400-
testHackathon.name,
401-
testAward.id,
402-
{ headers: { Authorization: `Bearer ${hackathonCreator.token}` } }
403-
);
404-
405-
try {
406-
await client.hackathon.awardControllerGetOne(
407-
testHackathon.name,
408-
testAward.id
409-
);
410-
fail('Should have thrown a 404 error');
411-
} catch (error) {
412-
expect((error as HttpResponse<unknown>).status).toBe(404);
413-
}
414-
});
415-
416398
it('should enroll a user in the hackathon', async () => {
417399
const { data: enrollment } =
418400
await client.hackathon.enrollmentControllerCreateOne(
@@ -511,6 +493,42 @@ describe('Main business logic', () => {
511493
expect(teamList.list[0].id).toBe(testTeam.id);
512494
});
513495

496+
it('should assign an award to a team by hackathon staffs', async () => {
497+
const { data: assignment } =
498+
await client.hackathon.awardAssignmentControllerCreateOne(
499+
testHackathon.name,
500+
testAward.id,
501+
{ team: testTeam },
502+
{
503+
headers: {
504+
Authorization: `Bearer ${hackathonCreator.token}`
505+
}
506+
}
507+
);
508+
expect(assignment.award.id).toBe(testAward.id);
509+
expect(assignment.team.id).toBe(testTeam.id);
510+
});
511+
512+
it('should get the list of award assignments for a hackathon', async () => {
513+
const { data: awardAssignments } =
514+
await client.hackathon.awardAssignmentControllerGetList(
515+
testHackathon.name,
516+
testAward.id
517+
);
518+
expect(awardAssignments.count).toBe(1);
519+
expect(awardAssignments.list[0].award.id).toBe(testAward.id);
520+
expect(awardAssignments.list[0].team.id).toBe(testTeam.id);
521+
522+
const { data: teamAssignments } =
523+
await client.hackathon.teamAwardAssignmentControllerGetList(
524+
testHackathon.name,
525+
testTeam.id
526+
);
527+
expect(teamAssignments.count).toBe(1);
528+
expect(teamAssignments.list[0].award.id).toBe(testAward.id);
529+
expect(teamAssignments.list[0].team.id).toBe(testTeam.id);
530+
});
531+
514532
it('should delete a hackathon by its admin', async () => {
515533
const { name } = testHackathon;
516534
const { status, data } =

0 commit comments

Comments
 (0)