fixed role update for users

This commit is contained in:
Pau 2025-05-02 17:58:23 +02:00
parent c2348fc678
commit b827eff9df
2 changed files with 9 additions and 6 deletions

View File

@ -60,9 +60,9 @@ const countUserListQuery = rest =>
* Insert query using parameters passed in request * Insert query using parameters passed in request
* @returns {String} INSERT query * @returns {String} INSERT query
*/ */
const insertUserQuery = ({email, fk_role, createdBy}) => { const insertUserQuery = ({email, role, createdBy}) => {
const emailCondition = email ? ':email' : null; const emailCondition = email ? ':email' : null;
const roleCondition = fk_role ? '(SELECT id FROM dbmaster.roles WHERE name = :fk_role),' : `(SELECT id FROM dbmaster.roles WHERE name = 'viewer'),`; const roleCondition = role ? '(SELECT id FROM dbmaster.roles WHERE uuid = :role),' : `(SELECT id FROM dbmaster.roles WHERE name = 'viewer'),`;
const createdByCondition = createdBy ? ':createdBy' : null; const createdByCondition = createdBy ? ':createdBy' : null;
return ` return `
INSERT INTO dbmaster.users ( INSERT INTO dbmaster.users (
@ -91,11 +91,12 @@ const insertUserQuery = ({email, fk_role, createdBy}) => {
* @param {Object} params All params involved in query to be modified in certain object matching uuid passed as req param * @param {Object} params All params involved in query to be modified in certain object matching uuid passed as req param
* @returns {String} UPDATE query * @returns {String} UPDATE query
*/ */
const modifyUserQuery = ({ username, password, email, role }) => { const modifyUserQuery = ({ username, password, email, role, roleName }) => {
const usernameCondition = username ? `username = :username, ` : ``; const usernameCondition = username ? `username = :username, ` : ``;
const passwordCondition = password ? `password = :password, ` : ``; const passwordCondition = password ? `password = :password, ` : ``;
const emailCondition = email ? `email = :email, ` : ``; const emailCondition = email ? `email = :email, ` : ``;
const roleCondition = role ? `fk_role = (SELECT id FROM dbmaster.roles WHERE name = :role),` : ``; const roleCondition = role ? `fk_role = (SELECT id FROM dbmaster.roles WHERE uuid = :role),` : ``;
const roleNameCondition = roleName ? `fk_role = (SELECT id FROM dbmaster.roles WHERE name = :roleName),` : ``;
return ` return `
UPDATE UPDATE
@ -105,6 +106,7 @@ const modifyUserQuery = ({ username, password, email, role }) => {
${passwordCondition} ${passwordCondition}
${emailCondition} ${emailCondition}
${roleCondition} ${roleCondition}
${roleNameCondition}
uuid = :uuid uuid = :uuid
WHERE WHERE
users.uuid = :uuid users.uuid = :uuid

View File

@ -179,7 +179,7 @@ export default(config) => {
varChar('username'), varChar('username'),
varChar('password'), varChar('password'),
varChar('email').optional({ nullable: true, values: 'falsy' }), varChar('email').optional({ nullable: true, values: 'falsy' }),
varChar('fk_role').optional({ nullable: false, values: 'falsy' }) uuid('role').optional({ nullable: false, values: 'falsy' })
], ],
(req, res, next) => payloadExpressValidator(req, res, next, config), (req, res, next) => payloadExpressValidator(req, res, next, config),
@ -216,7 +216,8 @@ export default(config) => {
uuid('uuid'), uuid('uuid'),
varChar('username').optional({ nullable: false, values: 'falsy' }), varChar('username').optional({ nullable: false, values: 'falsy' }),
varChar('email').optional({ nullable: true, values: 'falsy' }), varChar('email').optional({ nullable: true, values: 'falsy' }),
uuid('fk_role').optional({ nullable: false, values: 'falsy' }) uuid('role').optional({ nullable: false, values: 'falsy' }),
varChar('roleName').optional({ nullable: false, values: 'falsy' }),
], ],
(req, res, next) => payloadExpressValidator(req, res, next, config), (req, res, next) => payloadExpressValidator(req, res, next, config),
(req, res, next) => putUserController(req, res, next, config), (req, res, next) => putUserController(req, res, next, config),