Skip to content

Commit a235163

Browse files
author
ruiichen
committed
skull
1 parent eceb651 commit a235163

File tree

7 files changed

+99
-6
lines changed

7 files changed

+99
-6
lines changed

backend/typescript/graphql/resolvers/reviewPageResolvers.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApplicationDTO } from "../../types";
1+
import { ApplicationDTO, ReviewedApplicantsDTO } from "../../types";
22
import ReviewPageService from "../../services/implementations/reviewPageService";
33
import { getErrorMessage } from "../../utilities/errorUtils";
44

@@ -16,6 +16,18 @@ const reviewPageResolvers = {
1616
throw new Error(getErrorMessage(error));
1717
}
1818
},
19+
getReviewedApplicantsByUserId: async (
20+
_parent: undefined,
21+
args: { userId: number },
22+
): Promise<ReviewedApplicantsDTO[]> => {
23+
try {
24+
return await reviewPageService.getReviewedApplicantsByUserId(
25+
args.userId,
26+
);
27+
} catch (error) {
28+
throw new Error(getErrorMessage(error));
29+
}
30+
},
1931
},
2032
};
2133

backend/typescript/graphql/types/reviewPageType.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,16 @@ const reviewPageType = gql`
2525
timestamp: Int
2626
}
2727
28+
type ReviewedApplicantsDTO {
29+
applicantRecordId: String!
30+
reviewStatus: String!
31+
applicantFirstName: String!
32+
applicantLastName: String!
33+
}
34+
2835
extend type Query {
2936
reviewApplicantPage(applicantRecordId: String!): ApplicationDTO!
37+
getReviewedApplicantsByUserId(userId: Int!): [ReviewedApplicantsDTO!]!
3038
}
3139
`;
3240

backend/typescript/models/reviewedApplicantRecord.model.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,10 @@ export default class ReviewedApplicantRecord extends Model {
4747

4848
@BelongsTo(() => User, { foreignKey: "reviewerId", targetKey: "id" })
4949
user?: NonAttribute<User>;
50+
51+
@BelongsTo(() => ApplicantRecord, {
52+
foreignKey: "applicantRecordId",
53+
targetKey: "id",
54+
})
55+
applicantRecord?: NonAttribute<ApplicantRecord>;
5056
}

backend/typescript/models/user.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default class User extends Model {
3939

4040
@HasMany(() => ReviewedApplicantRecord, {
4141
foreignKey: "reviewerId",
42-
as: "user",
42+
sourceKey: "id",
4343
})
4444
reviewedApplicantRecords?: NonAttribute<ReviewedApplicantRecord[]>;
4545
}

backend/typescript/services/implementations/reviewPageService.ts

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
import { ApplicationDTO } from "../../types";
1+
import {
2+
ApplicationDTO,
3+
ReviewedApplicantsDTO,
4+
} from "../../types";
25
import IReviewPageService from "../interfaces/IReviewPageService";
36
import { getErrorMessage } from "../../utilities/errorUtils";
47
import logger from "../../utilities/logger";
58
import ApplicantRecord from "../../models/applicantRecord.model";
69
import ReviewedApplicantRecord from "../../models/reviewedApplicantRecord.model";
710
import Applicant from "../../models/applicant.model";
11+
import User from "../../models/user.model";
812

913
const Logger = logger(__filename);
1014

11-
function toDTO(model: Applicant): ApplicationDTO {
15+
function toApplicationDTO(model: Applicant): ApplicationDTO {
16+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
1217
const firstChoice = model.applicantRecords!.find((ar) => ar.choice === 1);
1318
const secondChoice = model.applicantRecords!.find((ar) => ar.choice === 2);
1419

@@ -37,6 +42,18 @@ function toDTO(model: Applicant): ApplicationDTO {
3742
};
3843
}
3944

45+
function toReviewedApplicantRecordDTO(
46+
model: ReviewedApplicantRecord,
47+
): ReviewedApplicantsDTO {
48+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
49+
return {
50+
applicantRecordId: model.applicantRecordId,
51+
reviewStatus: model.status,
52+
applicantFirstName: model.applicantRecord!.applicant!.firstName,
53+
applicantLastName: model.applicantRecord!.applicant!.lastName,
54+
};
55+
}
56+
4057
class ReviewPageService implements IReviewPageService {
4158
/* eslint-disable class-methods-use-this */
4259
async getReviewPage(applicantRecordId: string): Promise<ApplicationDTO> {
@@ -61,7 +78,42 @@ class ReviewPageService implements IReviewPageService {
6178
});
6279
if (!applicant) throw new Error(`Database integrity has been violated`);
6380

64-
return toDTO(applicant);
81+
return toApplicationDTO(applicant);
82+
} catch (error: unknown) {
83+
Logger.error(`Failed to fetch. Reason = ${getErrorMessage(error)}`);
84+
throw error;
85+
}
86+
}
87+
88+
async getReviewedApplicantsByUserId(
89+
userId: number,
90+
): Promise<ReviewedApplicantsDTO[]> {
91+
try {
92+
const user: User | null = await User.findOne({
93+
where: { id: userId },
94+
attributes: { exclude: ["createdAt", "updatedAt"] },
95+
include: [
96+
{
97+
attributes: { exclude: ["createdAt", "updatedAt"] },
98+
association: "reviewedApplicantRecords",
99+
include: [
100+
{
101+
attributes: { exclude: ["createdAt", "updatedAt"] },
102+
association: "applicantRecord",
103+
include: [
104+
{
105+
attributes: { exclude: ["createdAt", "updatedAt"] },
106+
association: "applicant",
107+
},
108+
],
109+
},
110+
],
111+
},
112+
],
113+
});
114+
if (!user) throw new Error(`No user with ${userId} found.`);
115+
if (!user.reviewedApplicantRecords) return [];
116+
return user.reviewedApplicantRecords.map(toReviewedApplicantRecordDTO);
65117
} catch (error: unknown) {
66118
Logger.error(`Failed to fetch. Reason = ${getErrorMessage(error)}`);
67119
throw error;
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
import { ApplicationDTO } from "../../types";
1+
import { ApplicationDTO, ReviewedApplicantsDTO } from "../../types";
22

33
interface IReviewPageService {
44
/**
55
* Fetch data from the Applicant and ApplicantRecord table to return an ApplicationDTO object
66
* @Param applicantRecordId the id of the applicant record that the viewer is interested in
77
*/
88
getReviewPage(applicantRecordId: string): Promise<ApplicationDTO>;
9+
10+
/**
11+
* Fetches information about all the applicants assigned to a user to review
12+
* @param userId the id of the user that the viewer is interested in
13+
*/
14+
getReviewedApplicantsByUserId(
15+
userId: number,
16+
): Promise<ReviewedApplicantsDTO[]>;
917
}
1018

1119
export default IReviewPageService;

backend/typescript/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ export type ReviewedApplicantRecordDTO = {
208208
reviewerHasConflict: boolean;
209209
};
210210

211+
export type ReviewedApplicantsDTO = {
212+
applicantRecordId: string;
213+
reviewStatus: ReviewStatus;
214+
applicantFirstName: string;
215+
applicantLastName: string;
216+
};
217+
211218
export type AdminCommentDTO = {
212219
id: string;
213220
userId: number;

0 commit comments

Comments
 (0)