Skip to content

Commit

Permalink
feat: change Prisma built-ins to DTOs
Browse files Browse the repository at this point in the history
Co-authored-by: Kathleen Xiong <KathleenX7@users.noreply.github.com>
  • Loading branch information
Danie1T and KathleenX7 committed Nov 11, 2023
1 parent 8f68db0 commit d77f117
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 21 deletions.
16 changes: 10 additions & 6 deletions backend/typescript/graphql/resolvers/adminResolvers.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { Prisma } from "@prisma/client"
import AdminService from "../../services/implementations/adminService"
import {IAdminService} from "../../services/interfaces/adminService"
import {
IAdminService,
ResidentDTO,
NotificationDTO
} from "../../services/interfaces/adminService"

// change staff_id to StaffService when its done
const adminService: IAdminService = new AdminService(1)
const adminService: IAdminService = new AdminService(3)

const adminResolvers = {
Query: {
notificationById: async (
_parent: undefined,
{ id }: { id: number},
): Promise<Prisma.notificationCreateInput> => {
): Promise<NotificationDTO> => {
return await adminService.getNotificationById(id)
},
activeResidents: async (
_parent: undefined,
): Promise<Prisma.residentUncheckedCreateInput[]> => {
): Promise<ResidentDTO[]> => {
const activeResidents = await adminService.getActiveResidents()
return activeResidents
}
Expand All @@ -25,14 +29,14 @@ const adminResolvers = {
sendNotification: async (
_parent: undefined,
{message, resident_id}: {message: String; resident_id: number},
): Promise<Prisma.notificationCreateInput> => {
): Promise<NotificationDTO> => {
const newNotification = await adminService.sendNotification(message, resident_id)
return newNotification
},
sendAnnouncement: async (
_parent: undefined,
{message}: {message: String},
): Promise<Prisma.notificationCreateInput> => {
): Promise<NotificationDTO> => {
const newAnnouncement = await adminService.sendAnnouncement(message);
return newAnnouncement
}
Expand Down
27 changes: 27 additions & 0 deletions backend/typescript/graphql/types/adminType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,33 @@ import { Prisma } from "@prisma/client"

const adminType = gql`
type NotificationDTO {
id: ID!
author_id: ID!
message: String!
created_at: String!
residents: [NotificationUserDTO]!
}
type NotificationUserDTO {
notification_id: ID!
recipient_id: ID!
}
type ResidentDTO {
id: ID!
first_name: String!
last_name: String!
email: String!
phone_number: String
display_name: String!
profile_picture_link: String
birthdate: String
credits: Float
date_joined: String!
date_left: String
}
extend type Query {
notificationById(id: ID!): String
activeResidents: String
Expand Down
28 changes: 17 additions & 11 deletions backend/typescript/services/implementations/adminService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Prisma } from "@prisma/client";
import prisma from "../../prisma";
import type {
IAdminService
IAdminService,
ResidentDTO,
NotificationDTO,
} from "../interfaces/adminService";
// import type ResidentDTO from "../interfaces/FILEOFDTO";
// import type IStaffService from "../interfaces/FILEOFDTO";
//import type IStaffService from "../interfaces/FILEOFDTO";
import logger from "../../utilities/logger";
import { getErrorMessage } from "../../utilities/errorUtils";

Expand All @@ -17,7 +19,7 @@ class AdminService implements IAdminService {
this.staffId = staffId
}

async getNotificationById(id: number): Promise<Prisma.notificationCreateInput>{
async getNotificationById(id: number): Promise<NotificationDTO>{
try {
const notification = await prisma.notification.findUnique({
where : {
Expand All @@ -33,19 +35,21 @@ class AdminService implements IAdminService {
}
}

async sendNotification(notif_message: String, resident_id: number): Promise<Prisma.notificationCreateInput>{
async sendNotification(notif_message: String, resident_id: number): Promise<NotificationDTO>{
// let newNotificationUserLink: Prisma.notification_userCreateInput;
let newNotification: Prisma.notificationCreateInput;
let newNotification: NotificationDTO;
try{
newNotification = await prisma.notification.create({
data: {
message: String(notif_message),
author_id: this.staffId,
author: {
connect: {id: this.staffId}
},
residents: {
create: [
{ recipient: {
connect: {
id: resident_id
id: Number(resident_id)
}
}}
],
Expand All @@ -70,7 +74,7 @@ class AdminService implements IAdminService {
}
}

async getActiveResidents(): Promise<Prisma.residentUncheckedCreateInput[]>{
async getActiveResidents(): Promise<ResidentDTO[]>{
try{
const residents = await prisma.resident.findMany({
where: {
Expand All @@ -88,16 +92,18 @@ class AdminService implements IAdminService {
}


async sendAnnouncement(notif_message: String): Promise<Prisma.notificationCreateInput>{
async sendAnnouncement(notif_message: String): Promise<NotificationDTO>{
// let newNotificationUserLink: Prisma.notification_userCreateInput;
let newNotification: Prisma.notificationCreateInput;
let newNotification: NotificationDTO;
try{
const activeResidents = await this.getActiveResidents()

newNotification = await prisma.notification.create({
data: {
message: String(notif_message),
author_id: this.staffId,
author: {
connect: {id: this.staffId}
},
residents: {
create: activeResidents.map(resident => ({
recipient: {
Expand Down
39 changes: 35 additions & 4 deletions backend/typescript/services/interfaces/adminService.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
import { Prisma } from "@prisma/client";

// export interface NotificationRequestDTO {
// message: string;
// }

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

export interface NotificationUserDTO {
notification_id: number;
recipient_id: number;
}

export interface ResidentDTO {
id: number;
first_name: string;
last_name: string;
email: string;
phone_number?: string | null;
display_name: string;
profile_picture_link?: string | null;
birthdate?: Date | null;
credits: number;
date_joined: Date;
date_left?: Date | null;
}

export interface IAdminService {

//Move to Residents Service when its done
getActiveResidents(): Promise<Prisma.residentUncheckedCreateInput[]>;
getActiveResidents(): Promise<ResidentDTO[]>;

/**
* Get a notification by a defined id
* @param id notification id
* @returns a notificationCreateInput associated with the notification id
* @throws Error if retrieval fails
*/
getNotificationById(id: number): Promise<Prisma.notificationCreateInput>;
getNotificationById(id: number): Promise<NotificationDTO>;

/**
* Post a notification to the specified resident
Expand All @@ -20,7 +51,7 @@ export interface IAdminService {
* @returns a notificationCreateInput associated with the posted notification
* @throws Error if creation fails
*/
sendNotification(notif_message: String, resident_id: number): Promise<Prisma.notificationCreateInput>;
sendNotification(notif_message: String, resident_id: number): Promise<NotificationDTO>;



Expand All @@ -30,5 +61,5 @@ export interface IAdminService {
* @returns the new updated notificationCreateInput
* @throws Error if creation fails
*/
sendAnnouncement(notif_messagej: String ): Promise<Prisma.notificationCreateInput>;
sendAnnouncement(notif_messagej: String ): Promise<NotificationDTO>;
}

0 comments on commit d77f117

Please sign in to comment.