@@ -2,6 +2,10 @@ const { number, object } = require("yup");
22
33const DemandeSejour = require ( "../../services/DemandeSejour" ) ;
44const 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
610const logger = require ( "../../utils/logger" ) ;
711const ValidationAppError = require ( "../../utils/validation-error" ) ;
@@ -11,6 +15,7 @@ const log = logger(module.filename);
1115module . 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+
0 commit comments