refactor
This commit is contained in:
parent
022d35217a
commit
dcf19c4655
@ -1,6 +1,12 @@
|
|||||||
import { RequestHandler } from "express";
|
import { RequestHandler } from "express";
|
||||||
import { Inbox } from "@/src/models/inboxModel";
|
import { Inbox } from "@/src/models/inboxModel";
|
||||||
import { createNewEventMessages, getAllMessagesSorted, getMessage } from "@/src/services/inboxService";
|
import {
|
||||||
|
createNewEventMessages,
|
||||||
|
deleteAllMessagesRead,
|
||||||
|
deleteMessageRead,
|
||||||
|
getAllMessagesSorted,
|
||||||
|
getMessage
|
||||||
|
} from "@/src/services/inboxService";
|
||||||
import { getAccountIdForRequest } from "@/src/services/loginService";
|
import { getAccountIdForRequest } from "@/src/services/loginService";
|
||||||
import { addItems, getInventory } from "@/src/services/inventoryService";
|
import { addItems, getInventory } from "@/src/services/inventoryService";
|
||||||
import { logger } from "@/src/utils/logger";
|
import { logger } from "@/src/utils/logger";
|
||||||
@ -12,12 +18,12 @@ export const inboxController: RequestHandler = async (req, res) => {
|
|||||||
|
|
||||||
if (deleteId) {
|
if (deleteId) {
|
||||||
if (deleteId === "DeleteAllRead") {
|
if (deleteId === "DeleteAllRead") {
|
||||||
await Inbox.deleteMany({ ownerId: accountId, r: true });
|
await deleteAllMessagesRead(accountId);
|
||||||
res.status(200).end();
|
res.status(200).end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await Inbox.findOneAndDelete({ _id: deleteId, r: true });
|
await deleteMessageRead(deleteId as string);
|
||||||
res.status(200).end();
|
res.status(200).end();
|
||||||
} else if (messageId) {
|
} else if (messageId) {
|
||||||
const message = await getMessage(messageId as string);
|
const message = await getMessage(messageId as string);
|
||||||
@ -71,6 +77,7 @@ export const inboxController: RequestHandler = async (req, res) => {
|
|||||||
//newly created event messages must be newer than account.LatestEventMessageDate
|
//newly created event messages must be newer than account.LatestEventMessageDate
|
||||||
await createNewEventMessages(req);
|
await createNewEventMessages(req);
|
||||||
const messages = await getAllMessagesSorted(accountId);
|
const messages = await getAllMessagesSorted(accountId);
|
||||||
res.json({ Inbox: messages });
|
const inbox = messages.map(m => m.toJSON());
|
||||||
|
res.json({ Inbox: inbox });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -5,9 +5,9 @@ import { Request } from "express";
|
|||||||
import messages from "@/static/fixed_responses/messages.json";
|
import messages from "@/static/fixed_responses/messages.json";
|
||||||
import { logger } from "@/src/utils/logger";
|
import { logger } from "@/src/utils/logger";
|
||||||
|
|
||||||
export const getAllMessagesSorted = async (accountId: string): Promise<IMessageDatabase[]> => {
|
export const getAllMessagesSorted = async (accountId: string): Promise<HydratedDocument<IMessageDatabase>[]> => {
|
||||||
const inbox = await Inbox.find({ ownerId: accountId }).sort({ date: -1 });
|
const inbox = await Inbox.find({ ownerId: accountId }).sort({ date: -1 });
|
||||||
return inbox.map(message => message.toJSON());
|
return inbox;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getMessage = async (messageId: string): Promise<HydratedDocument<IMessageDatabase>> => {
|
export const getMessage = async (messageId: string): Promise<HydratedDocument<IMessageDatabase>> => {
|
||||||
@ -19,11 +19,11 @@ export const getMessage = async (messageId: string): Promise<HydratedDocument<IM
|
|||||||
return message;
|
return message;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const deleteMessage = async (messageId: string): Promise<void> => {
|
export const deleteMessageRead = async (messageId: string): Promise<void> => {
|
||||||
await Inbox.findOneAndDelete({ _id: messageId });
|
await Inbox.findOneAndDelete({ _id: messageId, r: true });
|
||||||
};
|
};
|
||||||
|
|
||||||
export const deleteAllMessages = async (accountId: string): Promise<void> => {
|
export const deleteAllMessagesRead = async (accountId: string): Promise<void> => {
|
||||||
await Inbox.deleteMany({ ownerId: accountId, r: true });
|
await Inbox.deleteMany({ ownerId: accountId, r: true });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user