add inbox system

This commit is contained in:
Master 2024-07-06 01:15:01 +08:00
parent a4ec3833bf
commit ebc6d90261
4 changed files with 204 additions and 3 deletions

View File

@ -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 };

56
src/models/inboxModel.ts Normal file
View File

@ -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<ICountedAttDatabase>(
{
ItemType: String,
ItemCount: Number
},
{ _id: false }
);
countedAttSchema.set("toJSON", {
virtuals: true,
transform(_document, returnedObject) {
delete returnedObject._id;
delete returnedObject.__v;
}
});
const inboxSchema = new Schema<IInboxDatabase>(
{
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);

View File

@ -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<unknown, object, IInboxDatabase> &
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<void> => {
await Inbox.deleteMany({ OwnerId: accountId, r: true });
};

60
src/types/inboxTypes.ts Normal file
View File

@ -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[];
}