Skip to content

Commit

Permalink
add daily reports on discord
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbanSdl committed May 15, 2024
1 parent 9fccc05 commit bbe44cb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions web/.env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ CODE_LIFETIME=300
TOTAL_JOYCONS=4
API_KEY=test
BORROW_TIMEOUT=300
DISCORD_WEBHOOK_URL=
46 changes: 45 additions & 1 deletion web/src/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit bbe44cb

Please sign in to comment.