Skip to content

Commit

Permalink
✨- Refactor AccountController and add setAssociation endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Code-Nam committed Mar 1, 2024
1 parent d5fea95 commit 6a393b4
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 24 deletions.
84 changes: 64 additions & 20 deletions backend/controllers/Account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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$/;

Expand Down Expand Up @@ -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
Expand All @@ -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;
}
}

Expand All @@ -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;
Expand All @@ -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;
export default AccountController;
18 changes: 14 additions & 4 deletions backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions backend/models/Account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface Account {
timeAlive: number;
lastPixelTime: number;
lastSentMessageTimes: number[];
association: string;
}

export default Account;
1 change: 1 addition & 0 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ model Account {
timeAlive Int?
lastPixelTime DateTime?
lastSentMessageTimes Json?
association String?
}

model LogEntry {
Expand Down

0 comments on commit 6a393b4

Please sign in to comment.