Skip to content

Commit bbe44cb

Browse files
committed
add daily reports on discord
1 parent 9fccc05 commit bbe44cb

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

web/.env.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ CODE_LIFETIME=300
1212
TOTAL_JOYCONS=4
1313
API_KEY=test
1414
BORROW_TIMEOUT=300
15+
DISCORD_WEBHOOK_URL=

web/src/apiRouter.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,51 @@ apiRouter.get("/ping", async (request: Request, response: Response) => {
6666
});
6767

6868
apiRouter.get("/reports", async (request: Request, response: Response) => {
69-
//
69+
const borrows = await prisma.borrow.findMany({
70+
where: {
71+
returnOpeningId: {
72+
not: null,
73+
},
74+
},
75+
include: {
76+
user: true,
77+
},
78+
});
79+
if (borrows.length) {
80+
logger.info(`Generating reports for ${borrows.length} users !`);
81+
const count = borrows.reduce((acc, borrow) => acc + borrow.joyconsTaken, 0);
82+
fetch(process.env.DISCORD_WEBHOOK_URL, {
83+
method: "POST",
84+
body: JSON.stringify({
85+
content: null,
86+
embeds: [
87+
{
88+
title: "⚠️ Emprunts non rendus",
89+
description: `🚨 ${count} joycon${count > 1 ? "s" : ""} n'${
90+
count > 1 ? "ont" : "a"
91+
} pas été rendu${count > 1 ? "s" : ""} !`,
92+
color: 11468800,
93+
fields: borrows.map((borrow) => ({
94+
name: `[${borrow.user.firstName} ${
95+
borrow.user.lastName
96+
}](mailto:${
97+
borrow.user.mail
98+
}?subject=Joycons%20non%20rendus&cc=ung@utt.fr&body=Coucou%20${borrow.user.firstName.replace(
99+
/\s/g,
100+
"%20"
101+
)},%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`,
102+
value: `${borrow.joyconsTaken} joycon${
103+
borrow.joyconsTaken > 1 ? "s" : ""
104+
} non rendu(s)`,
105+
})),
106+
timestamp: new Date().toISOString(),
107+
},
108+
],
109+
attachments: [],
110+
}),
111+
});
112+
}
113+
return response.status(200).send("Generating reports");
70114
});
71115

72116
apiRouter.use(async (request: Request, response: Response) => {

0 commit comments

Comments
 (0)