feature(express) express endpoint for sending system messages to a specific room

This commit is contained in:
Tanner Shaw
2023-08-08 14:38:04 -05:00
parent 83c594b9fb
commit f528650d4f
2 changed files with 19 additions and 2 deletions

View File

@@ -107,9 +107,13 @@ export function findUpdatedRooms(roomIds: string[]): Promise<RoomI[]> {
});
}
export function createSystemMessages(message: string): Promise<any> {
return prisma.rooms.findMany()
export function createSystemMessages(message: string, roomId?: string): Promise<any> {
const query = roomId ? { where: { roomId } } : undefined;
return prisma.rooms.findMany(query)
.then(rooms => {
if (roomId && rooms.length === 0) {
Promise.reject('Room not found')
}
const createMessages = rooms.map(room => {
return prisma.messages.create({
data: {

View File

@@ -195,4 +195,17 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) {
res.status(500).json({ error: "Internal Server Error" });
}
});
app.post("/admin/message/:roomId", adminAuth, async (req, res) => {
const { roomId } = req.params;
const { message } = req.body;
pp(String("Express: sending system message: " + message + " to " + roomId));
try {
await createSystemMessages(message, roomId);
res.status(200).json({ message: "Message sent to room " + roomId });
} catch (err) {
console.error(err);
res.status(500).json({ error: "Internal Server Error" });
}
});
}