fixed badly written queries

This commit is contained in:
Pau 2025-04-11 20:32:39 +02:00
parent 5ca63437ca
commit 8e10c2aebb
2 changed files with 13 additions and 12 deletions

View File

@ -12,8 +12,10 @@ const _permissionsQuery = (_pagination = '') => ({count}) => ({uuid, action, end
acloc.permissions as p acloc.permissions as p
LEFT JOIN LEFT JOIN
acloc.endpoints as e ON p.fk_endpoint = e.id acloc.endpoints as e ON p.fk_endpoint = e.id
AND e.created <= :now WHERE
e.created <= :now
AND (e.deleted > :now OR e.deleted IS NULL) AND (e.deleted > :now OR e.deleted IS NULL)
AND
p.created <= :now p.created <= :now
AND AND
(p.deleted > :now OR p.deleted IS NULL) (p.deleted > :now OR p.deleted IS NULL)
@ -31,11 +33,11 @@ const getPermissionsQuery = ({ limit, page, ...rest }) =>
const countPermissionsQuery = rest => const countPermissionsQuery = rest =>
_permissionsQuery()({ count: 'COUNT(*) AS count' })(rest); _permissionsQuery()({ count: 'COUNT(*) AS count' })(rest);
const insertPermissionsQuery = () => { const insertPermissionsQuery = (createdBy) => {
return ` const createdByCondition = createdBy ? ':createdBy' : null;
return `
INSERT INTO acloc.permissions ( INSERT INTO acloc.permissions (
uuid, uuid,
name,
action, action,
fk_endpoint, fk_endpoint,
created, created,
@ -43,21 +45,20 @@ const insertPermissionsQuery = () => {
) )
VALUES ( VALUES (
:uuid, :uuid,
:name,
:action, :action,
(SELECT id FROM acloc.endpoints WHERE route = :endpoint), (SELECT id FROM acloc.endpoints WHERE route = :endpoint),
:now, :now,
:createdBy ${createdByCondition}
); );
SELECT * FROM acloc.permissions WHERE uuid = :uuid; SELECT * FROM acloc.permissions WHERE uuid = :uuid;
` `
} }
const modifyPermissionsQuery = (action, endpoint) => { const modifyPermissionsQuery = (action, endpoint) => {
const actionCondition = action ? 'action = :action ,' : ''; const actionCondition = action ? 'action = :action ,' : '';
const endpointCondition = endpoint ? 'fk_endpoint = (SELECT id from acloc.endpoints WHERE route = :endpoint),' : ''; const endpointCondition = endpoint ? 'fk_endpoint = (SELECT id from acloc.endpoints WHERE route = :endpoint),' : '';
return ` return `
UPDATE acloc.permissions UPDATE acloc.permissions AS permissions
SET SET
${actionCondition} ${actionCondition}
${endpointCondition} ${endpointCondition}
@ -73,13 +74,13 @@ const modifyPermissionsQuery = (action, endpoint) => {
const softDeletePermissionsQuery = () => { const softDeletePermissionsQuery = () => {
return ` return `
UPDATE UPDATE
acloc.permissions acloc.permissions AS permissions
SET SET
deleted = :now, deletedBy = :deletedBy deleted = :now, deletedBy = :deletedBy
WHERE WHERE
permissions.uuid = :uuid permissions.uuid = :uuid
AND AND
permissions.deleted IS NULL permissions.deleted IS NULL;
SELECT * FROM acloc.permissions WHERE uuid = :uuid; SELECT * FROM acloc.permissions WHERE uuid = :uuid;
` `
} }

View File

@ -23,7 +23,7 @@ const _userListSelectQuery = (_pagination = '') =>
${count || `users.*, r.name AS role`} ${count || `users.*, r.name AS role`}
FROM FROM
acloc.users as users acloc.users as users
LEFT JOIN acloc.roles as r ON acloc.fk_role = r.id LEFT JOIN acloc.roles as r ON users.fk_role = r.id
WHERE WHERE
users.created <= :now users.created <= :now
AND AND
@ -63,7 +63,7 @@ const countUserListQuery = rest =>
const insertUserQuery = ({email, fk_role, createdBy}) => { const insertUserQuery = ({email, fk_role, createdBy}) => {
const emailCondition = email ? ':email' : null; const emailCondition = email ? ':email' : null;
const roleCondition = fk_role ? '(SELECT id FROM acloc.roles WHERE name = :fk_role)' : `(SELECT id FROM acloc.roles WHERE name = 'viewer')`; const roleCondition = fk_role ? '(SELECT id FROM acloc.roles WHERE name = :fk_role)' : `(SELECT id FROM acloc.roles WHERE name = 'viewer')`;
const createdByCondition = createdBy ? 'createdBy = :createdBy' : null; const createdByCondition = createdBy ? ':createdBy' : null;
return ` return `
INSERT INTO acloc.users ( INSERT INTO acloc.users (
uuid, uuid,