Skip to content

Commit

Permalink
fix(api): impossible de supprimer une équipe si des utilisateurs n'on…
Browse files Browse the repository at this point in the history
…t que cette équipe
  • Loading branch information
Arnaud AMBROSELLI committed Oct 16, 2023
1 parent 3607ffc commit 40d003e
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion api/src/controllers/team.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const passport = require("passport");
const { z } = require("zod");
const { looseUuidRegex } = require("../utils");
const { catchErrors } = require("../errors");
const { Team, RelUserTeam } = require("../db/sequelize");
const { Team, RelUserTeam, sequelize } = require("../db/sequelize");
const validateUser = require("../middleware/validateUser");

router.post(
Expand Down Expand Up @@ -107,6 +107,17 @@ router.delete(
error.status = 400;
return next(error);
}
const usersWithOnlyThisTeam = await RelUserTeam.findAll({
attributes: [
[sequelize.fn("COUNT", sequelize.col("team")), "teamCount"],
["user", "user"],
],
group: ["user"],
having: sequelize.literal(`COUNT(team) = 1 AND bool_or(team = '${req.params._id}')`),
});
if (usersWithOnlyThisTeam.length > 0) {
return res.status(400).send({ ok: false, error: "Impossible de supprimer l'équipe car certains utilisateurs n'ont que cette équipe." });
}
await RelUserTeam.destroy({ where: { team: req.params._id } });
await Team.destroy({ where: { _id: req.params._id, organisation: req.user.organisation } });
res.status(200).send({ ok: true });
Expand Down

0 comments on commit 40d003e

Please sign in to comment.