@@ -11,64 +11,51 @@ def get_updated_contact_data():
11
11
Session = sessionmaker (engine )
12
12
13
13
qry = """ -- Collect latest foster/volunteer dates
14
- with ev_dates as
15
- (select
16
- person_id,
14
+ select json_agg (upd) as "cd"
15
+ from (
16
+ select
17
+ sf.source_id as "Id" , -- long salesforce string
18
+ sle.person_id as "Person_Id__c", -- short PAWS-local shelterluv id
19
+ case
20
+ when
21
+ (extract(epoch from now())::bigint - foster_out < 365*86400) -- foster out in last year
22
+ or (extract(epoch from now())::bigint - foster_return < 365*86400) -- foster return
23
+ then 'Active'
24
+ else 'Inactive'
25
+ end as "Foster_Activity__c",
26
+ foster_out as "Foster_Start_Date__c",
27
+ foster_return as "Foster_End_Date__c",
28
+ vol.first_date "First_volunteer_date__c",
29
+ vol.last_date "Last_volunteer_date__c",
30
+ vol.hours as "Total_volunteer_hours__c",
31
+ vc.source_id::integer as "Volgistics_Id__c"
32
+ from (
33
+ select source_id, matching_id from pdp_contacts sf
34
+ where sf.source_type = 'salesforcecontacts'
35
+ ) sf
36
+ left join pdp_contacts sl on sl.matching_id = sf.matching_id and sl.source_type = 'shelterluvpeople'
37
+ left join (
38
+ select
39
+ person_id,
17
40
max(case when event_type=1 then time else null end) * 1000 adopt,
18
41
max(case when event_type=2 then time else null end) * 1000 foster_out,
19
- -- max(case when event_type=3 then time else null end) rto,
42
+ -- max(case when event_type=3 then time else null end) rto,
20
43
max(case when event_type=5 then time else null end) * 1000 foster_return
21
-
22
- from
23
- sl_animal_events sla
24
- left join sl_event_types sle on sle.id = sla.event_type
25
-
26
- where sle.id in (1,2,5)
27
- group by person_id
28
- order by person_id
29
- )
30
-
31
-
32
- select json_agg (upd) as "cd" from (
44
+ from sl_animal_events
45
+ group by person_id
46
+ ) sle on sle.person_id::text = sl.source_id
47
+ left join pdp_contacts vc on vc.matching_id = sf.matching_id and vc.source_type = 'volgistics'
48
+ left join (
33
49
select
34
- slsf.source_id as "Contact_Record_Id__c" , -- long salesforce string
35
- slp.internal_id as "Person_Id__c" , -- short PAWS-local shelterluv id
36
-
37
- --case
38
- -- when
39
- -- (extract(epoch from now())::bigint - foster_out < 365*86400) -- foster out in last year
40
- -- or (extract(epoch from now())::bigint - foster_return < 365*86400) -- foster return
41
- -- then 'Active'
42
- -- else 'Inactive'
43
- --end as "Updated_Recent_Foster_Activity__c",
44
-
45
- foster_out as "Updated_Foster_Start_Date__c",
46
- foster_return as "Updated_Foster_End_Date__c",
47
-
48
- extract(epoch from min(vs.from_date)) * 1000 as "Updated_First_Volunteer_Date__c",
49
- extract(epoch from max(vs.from_date)) * 1000 as "Updated_Last_Volunteer_Date__c",
50
- sum(vs.hours) as "Updated_Total_Volunteer_Hours__c",
51
- vc.source_id::integer as "Volgistics_Id__c"
52
-
53
- from
54
- ev_dates
55
- left join pdp_contacts slc on slc.source_id = person_id::text and slc.source_type = 'shelterluvpeople'
56
- left join pdp_contacts slsf on slsf.matching_id = slc.matching_id and slsf.source_type = 'salesforcecontacts'
57
- left join shelterluvpeople slp on slp.internal_id = person_id::text
58
- left join pdp_contacts vc on vc.matching_id = slc.matching_id and vc.source_type = 'volgistics'
59
- left join volgisticsshifts vs on vs.volg_id::text = vc.source_id
60
-
61
- where
62
- slsf.source_id is not null
63
-
64
- group by
65
- slsf.source_id,
66
- slp.internal_id,
67
- vc.source_id,
68
- foster_out ,
69
- foster_return
70
-
71
- ) upd ;
50
+ volg_id,
51
+ sum(hours) as hours,
52
+ extract(epoch from min(from_date)) * 1000 as first_date,
53
+ extract(epoch from max(from_date)) * 1000 as last_date
54
+ from volgisticsshifts
55
+ group by volg_id
56
+ ) vol on vol.volg_id::text = vc.source_id
57
+ where sl.matching_id is not null or vc.matching_id is not null
58
+ ) upd;
72
59
"""
73
60
74
61
with Session () as session :
0 commit comments