mirror of
https://github.com/Discreetly/server.git
synced 2026-01-10 21:38:02 -05:00
feature(express) express endpoint for sending system messages to a specific room
This commit is contained in:
@@ -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: {
|
||||
|
||||
@@ -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" });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user