Skip to content

Commit

Permalink
added endpoints for staff
Browse files Browse the repository at this point in the history
  • Loading branch information
benymng committed Nov 23, 2023
1 parent bf2318c commit aa8950d
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 11 deletions.
5 changes: 4 additions & 1 deletion backend/typescript/graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import simpleEntityResolvers from "./resolvers/simpleEntityResolvers";
import simpleEntityType from "./types/simpleEntityType";
import userResolvers from "./resolvers/userResolvers";
import userType from "./types/userType";
import staffType from "./types/staffType";
import staffResolver from "./resolvers/staffResolver";

const query = gql`
type Query {
Expand All @@ -29,12 +31,13 @@ const mutation = gql`
`;

const executableSchema = makeExecutableSchema({
typeDefs: [query, mutation, authType, entityType, simpleEntityType, userType],
typeDefs: [query, mutation, authType, entityType, simpleEntityType, userType, staffType],
resolvers: merge(
authResolvers,
entityResolvers,
simpleEntityResolvers,
userResolvers,
staffResolver,
),
});

Expand Down
53 changes: 53 additions & 0 deletions backend/typescript/graphql/resolvers/staffResolver.ts
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;
45 changes: 45 additions & 0 deletions backend/typescript/graphql/types/staffType.ts
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;

2 changes: 1 addition & 1 deletion backend/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint": "eslint . --ext .ts,.js",
"fix": "eslint . --ext .ts,.js --fix",
"postinstall": "tsc",
"prismaInitAndRun": "npx prisma migrate deploy && nodemon -L"
"prismaInitAndRun": "npx prisma migrate deploy && npx prisma generate && nodemon -L"
},
"keywords": [],
"author": "",
Expand Down
18 changes: 9 additions & 9 deletions backend/typescript/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ model warning {

model staff {
id Int @id @default(autoincrement())
role_id Int
role role @relation(fields: [role_id], references: [id])
first_name String
last_name String
roleId Int
role role @relation(fields: [roleId], references: [id])
firstName String
lastName String
email String @unique
phone_number String?
display_name String
profile_picture_link String?
tasks_assigned task[]
warnings_assigned warning[]
phoneNumber String?
displayName String?
profilePictureLink String?
tasksAssigned task[]
warningsAssigned warning[]
notifications notification[]
}

Expand Down
78 changes: 78 additions & 0 deletions backend/typescript/services/implementations/staffService.ts
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;
41 changes: 41 additions & 0 deletions backend/typescript/services/interfaces/staffService.ts
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>>;
}

0 comments on commit aa8950d

Please sign in to comment.