From 6a393b4b219871a7af5d9353cfe3aa8dff395049 Mon Sep 17 00:00:00 2001 From: AnnamTRAN Date: Fri, 1 Mar 2024 11:06:45 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8-=20Refactor=20AccountController=20and?= =?UTF-8?q?=20add=20setAssociation=20endpoint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/controllers/Account.ts | 84 ++++++++++++++++++++++++++-------- backend/index.ts | 18 ++++++-- backend/models/Account.ts | 1 + backend/prisma/schema.prisma | 1 + 4 files changed, 80 insertions(+), 24 deletions(-) diff --git a/backend/controllers/Account.ts b/backend/controllers/Account.ts index cb18a78..b16ce16 100644 --- a/backend/controllers/Account.ts +++ b/backend/controllers/Account.ts @@ -18,7 +18,10 @@ class AccountController { * @param req The Express request object * @param res The Express response object */ - public static async sendMagicLink(req: express.Request, res: express.Response) { + public static async sendMagicLink( + req: express.Request, + res: express.Response + ) { const { email } = req.body; const expression: RegExp = /^[a-zA-Z0-9._-]+@edu\.devinci\.fr$/; @@ -118,22 +121,23 @@ class AccountController { public static async muteUser(req: express.Request, res: express.Response) { const { userId, isMuted } = req.body; - prisma.account.update({ - where: { - id: userId - }, - data: { - isMuted: isMuted - } - }).then(() => { - res.status(200).send("Utilisateur muté"); - }).catch(() => { - res.status(500).send("Une erreur s'est produite."); - }); - + prisma.account + .update({ + where: { + id: userId, + }, + data: { + isMuted: isMuted, + }, + }) + .then(() => { + res.status(200).send("Utilisateur muté"); + }) + .catch(() => { + res.status(500).send("Une erreur s'est produite."); + }); } - /** * Ban/unban a user * @server HTTP @@ -147,13 +151,13 @@ class AccountController { try { await prisma.account.update({ where: { id: userId }, - data: { isBanned: isBanned } + data: { isBanned: isBanned }, }); - + res.status(200).send("Successful"); } catch (error) { res.status(500).send("Not successful"); - return; + return; } } @@ -164,7 +168,10 @@ class AccountController { * @param socket The client socket * @param data The payload */ - public static async authSocket(socket: SocketIO.Socket, [token, email]: [string, string]) { + public static async authSocket( + socket: SocketIO.Socket, + [token, email]: [string, string] + ) { if (verifyAuthenticationToken(token, email)) { socket.data.token = token; socket.data.email = email; @@ -174,6 +181,43 @@ class AccountController { socket.emit("auth-callback", false); } } + + public static async setAssociation( + req: express.Request, + res: express.Response + ) { + const { association } = req.body; + + if (!association) + return res.status(400).send("Association is required"); + + try { + await prisma.account.update({ + where: { + devinciEmail: req.account.devinciEmail, + }, + data: { + association, + }, + }); + + // Log the action + prisma.logEntry.create({ + data: { + devinciEmail: req.account.devinciEmail, + time: new Date().getTime(), + ip: req.ip || "Unknown", + action: { + type: "set_association", + }, + }, + }); + + res.status(200).send("Association updated"); + } catch (error) { + res.status(500).send("Unable to connect to the database"); + } + } } -export default AccountController; \ No newline at end of file +export default AccountController; diff --git a/backend/index.ts b/backend/index.ts index 184d6bf..c1ef1f8 100644 --- a/backend/index.ts +++ b/backend/index.ts @@ -21,13 +21,20 @@ const server = http.createServer(app); WSS.init(server); WSS.io.on("connection", (socket: Socket) => { - const ip = socket.handshake.headers["x-forwarded-for"] || socket.handshake.address; + const ip = + socket.handshake.headers["x-forwarded-for"] || socket.handshake.address; const userAgent = socket.handshake.headers["user-agent"]; console.log(`Socket connected from ${ip} using ${userAgent}`); - socket.on("auth", (...data) => AccountController.authSocket(socket, ...data)); - socket.on("place-pixel", (...data) => CanvasController.placePixel(socket, ...data)); - socket.on("message", (...data) => ChatController.broadcastMessage(socket, ...data)); + socket.on("auth", (...data) => + AccountController.authSocket(socket, ...data) + ); + socket.on("place-pixel", (...data) => + CanvasController.placePixel(socket, ...data) + ); + socket.on("message", (...data) => + ChatController.broadcastMessage(socket, ...data) + ); WSS.updateClassement(socket); @@ -41,6 +48,9 @@ app.post("/auth/send-magic-link", AccountController.sendMagicLink); app.get("/auth/login", AccountController.login); app.get("/canvas/image", CanvasController.getCanvasImage); +// Asso routes +app.post("/api/asso", AccountController.setAssociation); + // Admin routes const router = express.Router(); router.use(verifyUser); diff --git a/backend/models/Account.ts b/backend/models/Account.ts index d74abc9..afa89e3 100644 --- a/backend/models/Account.ts +++ b/backend/models/Account.ts @@ -7,6 +7,7 @@ interface Account { timeAlive: number; lastPixelTime: number; lastSentMessageTimes: number[]; + association: string; } export default Account; diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prisma index 094660b..cc1aaea 100644 --- a/backend/prisma/schema.prisma +++ b/backend/prisma/schema.prisma @@ -20,6 +20,7 @@ model Account { timeAlive Int? lastPixelTime DateTime? lastSentMessageTimes Json? + association String? } model LogEntry {