-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add endpoint for querying all inductee points. (#153)
- Loading branch information
1 parent
fa19164
commit b73eb40
Showing
7 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { JsonController, Get, UseBefore } from 'routing-controllers'; | ||
import { ResponseSchema, OpenAPI } from 'routing-controllers-openapi'; | ||
|
||
import { AppUserService, AppUserServiceImpl } from '@Services'; | ||
import { OfficerAuthMiddleware } from '@Middlewares'; | ||
import { MultipleInducteePointsResponse, InducteePointsResponse, AppUserResponse } from '@Payloads'; | ||
import { InducteePointsView } from '@Entities'; | ||
|
||
@JsonController('/api/points') | ||
export class PointsController { | ||
constructor(private appUserService: AppUserService) {} | ||
|
||
@Get('/inductees') | ||
@ResponseSchema(MultipleInducteePointsResponse) | ||
@UseBefore(OfficerAuthMiddleware) | ||
@OpenAPI({ security: [{ TokenAuth: [] }] }) | ||
async getAllInducteePoints(): Promise<MultipleInducteePointsResponse> { | ||
const points: InducteePointsView[] = await this.appUserService.getAllInducteePoints(); | ||
|
||
const pointResponses = points.map((point: InducteePointsView) => { | ||
const res = new InducteePointsResponse(); | ||
res.points = point.points; | ||
|
||
// man i hate this | ||
res.user = point.user; | ||
res.email = point.email; | ||
res.hasMentorshipRequirement = point.hasMentorshipRequirement; | ||
res.hasProfessionalRequirement = point.hasProfessionalRequirement; | ||
return res; | ||
}); | ||
|
||
const multiplePointResponse = new MultipleInducteePointsResponse(); | ||
multiplePointResponse.inducteePoints = pointResponses; | ||
|
||
return multiplePointResponse; | ||
} | ||
} | ||
|
||
export const PointsControllerImpl = new PointsController(AppUserServiceImpl); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/migrations/1605509162887-AddEmailToInducteePointsView.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class AddEmailToInducteePointsView1605509162887 implements MigrationInterface { | ||
name = 'AddEmailToInducteePointsView1605509162887'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`DELETE FROM "typeorm_metadata" WHERE "type" = 'VIEW' AND "schema" = $1 AND "name" = $2`, | ||
['public', 'inductee_points_view'] | ||
); | ||
await queryRunner.query(`DROP VIEW "inductee_points_view"`); | ||
await queryRunner.query( | ||
`CREATE VIEW "inductee_points_view" AS SELECT "appUser"."id" AS "user", "appUser"."email" AS "email", SUM("attendance"."points") AS "points", SUM(CASE WHEN "event"."type" = 'professional' THEN 1 ELSE 0 END)::int::bool AS "hasProfessionalRequirement", SUM(CASE WHEN "event"."type" = 'mentorship' THEN 1 ELSE 0 END)::int::bool AS "hasMentorshipRequirement" FROM "app_user" "appUser" INNER JOIN "attendance" "attendance" ON "appUser"."id" = "attendance"."attendeeId" INNER JOIN "event" "event" ON "event"."id" = "attendance"."eventId" WHERE "attendance"."isInductee" GROUP BY "appUser"."id"` | ||
); | ||
await queryRunner.query( | ||
`INSERT INTO "typeorm_metadata"("type", "schema", "name", "value") VALUES ($1, $2, $3, $4)`, | ||
[ | ||
'VIEW', | ||
'public', | ||
'inductee_points_view', | ||
'SELECT "appUser"."id" AS "user", "appUser"."email" AS "email", SUM("attendance"."points") AS "points", SUM(CASE WHEN "event"."type" = \'professional\' THEN 1 ELSE 0 END)::int::bool AS "hasProfessionalRequirement", SUM(CASE WHEN "event"."type" = \'mentorship\' THEN 1 ELSE 0 END)::int::bool AS "hasMentorshipRequirement" FROM "app_user" "appUser" INNER JOIN "attendance" "attendance" ON "appUser"."id" = "attendance"."attendeeId" INNER JOIN "event" "event" ON "event"."id" = "attendance"."eventId" WHERE "attendance"."isInductee" GROUP BY "appUser"."id"', | ||
] | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`DELETE FROM "typeorm_metadata" WHERE "type" = 'VIEW' AND "schema" = $1 AND "name" = $2`, | ||
['public', 'inductee_points_view'] | ||
); | ||
await queryRunner.query(`DROP VIEW "inductee_points_view"`); | ||
await queryRunner.query( | ||
`CREATE VIEW "inductee_points_view" AS SELECT "appUser"."email" AS "email", SUM("attendance"."points") AS "points", SUM(CASE WHEN "event"."type" = 'professional' THEN 1 ELSE 0 END)::int::bool AS "hasProfessionalRequirement", SUM(CASE WHEN "event"."type" = 'mentorship' THEN 1 ELSE 0 END)::int::bool AS "hasMentorshipRequirement" FROM "app_user" "appUser" INNER JOIN "attendance" "attendance" ON "appUser"."id" = "attendance"."attendeeId" INNER JOIN "event" "event" ON "event"."id" = "attendance"."eventId" WHERE "attendance"."isInductee" GROUP BY "appUser"."id"` | ||
); | ||
await queryRunner.query( | ||
`INSERT INTO "typeorm_metadata"("type", "schema", "name", "value") VALUES ($1, $2, $3, $4)`, | ||
[ | ||
'VIEW', | ||
'public', | ||
'inductee_points_view', | ||
'SELECT "appUser"."email" AS "email", SUM("attendance"."points") AS "points", SUM(CASE WHEN "event"."type" = \'professional\' THEN 1 ELSE 0 END)::int::bool AS "hasProfessionalRequirement", SUM(CASE WHEN "event"."type" = \'mentorship\' THEN 1 ELSE 0 END)::int::bool AS "hasMentorshipRequirement" FROM "app_user" "appUser" INNER JOIN "attendance" "attendance" ON "appUser"."id" = "attendance"."attendeeId" INNER JOIN "event" "event" ON "event"."id" = "attendance"."eventId" WHERE "attendance"."isInductee" GROUP BY "appUser"."id"', | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { IsBoolean, IsInt, ValidateNested, IsEmail } from 'class-validator'; | ||
import { Type } from 'class-transformer'; | ||
|
||
export class InducteePointsResponse { | ||
@IsInt() | ||
user: number; | ||
|
||
@IsEmail() | ||
email: string; | ||
|
||
@IsInt() | ||
points: number; | ||
|
||
@IsBoolean() | ||
hasProfessionalRequirement: boolean; | ||
|
||
@IsBoolean() | ||
hasMentorshipRequirement: boolean; | ||
} | ||
|
||
export class MultipleInducteePointsResponse { | ||
@ValidateNested({ each: true }) | ||
@Type(() => InducteePointsResponse) | ||
inducteePoints: InducteePointsResponse[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters