Commit 820f4259 authored by Ramiro Nascimento's avatar Ramiro Nascimento
Browse files

🗃 add event to notification

parent cba4ded5
Showing with 44 additions and 18 deletions
+44 -18
-- AlterTable
ALTER TABLE "notifications" ADD COLUMN "eventId" INTEGER;
-- AddForeignKey
ALTER TABLE "notifications" ADD CONSTRAINT "notifications_eventId_fkey" FOREIGN KEY ("eventId") REFERENCES "events"("id") ON DELETE SET NULL ON UPDATE CASCADE;
......@@ -8,7 +8,7 @@ datasource db {
}
model Event {
id Int @id @default(autoincrement())
id Int @id @default(autoincrement())
authorId Int
patientId Int?
eventType EventType?
......@@ -18,22 +18,25 @@ model Event {
endTime String
status EventStatus?
active Boolean
updatedAt DateTime? @updatedAt
author User @relation("authorEvent", fields: [authorId], references: [id])
patient User? @relation("patientEvent", fields: [patientId], references: [id])
updatedAt DateTime? @updatedAt
author User @relation("authorEvent", fields: [authorId], references: [id])
patient User? @relation("patientEvent", fields: [patientId], references: [id])
notifications Notification[]
@@map("events")
}
model Notification {
id Int @id @default(autoincrement())
type NotificationType?
userId Int?
title String?
message String?
createdAt DateTime @default(now())
sentAt DateTime?
user User? @relation(fields: [userId], references: [id])
id Int @id @default(autoincrement())
type NotificationType?
eventId Int?
userId Int?
title String?
message String?
createdAt DateTime @default(now())
sentAt DateTime?
user User? @relation(fields: [userId], references: [id])
event Event? @relation(fields: [eventId], references: [id])
@@map("notifications")
}
......
import { NotificationType, Prisma, PrismaClient } from "@prisma/client";
import { NotificationType, PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
......@@ -9,18 +9,36 @@ export const getNotificationsByUserId = async (userId) => {
},
select: {
id: true,
user: true,
type: true,
title: true,
message: true,
createdAt: true,
sentAt: true,
event: {
select: {
id: true,
author: true,
startTime: true,
date: true,
},
},
},
});
};
export const createNotification = async (data: { userId: number; type: NotificationType; title: string; message: string; sentAt: Date; }) => {
const notification = prisma.notification.create({
type Event = "";
export const createNotification = async (data: {
userId: number;
eventId: number;
type: NotificationType;
title: string;
message: string;
sentAt: Date;
}) => {
const notification = await prisma.notification.create({
data: {
eventId: data.eventId,
userId: data.userId,
type: data.type,
title: data.title,
......
......@@ -12,7 +12,6 @@ export const getNotifications = (userId) => {
};
export async function pushNotification(event) {
logger.info(" -> Pushing notification for event " + event.id);
pushNotificationToPatient(event);
......@@ -62,6 +61,7 @@ function pushNotificationToPatient(event: any) {
createNotification({
userId: event.patient.id,
eventId: event.id,
message: message,
sentAt: new Date(),
title: title,
......@@ -77,7 +77,6 @@ function pushNotificationToPatient(event: any) {
message: message,
});
logger.info(" * Notification sent for patient " + event.patient.username);
}
function pushNotificationToAuthor(event: any) {
......@@ -114,6 +113,7 @@ function pushNotificationToAuthor(event: any) {
createNotification({
userId: event.patient.id,
eventId: event.id,
message: message,
sentAt: new Date(),
title: title,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment