Skip to content

Commit

Permalink
feat(historic): add historic in app (#718)
Browse files Browse the repository at this point in the history
## Tickets liés
Close #
tickets liés : 

## Description

### dont régressions potentielles à tester

## Screenshot / liens loom 

## Check-list

 - [x] Ma branche est rebase sur main
- [ ] Des tests ont été écrits pour tous les endpoints créés ou modifiés
 - [ ] Refacto "à la volée" des parties sur lesquelles j'ai codée
 - [x] Plus de `console.log`
  • Loading branch information
iNeoO authored Feb 11, 2025
2 parents 14d2719 + e118477 commit e1e5a41
Show file tree
Hide file tree
Showing 17 changed files with 430 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ fileignoreconfig:
- filename: packages/backend/src/services/Territoire.js
checksum: 9728f0b14665259bf62d6200be40153eb7a8bfaf0a8381e1a0ce4f90087cf59b
- filename: packages/backend/src/services/User.js
checksum: 78ddae12d185c4111aa837a178b75e6bda4083d06783ee6382503262298eefbc
checksum: 8203852ff6c9cc57bb7d177b130034264d3b03a5e3501038e72563f49cd757be
- filename: packages/backend/src/services/geo/Commune.js
checksum: 40213f6529d1282e73f0201199e28b66f9c99c76d16651d88aac0b59fb722ae2
- filename: packages/backend/src/services/geo/Departement.js
Expand Down
5 changes: 4 additions & 1 deletion packages/backend/src/controllers/bo-user/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ module.exports = async function create(req, res, next) {
)
) {
try {
await BoUser.create(user);
const response = await BoUser.create(user);

// used by trackBoUser middleware
Object.assign(req, { tracking: { id: response.user.id } });

try {
const email = user.email;
Expand Down
4 changes: 4 additions & 0 deletions packages/backend/src/controllers/eig/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ module.exports = async (req, res, next) => {
departement: eig.departement,
userId,
});

// used by trackEig middleware
Object.assign(req, { tracking: { id: eigId } });

return res.status(200).json({ id: eigId, userId });
} catch (error) {
log.w("DONE with error");
Expand Down
23 changes: 23 additions & 0 deletions packages/backend/src/helpers/tracking.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const actions = {
creation: "CREATION",
deactivation: "DEACTIVATION",
deletion: "DELETION",
modification: "MODIFICATION",
};

module.exports.actions = actions;

const entities = {
eig: "EIG",
userBack: "USER_BACK",
userFront: "USER_FRONT",
};

module.exports.entities = entities;

const userTypes = {
back: "BACK",
front: "FRONT",
};

module.exports.userTypes = userTypes;
39 changes: 39 additions & 0 deletions packages/backend/src/middlewares/trackBoUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { actions } = require("../helpers/tracking");
const boUser = require("../services/BoUser");

function trackFoUser({ action, userType, itself }) {
return async (req, res, next) => {
const { id: userId } = req.decoded;

const boUserId = itself ? userId : req.params.userId;

let oldUser = null;
if (action !== actions.creation) {
oldUser = await boUser.getByUserId(boUserId);
}
res.on("finish", async () => {
if (res.statusMessage !== "OK") {
return null;
}

let newUser = null;
const id = action === actions.creation ? req.tracking.id : boUserId;

if (action !== actions.deletion) {
newUser = await boUser.getByUserId(id);
}

boUser.addAsyncUserHistoric({
action,
boUserId: id,
data: { newData: newUser, olData: oldUser },
userId,
userType,
});
});

next();
};
}

module.exports = trackFoUser;
41 changes: 41 additions & 0 deletions packages/backend/src/middlewares/trackEig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { actions } = require("../helpers/tracking");
const eigService = require("../services/eig");

function trackEig({ action, userType }) {
return async (req, res, next) => {
const { id: userId } = req.decoded;
const { id: eigId } = req.params;

let oldEig = null;
if (action !== actions.creation) {
oldEig = await eigService.getByEigId(eigId);
}

res.on("finish", () => {
if (res.statusMessage !== "OK") {
return null;
}

let newEig = null;
const id = action === actions.creation ? req.tracking.id : eigId;

if (action !== actions.deletion) {
newEig = eigService.getByEigId(id);
}

if (eigId) {
eigService.addAsyncEigHistoric({
action,
data: { newData: newEig, olData: oldEig },
eigId: id,
userId,
userType,
});
}
});

next();
};
}

module.exports = trackEig;
42 changes: 42 additions & 0 deletions packages/backend/src/middlewares/trackFoUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { actions } = require("../helpers/tracking");
const foUser = require("../services/User");

function trackFoUser({ action, userType, itself }) {
return async (req, res, next) => {
const { id: userId } = req.decoded;

const foUserId = itself ? userId : req.params.userId;

let oldUser = null;
if (action !== actions.creation) {
oldUser = await foUser.getByUserId(foUserId);
}

res.on("finish", async () => {
if (res.statusMessage !== "OK") {
return null;
}

let newUser = null;
const id = action === actions.creation ? req.tracking.id : foUserId;

if (action !== actions.deletion) {
newUser = foUser.getByUserId(id);
}

if (foUserId) {
foUser.addAsyncUserHistoric({
action,
data: { newData: newUser, olData: oldUser },
foUserId: id,
userId,
userType,
});
}
});

next();
};
}

module.exports = trackFoUser;
16 changes: 15 additions & 1 deletion packages/backend/src/routes/bo-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const BOcheckRole = require("../middlewares/bo-check-role.js");
const BOUserController = require("../controllers/bo-user");
const checkTerrForAccountCreation = require("../middlewares/bo-check-terr-for-account-creation");
const getDepartements = require("../middlewares/getDepartements");
const trackBoUser = require("../middlewares/trackBoUser");

const { actions, userTypes } = require("../helpers/tracking");

const BOcheckRoleCompte = BOcheckRole(["Compte"]);

Expand All @@ -29,14 +32,24 @@ router.get(
// Renvoie les informations liées à l'utilisateur
router.get("/:userId", BOcheckJWT, BOcheckRoleCompte, BOUserController.getOne);
// Mise à jour de mes informations
router.post("/me", BOcheckJWT, BOUserController.updateMe);
router.post(
"/me",
BOcheckJWT,
trackBoUser({
action: actions.modification,
itself: true,
userType: userTypes.back,
}),
BOUserController.updateMe,
);
// Création d'un utilisateur
router.post(
"/",
BOcheckJWT,
BOcheckRoleCompte,
getDepartements,
checkTerrForAccountCreation,
trackBoUser({ action: actions.creation, userType: userTypes.back }),
BOUserController.create,
);
// Mise à jour d'un utilisateur
Expand All @@ -46,6 +59,7 @@ router.post(
BOcheckRoleCompte,
getDepartements,
checkTerrForAccountCreation,
trackBoUser({ action: actions.modification, userType: userTypes.back }),
BOUserController.update,
);
// Fonctione transverse de recherche du service compétent
Expand Down
8 changes: 8 additions & 0 deletions packages/backend/src/routes/eig.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const checkPermissionEIG = require("../middlewares/checkPermissionEIG");
const checkPermissionBOEIG = require("../middlewares/checkPermissionBOEIG");
const boCheckRole = require("../middlewares/bo-check-role");
const boCheckJWT = require("../middlewares/bo-check-JWT");
const trackEig = require("../middlewares/trackEig");

const { actions, userTypes } = require("../helpers/tracking");

const { eigController } = require("../controllers");
const getDepartements = require("../middlewares/getDepartements");
Expand Down Expand Up @@ -50,6 +53,7 @@ router.post(
"/",
checkJWT,
checkPermissionDeclarationSejourForEig,
trackEig({ action: actions.creation, userType: userTypes.front }),
eigController.create,
);
router.put(
Expand All @@ -58,13 +62,15 @@ router.put(
checkPermissionEIG,
checkPermissionDeclarationSejourForEig,
canUpdateEig,
trackEig({ action: actions.modification, userType: userTypes.front }),
eigController.update,
);
router.post(
"/depose/:id",
checkJWT,
checkPermissionEIG,
canUpdateEig,
trackEig({ action: actions.modification, userType: userTypes.front }),
eigController.depose,
);

Expand All @@ -73,13 +79,15 @@ router.delete(
checkJWT,
checkPermissionEIG,
canUpdateEig,
trackEig({ action: actions.deletion, userType: userTypes.front }),
eigController.delete,
);

router.post(
"/admin/:id/mark-as-read",
boCheckJWT,
boCheckRoleEig,
trackEig({ action: actions.modification, userType: userTypes.back }),
eigController.markAsRead,
);

Expand Down
9 changes: 8 additions & 1 deletion packages/backend/src/routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ const router = express.Router();

const checkJWT = require("../middlewares/checkJWT");
const userController = require("../controllers/user");
const trackFoUser = require("../middlewares/trackFoUser");
const { actions, userTypes } = require("../helpers/tracking");

// Gère une connexion via mot de passe.
router.get("/me", checkJWT, userController.getMe);
router.patch("/me", checkJWT, userController.patchMe);
router.patch(
"/me",
checkJWT,
trackFoUser({ action: actions.modification, userType: userTypes.front }),
userController.patchMe,
);

module.exports = router;
Loading

0 comments on commit e1e5a41

Please sign in to comment.