Skip to content

Commit a713c3c

Browse files
committed
avniproject/avni-server#825 | Update rwb nudge custom query
1 parent 9bd4e8c commit a713c3c

File tree

2 files changed

+113
-17
lines changed

2 files changed

+113
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
INSERT INTO public.custom_query (id, uuid, name, query, organisation_id, is_voided, version, created_by_id,
2+
last_modified_by_id, created_date_time, last_modified_date_time)
3+
VALUES (DEFAULT, '69f9f68d-7870-4ea4-b69f-49f68da0c17a', 'Inactive users', 'WITH primary_users as (
4+
select distinct u.id user_id, u.name first_name, catchment_id
5+
from users u
6+
join user_group ug on u.id = ug.user_id and ug.is_voided = false
7+
join groups g on g.id = ug.group_id and g.is_voided = false
8+
where g.name = ''Primary Users''
9+
and u.is_voided = false
10+
),
11+
work_orders as (
12+
select i.id wo_id, i.address_id, organisation_id
13+
from individual i
14+
where i.is_voided = false
15+
and i.subject_type_id = (select id
16+
from subject_type
17+
where name = ''Work Order''
18+
and organisation_id = (select id from organisation where db_user = :org_db_user)
19+
and not subject_type.is_voided)
20+
),
21+
closed_work_orders as (select wo.wo_id, wo.address_id
22+
from work_orders wo
23+
join encounter e on wo.wo_id = e.individual_id
24+
where e.encounter_type_id =
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)
29+
and not encounter_type.is_voided)
30+
and (e.is_voided is null or e.is_voided = false)
31+
and wo.organisation_id = :org_id
32+
group by 1, 2
33+
having count(e.id) = 1),
34+
catchments_without_work_orders_or_atleast_one_open_work_order as (
35+
select c.id
36+
from catchment c
37+
join virtual_catchment_address_mapping_table cam on cam.catchment_id = c.id
38+
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
40+
where c.is_voided = false
41+
group by 1
42+
having count(wo.wo_id) = null
43+
OR count(wo.wo_id) > count(cwo.wo_id)
44+
),
45+
active_user_ids as (select (case
46+
when ind.created_date_time > TO_TIMESTAMP(:cutOffDate, ''YYYY-MM-DDTHH24:MI:ss.MS'')
47+
then ind.created_by_id end) as cuid,
48+
ind.last_modified_by_id as muid
49+
from individual ind
50+
where ind.last_modified_date_time > TO_TIMESTAMP(:cutOffDate, ''YYYY-MM-DDTHH24:MI:ss.MS'')
51+
UNION
52+
select (case
53+
when enc.created_date_time > TO_TIMESTAMP(:cutOffDate, ''YYYY-MM-DDTHH24:MI:ss.MS'')
54+
then enc.created_by_id end) as cuid,
55+
enc.last_modified_by_id as muid
56+
from encounter enc
57+
where enc.last_modified_date_time > TO_TIMESTAMP(:cutOffDate, ''YYYY-MM-DDTHH24:MI:ss.MS'')
58+
order by 1 asc)
59+
select distinct user_id, first_name
60+
from primary_users pu
61+
join catchments_without_work_orders_or_atleast_one_open_work_order cat on pu.catchment_id = cat.id
62+
where user_id not in (select cuid
63+
from active_user_ids
64+
union
65+
select muid
66+
from active_user_ids);',
67+
:org_id,
68+
false,
69+
0,
70+
1,
71+
1,
72+
now(),
73+
now());

rwb/src/main/resources/select_inactive_users_id.sql

+40-17
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,45 @@
1-
WITH impacted_users as (
2-
WITH open_work_orders as (select i.id open_wo_id, i.address_id
3-
from individual i
4-
left join encounter e on i.id = e.individual_id
5-
where i.is_voided = false
6-
and i.subject_type_id = (select id from subject_type where name = 'Work Order' and organisation_id = (select id from organisation where db_user = 'rwbniti') and not subject_type.is_voided)
7-
and e.encounter_type_id =
8-
(select id from encounter_type where name = 'Work order endline' and organisation_id = (select id from organisation where db_user = 'rwbniti') and not encounter_type.is_voided)
9-
and (e.is_voided is null or e.is_voided = false)
10-
and i.organisation_id = (select id from organisation where db_user = 'rwbniti')
11-
group by 1
12-
having count(e.id) = 1) -- Use = 1 for testing, < 1 for prod
13-
select distinct u.id user_id, u.username first_name
14-
from open_work_orders owo
15-
join virtual_catchment_address_mapping_table cam on cam.addresslevel_id = owo.address_id
16-
join users u on u.catchment_id = cam.catchment_id and u.is_voided = false
1+
WITH primary_users as (
2+
select distinct u.id user_id, u.name first_name, catchment_id
3+
from users u
174
join user_group ug on u.id = ug.user_id and ug.is_voided = false
185
join groups g on g.id = ug.group_id and g.is_voided = false
196
where g.name = 'Primary Users'
7+
and u.is_voided = false
208
),
9+
work_orders as (
10+
select i.id wo_id, i.address_id, organisation_id
11+
from individual i
12+
where i.is_voided = false
13+
and i.subject_type_id = (select id
14+
from subject_type
15+
where name = 'Work Order'
16+
and organisation_id = (select id from organisation where db_user = :org_db_user)
17+
and not subject_type.is_voided)
18+
),
19+
closed_work_orders as (select wo.wo_id, wo.address_id
20+
from work_orders wo
21+
join encounter e on wo.wo_id = e.individual_id
22+
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)
27+
and not encounter_type.is_voided)
28+
and (e.is_voided is null or e.is_voided = false)
29+
and wo.organisation_id = :org_id
30+
group by 1, 2
31+
having count(e.id) = 1),
32+
catchments_without_work_orders_or_atleast_one_open_work_order as (
33+
select c.id
34+
from catchment c
35+
join virtual_catchment_address_mapping_table cam on cam.catchment_id = c.id
36+
left join work_orders wo on wo.address_id = cam.addresslevel_id
37+
left join closed_work_orders cwo on cwo.address_id = cam.addresslevel_id
38+
where c.is_voided = false
39+
group by 1
40+
having count(wo.wo_id) = null
41+
OR count(wo.wo_id) > count(cwo.wo_id)
42+
),
2143
active_user_ids as (select (case
2244
when ind.created_date_time > TO_TIMESTAMP(:cutOffDate, 'YYYY-MM-DDTHH24:MI:ss.MS')
2345
then ind.created_by_id end) as cuid,
@@ -33,7 +55,8 @@ WITH impacted_users as (
3355
where enc.last_modified_date_time > TO_TIMESTAMP(:cutOffDate, 'YYYY-MM-DDTHH24:MI:ss.MS')
3456
order by 1 asc)
3557
select distinct user_id, first_name
36-
from impacted_users iu
58+
from primary_users pu
59+
join catchments_without_work_orders_or_atleast_one_open_work_order cat on pu.catchment_id = cat.id
3760
where user_id not in (select cuid
3861
from active_user_ids
3962
union

0 commit comments

Comments
 (0)