From fc83d5ad0dad4430624ec300badc61f3e5502758 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=98=81=ED=9B=88?= Date: Wed, 4 Sep 2024 14:19:26 +0900 Subject: [PATCH] =?UTF-8?q?feature:=20[10]=20Notification=20service=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/prisma/schema.prisma | 2 +- server/src/services/notification.service.ts | 131 ++++++++++++++++++++ server/src/types/notification.type.ts | 16 +++ 3 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 server/src/services/notification.service.ts create mode 100644 server/src/types/notification.type.ts diff --git a/server/prisma/schema.prisma b/server/prisma/schema.prisma index fc0f618..bd93019 100644 --- a/server/prisma/schema.prisma +++ b/server/prisma/schema.prisma @@ -28,7 +28,7 @@ model Notification { createdAt DateTime @default(now()) groupName String - emails String? + emails String userId String user User @relation(fields: [userId], references: [id]) diff --git a/server/src/services/notification.service.ts b/server/src/services/notification.service.ts new file mode 100644 index 0000000..419bf45 --- /dev/null +++ b/server/src/services/notification.service.ts @@ -0,0 +1,131 @@ +import { db } from "@/server/db"; +import log from "@/server/log"; +import { ResponseType } from "@/types/common.type"; +import { NotificationType } from "@/types/notification.type"; + +export async function createNotificationGroup( + data: NotificationType +): Promise> { + try { + const result = await db.notification.create({ + data, + }); + + return { + data: result, + message: "Notification이 정상적으로 생성되었습니다.", + success: true, + }; + } catch (error) { + log.error(error); + return { + data: null, + message: "Notification을 생성하는데 실패하였습니다.", + success: false, + }; + } +} + +export async function getSingleNotificationGroup( + notificationId: string +): Promise> { + try { + const notification: NotificationType = (await db.notification.findFirst({ + where: { + id: notificationId, + }, + orderBy: { + createdAt: "desc", + }, + })) as unknown as NotificationType; + + if (!notification) { + return { + data: null, + message: "Notification가 존재하지 않습니다.", + success: false, + }; + } + + return { + data: notification, + message: "Notification을 성공적으로 불러왔습니다.", + success: true, + }; + } catch (error) { + log.error(error); + return { + data: null, + message: "Notification을 불러오는데 실패하였습니다.", + success: false, + }; + } +} + +export async function getAllNotificationGroups( + userId: string +): Promise> { + try { + const notifications: NotificationType[] = (await db.notification.findMany({ + where: { + userId, + }, + orderBy: { + createdAt: "desc", + }, + })) as unknown as NotificationType[]; + + if (!notifications || notifications.length === 0) { + return { + data: null, + message: "Notifications가 존재하지 않습니다.", + success: false, + }; + } + + return { + data: notifications, + message: "Notifications을 성공적으로 불러왔습니다.", + success: true, + }; + } catch (error) { + log.error(error); + return { + data: null, + message: "Notifications을 불러오는데 실패하였습니다.", + success: false, + }; + } +} + +export async function updateNotificationGroup( + notificationId: string, + data: NotificationType +): Promise { + try { + await db.notification.update({ + where: { + id: notificationId, + }, + data, + }); + } catch (error) { + log.error(error); + throw new Error(error); + } +} + +export async function deleteNotificationGroup( + notificationId: string +): Promise { + try { + await db.notification.delete({ + where: { + id: notificationId, + }, + }); + } catch (error) { + log.error(error); + throw new Error(error); + } +} diff --git a/server/src/types/notification.type.ts b/server/src/types/notification.type.ts new file mode 100644 index 0000000..a7a4982 --- /dev/null +++ b/server/src/types/notification.type.ts @@ -0,0 +1,16 @@ +export type NotificationType = { + id?: string; + userId: string; + groupName: string; + emails: string; + createdAt?: Date; +}; + +export type EmailLocalsType = { + sender?: string; + appLink: string; + appIcon: string; + appName: string; + subject?: string; + username?: string; +};