Skip to content

Commit

Permalink
feat(BO): liste territoire et détail 635-636
Browse files Browse the repository at this point in the history
  • Loading branch information
l-scherer committed Nov 7, 2024
1 parent f628aec commit 8539df0
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 137 deletions.
1 change: 1 addition & 0 deletions packages/backend/src/controllers/bo-user/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports.list = require("./list");
module.exports.listUsersTerritoire = require("./list-users-territoire");
module.exports.getExtract = require("./getExtract");
module.exports.getOne = require("./get-one");
module.exports.getMe = require("./get-me");
Expand Down
17 changes: 17 additions & 0 deletions packages/backend/src/controllers/bo-user/list-users-territoire.js
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);
}
};
1 change: 0 additions & 1 deletion packages/backend/src/controllers/territoire/get-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ 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 });
};
26 changes: 17 additions & 9 deletions packages/backend/src/controllers/territoire/update.js
Original file line number Diff line number Diff line change
@@ -1,20 +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) {
module.exports = async function update(req, res, next) {
log.i("IN");
console.log("req.params", req.decoded);
const { id, territoireCode } = req.params;
const { userTerritoireCode } = req.decoded;
console.log("territoireCode : ", territoireCode);
const { id } = req.params;
const { territoire, parent } = req.body;
const { territoireCode } = req.decoded;
let response;
if (userTerritoireCode === "FRA" || territoireCode === userTerritoireCode) {
if (
territoireCode === "FRA" ||
territoireCode === territoire ||
territoireCode === parent
) {
response = await Territoire.update(id, req.body);
console.log("territoires", response);
} else response = "UnAuthorized";
} else {
return next(
new AppError("UnAuthorized", {
statusCode: 400,
}),
);
}
log.i("DONE");
return res.json({ response });
};
6 changes: 6 additions & 0 deletions packages/backend/src/routes/bo-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ router.get(
);
// Gère une connexion via mot de passe.
router.get("/me", BOcheckJWT, BOUserController.getMe);
// Liste des utilisateurs BO Liés à un territoire et sous territoires
router.get(
"/territoires/:territoireCode",
BOcheckJWT,
BOUserController.listUsersTerritoire,
);
// Renvoie les informations liées à l'utilisateur
router.get("/:userId", BOcheckJWT, BOcheckRoleCompte, BOUserController.getOne);
// Mise à jour de mes informations
Expand Down
44 changes: 43 additions & 1 deletion packages/backend/src/services/BoUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,21 @@ ${additionalParamsQuery}
`,
additionalParams,
],
listUsersTerritoire: `
SELECT
us.mail as email,
us.blocked as "isBlocked",
us.nom as nom,
us.prenom as prenom,
us.ter_code as "territoireCode",
terp.label as "territoireLibelle"
FROM geo.territoires terp
LEFT JOIN geo.territoires ters ON ters.parent_code = terp.code
INNER JOIN back.users us ON (terp.code = us.ter_code OR ters.code = us.ter_code) AND us.ter_code <> 'FRA'
WHERE terp.code = $1 AND us.validated = true AND us.deleted = false
GROUP BY 1,2,3,4,5,6
ORDER BY nom
`,
login: `
SELECT
us.id as id,
Expand Down Expand Up @@ -428,12 +443,26 @@ module.exports.read = async (
) {
searchQuery += ` AND (
unaccent(ter.label) ILIKE unaccent($${searchParams.length + 1})
OR code ILIKE $${searchParams.length + 1}
OR ter.code ILIKE $${searchParams.length + 1}
OR ter.parent_code IN (SELECT code FROM matched_elements)
)\n`;
territoireSearchParamId = searchParams.length + 1;
searchParams.push(`%${search.territoire}%`);
}
// Filtre utilisé pour rechercher tous les users pour un territoire et ses enfants
if (
search?.territoireParent &&
search?.territoireParent !== "FRA" &&
search.territoireParent.length
) {
searchQuery += ` AND (
ter.code = $${searchParams.length + 1}
OR ter.parent_code = $${searchParams.length + 1}
)\n`;
territoireSearchParamId = searchParams.length + 1;
searchParams.push(`%${search.territoireParent}%`);
}

if (search?.statut === "validated") {
searchQuery += `AND us.validated = true\n`;
}
Expand Down Expand Up @@ -495,6 +524,7 @@ module.exports.read = async (
WHERE unaccent(label) ILIKE unaccent($${territoireSearchParamId})
OR code ILIKE $${territoireSearchParamId}
)\n`;

const response = await pool.query(
`${territoireSearchParamId ? territoirePreQuery : ""}
${queryPrepared[0]}`,
Expand All @@ -515,6 +545,18 @@ module.exports.read = async (
};
};

module.exports.readTerritoires = async (territoireParent) => {
log.i("readTerritoires - IN", territoireParent);
const userTerritoire = await pool.query(query.listUsersTerritoire, [
territoireParent,
]);
log.i("readTerritoires - DONE");
return {
total: userTerritoire.rows.count,
users: userTerritoire.rows,
};
};

module.exports.readOne = async (id, territoireCode) => {
log.i("readOne - IN", { id });

Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/services/Territoire.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const query = {
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,
Expand Down
1 change: 0 additions & 1 deletion packages/frontend-bo/src/components/user/Compte.vue
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,6 @@ const closeModal = () => (popUpParams.value = null);
const modalOpenCounter = ref(0);
const openModal = (p) => {
console.log(p);
modalOpenCounter.value++;
popUpParams.value = {
cb: () => {
Expand Down
101 changes: 73 additions & 28 deletions packages/frontend-bo/src/pages/territoires/[[territoireId]].vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@
<div>
<h4>
Fiche territoire
{{
TerritoireStore.territoire.type === "DEP"
? "du département"
: "de la région"
}}
{{ TerritoireStore.territoire.value }} -
{{ TerritoireStore.territoire.text }}
{{ titleTerritoire }}
</h4>
<div
class="fr-fieldset__element fr-col-12 fr-col-sm-8 fr-col-md-8 fr-col-lg-8 fr-col-xl-8"
Expand Down Expand Up @@ -87,19 +81,69 @@
@update:model-value="checkValidTelephone"
/>
</div>
<DsfrButton :disabled="!canSubmit && isModifiable" @click.prevent="update"
<DsfrButton
v-if="isModifiable"
:disabled="!canSubmit"
@click.prevent="update"
>Enregistrer
</DsfrButton>
<TableFull
:title="titleUser"
:headers="headers"
:data="userStore.users ?? []"
/>
<DsfrButton @click.prevent="close">Retour</DsfrButton>
</div>
</div>
</template>
<script setup>
import dayjs from "dayjs";
import { TableFull } from "@vao/shared";
const toaster = useToaster();
const TerritoireStore = useTerritoireStore();
const route = useRoute();
const log = logger("pages/territoires/[[territoireId]]");
const userStore = useUserStore();
const titleUser = computed(() => {
const territoireType =
TerritoireStore.territoire.type === "DEP"
? "du département"
: "de la région et de ses départements";
return `Liste des comptes ${territoireType}`;
});
const titleTerritoire = computed(() => {
const territoireType =
TerritoireStore.territoire.type === "DEP"
? "du département"
: "de la région";
const territoireValue = TerritoireStore.territoire.value;
const territoireText = TerritoireStore.territoire.text;
return `${territoireType} ${territoireValue} - ${territoireText}`;
});
const headers = [
{
column: "nom",
text: "Nom",
sort: true,
},
{
column: "prenom",
text: "Prénom",
sort: true,
},
{
column: "email",
text: "Adresse courriel",
sort: true,
},
{
column: "territoireCode",
text: "Territoire",
sort: true,
},
];
const displayType = ref(null);
Expand All @@ -108,9 +152,12 @@ definePageMeta({
});
await TerritoireStore.get(route.params.territoireId);
await userStore.fetchUsersTerritoire(TerritoireStore.territoire.value);
const isModifiable =
userStore.user.territoireCode === TerritoireStore.territoire.value;
userStore.user.territoireCode === "FRA" ||
userStore.user.territoireCode === TerritoireStore.territoire.value ||
userStore.user.territoireCode === TerritoireStore.territoire.parent;
const nomField = reactive({
errorMessage: "",
Expand Down Expand Up @@ -141,8 +188,6 @@ 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 @@ -155,7 +200,6 @@ 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 @@ -170,8 +214,6 @@ 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 @@ -181,11 +223,9 @@ 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 @@ -197,10 +237,6 @@ const canSubmit = computed(() => {
async function update() {
log.i("update - IN");
try {
console.log(
"update : route.params.territoireId : ",
route.params.territoireId,
);
await $fetchBackend("/territoire/" + route.params.territoireId, {
credentials: "include",
method: "POST",
Expand All @@ -213,19 +249,23 @@ async function update() {
email: emailField.modelValue,
telephone: telephoneField.modelValue,
territoire: TerritoireStore.territoire.value,
parent: TerritoireStore.territoire.parent,
}),
})
.then((response) => {
displayType.value = "UpdateDoneWithSucces";
toaster.success({
titleTag: "h2",
description: `Territoire mis à jour avec succès`,
});
log.d("update", { response });
return navigateTo("/territoires/liste");
})
.catch((error) => {
const body = error.data;
const codeError = body.name;
log.w("update", { body, codeError });
displayType.value = codeError;
formStatus.value = formStates.SUBMITTED;
log.w("update", error);
toaster.error({
titleTag: "h2",
description: `Erreur lors de la mise à jour du terrritoire`,
});
});
} catch (error) {
log.w("update", { error });
Expand All @@ -234,6 +274,11 @@ async function update() {
log.i("update - DONE");
}
}
const close = () => {
navigateTo(`/territoires/liste`);
};
onMounted(async () => {
log.i("Mounted - IN");
if (TerritoireStore.territoire) {
Expand Down
Loading

0 comments on commit 8539df0

Please sign in to comment.