mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-22 18:24:23 +00:00
Refactor removeUserNotRelatedToJob
logic to improve SQL clarity and handle edge cases
We remove the user from the group if the user is deactivated, or of the user does not have any job_history line currently, or if the userjob_id is null in the job_history.
This commit is contained in:
parent
51168ac3c4
commit
81b6ae193c
@ -77,24 +77,38 @@ final readonly class UserGroupRelatedToUserJobSync implements UserGroupRelatedTo
|
|||||||
return $counter;
|
return $counter;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function removeUserNotRelatedToJob(Connection $connection): int
|
private function removeUserNotRelatedToJob(Connection $connection): int
|
||||||
{
|
{
|
||||||
$sql = <<<'SQL'
|
$sql = <<<'SQL'
|
||||||
DELETE FROM chill_main_user_group_user
|
DELETE FROM chill_main_user_group_user
|
||||||
USING users AS u, chill_main_user_group ug, chill_main_user_job_history jh
|
USING users AS u, chill_main_user_group ug
|
||||||
WHERE
|
WHERE
|
||||||
chill_main_user_group_user.usergroup_id = ug.id
|
chill_main_user_group_user.usergroup_id = ug.id
|
||||||
AND chill_main_user_group_user.user_id = u.id
|
AND chill_main_user_group_user.user_id = u.id
|
||||||
AND jh.user_id = u.id AND tsrange(jh.startdate, jh.enddate) @> localtimestamp
|
-- only where user_group.userjob_id is set (we ignore groups not automatically created)
|
||||||
-- only when the user's jobid is different than the user_group id
|
AND ug.userjob_id IS NOT NULL
|
||||||
-- or where the user.enabled is null
|
AND (
|
||||||
AND ((ug.userjob_id IS NOT NULL AND jh.job_id <> ug.userjob_id) OR u.enabled IS NULL)
|
-- Case 1: User has no job history records matching the time period
|
||||||
SQL;
|
NOT EXISTS (
|
||||||
|
SELECT 1 FROM chill_main_user_job_history jh
|
||||||
|
WHERE jh.user_id = u.id
|
||||||
|
AND tsrange(jh.startdate, jh.enddate) @> localtimestamp
|
||||||
|
)
|
||||||
|
OR
|
||||||
|
-- Case 2: User has job history but with different job_id or user is disabled
|
||||||
|
EXISTS (
|
||||||
|
SELECT 1 FROM chill_main_user_job_history jh
|
||||||
|
WHERE jh.user_id = u.id
|
||||||
|
AND tsrange(jh.startdate, jh.enddate) @> localtimestamp
|
||||||
|
AND (jh.job_id <> ug.userjob_id OR u.enabled IS FALSE OR jh.job_id IS NULL)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
SQL;
|
||||||
|
|
||||||
$result = $connection->executeQuery($sql);
|
$result = $connection->executeQuery($sql);
|
||||||
|
|
||||||
return $result->rowCount();
|
return $result->rowCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createNewAssociations(Connection $connection): int
|
private function createNewAssociations(Connection $connection): int
|
||||||
{
|
{
|
||||||
@ -102,8 +116,8 @@ final readonly class UserGroupRelatedToUserJobSync implements UserGroupRelatedTo
|
|||||||
INSERT INTO chill_main_user_group_user (usergroup_id, user_id)
|
INSERT INTO chill_main_user_group_user (usergroup_id, user_id)
|
||||||
SELECT cmug.id, jh.user_id
|
SELECT cmug.id, jh.user_id
|
||||||
FROM chill_main_user_group cmug
|
FROM chill_main_user_group cmug
|
||||||
JOIN chill_main_user_job_history jh ON jh.job_id = cmug.userjob_id AND tsrange(jh.startdate, jh.enddate) @> localtimestamp
|
JOIN chill_main_user_job_history jh ON jh.job_id = cmug.userjob_id AND tsrange(jh.startdate, jh.enddate) @> localtimestamp
|
||||||
JOIN users u ON u.id = jh.user_id
|
JOIN users u ON u.id = jh.user_id
|
||||||
WHERE cmug.userjob_id IS NOT NULL AND u.enabled IS TRUE
|
WHERE cmug.userjob_id IS NOT NULL AND u.enabled IS TRUE
|
||||||
ON CONFLICT DO NOTHING
|
ON CONFLICT DO NOTHING
|
||||||
SQL;
|
SQL;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user