Skip to content

Commit

Permalink
feat: notification-resident relation working
Browse files Browse the repository at this point in the history
Co-authored-by: Kathleen Xiong <[email protected]>
  • Loading branch information
Danie1T and KathleenX7 committed Nov 23, 2023
1 parent 7c90474 commit 1158c81
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 35 deletions.
7 changes: 7 additions & 0 deletions backend/typescript/graphql/resolvers/residentResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ const residentResolvers = {
const deletedResident = await residentService.delete_resident(parseInt(id));
return deletedResident;
},
createResidentWithNotification: async (
_parent: undefined,
{ email, id }: { email: String, id: number },
): Promise<ResidentDTO> => {
const newResident = await residentService.create_resident_with_notification(email, id);
return newResident;
},
},
};

Expand Down
2 changes: 1 addition & 1 deletion backend/typescript/graphql/types/adminType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const adminType = gql`
id: ID!
message: String!
created_at: String!
author_id: ID!
residents: [ResidentDTO!]
}
extend type Query {
Expand Down
3 changes: 2 additions & 1 deletion backend/typescript/graphql/types/residentType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const residentType = gql`
credits: Float
date_joined: String
date_left: String
notification: [NotificationDTO!]
notifications: [NotificationDTO!]
}
input CreateResidentDTO {
Expand Down Expand Up @@ -52,6 +52,7 @@ const residentType = gql`
updateResident(id: ID!, resident: UpdateResidentDTO!): ResidentDTO!
addResident(resident: CreateResidentDTO!): ResidentDTO!
deleteResident(id: ID!): ResidentDTO!
createResidentWithNotification(email: String, notif_id: ID): ResidentDTO!
}
`;

Expand Down
8 changes: 4 additions & 4 deletions backend/typescript/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@ model resident {
date_left DateTime?
tasks task[]
warnings warning[]
notifications notification[] //@relation("NotificationUserRelation")
notifications notification[] @relation("NotificationUserRelation")
}

model notification {
id Int @id @default(autoincrement())
message String
author_id Int
author resident? @relation(fields: [author_id], references: [id]) //change back to staff
//author_id Int
//author resident? @relation(fields: [author_id], references: [id]) //change back to staff
created_at DateTime @default(now())
//residents resident[] @relation("NotificationUserRelation")
residents resident[] @relation("NotificationUserRelation")
}

//model notification_user {
Expand Down
62 changes: 35 additions & 27 deletions backend/typescript/services/implementations/adminService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AdminService implements IAdminService {
async getAllNotifications(): Promise<NotificationDTO[]>{
try {
const notifications = await prisma.notification.findMany({
// include: {residents: true}
include: {residents: true}
})
if(!notifications) throw new Error(`No residents found.`);
return notifications;
Expand All @@ -38,9 +38,9 @@ class AdminService implements IAdminService {
where : {
id: Number(id)
},
// include: {
// residents: true
// }
include: {
residents: true
}
});
if(!notification) throw new Error(`notification id ${id} not found`);

Expand All @@ -58,20 +58,28 @@ class AdminService implements IAdminService {
newNotification = await prisma.notification.create({
data: {
message: String(notif_message),
author: {
// connect: {id: this.staffId}
connect: {id: Number(resident_id)}
},
// residents: {
// connect: {
// id: Number(resident_id),
// },
// author: {
// // connect: {id: this.staffId}
// connect: {id: Number(resident_id)}
// // create: {
// // first_name: "TOM",
// // last_name: "BOB",
// // email: "[email protected]",
// // credits: 1.0,
// // date_joined: "2011-10-05T14:48:00.000Z",
// // display_name: "Display_Name"
// // }
// },
residents: {
connect: {
id: Number(resident_id),
},

// }
}
},
// include: {
// residents: true
// }
include: {
residents: true
}
})

// ASK WILLIAM ABOUT ADDING THINGS TO THE LIST OF RESIDENTS IN NOTIFICATION
Expand All @@ -97,7 +105,7 @@ class AdminService implements IAdminService {
where: {
date_left: null //check how we are implementing date_left
},
// include : { notifications: true }
include : { notifications: true }
})

if(!residents) throw new Error(`No residents found.`);
Expand All @@ -119,18 +127,18 @@ class AdminService implements IAdminService {
newNotification = await prisma.notification.create({
data: {
message: String(notif_message),
author: {
connect: {id: this.staffId}
},
// author: {
// connect: activeResidents.map(resident => ({
// id: resident.id
// }))
// }
// connect: {id: this.staffId}
// },
residents: {
connect: activeResidents.map(resident => ({
id: resident.id
}))
}
},
// include: {
// residents: true
// }
include: {
residents: true
}
})

// let resident: Prisma.residentUncheckedCreateInput;
Expand Down
29 changes: 29 additions & 0 deletions backend/typescript/services/implementations/residentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,35 @@ class ResidentService implements IResidentService {
}
}

async create_resident_with_notification(email: String, notif_id: number): Promise<ResidentDTO> {
let newResident: ResidentDTO;
try{
newResident = await Prisma.resident.create({
data: {
first_name: "first",
last_name: "last",
email: String(email),
phone_number: "1231234123",
display_name: "display",
credits: 1.0,
date_joined: "2011-10-05T14:48:00.000Z",
notifications: {
connect: {
id: 3,
},
}
},
include: {
notifications: true
}
})

return newResident;

}catch(error){
throw error;
}
}
}

export default ResidentService;
4 changes: 2 additions & 2 deletions backend/typescript/services/interfaces/adminService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { ResidentDTO } from "./residentService"

export interface NotificationDTO {
id: number;
author_id: number;
//author_id: number;
message: string;
created_at: Date;
// residents?: ResidentDTO[]; //might need to change when integrated
residents?: ResidentDTO[]; //might need to change when integrated
}

// export interface NotificationUserDTO {
Expand Down
2 changes: 2 additions & 0 deletions backend/typescript/services/interfaces/residentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ interface IResidentService {
* @throws Error if retrieval fails
*/
get_residents_by_id(residentId: number[]): Promise<Array<ResidentDTO>>;

create_resident_with_notification(email: String, notif_id: number): Promise<ResidentDTO>;
}

export default IResidentService;

0 comments on commit 1158c81

Please sign in to comment.