Skip to content

Commit ae49307

Browse files
committed
Merge branch '12.1'
2 parents 6e25013 + 34c6600 commit ae49307

File tree

3 files changed

+106
-15
lines changed

3 files changed

+106
-15
lines changed

rwb/src/main/resources/custom_query_insert.sql

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ INSERT INTO public.custom_query (id, uuid, name, query, organisation_id, is_void
22
last_modified_by_id, created_date_time, last_modified_date_time)
33
VALUES (DEFAULT, '69f9f68d-7870-4ea4-b69f-49f68da0c17a', 'Inactive users', 'WITH primary_users as (
44
select distinct u.id user_id, u.name first_name, catchment_id
5-
from users u
5+
from public.users u
66
join user_group ug on u.id = ug.user_id and ug.is_voided = false
77
join groups g on g.id = ug.group_id and g.is_voided = false
88
where g.name = ''Primary Users''
9-
and u.is_voided = false
9+
and u.disabled_in_cognito = false
10+
and u.last_activated_date_time is null OR u.last_activated_date_time < now() - INTERVAL ''3 DAYS''
11+
and u.is_voided = false
1012
),
1113
work_orders as (
1214
select i.id wo_id, i.address_id, organisation_id
@@ -28,19 +30,19 @@ VALUES (DEFAULT, '69f9f68d-7870-4ea4-b69f-49f68da0c17a', 'Inactive users', 'WITH
2830
and organisation_id = (select id from organisation where db_user = :org_db_user)
2931
and not encounter_type.is_voided)
3032
and (e.is_voided is null or e.is_voided = false)
31-
and wo.organisation_id = :org_id
33+
and wo.organisation_id = (select id from organisation where db_user = :org_db_user)
3234
group by 1, 2
3335
having count(e.id) = 1),
3436
catchments_without_work_orders_or_atleast_one_open_work_order as (
3537
select c.id
3638
from catchment c
3739
join virtual_catchment_address_mapping_table cam on cam.catchment_id = c.id
3840
left join work_orders wo on wo.address_id = cam.addresslevel_id
39-
left join closed_work_orders cwo on cwo.address_id = cam.addresslevel_id
41+
left join closed_work_orders cwo on cwo.wo_id = wo.wo_id
4042
where c.is_voided = false
4143
group by 1
4244
having count(wo.wo_id) = null
43-
OR count(wo.wo_id) > count(cwo.wo_id)
45+
OR count( wo.wo_id) > count( cwo.wo_id)
4446
),
4547
active_user_ids as (select (case
4648
when ind.created_date_time > TO_TIMESTAMP(:cutOffDate, ''YYYY-MM-DDTHH24:MI:ss.MS'')
@@ -60,10 +62,10 @@ select distinct user_id, first_name
6062
from primary_users pu
6163
join catchments_without_work_orders_or_atleast_one_open_work_order cat on pu.catchment_id = cat.id
6264
where user_id not in (select cuid
63-
from active_user_ids
65+
from active_user_ids where cuid is not null
6466
union
6567
select muid
66-
from active_user_ids);',
68+
from active_user_ids where muid is not null);',
6769
:org_id,
6870
false,
6971
0,

rwb/src/main/resources/select_inactive_users_id.sql

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
WITH primary_users as (
22
select distinct u.id user_id, u.name first_name, catchment_id
3-
from users u
3+
from public.users u
44
join user_group ug on u.id = ug.user_id and ug.is_voided = false
55
join groups g on g.id = ug.group_id and g.is_voided = false
66
where g.name = 'Primary Users'
7-
and u.is_voided = false
7+
and u.disabled_in_cognito = false
8+
and u.last_activated_date_time is null OR u.last_activated_date_time < now() - INTERVAL '3 DAYS'
9+
and u.is_voided = false
810
),
911
work_orders as (
1012
select i.id wo_id, i.address_id, organisation_id
@@ -20,10 +22,10 @@ WITH primary_users as (
2022
from work_orders wo
2123
join encounter e on wo.wo_id = e.individual_id
2224
where e.encounter_type_id =
23-
(select id
24-
from encounter_type
25-
where name = 'Work order endline'
26-
and organisation_id = (select id from organisation where db_user = :org_db_user)
25+
(select id
26+
from encounter_type
27+
where name = 'Work order endline'
28+
and organisation_id = (select id from organisation where db_user = :org_db_user)
2729
and not encounter_type.is_voided)
2830
and (e.is_voided is null or e.is_voided = false)
2931
and wo.organisation_id = (select id from organisation where db_user = :org_db_user)
@@ -58,7 +60,7 @@ select distinct user_id, first_name
5860
from primary_users pu
5961
join catchments_without_work_orders_or_atleast_one_open_work_order cat on pu.catchment_id = cat.id
6062
where user_id not in (select cuid
61-
from active_user_ids
63+
from active_user_ids where cuid is not null
6264
union
6365
select muid
64-
from active_user_ids);
66+
from active_user_ids where muid is not null);
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# RWB Inactive Users Custom Query Business Logic
2+
================================================
3+
4+
This document summarizes the business logic behind **"RWB Inactive Users"** PostgreSQL query that identifies specific users based on their group membership, catchment areas, and work order status.
5+
6+
## Overview
7+
8+
------------
9+
10+
The query involves several steps to filter users who meet certain criteria related to their group membership, catchment areas, and recent activity.
11+
12+
## Steps Involved
13+
14+
------------------
15+
16+
### 1. **Primary Users Identification**
17+
18+
--------------------------------------
19+
20+
- **Purpose**: Identify users who belong to the 'Primary Users' group and are not disabled in Cognito.
21+
- **Conditions**:
22+
- Users must be part of the 'Primary Users' group.
23+
- Users must not be disabled in Cognito (`disabled_in_cognito = false`).
24+
- Users must either have no `last_activated_date_time` or it must be older than 3 days (`last_activated_date_time is null OR last_activated_date_time < now() - INTERVAL '3 DAYS'`).
25+
- Users must not be voided (`is_voided = false`).
26+
27+
28+
### 2. **Work Orders Identification**
29+
30+
--------------------------------------
31+
32+
- **Purpose**: Identify work orders that are not voided and belong to a specific organization.
33+
- **Conditions**:
34+
- Work orders must not be voided (`is_voided = false`).
35+
- Work orders must be of type 'Work Order' for a specific organization (`subject_type_id` matches the 'Work Order' type for the organization identified by `:org_db_user`).
36+
37+
38+
### 3. **Closed Work Orders Identification**
39+
40+
--------------------------------------------
41+
42+
- **Purpose**: Identify work orders that have been closed.
43+
- **Conditions**:
44+
- Work orders must have an encounter of type 'Work order endline' for the same organization.
45+
- The encounter must not be voided (`is_voided is null or is_voided = false`).
46+
- There must be exactly one such encounter per work order (`having count(e.id) = 1`).
47+
48+
49+
### 4. **Catchments Without Work Orders or At Least One Open Work Order**
50+
51+
-------------------------------------------------------------------
52+
53+
- **Purpose**: Identify catchment areas that either have no work orders or have more open work orders than closed ones.
54+
- **Conditions**:
55+
- Catchments must not be voided (`is_voided = false`).
56+
- Either there are no work orders (`count(wo.wo_id) = null`), or there are more work orders than closed work orders (`count(wo.wo_id) > count(cwo.wo_id)`).
57+
58+
59+
### 5. **Active User IDs**
60+
61+
-------------------------
62+
63+
- **Purpose**: Identify user IDs that have been active recently (created or modified entities after a certain cutoff date).
64+
- **Conditions**:
65+
- For individuals and encounters, if the creation or modification date is after the specified cutoff date, the created_by_id or last_modified_by_id is considered an active user ID.
66+
67+
68+
### 6. **Final Selection**
69+
70+
-------------------------
71+
72+
- **Purpose**: Select primary users who are in catchments without work orders or at least one open work order and have not been active recently.
73+
- **Conditions**:
74+
- Users must be primary users in catchments identified in step 4.
75+
- Users must not have been active recently (their IDs must not be in the list of active user IDs).
76+
77+
78+
## Summary
79+
80+
----------
81+
82+
This query identifies primary users in catchments without work order or atleast one open wor-order, who have not been recently active in the system. It ensures that these users meet all the specified criteria related to their group membership, catchment areas, and recent activity.
83+
84+
## IMPORTANT Additional Details
85+
86+
- The CutOffDate for determining in-activity is 3 Days from current date.
87+
- Also, if the user has been nudged in the last 3 days, we do not nudge him again.

0 commit comments

Comments
 (0)