minor fix regarding query sintaxis and cohesion

This commit is contained in:
Pau 2025-04-02 19:05:58 +02:00
parent f06ef22eb5
commit 9f15b06632
5 changed files with 19 additions and 23 deletions

View File

@ -1,4 +1,4 @@
import { pagination } from "../../utils/pagination";
import { pagination } from "../../utils/pagination.js";
const _endpointsQuery = (_pagination) => ({ count }) => ({ uuid, route, method }) => {
const uuidCondition = uuid ? "AND uuid = :uuid " : "";
@ -12,7 +12,7 @@ const _endpointsQuery = (_pagination) => ({ count }) => ({ uuid, route, method }
WHERE
e.created <= :now
AND
(e.created > :now OR e.deleted IS NULL)
(e.deleted > :now OR e.deleted IS NULL)
AND
true
${uuidCondition}

View File

@ -1,8 +1,7 @@
import { pagination } from '../../utils/pagination.js'
const _permissionsQuery = (_pagination) => ({count}) => ({uuid, name, action, endpoint}) => {
const _permissionsQuery = (_pagination) => ({count}) => ({uuid, action, endpoint}) => {
const uuidCondition = uuid ? 'AND uuid = :uuid ' : '';
const nameCondition = name ? 'AND name = :name ' : '';
const actionCondition = action ? 'AND action = :action ' : '';
const endpointCondition = endpoint ? 'AND fk_endpoint = (SELECT id from mydb.endpoints WHERE route = :endpoint)' : '';
return `
@ -15,12 +14,9 @@ const _permissionsQuery = (_pagination) => ({count}) => ({uuid, name, action, en
mydb.endpoints as e ON p.fk_endpoint = e.id
AND e.created <= :now
AND (e.deleted > :now OR e.deleted IS NULL)
WHERE
p.created <= :now
WHERE
p.created <= :now
AND
(p.created > :now OR p.deleted IS NULL)
(p.deleted > :now OR p.deleted IS NULL)
AND
true
${uuidCondition}
@ -58,14 +54,12 @@ const insertPermissionsQuery = () => {
`
}
const modifyPermissionsQuery = () => {
const nameCondition = name ? 'name = :name ' : '';
const modifyPermissionsQuery = (action, endpoint) => {
const actionCondition = action ? 'action = :action ' : '';
const endpointCondition = endpoint ? 'fk_endpoint = (SELECT id from mydb.endpoints WHERE route = :endpoint)' : '';
return `
UPDATE mydb.permissions
SET
${nameCondition}
${actionCondition}
${endpointCondition}
WHERE

View File

@ -20,7 +20,7 @@ const _roleSelectQuery = (_pagination = '') =>
WHERE
r.created <= :now
AND
(r.created > :now OR r.deleted IS NULL)
(r.deleted > :now OR r.deleted IS NULL)
AND
true
${uuidCondition}

View File

@ -12,7 +12,7 @@ const _rolesHasPermissionsQuery = (_pagination) => ({count}) => ({uuid, permissi
r2.uuid as role_uuid,
p.action as permission_action,
p.uuid as permission_uuid,
p.resource_type as permission_resource_type`}
p.fk_endpoint as permission_endpoint`}
FROM
mydb.roles_has_permissions as r
JOIN

View File

@ -9,7 +9,7 @@ import { pagination } from "../../utils/pagination.js";
*/
const _userListSelectQuery = (_pagination = '') =>
({ count }) =>
({ uuid, username, loginUsername, uuidList, email, role }) => {
({ uuid, username, loginUsername, uuidList, email, role, roleName }) => {
const uuidCondition = uuid ? 'AND users.uuid = :uuid ' : '';
//for uuidList, we use IN clause to check if the uuid is in the list of uuids passed
const uuidListCondition = uuidList ? 'AND users.uuid in(:uuidList)' : ''
@ -17,6 +17,7 @@ const _userListSelectQuery = (_pagination = '') =>
const usernameCondition = username ? `AND users.username LIKE CONCAT('%',:username,'%')` : '';
const emailCondition = email ? 'AND users.email = :email ' : '';
const roleCondition = role ? 'AND users.fk_role = :role ' : '';
const roleNameCondition = roleName ? 'AND users.fk_role = (SELECT id FROM mydb.roles WHERE name = :roleName)' : '';
return `
SELECT
${count || `users.*, r.name AS role`}
@ -26,7 +27,7 @@ const _userListSelectQuery = (_pagination = '') =>
WHERE
users.created <= :now
AND
(users.created > :now OR users.deleted IS NULL)
(users.deleted > :now OR users.deleted IS NULL)
AND
true
${uuidCondition}
@ -35,6 +36,7 @@ const _userListSelectQuery = (_pagination = '') =>
${emailCondition}
${roleCondition}
${loginUsernameCondition}
${roleNameCondition}
${_pagination}
`;
};
@ -88,12 +90,12 @@ const insertUserQuery = () => {
* @returns {String} UPDATE query
*/
const modifyUserQuery = ({ username, email, role }) => {
const usernameCondition = username ? 'username = :username, ' : '';
const passwordCondition = password ? 'password = :password, ' : '';
const emailCondition = email ? 'email = :email, ' : '';
const roleCondition = role ? 'fk_role = (SELECT id FROM mydb.roles WHERE name = :role) ' : '';
const lastLoginDateCondition = lastLoginDate ? 'last_login_date = :lastLoginDate, ' : '';
const userStatusCondition = userStatus ? 'user_status = :userStatus, ' : '';
const usernameCondition = username ? ':username, ' : '';
const passwordCondition = password ? ':password, ' : '';
const emailCondition = email ? ':email, ' : '';
const roleCondition = role ? '(SELECT id FROM mydb.roles WHERE name = :role) ' : '';
const lastLoginDateCondition = lastLoginDate ? ':lastLoginDate, ' : '';
const userStatusCondition = userStatus ? ':userStatus, ' : '';
return `
UPDATE