diff --git a/packages/backend/src/controllers/bo-authentication/email/login.js b/packages/backend/src/controllers/bo-authentication/email/login.js index 747699ec9..4a30acbad 100644 --- a/packages/backend/src/controllers/bo-authentication/email/login.js +++ b/packages/backend/src/controllers/bo-authentication/email/login.js @@ -55,13 +55,6 @@ module.exports = async function login(req, res, next) { ); } - user.serviceCompetent = - user.territoireCode === "FRA" - ? "NAT" - : /^[0-9]+$/.test(user.territoireCode) - ? "DEP" - : "REG"; - try { const accessToken = jwt.sign(buildAccessToken(user), config.tokenSecret, { algorithm: "ES512", diff --git a/packages/backend/src/controllers/bo-user/index.js b/packages/backend/src/controllers/bo-user/index.js index 9e2ff9133..ef93e3172 100644 --- a/packages/backend/src/controllers/bo-user/index.js +++ b/packages/backend/src/controllers/bo-user/index.js @@ -4,5 +4,6 @@ module.exports.getOne = require("./get-one"); module.exports.getMe = require("./get-me"); module.exports.create = require("./create"); module.exports.update = require("./update"); +module.exports.updateMe = require("./update-me"); module.exports.serviceCompetence = require("./service-competence"); module.exports.verifyCompetence = require("./service-competence"); diff --git a/packages/backend/src/controllers/bo-user/update-me.js b/packages/backend/src/controllers/bo-user/update-me.js new file mode 100644 index 000000000..b642171cf --- /dev/null +++ b/packages/backend/src/controllers/bo-user/update-me.js @@ -0,0 +1,37 @@ +const BoUser = require("../../services/BoUser"); + +const logger = require("../../utils/logger"); +const ValidationAppError = require("../../utils/validation-error"); + +const BOUserSchema = require("../../schemas/bo-user"); + +const log = logger(module.filename); + +module.exports = async function updateMe(req, res, next) { + log.i("IN", req.body); + const { id } = req.decoded; + let user; + if (id) { + try { + user = await BOUserSchema() + .omit(["email", "deleted", "roles", "territoireCode"]) + .validate(req.body, { + abortEarly: false, + stripUnknown: true, + }); + } catch (error) { + log.w(error); + return next(new ValidationAppError(error)); + } + try { + await BoUser.updateMe(id, user); + return res.status(200).json({ message: "Utilisateur mis à jour" }); + } catch (error) { + log.w("DONE with error"); + return next(error); + } + } else + return res.status(403).json({ + message: "Permission refusée. Utilisateur non reconnu", + }); +}; diff --git a/packages/backend/src/routes/bo-user.js b/packages/backend/src/routes/bo-user.js index 23089c5cc..cdce30363 100644 --- a/packages/backend/src/routes/bo-user.js +++ b/packages/backend/src/routes/bo-user.js @@ -22,6 +22,8 @@ router.get( router.get("/me", BOcheckJWT, BOUserController.getMe); // Renvoie les informations liées à l'utilisateur router.get("/:userId", BOcheckJWT, BOcheckRoleCompte, BOUserController.getOne); +// Mise à jour de mes informations +router.post("/me", BOcheckJWT, BOUserController.updateMe); // Création d'un utilisateur router.post( "/", diff --git a/packages/backend/src/services/BoUser.js b/packages/backend/src/services/BoUser.js index d4179fc68..14742ae72 100644 --- a/packages/backend/src/services/BoUser.js +++ b/packages/backend/src/services/BoUser.js @@ -209,6 +209,17 @@ ${additionalParamsQuery} `, [id, nom, prenom, territoireCode], ], + updateMe: (id, nom, prenom) => [ + ` + UPDATE back.users + SET + nom = $2, + prenom = $3 + WHERE + id = $1 + `, + [id, nom, prenom], + ], }; module.exports.create = async ({ @@ -250,6 +261,27 @@ module.exports.create = async ({ return { code: "CreationCompte", user }; }; +module.exports.updateMe = async (id, { nom, prenom }) => { + log.i("update - IN", { id }); + if (!id) { + throw new AppError("Paramètre manquant ou erroné de patate", { + statusCode: 500, + }); + } + // Mise à jour du compte en base de données + const { rowCount } = await pool.query(...query.updateMe(id, nom, prenom)); + + if (rowCount === 0) { + log.d("update - DONE - Utilisateur BO inexistant"); + throw new AppError("Utilisateur déjà inexistant", { + name: "UserNotFound", + }); + } + log.i("updateMe - DONE"); + return { code: "MajCompte" }; +}; + + module.exports.update = async ( id, { nom, prenom, roles, territoireCode, deleted }, diff --git a/packages/frontend-bo/src/components/user/Compte.vue b/packages/frontend-bo/src/components/user/Compte.vue new file mode 100644 index 000000000..ad37c68f3 --- /dev/null +++ b/packages/frontend-bo/src/components/user/Compte.vue @@ -0,0 +1,729 @@ + + + \ No newline at end of file diff --git a/packages/frontend-bo/src/layouts/default.vue b/packages/frontend-bo/src/layouts/default.vue index 2acfdeb91..740219b3d 100644 --- a/packages/frontend-bo/src/layouts/default.vue +++ b/packages/frontend-bo/src/layouts/default.vue @@ -3,10 +3,9 @@ const navItems = useMenuNavItems(); const log = logger("layouts/default"); const userStore = useUserStore(); - const config = useRuntimeConfig(); -const header = reactive({ +const header = computed(() => ({ dimension: { height: "80px" }, logoText: ["Republique", "française"], quickLinks: [ @@ -20,10 +19,10 @@ const header = reactive({ }, { label: "Mon compte", - to: "/", + to: `/comptes/mon-compte`, icon: "ri-account-circle-line", iconRight: false, - class: computed(() => (userStore.isConnected ? "" : "fr-hidden")), + class: userStore.isConnected ? "" : "fr-hidden", }, { label: "Se déconnecter", @@ -31,10 +30,10 @@ const header = reactive({ icon: "ri-logout-box-line", iconRight: false, button: true, - class: computed(() => (userStore.isConnected ? "" : "fr-hidden")), + class: userStore.isConnected ? "" : "fr-hidden", }, ], -}); +})); const homeTo = computed(() => { return userStore.isConnected ? "/" : "/connexion/"; diff --git a/packages/frontend-bo/src/pages/comptes/[[userId]].vue b/packages/frontend-bo/src/pages/comptes/[[userId]].vue index 98df90d21..f7f65001e 100644 --- a/packages/frontend-bo/src/pages/comptes/[[userId]].vue +++ b/packages/frontend-bo/src/pages/comptes/[[userId]].vue @@ -1,275 +1,18 @@ - - + \ No newline at end of file diff --git a/packages/frontend-bo/src/pages/comptes/mon-compte.vue b/packages/frontend-bo/src/pages/comptes/mon-compte.vue new file mode 100644 index 000000000..e9b369be2 --- /dev/null +++ b/packages/frontend-bo/src/pages/comptes/mon-compte.vue @@ -0,0 +1,38 @@ + + + + + diff --git a/packages/frontend-bo/src/stores/user.js b/packages/frontend-bo/src/stores/user.js index a2e5dc619..22f917346 100644 --- a/packages/frontend-bo/src/stores/user.js +++ b/packages/frontend-bo/src/stores/user.js @@ -26,6 +26,12 @@ export const useUserStore = defineStore("user", { if (user) { log.i("refreshProfile - DONE"); this.user = user; + this.user.serviceCompetent = + this.user.territoireCode === "FRA" + ? "NAT" + : /^"\d"+$/.test(user.territoireCode) + ? "DEP" + : "REG"; } } catch (err) { log.w("refreshProfile - DONE with error", err); @@ -116,7 +122,6 @@ export const useUserStore = defineStore("user", { async getUser(id) { log.i("getUser - IN", { id }); try { - // Appel du back pour la liste des utilisateurs const user = await $fetchBackend("/bo-user/" + id, { credentials: "include", method: "GET",