Skip to content

Commit

Permalink
feature: [10] Notification service 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph0926 committed Sep 4, 2024
1 parent f589b9f commit fc83d5a
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 1 deletion.
2 changes: 1 addition & 1 deletion server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
131 changes: 131 additions & 0 deletions server/src/services/notification.service.ts
Original file line number Diff line number Diff line change
@@ -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<ResponseType<NotificationType>> {
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<ResponseType<NotificationType>> {
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<ResponseType<NotificationType[]>> {
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<void> {
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<void> {
try {
await db.notification.delete({
where: {
id: notificationId,
},
});
} catch (error) {
log.error(error);
throw new Error(error);
}
}
16 changes: 16 additions & 0 deletions server/src/types/notification.type.ts
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit fc83d5a

Please sign in to comment.