Skip to content

Commit

Permalink
refactor: /api/user/clusters/:id/unbind
Browse files Browse the repository at this point in the history
  • Loading branch information
SALTWOOD committed Nov 2, 2024
1 parent 65f420d commit 4915dad
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/routes/ApiUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,22 @@ export class ApiUser {
res.status(200).json(cluster.getJson(true, false));
});

inst.app.post("/api/user/clusters/unbind", async (req, res) => {
inst.app.post("/api/user/clusters/:id/unbind", async (req, res) => {
if (!Utilities.verifyUser(req, res, inst.db)) return;
const token = req.cookies.token;
const user = inst.db.getEntity<UserEntity>(UserEntity, (JwtHelper.instance.verifyToken(token, 'user') as { userId: number }).userId);
if (!user) {
res.status(404).send({ message: 'User not found' });
return;
}
const body = req.body as { clusterId: string };
res.setHeader('Content-Type', 'application/json');
const cluster = inst.clusters.find(c => c.clusterId === body.clusterId && Number(c.owner) === user.id);
const cluster = inst.clusters.find(c => c.clusterId === req.params.id);
if (!cluster) {
res.status(404).send({ message: 'Cluster not found or not bound to this user' });
res.status(404).send({ message: 'Cluster not found' });
return;
}
if (cluster.owner !== user.id) {
res.status(403).send({ message: 'That\'s not your cluster!' });
return;
}
cluster.owner = 0;
Expand Down

0 comments on commit 4915dad

Please sign in to comment.