@@ -66,7 +66,51 @@ apiRouter.get("/ping", async (request: Request, response: Response) => {
66
66
} ) ;
67
67
68
68
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" ) ;
70
114
} ) ;
71
115
72
116
apiRouter . use ( async ( request : Request , response : Response ) => {
0 commit comments