diff --git a/server/src/common/model/schema/specific/dossier/cerfa/History.js b/server/src/common/model/schema/specific/dossier/cerfa/History.js index 9171a8bf..20c9b7ae 100644 --- a/server/src/common/model/schema/specific/dossier/cerfa/History.js +++ b/server/src/common/model/schema/specific/dossier/cerfa/History.js @@ -14,6 +14,10 @@ const historySchema = { history: { type: [ new mongoose.Schema({ + fieldName: { + type: String, + required: true, + }, from: { type: String, default: null, diff --git a/server/src/http/routes/specific/cerfa.js b/server/src/http/routes/specific/cerfa.js index 5b5a1691..67481a08 100644 --- a/server/src/http/routes/specific/cerfa.js +++ b/server/src/http/routes/specific/cerfa.js @@ -1,9 +1,9 @@ const express = require("express"); const Joi = require("joi"); const Boom = require("boom"); -const { cloneDeep, mergeWith, find } = require("lodash"); +const { cloneDeep, mergeWith, find, isNil } = require("lodash"); const merge = require("deepmerge"); -const { Cerfa } = require("../../../common/model/index"); +const { Cerfa, History } = require("../../../common/model/index"); const tryCatch = require("../../middlewares/tryCatchMiddleware"); const permissionsDossierMiddleware = require("../../middlewares/permissionsDossierMiddleware"); const cerfaSchema = require("../../../common/model/schema/specific/dossier/cerfa/Cerfa"); @@ -11,6 +11,7 @@ const pdfCerfaController = require("../../../logic/controllers/pdfCerfa/pdfCerfa const { getFromStorage } = require("../../../common/utils/ovhUtils"); const { oleoduc, writeData } = require("oleoduc"); const { PassThrough } = require("stream"); +const { get } = require("lodash/object"); module.exports = (components) => { const router = express.Router(); @@ -193,7 +194,7 @@ module.exports = (components) => { router.put( "/:id", permissionsDossierMiddleware(components, ["dossier/sauvegarder"]), - tryCatch(async ({ body, params }, res) => { + tryCatch(async ({ body, params, user }, res) => { const data = await Joi.object({ employeur: Joi.object({ denomination: Joi.string().allow(null), @@ -362,6 +363,7 @@ module.exports = (components) => { }), isLockedField: Joi.object({}).unknown(), dossierId: Joi.string().required(), + inputNames: Joi.array().items(Joi.string()).required(), }).validateAsync(body, { abortEarly: false }); let cerfaDb = await Cerfa.findOne({ _id: params.id }, { _id: 0, __v: 0 }).lean(); @@ -389,6 +391,32 @@ module.exports = (components) => { ? cerfaDb.contrat.remunerationsAnnuelles : remunerationsAnnuelles; + let history = await History.findOne({ dossierId: cerfaDb.dossierId }); + if (!history) { + history = await History.create({ + dossierId: cerfaDb.dossierId, + context: "cerfa", + history: [], + }); + } + + await History.findOneAndUpdate( + { _id: history._id }, + { + $push: { + history: { + $each: data.inputNames.map((inputName) => ({ + fieldName: inputName, + from: get(cerfaDb, inputName), + to: get(data, inputName), + when: new Date(), + who: user.username, + })), + }, + }, + } + ); + const cerfaUpdated = await Cerfa.findOneAndUpdate({ _id: params.id }, mergedData, { new: true, }).lean(); diff --git a/ui/modules/Dossier/formEngine/hooks/useAutoSave.js b/ui/modules/Dossier/formEngine/hooks/useAutoSave.js index 1b3602ca..e9f82df5 100644 --- a/ui/modules/Dossier/formEngine/hooks/useAutoSave.js +++ b/ui/modules/Dossier/formEngine/hooks/useAutoSave.js @@ -1,4 +1,4 @@ -import { useEffect } from "react"; +import { useEffect, useRef } from "react"; import { atom, useRecoilCallback, useSetRecoilState } from "recoil"; import { isEmptyValue } from "../utils/isEmptyValue"; import { getValues } from "../utils/getValues"; @@ -28,13 +28,13 @@ export const useAutoSave = ({ controller }) => { snapshot.getPromise(dossierAtom), [] ); - + const inputNamesRef = useRef([]); const setAutoSave = useSetRecoilState(autoSaveStatusAtom); useEffect(() => { let timeout; const save = debounce( - async (fields) => { + async ({ fields }) => { clearTimeout(timeout); const toSave = Object.fromEntries( Object.entries(fields) @@ -50,7 +50,13 @@ export const useAutoSave = ({ controller }) => { const data = { ...getValues(toSave), isLockedField: getIsLocked(toSave) }; const dossier = await getDossier(); try { - await apiService.saveCerfa({ dossierId: dossier._id, data, cerfaId: dossier.cerfaId }); + await apiService.saveCerfa({ + dossierId: dossier._id, + data, + cerfaId: dossier.cerfaId, + inputNames: inputNamesRef.current, + }); + inputNamesRef.current = []; } catch (e) { setAutoSave("ERROR"); throw e; @@ -63,9 +69,13 @@ export const useAutoSave = ({ controller }) => { { trailing: true } ); - const handler = (...args) => { + const handler = ({ fields, inputName }) => { setAutoSave("PENDING"); - save(...args); + console.log(inputNamesRef, inputNamesRef.current); + if (inputNamesRef.current.indexOf(inputName) === -1) { + inputNamesRef.current = [...inputNamesRef.current, inputName]; + } + save({ fields, inputName }); }; controller.on("CHANGE", handler); diff --git a/ui/modules/Dossier/formEngine/useCerfa.js b/ui/modules/Dossier/formEngine/useCerfa.js index f6e5a95e..1ebf478e 100644 --- a/ui/modules/Dossier/formEngine/useCerfa.js +++ b/ui/modules/Dossier/formEngine/useCerfa.js @@ -193,7 +193,7 @@ export const useCerfa = ({ schema } = {}) => { const currentValue = await getValue(name); await processField({ name, value: currentValue }); if (triggerSave) { - controller.dispatch("CHANGE", await getFields()); + controller.dispatch("CHANGE", { fields: await getFields(), inputName: name }); } }); }, diff --git a/ui/modules/Dossier/services/api.service.js b/ui/modules/Dossier/services/api.service.js index efa219b8..630cd6a1 100644 --- a/ui/modules/Dossier/services/api.service.js +++ b/ui/modules/Dossier/services/api.service.js @@ -1,10 +1,11 @@ import { _post, _put } from "../../../common/httpClient"; -const saveCerfa = async ({ dossierId, cerfaId, data }) => { +const saveCerfa = async ({ dossierId, cerfaId, data, inputNames }) => { try { return await _put(`/api/v1/cerfa/${cerfaId}`, { ...data, dossierId, + inputNames, }); } catch (e) { console.log(e);