-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc7aacb
commit 776ceb7
Showing
22 changed files
with
620 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/backend/src/controllers/hebergement/update-brouillon.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 0 additions & 38 deletions
38
packages/backend/src/middlewares/checkPermissionBOHebergement.js
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
packages/backend/src/middlewares/checkStatutHebergement.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.