Skip to content

Commit

Permalink
feat: cerfa changes history
Browse files Browse the repository at this point in the history
  • Loading branch information
flodlc committed May 3, 2022
1 parent b9884d2 commit cc2abd1
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const historySchema = {
history: {
type: [
new mongoose.Schema({
fieldName: {
type: String,
required: true,
},
from: {
type: String,
default: null,
Expand Down
34 changes: 31 additions & 3 deletions server/src/http/routes/specific/cerfa.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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");
const pdfCerfaController = require("../../../logic/controllers/pdfCerfa/pdfCerfaController");
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();
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
22 changes: 16 additions & 6 deletions ui/modules/Dossier/formEngine/hooks/useAutoSave.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion ui/modules/Dossier/formEngine/useCerfa.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
});
},
Expand Down
3 changes: 2 additions & 1 deletion ui/modules/Dossier/services/api.service.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down

0 comments on commit cc2abd1

Please sign in to comment.