Skip to content

Commit

Permalink
feat(BO): ficher territoire wip 2 636
Browse files Browse the repository at this point in the history
  • Loading branch information
l-scherer committed Nov 7, 2024
1 parent 3e599fc commit 9708036
Show file tree
Hide file tree
Showing 16 changed files with 277 additions and 142 deletions.
1 change: 1 addition & 0 deletions packages/backend/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ app.use(`/geo`, routes.geo);
// TODO(eig): unhide when ok
//app.use(`/eig`, routes.eig);
app.use(`/message`, routes.message);
app.use(`/territoire`, routes.territoire);
app.use(`/healthz`, routes.healthz);

if (config.sentry.environment !== "production") {
Expand Down
1 change: 0 additions & 1 deletion packages/backend/src/controllers/geo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ module.exports.commune = require("./commune");
module.exports.departement = require("./departement");
module.exports.region = require("./region");
module.exports.pays = require("./pays");
module.exports.territoire = require("./territoire");
module.exports.adresse = require("./adresse");
24 changes: 0 additions & 24 deletions packages/backend/src/controllers/geo/territoire.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/backend/src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ module.exports.demandeSejourController = require("./demandeSejour");
module.exports.hebergementController = require("./hebergement");
module.exports.eigController = require("./eig");
module.exports.messageController = require("./message");
module.exports.territoireController = require("./territoire");
14 changes: 14 additions & 0 deletions packages/backend/src/controllers/territoire/get-one.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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);
//console.log("territoires", territoires);
log.i("DONE");
return res.json({ territoires });
};
3 changes: 3 additions & 0 deletions packages/backend/src/controllers/territoire/index.js
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");
12 changes: 12 additions & 0 deletions packages/backend/src/controllers/territoire/list.js
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 });
};
20 changes: 20 additions & 0 deletions packages/backend/src/controllers/territoire/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const Territoire = require("../../services/Territoire");

const logger = require("../../utils/logger");

const log = logger(module.filename);

module.exports = async function update(req, res) {
log.i("IN");
console.log("req.params", req.decoded);
const { id, territoireCode } = req.params;
const { userTerritoireCode } = req.decoded;
console.log("territoireCode : ", territoireCode);
let response;
if (userTerritoireCode === "FRA" || territoireCode === userTerritoireCode) {
response = await Territoire.update(id, req.body);
console.log("territoires", response);
} else response = "UnAuthorized";
log.i("DONE");
return res.json({ response });
};
5 changes: 0 additions & 5 deletions packages/backend/src/routes/geo.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ router.get("/communes", geoController.commune.fetch);
router.get("/communes/:communeCode", geoController.commune.get);
router.get("/departements", geoController.departement.fetch);
router.get("/regions", geoController.region.fetch);
router.get("/territoires", geoController.territoire.fetch);
router.get(
"/territoires/:idTerritoire",
geoController.territoire.getOne,
);
router.post("/adresse", geoController.adresse.fetch);

module.exports = router;
1 change: 1 addition & 0 deletions packages/backend/src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports.sejour = require("./sejour");
module.exports.hebergement = require("./hebergement");
module.exports.agrement = require("./agrement");
module.exports.message = require("./message");
module.exports.territoire = require("./territoire");

module.exports.documents = require("./documents");

Expand Down
12 changes: 12 additions & 0 deletions packages/backend/src/routes/territoire.js
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;
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
const AppError = require("../../utils/error");
const logger = require("../../utils/logger");
const pool = require("../../utils/pgpool").getPool();
const { email } = require("../controllers/authentication");
const telephone = require("../schemas/parts/telephone");
const AppError = require("../utils/error");
const logger = require("../utils/logger");
const pool = require("../utils/pgpool").getPool();

const log = logger(module.filename);

const query = {
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_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`,
getOne: (idTerritoire) => [`
select
fte.id AS territoire_id,
Expand All @@ -41,13 +26,42 @@ const query = {
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],
],
};
/*
service_telephone VARCHAR(12) NULL,
corresp_vao_nom VARCHAR(128) NULL,
corresp_vao_prenom VARCHAR(128) NULL,
edited_at
*/

module.exports.fetch = async (criterias = {}) => {
log.i("fetch - IN");
const { rows } = await pool.query(query.select);
Expand All @@ -63,12 +77,31 @@ module.exports.fetch = async (criterias = {}) => {

module.exports.readOne = async (idTerritoire) => {
log.i("fetch - IN");
//const preQuery = query.getOne(idTerritoire);
console.log("idTerritoire : ", idTerritoire);
console.log("Queyr", ...query.getOne(idTerritoire));
const { rows } = await pool.query(...query.getOne(idTerritoire));
console.log("territoires", rows);
log.i("fetch - DONE");
const territoires = rows;
return territoires;
};

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" };
};
42 changes: 30 additions & 12 deletions packages/frontend-bo/src/pages/territoires/[[territoireId]].vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
type="text"
label="Nom"
name="nom"
:disabled="false"
:disabled="!isModifiable"
:required="true"
:label-visible="true"
placeholder=""
Expand All @@ -40,7 +40,7 @@
type="text"
label="Prénom"
name="prenom"
:disabled="false"
:disabled="!isModifiable"
:required="true"
:label-visible="true"
placeholder=""
Expand All @@ -60,7 +60,7 @@
type="text"
label="Adresse courriel"
name="email"
:disabled="false"
:disabled="!isModifiable"
:required="true"
:label-visible="true"
placeholder=""
Expand All @@ -80,13 +80,14 @@
type="text"
label="Numéro de téléphone"
name="telephone"
:disabled="!isModifiable"
:label-visible="true"
hint="Veuillez saisir votre numéro de téléphone. Exemple: 0612345678"
:is-valid="telephoneField.isValid"
@update:model-value="checkValidTelephone"
/>
</div>
<DsfrButton :disabled="!canSubmit" @click.prevent="update"
<DsfrButton :disabled="!canSubmit && isModifiable" @click.prevent="update"
>Enregistrer
</DsfrButton>
</div>
Expand All @@ -98,13 +99,19 @@ import dayjs from "dayjs";
const TerritoireStore = useTerritoireStore();
const route = useRoute();
const log = logger("pages/territoires/[[territoireId]]");
const userStore = useUserStore();
const displayType = ref(null);
definePageMeta({
middleware: ["is-connected"],
});
await TerritoireStore.get(route.params.territoireId);
const isModifiable =
userStore.user.territoireCode === TerritoireStore.territoire.value;
const nomField = reactive({
errorMessage: "",
modelValue: null,
Expand Down Expand Up @@ -134,6 +141,8 @@ function checkValidEmail(email) {
emailField.isValid = !email || regex.emailRegex.test(email);
emailField.errorMessage =
!email || emailField.isValid ? "" : "Cet email semble incorrect";
console.log("emailField.isValid : ", emailField.isValid);
}
function checkValidNom(n) {
Expand All @@ -146,6 +155,7 @@ function checkValidNom(n) {
!regex.tripleDashRegex.test(n);
nomField.errorMessage =
!n || nomField.isValid ? "" : "Le nom contient des caractères incorrects";
console.log("nomField.isValid : ", nomField.isValid);
}
function checkValidPrenom(p) {
Expand All @@ -160,6 +170,8 @@ function checkValidPrenom(p) {
!p || prenomField.isValid
? ""
: "Le prénom contient des caractères incorrects";
console.log("prenomField.isValid : ", prenomField.isValid);
}
function checkValidTelephone(p) {
Expand All @@ -169,9 +181,11 @@ function checkValidTelephone(p) {
!p || telephoneField.isValid
? ""
: "Le numéro de téléphone n'est pas au format attendu";
console.log("telephoneField.isValid : ", telephoneField.isValid);
}
const canSubmit = computed(() => {
console.log("canSubmit");
return (
emailField.isValid &&
nomField.isValid &&
Expand All @@ -183,7 +197,11 @@ const canSubmit = computed(() => {
async function update() {
log.i("update - IN");
try {
await $fetch(config.public.backendUrl + "/bo-user/" + props.user.id, {
console.log(
"update : route.params.territoireId : ",
route.params.territoireId,
);
await $fetchBackend("/territoire/" + route.params.territoireId, {
credentials: "include",
method: "POST",
headers: {
Expand All @@ -192,9 +210,9 @@ async function update() {
body: JSON.stringify({
nom: nomField.modelValue,
prenom: prenomField.modelValue,
roles: roleUtilisateurField.modelValue,
territoireCode: territoireField.modelValue,
deleted: !actifField.modelValue,
email: emailField.modelValue,
telephone: telephoneField.modelValue,
territoire: TerritoireStore.territoire.value,
}),
})
.then((response) => {
Expand All @@ -219,10 +237,10 @@ async function update() {
onMounted(async () => {
log.i("Mounted - IN");
if (TerritoireStore.territoire) {
nomField.modelValue = TerritoireStore.territoire.corresp_vao_nom;
prenomField.modelValue = TerritoireStore.territoire.corresp_vao_prenom;
emailField.modelValue = TerritoireStore.territoire.service_mail;
telephoneField.modelValue = TerritoireStore.territoire.service_telephone;
checkValidNom(TerritoireStore.territoire.corresp_vao_nom);
checkValidPrenom(TerritoireStore.territoire.corresp_vao_prenom);
checkValidEmail(TerritoireStore.territoire.service_mail);
checkValidTelephone(TerritoireStore.territoire.service_telephone);
}
log.i("Mounted - DONE");
});
Expand Down
Loading

0 comments on commit 9708036

Please sign in to comment.