From 40d003ecc18fdef5d941b5f09184b8710e5e53af Mon Sep 17 00:00:00 2001 From: Arnaud AMBROSELLI Date: Mon, 16 Oct 2023 16:54:06 +0200 Subject: [PATCH] =?UTF-8?q?fix(api):=20impossible=20de=20supprimer=20une?= =?UTF-8?q?=20=C3=A9quipe=20si=20des=20utilisateurs=20n'ont=20que=20cette?= =?UTF-8?q?=20=C3=A9quipe?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/src/controllers/team.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/api/src/controllers/team.js b/api/src/controllers/team.js index 988150810..eefc6a25e 100644 --- a/api/src/controllers/team.js +++ b/api/src/controllers/team.js @@ -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( @@ -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 });