Skip to content

Commit a387ad8

Browse files
committed
feat(fusage): modif vacancier personnel j8 727
1 parent 2566b48 commit a387ad8

10 files changed

Lines changed: 492 additions & 20 deletions

File tree

packages/backend/src/controllers/demandeSejour/depose.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const expectedStates = [
2424
statuts.A_MODIFIER,
2525
statuts.ATTENTE_8_JOUR,
2626
statuts.A_MODIFIER_8J,
27+
statuts.VALIDEE_8J,
28+
statuts.SEJOUR_EN_COURS,
2729
];
2830

2931
module.exports = async function post(req, res, next) {
@@ -117,6 +119,7 @@ module.exports = async function post(req, res, next) {
117119
idFonctionnelle = `DS-${currentYear}-${departementSuivi}-${numSeq.padStart(4, "0")}`;
118120
}
119121

122+
120123
if (statut == statuts.ATTENTE_8_JOUR || statut == statuts.A_MODIFIER_8J) {
121124
log.d("Déclaration à 8 jours");
122125
await DemandeSejour.finalize8jours(declarationId, declaration);

packages/backend/src/controllers/demandeSejour/update.js

Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ const { number, object } = require("yup");
22

33
const DemandeSejour = require("../../services/DemandeSejour");
44
const AppError = require("../../utils/error");
5+
const { statuts } = require("../../helpers/ds-statuts");
6+
const Send = require("../../services/mail").mailService.send;
7+
const MailUtils = require("../../utils/mail");
8+
const { actions, userTypes } = require("../../helpers/tracking");
59

610
const logger = require("../../utils/logger");
711
const ValidationAppError = require("../../utils/validation-error");
@@ -11,6 +15,7 @@ const log = logger(module.filename);
1115
module.exports = async function post(req, res, next) {
1216
let { declarationId } = req.params;
1317
const { type } = req.body;
18+
const { id: userId } = req.decoded;
1419
let { parametre } = req.body;
1520
log.i("IN", { declarationId, parametre, type });
1621

@@ -28,6 +33,249 @@ module.exports = async function post(req, res, next) {
2833
} catch (error) {
2934
return next(new ValidationAppError(error));
3035
}
36+
//console.log("parametre", parametre);
37+
const declaration = await DemandeSejour.getOne({ "ds.id": declarationId });
38+
const { informationsVacanciers, informationsPersonnel, statut } = declaration;
39+
40+
/* ==================================================== */
41+
/* Mise à jour d'une déclaration 8J validée ou en cours */
42+
/* ==================================================== */
43+
if (
44+
(statut == statuts.VALIDEE_8J || statut == statuts.SEJOUR_EN_COURS) &&
45+
(type === "informationsVacanciers" || type === "informationsPersonnel")
46+
) {
47+
const destinatairesBack = await DemandeSejour.getEmailBack(
48+
declaration.departementSuivi,
49+
);
50+
if (destinatairesBack) {
51+
const departements = declaration.hebergement.hebergements.map(
52+
(h) => h.coordonnees.adresse.departement,
53+
);
54+
const destinatairesBackCc =
55+
await DemandeSejour.getEmailBackCc(departements);
56+
const filteredBackCc = destinatairesBackCc.filter(
57+
(d) => !destinatairesBack.includes(d),
58+
);
59+
let differences = "";
60+
let message = "";
61+
let oldData = "";
62+
switch (type) {
63+
case "informationsVacanciers": {
64+
oldData = informationsVacanciers;
65+
differences = deepCompare(informationsVacanciers, parametre);
66+
message = "Mise à jour du nombre de vacanciers";
67+
break;
68+
}
69+
case "informationsPersonnel": {
70+
log.d("informationsPersonnel", declarationId);
71+
message = "Mise à jour du personnel";
72+
oldData = informationsPersonnel;
73+
differences = deepCompare(informationsPersonnel, parametre);
74+
break;
75+
}
76+
default:
77+
log.d("wrong type");
78+
return null;
79+
}
80+
console.log(parametre);
81+
console.log(differences);
82+
await DemandeSejour.insertEvent(
83+
"Organisateur",
84+
declarationId,
85+
userId,
86+
null,
87+
"declaration_sejour",
88+
message,
89+
differences,
90+
);
91+
DemandeSejour.addAsyncDeclarationSejourHistoric({
92+
action: actions.modification,
93+
data: {
94+
newData: parametre,
95+
oldData,
96+
},
97+
declarationId,
98+
userId,
99+
userType: userTypes.front,
100+
});
101+
try {
102+
await Send(
103+
MailUtils.bo.declarationSejour.sendUpdateValide8jours({
104+
cc: filteredBackCc,
105+
declaration,
106+
departementSuivi: declaration.departementSuivi,
107+
departementsSecondaires: departements.filter(
108+
(d) => d !== declaration.departementSuivi,
109+
),
110+
destinataires: destinatairesBack,
111+
dataUpdated: differences,
112+
type,
113+
}),
114+
);
115+
} catch (error) {
116+
log.w(error);
117+
return next(
118+
new AppError(
119+
"Une erreur est survenue lors de l'envoi de mails aux usagers back office",
120+
{
121+
statusCode: 500,
122+
},
123+
),
124+
);
125+
}
126+
/*
127+
const includesKeysPersonnel = [
128+
"encadrants",
129+
"accompagnants",
130+
"prestatairesActivites",
131+
"prestatairesEntretien",
132+
"prestatairesTransport",
133+
"prestatairesMedicaments",
134+
];
135+
console.log(
136+
"includesKeysPersonnel.every((key) => Object.hasOwn(parametre, key))",
137+
includesKeysPersonnel.every((key) => Object.hasOwn(parametre, key)),
138+
);
139+
if (includesKeysPersonnel.every((key) => Object.hasOwn(parametre, key))) {
140+
console.log("informationsPersonnel", informationsPersonnel);
141+
const diffEncadrants = deepCompare(
142+
informationsPersonnel.encadrants,
143+
parametre?.encadrants,
144+
);
145+
const diffAccompagnants = deepCompare(
146+
informationsPersonnel.accompagnants,
147+
parametre?.accompagnants,
148+
);
149+
const diffPrestatairesActivites = deepCompare(
150+
informationsPersonnel.prestatairesActivites,
151+
parametre?.prestatairesActivites,
152+
);
153+
const diffPrestatairesEntretien = deepCompare(
154+
informationsPersonnel.prestatairesEntretien,
155+
parametre?.prestatairesEntretien,
156+
);
157+
const diffPrestatairesTransport = deepCompare(
158+
informationsPersonnel.prestatairesTransport,
159+
parametre?.prestatairesTransport,
160+
);
161+
console.log("diffEncadrants:", diffEncadrants);
162+
console.log("diffAccompagnants:", diffAccompagnants);
163+
console.log("diffPrestatairesActivites:", diffPrestatairesActivites);
164+
console.log("diffPrestatairesEntretien:", diffPrestatairesEntretien);
165+
console.log("diffPrestatairesTransport:", diffPrestatairesTransport);
166+
167+
if (Object.entries(diffEncadrants).length !== 0) {
168+
await DemandeSejour.insertEvent(
169+
"Organisateur",
170+
declarationId,
171+
userId,
172+
null,
173+
"declaration_sejour",
174+
"Mise à jour des encadrants'",
175+
diffEncadrants,
176+
);
177+
}
178+
if (Object.entries(diffAccompagnants).length !== 0) {
179+
await DemandeSejour.insertEvent(
180+
"Organisateur",
181+
declarationId,
182+
userId,
183+
null,
184+
"declaration_sejour",
185+
"Mise à jour des accompagnants",
186+
diffAccompagnants,
187+
);
188+
}
189+
}*/
190+
}
191+
/*
192+
const params = sendNotificationMail({
193+
content: mailContent,
194+
email: "toto@toto.fr",
195+
isBo: false,
196+
});
197+
console.log(params);
198+
Send(params);
199+
*/
200+
//console.log("mail", mail);
201+
/*
202+
try {
203+
const destinataires = await DemandeSejour.getEmailToList(
204+
declaration.organismeId,
205+
);
206+
const filteredCc = declaration.organisme.personneMorale.siren
207+
? (
208+
await DemandeSejour.getEmailCcList(
209+
declaration.organisme.personneMorale.siren,
210+
)
211+
).filter((d) => !destinataires.includes(d))
212+
: [];
213+
214+
if (destinataires) {
215+
await Send(
216+
MailUtils.usagers.declarationSejour.sendUpdate8jours({
217+
cc: filteredCc,
218+
declaration,
219+
dest: destinataires,
220+
}),
221+
);
222+
}
223+
} catch (error) {
224+
log.w("DONE with error");
225+
return next(
226+
new AppError("Une erreur est survenue lors de l'envoi de mails", {
227+
statusCode: 500,
228+
}),
229+
);
230+
}
231+
*/
232+
/*
233+
try {
234+
const destinatairesBack = await DemandeSejour.getEmailBack(
235+
declaration.departementSuivi,
236+
);
237+
238+
if (destinatairesBack) {
239+
const departements = declaration.hebergement.hebergements.map(
240+
(h) => h.coordonnees.adresse.departement,
241+
);
242+
const destinatairesBackCc =
243+
await DemandeSejour.getEmailBackCc(departements);
244+
245+
const filteredBackCc = destinatairesBackCc.filter(
246+
(d) => !destinatairesBack.includes(d),
247+
);
248+
await Send(
249+
MailUtils.bo.declarationSejour.sendDeclarationA8joursNotify({
250+
cc: filteredBackCc,
251+
declaration,
252+
departementSuivi: declaration.departementSuivi,
253+
departementsSecondaires: departements.filter(
254+
(d) => d !== declaration.departementSuivi,
255+
),
256+
destinataires: destinatairesBack,
257+
}),
258+
);
259+
}
260+
} catch (error) {
261+
log.w(error);
262+
return next(
263+
new AppError(
264+
"Une erreur est survenue lors de l'envoi de mails aux usagers back office",
265+
{
266+
statusCode: 500,
267+
},
268+
),
269+
);
270+
}
271+
*/
272+
log.i("DONE");
273+
//return res.status(200).json({ ARuuid, DSuuid });
274+
}
275+
/* ==================================================== */
276+
/* FIN */
277+
/* ==================================================== */
278+
31279
try {
32280
const updatedDeclarationId = await DemandeSejour.update(
33281
type,
@@ -42,3 +290,34 @@ module.exports = async function post(req, res, next) {
42290
return next(error);
43291
}
44292
};
293+
294+
function deepCompare(obj1, obj2) {
295+
const changes = {};
296+
297+
function findChanges(o1, o2, path = "") {
298+
for (const key in o1) {
299+
const newPath = path ? `${path}.${key}` : key;
300+
if (
301+
typeof o1[key] === "object" &&
302+
o1[key] !== null &&
303+
typeof o2[key] === "object" &&
304+
o2[key] !== null
305+
) {
306+
findChanges(o1[key], o2[key], newPath);
307+
} else if (o1[key] !== o2[key]) {
308+
changes[newPath] = { avant: o1[key], apres: o2[key] };
309+
}
310+
}
311+
312+
for (const key in o2) {
313+
const newPath = path ? `${path}.${key}` : key;
314+
if (!(key in o1)) {
315+
changes[newPath] = { avant: undefined, apres: o2[key] };
316+
}
317+
}
318+
}
319+
320+
findChanges(obj1, obj2);
321+
return changes;
322+
}
323+

packages/backend/src/helpers/tracking.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const actions = {
88
module.exports.actions = actions;
99

1010
const entities = {
11+
demandeSejour: "DEMANDE_SEJOUR",
1112
eig: "EIG",
1213
userBack: "USER_BACK",
1314
userFront: "USER_FRONT",

packages/backend/src/middlewares/can-update-ds.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ async function canUpdateDs(req, _res, next) {
4343
statuts.A_MODIFIER,
4444
statuts.ATTENTE_8_JOUR,
4545
statuts.A_MODIFIER_8J,
46+
statuts.VALIDEE_8J,
47+
statuts.SEJOUR_EN_COURS,
4648
].includes(statut)
4749
) {
4850
next();

packages/backend/src/services/DemandeSejour.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
/* eslint-disable no-param-reassign */
1+
const Sentry = require("@sentry/node");
2+
const { sentry } = require("../config");
23
const dayjs = require("dayjs");
34
const logger = require("../utils/logger");
45
const pool = require("../utils/pgpool").getPool();
56
const dsStatus = require("../helpers/ds-statuts");
67
const PersonneMorale = require("./organisme/PersonneMorale");
78
const PersonnePhysique = require("./organisme/PersonnePhysique");
9+
const { entities, userTypes } = require("../helpers/tracking");
10+
const { addHistoric } = require("./Tracking");
811
const { getComplementOrganisme } = require("./Organisme");
912
const {
1013
getByDSId: getHebergementsByDSIds,
@@ -1724,3 +1727,30 @@ module.exports.updateStatut = async (
17241727
client.release();
17251728
}
17261729
};
1730+
1731+
module.exports.addAsyncDeclarationSejourHistoric = async ({
1732+
data: { oldData, newData },
1733+
declarationId,
1734+
userId,
1735+
action,
1736+
userType,
1737+
}) => {
1738+
try {
1739+
addHistoric({
1740+
action,
1741+
data: {
1742+
after: newData,
1743+
before: oldData,
1744+
},
1745+
entity: entities.demandeSejour,
1746+
entityId: declarationId,
1747+
userId,
1748+
userType: userType ?? userTypes.front,
1749+
});
1750+
} catch (error) {
1751+
log.w("addAsyncHistoric - DONE with error", error);
1752+
if (sentry.enabled) {
1753+
Sentry.captureException(error);
1754+
}
1755+
}
1756+
};

0 commit comments

Comments
 (0)