-
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.
feat(BO): ficher territoire wip 2 636 feat(BO): liste territoire et détail 635-636 fix: prise en compte Issues
- Loading branch information
Showing
20 changed files
with
700 additions
and
2 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
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
17 changes: 17 additions & 0 deletions
17
packages/backend/src/controllers/bo-user/list-users-territoire.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,17 @@ | ||
const BoUser = require("../../services/BoUser"); | ||
const logger = require("../../utils/logger"); | ||
|
||
const log = logger(module.filename); | ||
|
||
module.exports = async function listUsersTerritoire(req, res, next) { | ||
log.i("IN"); | ||
const territoireCode = req.params.territoireCode; | ||
try { | ||
const result = await BoUser.readTerritoires(territoireCode); | ||
log.d(result); | ||
return res.status(200).json(result); | ||
} catch (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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const Territoire = require("../../services/Territoire"); | ||
|
||
const logger = require("../../utils/logger"); | ||
|
||
const log = logger(module.filename); | ||
|
||
module.exports = async function getOne(req, res) { | ||
log.i("IN"); | ||
const { idTerritoire } = req.params; | ||
const territoires = await Territoire.readOne(idTerritoire); | ||
log.i("DONE"); | ||
return res.json({ territoires }); | ||
}; |
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,3 @@ | ||
module.exports.getOne = require("./get-one"); | ||
module.exports.list = require("./list"); | ||
module.exports.update = require("./update"); |
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,12 @@ | ||
const Territoire = require("../../services/Territoire"); | ||
|
||
const logger = require("../../utils/logger"); | ||
|
||
const log = logger(module.filename); | ||
|
||
module.exports = async function list(_req, res) { | ||
log.i("IN"); | ||
const territoires = await Territoire.fetch(); | ||
log.i("DONE"); | ||
return res.json({ territoires }); | ||
}; |
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,28 @@ | ||
const AppError = require("../../utils/error"); | ||
const Territoire = require("../../services/Territoire"); | ||
const logger = require("../../utils/logger"); | ||
|
||
const log = logger(module.filename); | ||
|
||
module.exports = async function update(req, res, next) { | ||
log.i("IN"); | ||
const { id } = req.params; | ||
const { territoire, parent } = req.body; | ||
const { territoireCode } = req.decoded; | ||
let response; | ||
if ( | ||
territoireCode === "FRA" || | ||
territoireCode === territoire || | ||
territoireCode === parent | ||
) { | ||
response = await Territoire.update(id, req.body); | ||
} else { | ||
return next( | ||
new AppError("UnAuthorized", { | ||
statusCode: 400, | ||
}), | ||
); | ||
} | ||
log.i("DONE"); | ||
return res.json({ response }); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const express = require("express"); | ||
|
||
const router = express.Router(); | ||
|
||
const territoireController = require("../controllers/territoire"); | ||
const BOcheckJWT = require("../middlewares/bo-check-JWT"); | ||
|
||
router.get("/list", BOcheckJWT, territoireController.list); | ||
router.get("/get-one/:idTerritoire", BOcheckJWT, territoireController.getOne); | ||
router.post("/:id", BOcheckJWT, territoireController.update); | ||
|
||
module.exports = router; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
const AppError = require("../utils/error"); | ||
const logger = require("../utils/logger"); | ||
const pool = require("../utils/pgpool").getPool(); | ||
|
||
const log = logger(module.filename); | ||
|
||
const query = { | ||
getOne: (idTerritoire) => [` | ||
select | ||
fte.id AS territoire_id, | ||
CASE (ter.code ~ '[0-9]') | ||
WHEN true THEN 'DEP' | ||
ELSE 'REG' | ||
END AS type, | ||
ter.code AS value, | ||
ter.label AS text, | ||
ter.parent_code AS parent, | ||
fte.service_mail AS service_mail, | ||
fte.service_telephone AS service_telephone, | ||
fte.corresp_vao_nom AS corresp_vao_nom, | ||
fte.corresp_vao_prenom AS corresp_vao_prenom, | ||
fte.edited_at AS edited_at | ||
FROM back.fiche_territoire fte | ||
INNER JOIN geo.territoires ter ON fte.ter_code = ter.code | ||
WHERE fte.id = $1`, | ||
[idTerritoire], | ||
], | ||
select: ` | ||
select | ||
fte.id AS territoire_id, | ||
CASE (ter.code ~ '[0-9]') | ||
WHEN true THEN 'DEP' | ||
ELSE 'REG' | ||
END AS type, | ||
ter.code AS value, | ||
ter.label AS text, | ||
fte.service_telephone AS service_telephone, | ||
fte.corresp_vao_nom AS corresp_vao_nom, | ||
fte.corresp_vao_prenom AS corresp_vao_prenom, | ||
fte.service_mail as service_mail, | ||
COUNT(distinct(usr.id)) as nbusersbo | ||
FROM geo.territoires ter | ||
INNER JOIN back.fiche_territoire fte ON fte.ter_code = ter.code | ||
LEFT JOIN back.users usr ON usr.ter_code = ter.code | ||
WHERE code <> 'FRA' | ||
GROUP BY 1, 2, 3, 4, 5 | ||
ORDER BY 2, 4 ASC`, | ||
update: (id, nom, prenom, email, telephone) => [ | ||
` | ||
UPDATE back.fiche_territoire | ||
SET | ||
corresp_vao_nom = $2, | ||
corresp_vao_prenom = $3, | ||
service_mail = $4, | ||
service_telephone = $5, | ||
edited_at = NOW() | ||
WHERE | ||
id = $1 | ||
`, | ||
[id, nom, prenom, email, telephone], | ||
], | ||
}; | ||
|
||
module.exports.fetch = async (criterias = {}) => { | ||
log.i("fetch - IN"); | ||
const { rows } = await pool.query(query.select); | ||
|
||
const filters = Object.entries(criterias); | ||
const territoires = rows.filter((territoire) => { | ||
return filters.every(([key, value]) => territoire[key] == value); | ||
}); | ||
|
||
log.i("fetch - DONE"); | ||
return territoires; | ||
}; | ||
|
||
module.exports.readOne = async (idTerritoire) => { | ||
log.i("fetch - IN"); | ||
const { rows } = await pool.query(...query.getOne(idTerritoire)); | ||
log.i("fetch - DONE"); | ||
return rows; | ||
}; | ||
|
||
module.exports.update = async (id, { nom, prenom, email, telephone }) => { | ||
log.i("update - IN", { id }); | ||
if (!id) { | ||
throw new AppError("Paramètre manquant", { | ||
statusCode: 500, | ||
}); | ||
} | ||
|
||
const { rowCount } = await pool.query( | ||
...query.update(id, nom, prenom, email, telephone), | ||
); | ||
|
||
if (rowCount === 0) { | ||
log.d("update - DONE - fiche territoire inexistante"); | ||
throw new AppError("Fiche territoire déjà inexistant", { | ||
name: "FicheTerritoireNotFound", | ||
}); | ||
} | ||
|
||
log.i("update - DONE"); | ||
return { code: "MajCompte" }; | ||
}; |
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.