Skip to content

Commit

Permalink
Denormalize signup res and query types (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatoPlus authored Mar 21, 2022
1 parent 08284d1 commit 7f92731
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 2 additions & 0 deletions backend/graphql/types/shiftSignupType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const shiftSignupType = gql`
type ShiftSignupResponseDTO {
shiftId: ID!
shiftStartTime: DateTime!
shiftEndTime: DateTime!
userId: ID!
numVolunteers: Int!
note: String!
Expand Down
27 changes: 21 additions & 6 deletions backend/services/implementations/shiftSignupService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { PrismaClient, Signup, SignupStatus } from "@prisma/client";
import { Prisma } from ".prisma/client";
import { Prisma, Shift } from ".prisma/client";

import IShiftSignupService from "../interfaces/shiftSignupService";
import {
CreateShiftSignupDTO,
ShiftSignupDTO,
ShiftSignupResponseDTO,
UpdateShiftSignupRequestDTO,
} from "../../types";
Expand All @@ -16,11 +15,16 @@ const prisma = new PrismaClient();
const Logger = logger(__filename);

class ShiftSignupService implements IShiftSignupService {
convertSignupToDTO = (signup: Signup): ShiftSignupDTO => {
convertSignupResponseToDTO = (
signup: Signup,
shift: Shift,
): ShiftSignupResponseDTO => {
return {
...signup,
shiftId: String(signup.shiftId),
userId: String(signup.userId),
shiftStartTime: shift.startTime,
shiftEndTime: shift.endTime,
};
};

Expand All @@ -43,8 +47,13 @@ class ShiftSignupService implements IShiftSignupService {
};
const shiftSignups = await prisma.signup.findMany({
where: filter,
include: {
shift: true,
},
});
return shiftSignups.map((signup) => this.convertSignupToDTO(signup));
return shiftSignups.map((signup) =>
this.convertSignupResponseToDTO(signup, signup.shift),
);
} catch (error: unknown) {
Logger.error(
`Failed to shift signups. Reason = ${getErrorMessage(error)}`,
Expand All @@ -66,11 +75,14 @@ class ShiftSignupService implements IShiftSignupService {
userId: Number(shiftSignup.userId),
status: SignupStatus.PENDING,
},
include: {
shift: true,
},
}),
),
);
return newShiftSignups.map((newShiftSignup) =>
this.convertSignupToDTO(newShiftSignup),
this.convertSignupResponseToDTO(newShiftSignup, newShiftSignup.shift),
);
} catch (error: unknown) {
Logger.error(
Expand All @@ -94,8 +106,11 @@ class ShiftSignupService implements IShiftSignupService {
},
},
data: shiftSignup,
include: {
shift: true,
},
});
return this.convertSignupToDTO(updateResult);
return this.convertSignupResponseToDTO(updateResult, updateResult.shift);
} catch (error: unknown) {
Logger.error(
`Failed to update shift signup. Reason = ${getErrorMessage(error)}`,
Expand Down
5 changes: 4 additions & 1 deletion backend/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ export type UpdateShiftSignupRequestDTO = Omit<
"shiftId" | "userId"
>;

export type ShiftSignupResponseDTO = ShiftSignupDTO;
export type ShiftSignupResponseDTO = {
shiftStartTime: Date;
shiftEndTime: Date;
} & ShiftSignupDTO;

export type SkillDTO = {
id: string;
Expand Down

0 comments on commit 7f92731

Please sign in to comment.