Skip to content

Commit

Permalink
feat(eig): differentiate read by ddets and dreets (#498)
Browse files Browse the repository at this point in the history
close jira 596, 597.

j'ai rajouté la jira 584 : envoie d'un mail au reponsable du sejour et
613 : nouveau texte des mails qui s'inseraient bien dans cette PR


loom :
https://www.loom.com/share/a9b5d74d611248fbae2ffecb1442ba15?sid=379a4e6b-9ec0-4f92-b95d-3307c227fbb9

**Remarque :** j'utilise le service commune de geo qui semble etre un
pur copié collé du SIHONO : les nom de colonnes ne sont pas les bon,
j'ai corrigé ca, a priori, on utilisait ce service nul par ailleurs
  • Loading branch information
benjaminDNUM authored Sep 24, 2024
2 parents a90d7c9 + 250bbb4 commit 413a10c
Show file tree
Hide file tree
Showing 19 changed files with 456 additions and 113 deletions.
25 changes: 20 additions & 5 deletions packages/backend/src/controllers/eig/depose.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const DemandeSejour = require("../../services/DemandeSejour");
const MailUtils = require("../../utils/mail");
const AppError = require("../../utils/error");
const { getEmails } = require("../../helpers/eigMail");
const Commune = require("../../services/geo/Commune");
const Send = require("../../services/mail").mailService.send;

const log = logger(module.filename);
Expand Down Expand Up @@ -79,11 +80,26 @@ module.exports = async (req, res, next) => {
userName,
} = await getEmails(eig.departement, eig.userId);

const hebergementCodeInsee = ds.hebergement.hebergements.find(
(h) => h?.coordonnees?.adresse?.departement === eig.departement,
)?.coordonnees?.adresse?.codeInsee;

const communeName = (
await Commune.get({
code_insee: hebergementCodeInsee,
date: new Date(),
})
)?.text;

emailsDDETS?.length &&
(await Send(
MailUtils.bo.eig.sendToDDETS({
dest: emailsDDETS,
departementName,
eig,
declarationSejour: ds,
regionName,
communeName,
}),
));

Expand All @@ -93,27 +109,26 @@ module.exports = async (req, res, next) => {
departementName,
dest: emailsDREETS,
eig,
declarationSejour: ds,
communeName,
}),
));

emailsOrganisateur?.length > 0 &&
(await Send(
MailUtils.bo.eig.sendToOrganisme({
departementName,
dest: emailsOrganisateur,
dest: [...emailsOrganisateur, ds.responsableSejour.email],
eig,
regionName,
userName,
declarationSejour: ds,
}),
));

eig.emailAutresDestinataires?.length > 0 &&
(await Send(
MailUtils.bo.eig.sendToAutre({
departementName,
dest: eig.emailAutresDestinataires,
eig,
regionName,
userName,
}),
));
Expand Down
1 change: 0 additions & 1 deletion packages/backend/src/controllers/eig/get-by-ds-id-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module.exports = async function get(req, res, next) {

try {
const eigs = await eigService.getByDsIdAdmin(declarationId);

return res.status(200).json({ eigs });
} catch (error) {
log.w("DONE with error");
Expand Down
54 changes: 51 additions & 3 deletions packages/backend/src/controllers/eig/mark-as-read.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
const AppError = require("../../utils/error");
const eigService = require("../../services/eig");
const Departement = require("../../services/geo/Departement");
const Region = require("../../services/geo/Region");

const logger = require("../../utils/logger");
const { statuts } = require("../../helpers/eig");
const {
statuts,
isUserDreetsWhoDeliveredAgrement,
isUserDdetsWhereEigHappened,
mustMarkAsRead,
} = require("../../helpers/eig");
const DemandeSejour = require("../../services/DemandeSejour");
const MailUtils = require("../../utils/mail");
const Send = require("../../services/mail").mailService.send;
Expand Down Expand Up @@ -41,10 +48,36 @@ module.exports = async function markAsRead(req, res, next) {
);
}

if (!mustMarkAsRead(territoireCode, eig)) {
log.w("L'EIG n'as pas à être marqué comme lu par le user");
return next(
new AppError("Statut incompatible", {
statusCode: 400,
}),
);
}

const typeReader = isUserDreetsWhoDeliveredAgrement(
territoireCode,
eig.agrementRegionObtention,
)
? "DREETS"
: isUserDdetsWhereEigHappened(territoireCode, eig.departement)
? "DDETS"
: null;

if (!typeReader) {
return next(
new AppError("L'utilisateur BO n'a aucune action a faire", {
statusCode: 400,
}),
);
}

try {
await eigService.markAsRead(eigId);
await eigService.markAsRead(eigId, typeReader);
await DemandeSejour.insertEvent(
`DDETS/DREETS ${territoireCode}`,
`${typeReader} ${territoireCode}`,
eig.declarationId,
null,
userId,
Expand All @@ -57,13 +90,28 @@ module.exports = async function markAsRead(req, res, next) {
return next(error);
}

let territoireName;

try {
territoireName =
typeReader === "DREETS"
? await Region.fetchOne(territoireCode)
: await Departement.fetchOne(territoireCode);
} catch (error) {
log.w("DONE with error");
return next(error);
}

try {
const destinataires = await DemandeSejour.getEmailToList(eig.organismeId);
destinataires?.length &&
(await Send(
MailUtils.bo.eig.sendMarkAsRead({
dest: destinataires,
eig,
typeReader,
territoireCode,
territoireName: territoireName.text,
}),
));
} catch (error) {
Expand Down
25 changes: 25 additions & 0 deletions packages/backend/src/helpers/eig.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,28 @@ module.exports.isDeclarationligibleToEig = (d) =>
dsStatuts.statuts.SEJOUR_EN_COURS,
dsStatuts.statuts.TERMINEE,
].includes(d.statut);

const isUserDreetsWhoDeliveredAgrement = (
territoireCode,
agrementRegionObtention,
) => territoireCode === agrementRegionObtention;

const isUserDdetsWhereEigHappened = (territoireCode, eigDepartement) =>
territoireCode === eigDepartement;

module.exports.isUserDreetsWhoDeliveredAgrement =
isUserDreetsWhoDeliveredAgrement;

module.exports.isUserDdetsWhereEigHappened = isUserDdetsWhereEigHappened;

module.exports.mustMarkAsRead = (territoireCode, eig) => {
return (
(!eig.readByDdets &&
isUserDdetsWhereEigHappened(territoireCode, eig.departement)) ||
(!eig.readByDreets &&
isUserDreetsWhoDeliveredAgrement(
territoireCode,
eig.agrementRegionObtention,
))
);
};
61 changes: 61 additions & 0 deletions packages/backend/src/middlewares/checkPermissionBOEIG.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const logger = require("../utils/logger");
const AppError = require("../utils/error");
const { getById } = require("../services/eig");
const { statuts, mustMarkAsRead } = require("../helpers/eig");

const log = logger(module.filename);

async function checkPermissionEIG(req, res, next) {
const { id: userId, territoireCode } = req.decoded;
const { id: eigId } = req.params;

if (!eigId || isNaN(eigId)) {
return next(
new AppError("Vous n'êtes pas autorisé à accéder à cet EIG", {
statusCode: 400,
}),
);
}

log.i("IN", { eigId, userId });

let eig;

try {
eig = await getById({ eigId });
} catch (err) {
return res.status(400).send({ errors: err.errors, name: err.name });
}

if (!eig) {
log.w("EIG introuvable");
return next(
new AppError("Not found", {
statusCode: 400,
}),
);
}

if (eig.statut === statuts.BROUILLON) {
log.w("L'EIG a un statut brouillon et ne peut pas être lu");
return next(
new AppError("Statut incompatible", {
statusCode: 400,
}),
);
}

if (mustMarkAsRead(territoireCode, eig)) {
log.w("L'EIG doit d'abord être marqué comme lu");
return next(
new AppError("l'EIG doit d'abord être marqué comme lu", {
statusCode: 400,
}),
);
}

log.i("DONE");
next();
}

module.exports = checkPermissionEIG;
9 changes: 8 additions & 1 deletion packages/backend/src/routes/eig.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const canUpdateEig = require("../middlewares/can-update-or-delete-eig");
const checkPermissionDeclarationSejourForEig = require("../middlewares/checkPermissionDeclarationSejourEig");
const checkPermissionDeclarationSejour = require("../middlewares/checkPermissionDeclarationSejour");
const checkPermissionEIG = require("../middlewares/checkPermissionEIG");
const checkPermissionBOEIG = require("../middlewares/checkPermissionBOEIG");
const boCheckRole = require("../middlewares/bo-check-role");
const boCheckJWT = require("../middlewares/bo-check-JWT");

Expand Down Expand Up @@ -37,7 +38,13 @@ router.get(
);
router.get("/admin", boCheckJWT, boCheckRoleEig, eigController.getAdmin);
router.get("/:id", checkJWT, checkPermissionEIG, eigController.getById);
router.get("/admin/:id", boCheckJWT, boCheckRoleEig, eigController.getById);
router.get(
"/admin/:id",
boCheckJWT,
boCheckRoleEig,
checkPermissionBOEIG,
eigController.getById,
);
router.post(
"/",
checkJWT,
Expand Down
Loading

0 comments on commit 413a10c

Please sign in to comment.