-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
231 additions
and
11 deletions.
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
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,53 @@ | ||
import StaffService from "../../services/implementations/staffService"; | ||
import type { | ||
IStaffService, | ||
StaffDTO, | ||
CreateStaffDTO, | ||
UpdateStaffDTO, | ||
} from "../../services/interfaces/staffService"; | ||
|
||
const staffService: IStaffService = new StaffService(); | ||
|
||
const staffResolvers = { | ||
Query: { | ||
getStaffById: async ( | ||
_parent: undefined, | ||
{ id }: { id: string[] }, | ||
): Promise<Array<StaffDTO>> => { | ||
return staffService.getStaffById(id.map(Number)); | ||
}, | ||
getAllStaff: async (): Promise<Array<StaffDTO>> => { | ||
return staffService.getAllStaff(); | ||
}, | ||
}, | ||
Mutation: { | ||
addStaff: async ( | ||
_parent: undefined, | ||
{ staff }: { staff: CreateStaffDTO }, | ||
): Promise<StaffDTO> => { | ||
const newStaff = await staffService.addStaff(staff); | ||
return newStaff; | ||
}, | ||
updateStaff: async ( | ||
_parent: undefined, | ||
{ id, staff }: { id: string; staff: UpdateStaffDTO }, | ||
): Promise<StaffDTO> => { | ||
const newStaff = await staffService.updateStaff( | ||
parseInt(id, 10), | ||
staff, | ||
); | ||
return newStaff; | ||
}, | ||
deleteStaff: async ( | ||
_parent: undefined, | ||
{ id }: { id: string }, | ||
): Promise<StaffDTO> => { | ||
const deletedStaff = await staffService.deleteStaff( | ||
parseInt(id, 10), | ||
); | ||
return deletedStaff; | ||
}, | ||
}, | ||
}; | ||
|
||
export default staffResolvers; |
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 { gql } from "apollo-server-express" | ||
|
||
const staffType = gql ` | ||
type StaffDTO { | ||
id: ID! | ||
roleId: Float! | ||
firstName: String! | ||
lastName: String! | ||
email: String! | ||
phoneNumber: String | ||
displayName: String! | ||
profilePictureLink: String | ||
} | ||
input CreateStaffDTO { | ||
roleId: Float! | ||
firstName: String! | ||
lastName: String! | ||
email: String! | ||
phoneNumber: String | ||
displayName: String! | ||
profilePictureLink: String | ||
} | ||
input UpdateStaffDTO { | ||
roleId: Float! | ||
firstName: String! | ||
lastName: String! | ||
email: String! | ||
phoneNumber: String | ||
displayName: String! | ||
profilePictureLink: String | ||
} | ||
extend type Query { | ||
getStaffById(id: [ID!]): [StaffDTO!] | ||
getAllStaff: [StaffDTO!] | ||
} | ||
extend type Mutation { | ||
updateStaff(id: ID!, staff: UpdateStaffDTO!): StaffDTO! | ||
addStaff(staff: CreateStaffDTO!): StaffDTO! | ||
deleteStaff(id: ID!): StaffDTO! | ||
} | ||
`; | ||
|
||
export default staffType; | ||
|
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
78 changes: 78 additions & 0 deletions
78
backend/typescript/services/implementations/staffService.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,78 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
import type { | ||
IStaffService, | ||
StaffDTO, | ||
CreateStaffDTO, | ||
UpdateStaffDTO, | ||
} from "../interfaces/staffService"; | ||
import logger from "../../utilities/logger"; | ||
import { getErrorMessage } from "../../utilities/errorUtils"; | ||
|
||
const Prisma = new PrismaClient(); | ||
const Logger = logger(__filename); | ||
|
||
class StaffService implements IStaffService { | ||
async addStaff(staff: CreateStaffDTO): Promise<StaffDTO> { | ||
try { | ||
const newStaff = await Prisma.staff.create({ | ||
data: { ...staff }, | ||
}) | ||
return newStaff; | ||
} catch(error) { | ||
Logger.error(`Failed to create staff because ${getErrorMessage(error)}`) | ||
throw error; | ||
} | ||
} | ||
async updateStaff(id: number, staffInfo: UpdateStaffDTO): Promise<StaffDTO> { | ||
try { | ||
const updatedStaff = await Prisma.staff.update({ | ||
where: { id }, | ||
data: staffInfo, | ||
}); | ||
return updatedStaff; | ||
} catch(error) { | ||
Logger.error(`Failed to update staff #${id} because ${getErrorMessage(error)}`); | ||
throw error; | ||
} | ||
} | ||
async deleteStaff(id: number): Promise <StaffDTO> { | ||
try { | ||
const deletedStaff = await Prisma.staff.delete({ | ||
where: { id }, | ||
}); | ||
return deletedStaff; | ||
} catch(error) { | ||
Logger.error(`Failed to update staff #${id} because ${getErrorMessage(error)}`); | ||
throw error; | ||
} | ||
} | ||
async getAllStaff(): Promise<Array<StaffDTO>> { | ||
try { | ||
const allStaff = await Prisma.staff.findMany(); | ||
return allStaff; | ||
} catch (error: unknown) { | ||
Logger.error( | ||
`Failed to get all Staff because ${getErrorMessage(error)}`, | ||
); | ||
throw error; | ||
} | ||
|
||
} | ||
async getStaffById(id: number[]): Promise<Array<StaffDTO>> { | ||
try { | ||
const getStaffById = await Prisma.staff.findMany({ | ||
where: { id: { in: id } }, | ||
}); | ||
return getStaffById; | ||
} catch (error: unknown) { | ||
Logger.error( | ||
`Failed to get staff by IDs. IDs = ${id} because ${getErrorMessage( | ||
error, | ||
)}`, | ||
); | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
export default StaffService; |
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,41 @@ | ||
export interface StaffDTO { | ||
id: number; | ||
roleId: number; | ||
firstName: string; | ||
lastName: string; | ||
email: string; | ||
phoneNumber: string | null; | ||
displayName: string | null; | ||
profilePictureLink: string | null; | ||
} | ||
|
||
export interface CreateStaffDTO { | ||
id: number; | ||
roleId: number; | ||
firstName: string; | ||
lastName: string; | ||
email: string; | ||
phoneNumber: string | null; | ||
displayName: string | null; | ||
profilePictureLink: string | null; | ||
} | ||
|
||
export interface UpdateStaffDTO { | ||
id: number; | ||
roleId: number; | ||
firstName: string; | ||
lastName: string; | ||
email: string; | ||
phoneNumber: string | null; | ||
displayName: string | null; | ||
profilePictureLink: string | null; | ||
} | ||
|
||
|
||
export interface IStaffService { | ||
addStaff(reidentInfo: CreateStaffDTO): Promise<StaffDTO>; | ||
updateStaff(staffId: number, staffInfo: UpdateStaffDTO): Promise<StaffDTO> | ||
deleteStaff(residentId: number): Promise <StaffDTO>; | ||
getAllStaff(): Promise<Array<StaffDTO>>; | ||
getStaffById(staffId: number[]): Promise<Array<StaffDTO>>; | ||
} |