From ebc6d90261908e8b8aef53d32179b954ce5794fa Mon Sep 17 00:00:00 2001 From: Master Date: Sat, 6 Jul 2024 01:15:01 +0800 Subject: [PATCH] add inbox system --- src/controllers/api/inboxController.ts | 34 +++++++++++++-- src/models/inboxModel.ts | 56 ++++++++++++++++++++++++ src/services/inboxService.ts | 57 ++++++++++++++++++++++++ src/types/inboxTypes.ts | 60 ++++++++++++++++++++++++++ 4 files changed, 204 insertions(+), 3 deletions(-) create mode 100644 src/models/inboxModel.ts create mode 100644 src/services/inboxService.ts create mode 100644 src/types/inboxTypes.ts diff --git a/src/controllers/api/inboxController.ts b/src/controllers/api/inboxController.ts index cbd2af06..ab662878 100644 --- a/src/controllers/api/inboxController.ts +++ b/src/controllers/api/inboxController.ts @@ -1,8 +1,36 @@ import { RequestHandler } from "express"; -import inbox from "@/static/fixed_responses/inbox.json"; +import { deleteAllReadInbox, deleteInbox, getInboxReponse, readInbox } from "@/src/services/inboxService"; +import { getAccountIdForRequest } from "@/src/services/loginService"; -const inboxController: RequestHandler = (_req, res) => { - res.json(inbox); +// eslint-disable-next-line @typescript-eslint/no-misused-promises +const inboxController: RequestHandler = async (req, res) => { + const accountId = await getAccountIdForRequest(req); + const messageId = req.query.messageId as string; + const deleteId = req.query.deleteId as string; + const lastMessage = req.query.lastMessage as string; + + if (messageId) { + const inbox = await readInbox(messageId); + res.json({ Inbox: [inbox] }); + } + + if (deleteId) { + if (deleteId == "DeleteAllRead") { + await deleteAllReadInbox(accountId); + } else { + await deleteInbox(deleteId); + } + const result = await getInboxReponse(accountId); + res.json(result); + } + + if (lastMessage) { + /* empty */ + } + + const result = await getInboxReponse(accountId); + if (result) res.json(result); + else res.json({ Inbox: [] }); }; export { inboxController }; diff --git a/src/models/inboxModel.ts b/src/models/inboxModel.ts new file mode 100644 index 00000000..966da4b8 --- /dev/null +++ b/src/models/inboxModel.ts @@ -0,0 +1,56 @@ +import { Schema, Types, model } from "mongoose"; +import { ICountedAttDatabase, IInboxDatabase, IInboxReponseClient } from "@/src/types/inboxTypes"; +import { toMongoDate, toOid } from "@/src/helpers/inventoryHelpers"; + +const countedAttSchema = new Schema( + { + ItemType: String, + ItemCount: Number + }, + { _id: false } +); + +countedAttSchema.set("toJSON", { + virtuals: true, + transform(_document, returnedObject) { + delete returnedObject._id; + delete returnedObject.__v; + } +}); + +const inboxSchema = new Schema( + { + OwnerId: Types.ObjectId, + sndr: String, + msg: String, + sub: String, + icon: String, + highPriority: Boolean, + date: Date, + r: Boolean, + countedAtt: [countedAttSchema] + }, + { id: false } +); + +inboxSchema.virtual("messageId").get(function () { + return toOid(this._id); +}); + +inboxSchema.set("toJSON", { + virtuals: true, + transform(_document, returnedObject) { + delete returnedObject._id; + delete returnedObject.__v; + delete returnedObject.OwnerId; + const inboxDatabase = returnedObject as IInboxDatabase; + const inboxReponse = returnedObject as IInboxReponseClient; + inboxReponse.date = toMongoDate(inboxDatabase.date); + } +}); + +inboxSchema.set("toObject", { + virtuals: true +}); + +export const Inbox = model("Inbox", inboxSchema); diff --git a/src/services/inboxService.ts b/src/services/inboxService.ts new file mode 100644 index 00000000..96565d94 --- /dev/null +++ b/src/services/inboxService.ts @@ -0,0 +1,57 @@ +import { Document, Types } from "mongoose"; +import { Inbox } from "@/src/models/inboxModel"; +import { IInboxDatabase, IInboxReponseClient } from "@/src/types/inboxTypes"; + +export const getInboxList = async ( + accountId: string +): Promise< + (Document & + IInboxDatabase & { + _id: Types.ObjectId; + })[] +> => { + const inboxList = await Inbox.find({ OwnerId: accountId }); + return inboxList; +}; + +export const getInboxReponse = async (accountId: string): Promise<{ Inbox: IInboxReponseClient[] }> => { + const inboxList = await getInboxList(accountId); + const list: IInboxReponseClient[] = []; + inboxList.forEach(inbox => { + list.push(inbox.toJSON()); + }); + return { Inbox: list }; +}; + +export const readInbox = async (inboxId: string) => { + const inbox = await Inbox.findByIdAndUpdate(inboxId, { r: true }); + if (!inbox) { + throw new Error("inbox not found"); + } + return inbox.toJSON(); +}; + +export const addInbox = async (inboxData: IInboxDatabase) => { + console.log(inboxData); + const inbox = new Inbox(inboxData); + try { + await inbox.save(); + } catch (error) { + if (error instanceof Error) { + throw new Error(error.message); + } + throw new Error("error creating inbox that is not of instance Error"); + } +}; + +export const deleteInbox = async (inboxId: string) => { + const inbox = await Inbox.findByIdAndDelete(inboxId); + if (!inbox) { + throw new Error("inbox not found"); + } + return inbox; +}; + +export const deleteAllReadInbox = async (accountId: string): Promise => { + await Inbox.deleteMany({ OwnerId: accountId, r: true }); +}; diff --git a/src/types/inboxTypes.ts b/src/types/inboxTypes.ts new file mode 100644 index 00000000..b00728d9 --- /dev/null +++ b/src/types/inboxTypes.ts @@ -0,0 +1,60 @@ +import { Types } from "mongoose"; +import { IMongoDate, IOid } from "./commonTypes"; + +export interface ICountedAttDatabase { + ItemType: string; + ItemCount: number; +} + +export interface IInboxArgDatabase { + Key: string; + Tag: number; +} + +export interface IInboxDatabase { + OwnerId: Types.ObjectId; + sndr: string; + msg: string; + arg?: IInboxArgDatabase[]; + att?: string[]; + sub: string; + icon: string; + startDate?: Date; + endDate?: Date; + url?: string; + highPriority: boolean; + lowPrioNewPlayers?: boolean; + CrossPlatform?: boolean; + date: Date; + r: boolean; + countedAtt?: ICountedAttDatabase[]; +} + +export interface IInboxDatabaseDocument extends IInboxDatabase { + id: string; +} + +export interface IInboxReponseClient { + messageId: IOid; + sndr: string; + msg: string; + arg: IInboxArgDatabase[]; + att?: string[]; + sub: string; + icon: string; + startDate?: IMongoDate; + endDate?: IMongoDate; + url?: string; + highPriority: boolean; + lowPrioNewPlayers?: boolean; + CrossPlatform?: boolean + date: IMongoDate; + r: boolean; + countedAtt?: ICountedAttDatabase[]; +} + +export interface IInboxRequest {} + +export interface IInboxReponse { + Inbox: IInboxReponseClient[]; +}