From bbe44cb0a12de13b97eee392be0ffb84e4a6afb3 Mon Sep 17 00:00:00 2001 From: AlbanSdl Date: Wed, 15 May 2024 16:58:09 +0200 Subject: [PATCH] add daily reports on discord --- web/.env.dist | 1 + web/src/apiRouter.ts | 46 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/web/.env.dist b/web/.env.dist index 3b004d1..10df9f4 100644 --- a/web/.env.dist +++ b/web/.env.dist @@ -12,3 +12,4 @@ CODE_LIFETIME=300 TOTAL_JOYCONS=4 API_KEY=test BORROW_TIMEOUT=300 +DISCORD_WEBHOOK_URL= \ No newline at end of file diff --git a/web/src/apiRouter.ts b/web/src/apiRouter.ts index 8901667..3133011 100644 --- a/web/src/apiRouter.ts +++ b/web/src/apiRouter.ts @@ -66,7 +66,51 @@ apiRouter.get("/ping", async (request: Request, response: Response) => { }); apiRouter.get("/reports", async (request: Request, response: Response) => { - // + const borrows = await prisma.borrow.findMany({ + where: { + returnOpeningId: { + not: null, + }, + }, + include: { + user: true, + }, + }); + if (borrows.length) { + logger.info(`Generating reports for ${borrows.length} users !`); + const count = borrows.reduce((acc, borrow) => acc + borrow.joyconsTaken, 0); + fetch(process.env.DISCORD_WEBHOOK_URL, { + method: "POST", + body: JSON.stringify({ + content: null, + embeds: [ + { + title: "⚠️ Emprunts non rendus", + description: `🚨 ${count} joycon${count > 1 ? "s" : ""} n'${ + count > 1 ? "ont" : "a" + } pas été rendu${count > 1 ? "s" : ""} !`, + color: 11468800, + fields: borrows.map((borrow) => ({ + name: `[${borrow.user.firstName} ${ + borrow.user.lastName + }](mailto:${ + borrow.user.mail + }?subject=Joycons%20non%20rendus&cc=ung@utt.fr&body=Coucou%20${borrow.user.firstName.replace( + /\s/g, + "%20" + )},%0D%0A%0D%0AIl%20semblerait%20que%20tu%20ne%20nous%20a%20pas%20rendu%20les%20joycons%20que%20tu%20as%20empruntés%20aujourd'hui.%0D%0AMerci%20de%20les%20rendre%20au%20plus%20vite.%0D%0A%0D%0ACordialement`, + value: `${borrow.joyconsTaken} joycon${ + borrow.joyconsTaken > 1 ? "s" : "" + } non rendu(s)`, + })), + timestamp: new Date().toISOString(), + }, + ], + attachments: [], + }), + }); + } + return response.status(200).send("Generating reports"); }); apiRouter.use(async (request: Request, response: Response) => {