LocationAPI/src/repositories/authorization/roles_has_permissionsReposi...

110 lines
3.6 KiB
JavaScript
Raw Normal View History

2025-04-01 18:34:37 +00:00
import { pagination } from '../../utils/pagination.js'
2025-04-07 21:29:09 +00:00
const _rolesHasPermissionsQuery = (_pagination = '') => ({count}) => ({uuid, fk_permission, fk_role, roleName}) => {
const uuidCondition = uuid ? 'AND r.uuid = :uuid' : '';
2025-04-07 22:10:37 +00:00
const roleNameCondition = roleName ? 'AND fk_role = (SELECT id from acloc.roles WHERE name = :roleName)' : '';
const roleUuidCondition = fk_role ? 'AND fk_role = (SELECT id from acloc.roles WHERE uuid = :fk_role)' : '';
const permissionsUuidCondition = fk_permission ? 'AND fk_permission = (SELECT id from acloc.permissions WHERE uuid = :fk_permission)' : '';
2025-04-01 18:34:37 +00:00
return `
SELECT
${count ||
`r.*,
r2.name as role_name,
r2.uuid as role_uuid,
p.action as permission_action,
p.uuid as permission_uuid,
p.fk_endpoint as permission_endpoint`}
2025-04-01 18:34:37 +00:00
FROM
2025-04-07 22:10:37 +00:00
acloc.roles_has_permissions as r
2025-04-01 18:34:37 +00:00
JOIN
2025-04-07 22:10:37 +00:00
acloc.roles as r2 ON r.fk_role = r2.id
2025-04-01 18:34:37 +00:00
AND r2.created <= :now
AND (r2.deleted > :now OR r2.deleted IS NULL)
JOIN
2025-04-07 22:10:37 +00:00
acloc.permissions as p ON r.fk_permission = p.id
2025-04-01 18:34:37 +00:00
AND p.created <= :now
AND (p.deleted > :now OR p.deleted IS NULL)
WHERE
r.created <= :now
AND
(r.created > :now OR r.deleted IS NULL)
AND
true
${uuidCondition}
${permissionsUuidCondition}
${roleNameCondition}
2025-04-07 21:29:09 +00:00
${roleUuidCondition}
2025-04-01 18:34:37 +00:00
${_pagination}
`;
}
const getRolesHasPermissionsQuery = ({ limit, page, ...rest }) =>
_rolesHasPermissionsQuery(pagination({ limit, page }))({ count: false })(rest);
const countRolesHasPermissionsQuery = rest =>
_rolesHasPermissionsQuery()({ count: 'COUNT(*) AS count' })(rest);
const insertRolesHasPermissionsQuery = () => {
return `
2025-04-07 22:10:37 +00:00
INSERT INTO acloc.roles_has_permissions (
2025-04-01 18:34:37 +00:00
uuid,
fk_role,
fk_permission,
created,
createdBy
)
VALUES (
:uuid,
2025-04-07 22:10:37 +00:00
(SELECT id FROM acloc.roles WHERE uuid = :fk_role),
(SELECT id FROM acloc.permissions WHERE uuid = :fk_permission),
2025-04-01 18:34:37 +00:00
:now,
:createdBy
);
2025-04-07 22:10:37 +00:00
SELECT acloc.permissions.*,
2025-04-01 18:34:37 +00:00
permissions.uuid as permission_uuid,
roles.uuid as role_uuid,
roles.name as role_name
2025-04-07 22:10:37 +00:00
FROM acloc.roles_has_permissions as rp
LEFT JOIN acloc.permissions as permissions ON rp.fk_permission = permissions.id
LEFT JOIN acloc.roles as roles ON rp.fk_role = roles.id
2025-04-01 18:34:37 +00:00
WHERE rp.uuid = :uuid;
`
}
2025-04-06 16:36:19 +00:00
const modifyRolesHasPermissionsQuery = (roleUuid, permissionUuid) => {
2025-04-07 22:10:37 +00:00
const roleUuidCondition = roleUuid ? 'fk_role = (SELECT id from acloc.roles WHERE uuid = :fk_role),' : '';
const permissionUuidCondition = permissionUuid ? 'fk_permission = (SELECT id from acloc.permissions WHERE uuid = :fk_permission),' : '';
2025-04-06 16:36:19 +00:00
return `
UPDATE
2025-04-07 22:10:37 +00:00
acloc.roles_has_permissions as roles_has_permissions
2025-04-06 16:36:19 +00:00
SET
${roleUuidCondition}
${permissionUuidCondition}
uuid = :uuid
WHERE
roles_has_permissions.uuid = :uuid
AND
roles_has_permissions.deleted IS NULL;
2025-04-07 22:10:37 +00:00
SELECT * FROM acloc.roles_has_permissions WHERE uuid = :uuid;
2025-04-06 16:36:19 +00:00
`
}
2025-04-01 18:34:37 +00:00
const softDeleteRolesHasPermissionsQuery = () => {
2025-04-06 16:36:19 +00:00
return `
2025-04-01 18:34:37 +00:00
UPDATE
2025-04-07 22:10:37 +00:00
acloc.roles_has_permissions as roles_has_permissions
2025-04-01 18:34:37 +00:00
SET
2025-04-07 21:29:09 +00:00
deleted = :deleted, deletedBy = :deletedBy
2025-04-01 18:34:37 +00:00
WHERE
2025-04-07 21:29:09 +00:00
roles_has_permissions.uuid = :uuid
AND
2025-04-01 18:34:37 +00:00
roles_has_permissions.deleted IS NULL
2025-04-06 16:36:19 +00:00
`
2025-04-01 18:34:37 +00:00
}
export{
getRolesHasPermissionsQuery,
insertRolesHasPermissionsQuery,
2025-04-06 16:36:19 +00:00
countRolesHasPermissionsQuery,
softDeleteRolesHasPermissionsQuery,
modifyRolesHasPermissionsQuery
2025-04-01 18:34:37 +00:00
}