Skip to content

Commit

Permalink
feat(hebergement): add statut
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminDNUM committed Dec 6, 2024
1 parent dc7aacb commit c57407b
Show file tree
Hide file tree
Showing 23 changed files with 645 additions and 170 deletions.
69 changes: 69 additions & 0 deletions packages/backend/src/controllers/hebergement/activate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const yup = require("yup");
const logger = require("../../utils/logger");
const ValidationAppError = require("../../utils/validation-error");
const HebergementSchema = require("../../schemas/hebergement");
const AppError = require("../../utils/error");
const Hebergement = require("../../services/hebergement/Hebergement");
const HebergementHelper = require("../../helpers/hebergement");

const log = logger(module.filename);

module.exports = async function activate(req, res, next) {
const hebergementId = req.params.id;
const { body, decoded } = req;
const userId = decoded.id;

const { nom, coordonnees, informationsLocaux, informationsTransport } = body;

if (
!nom ||
!coordonnees ||
!informationsLocaux ||
!informationsTransport ||
!hebergementId
) {
log.w("missing or invalid parameter");

return next(
new AppError("Paramètre incorrect", {
statusCode: 400,
}),
);
}
let hebergement;

try {
hebergement = await yup.object(HebergementSchema.schema(false)).validate(
{
coordonnees,
informationsLocaux,
informationsTransport,
nom,
},
{
abortEarly: false,
stripped: true,
},
);
} catch (error) {
return next(new ValidationAppError(error));
}

try {
await Hebergement.updateWithoutHistory(
userId,
hebergementId,
HebergementHelper.statuts.ACTIF,
hebergement,
);

log.i("DONE");
return res.sendStatus(200);
} catch (error) {
if (error.cause === "archive") {
return next(new AppError(error));
}
log.w("DONE with error");
return next(error);
}
};
37 changes: 17 additions & 20 deletions packages/backend/src/controllers/hebergement/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,30 @@ const log = logger(module.filename);
module.exports = async function get(req, res) {
log.i("IN");
const { id: userId } = req.decoded;
const search = req.query.search ? JSON.parse(req.query.search) : {};
let hebergements;
let searchByUserId = true;
try {
const search = req.query.search ? JSON.parse(req.query.search) : {};
if (search?.organismeId) {
const organismeUserConnected = await Organisme.getOne({
use_id: userId,
});
if (organismeUserConnected?.organismeId !== search.organismeId) {
// Si c'est l'organisme de l'utilisateur connecté on remonte tous les hébergements de cet utilisateur
searchByUserId = false;
const organismeSiege = await Organisme.getSiege(
organismeUserConnected.personneMorale.siret,
const organismeUserConnected = await Organisme.getOne({
use_id: userId,
});
const searchByUserId =
!search?.organismeId ||
organismeUserConnected?.organismeId === search.organismeId;
if (!searchByUserId) {
const organismeSiege = await Organisme.getSiege(
organismeUserConnected.personneMorale.siret,
);
// L'organisme Siege est bien le même organisme que l'utilisateur connecté
if (organismeSiege.organismeId === organismeUserConnected?.organismeId) {
hebergements = await Hebergement.getBySiren(
organismeSiege.personneMorale.siren,
search,
);
// L'organisme Siege est bien le même organisme que l'utilisateur connecté
if (
organismeSiege.organismeId === organismeUserConnected?.organismeId
) {
hebergements = await Hebergement.getBySiren(
organismeSiege.personneMorale.siren,
);
}
}
}
if (searchByUserId) {
// Si c'est l'oganisme de l'utilisateur connecté on remonte tous les hébergements de cet utilisateur
hebergements = await Hebergement.getByUserId(userId);
hebergements = await Hebergement.getByUserId(userId, search);
}
log.d(hebergements);
return res.status(200).json({ hebergements });
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/controllers/hebergement/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ module.exports.getExtract = require("./getExtract");
module.exports.post = require("./post");
module.exports.postBrouillon = require("./post-brouillon");
module.exports.update = require("./update");
module.exports.updateBrouillon = require("./update-brouillon");
module.exports.activate = require("./activate");
11 changes: 6 additions & 5 deletions packages/backend/src/controllers/hebergement/post-brouillon.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const HebergementHelper = require("../../helpers/hebergement");
const HebergementSchema = require("../../schemas/hebergement");
const logger = require("../../utils/logger");
const ValidationAppError = require("../../utils/validation-error");
const AppError = require("../../utils/error");
const FOUser = require("../../services/FoUser");

const log = logger(module.filename);
Expand Down Expand Up @@ -37,10 +36,12 @@ module.exports = async function postbrouillon(req, res, next) {

try {
const organismeId = await FOUser.getUserOrganisme(userId);
const id = await Hebergement.create(userId, organismeId, {
...hebergement,
statut: HebergementHelper.statuts.BROUILLON,
});
const id = await Hebergement.create(
userId,
organismeId,
HebergementHelper.statuts.BROUILLON,
hebergement,
);

return res.status(200).json({
id,
Expand Down
10 changes: 6 additions & 4 deletions packages/backend/src/controllers/hebergement/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ module.exports = async function post(req, res, next) {

try {
const organismeId = await FOUser.getUserOrganisme(userId);
const id = await Hebergement.create(userId, organismeId, {
...hebergement,
statut: HebergementHelper.statuts.ACTIF,
});
const id = await Hebergement.create(
userId,
organismeId,
HebergementHelper.statuts.ACTIF,
hebergement,
);

return res.status(200).json({
id,
Expand Down
69 changes: 69 additions & 0 deletions packages/backend/src/controllers/hebergement/update-brouillon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const yup = require("yup");
const logger = require("../../utils/logger");
const ValidationAppError = require("../../utils/validation-error");
const HebergementSchema = require("../../schemas/hebergement");
const AppError = require("../../utils/error");
const Hebergement = require("../../services/hebergement/Hebergement");
const HebergementHelper = require("../../helpers/hebergement");

const log = logger(module.filename);

module.exports = async function updateBrouillon(req, res, next) {
const hebergementId = req.params.id;
const { body, decoded } = req;
const userId = decoded.id;

const { nom, coordonnees, informationsLocaux, informationsTransport } = body;

if (
!nom ||
!coordonnees ||
!informationsLocaux ||
!informationsTransport ||
!hebergementId
) {
log.w("missing or invalid parameter");

return next(
new AppError("Paramètre incorrect", {
statusCode: 400,
}),
);
}
let hebergement;

try {
hebergement = await yup.object(HebergementSchema.schema(true)).validate(
{
coordonnees,
informationsLocaux,
informationsTransport,
nom,
},
{
abortEarly: false,
stripped: true,
},
);
} catch (error) {
return next(new ValidationAppError(error));
}

try {
await Hebergement.updateWithoutHistory(
userId,
hebergementId,
HebergementHelper.statuts.BROUILLON,
hebergement,
);

log.i("DONE");
return res.sendStatus(200);
} catch (error) {
if (error.cause === "archive") {
return next(new AppError(error));
}
log.w("DONE with error");
return next(error);
}
};
2 changes: 1 addition & 1 deletion packages/backend/src/controllers/hebergement/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = async function post(req, res, next) {
let hebergement;

try {
hebergement = await yup.object(HebergementSchema.schema()).validate(
hebergement = await yup.object(HebergementSchema.schema(false)).validate(
{
coordonnees,
informationsLocaux,
Expand Down
38 changes: 0 additions & 38 deletions packages/backend/src/middlewares/checkPermissionBOHebergement.js

This file was deleted.

39 changes: 39 additions & 0 deletions packages/backend/src/middlewares/checkStatutHebergement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const logger = require("../utils/logger");
const AppError = require("../utils/error");
const Hebergement = require("../services/hebergement/Hebergement");

const log = logger(module.filename);

function checkStatutHebergement(statut) {
return async (req, res, next) => {
const { id: hebergementId } = req.params;

if (!hebergementId) {
return next(
new AppError("Paramètres invalides", {
statusCode: 400,
}),
);
}

let hebergementStatut;
try {
hebergementStatut = await Hebergement.getStatut(hebergementId);
} catch (error) {
log.w("DONE with error");
return next(error);
}

if (statut !== hebergementStatut) {
return next(
new AppError("Vous n'êtes pas autorisé à accéder à cet hébergement", {
statusCode: 403,
}),
);
}

next();
};
}

module.exports = checkStatutHebergement;
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ const checkPermissionHebergement = require("../../../middlewares/checkPermission
jest.mock("../../../services/hebergement/Hebergement");
jest.mock("../../../middlewares/checkJWT");
jest.mock("../../../middlewares/checkPermissionHebergement");

jest.mock(
"../../../middlewares/checkStatutHebergement",
() => () => (req, res, next) => {
next();
},
);
describe("GET /hebergement/:id", () => {
const user = {
id: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ const checkPermissionHebergement = require("../../../middlewares/checkPermission
jest.mock("../../../services/hebergement/Hebergement");
jest.mock("../../../middlewares/bo-check-JWT");
jest.mock("../../../middlewares/checkPermissionHebergement");

jest.mock(
"../../../middlewares/checkStatutHebergement",
() => () => (req, res, next) => {
next();
},
);
describe("GET /hebergement/admin/:id", () => {
const user = {
id: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ jest.mock("../../../middlewares/checkJWT");
jest.mock("../../../middlewares/checkPermissionHebergement");
jest.mock("../../../schemas/hebergement");
jest.mock("../../../services/FoUser");

jest.mock(
"../../../middlewares/checkStatutHebergement",
() => () => (req, res, next) => {
next();
},
);
describe("POST /", () => {
const user = {
id: 1,
Expand Down Expand Up @@ -58,6 +63,7 @@ describe("POST /", () => {
expect(Hebergement.create).toHaveBeenCalledWith(
1,
1,
"actif",
expect.objectContaining(body),
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ const yup = require("yup");
jest.mock("../../../services/hebergement/Hebergement");
jest.mock("../../../middlewares/checkJWT");
jest.mock("../../../middlewares/checkPermissionHebergement");
jest.mock(
"../../../middlewares/checkStatutHebergement",
() => () => (req, res, next) => {
next();
},
);
jest.mock("../../../schemas/hebergement");

describe("POST /:id", () => {
Expand Down
Loading

0 comments on commit c57407b

Please sign in to comment.