@@ -13,56 +13,47 @@ def get_updated_contact_data():
1313 qry = """ -- Collect latest foster/volunteer dates
1414 select json_agg (upd) as "cd"
1515 from (
16- select
17- sf .source_id as "Id" , -- long salesforce string
18- array_agg(sl.source_id) filter (where sl.source_id is not null) as "Person_Id__c", -- short PAWS-local shelterluv id
16+ select
17+ salesforce .source_id as "contactId",
18+ shelterluv.person_ids as "personIds",
1919 case
20- when
21- (extract(epoch from now())::bigint - max(foster_out) < 365*86400) -- foster out in last year
22- or (extract(epoch from now())::bigint - max(foster_return) < 365*86400) -- foster return
23- then 'Active'
24- else 'Inactive'
25- end as "Foster_Activity__c",
26- max(foster_out) as "Foster_Start_Date__c",
27- max(foster_return) as "Foster_End_Date__c",
28- min(vol.first_date) "First_volunteer_date__c",
29- max(vol.last_date) "Last_volunteer_date__c",
30- sum(vol.hours) as "Total_volunteer_hours__c",
31- array_agg(vc.source_id::integer) filter(where vc.source_id is not null) as "Volgistics_Id__c"
20+ when volgistics.last_shift_date > now() - interval '1 year' then 'Active' else 'InActive'
21+ end as "volunteerStatus",
22+ shelterluv.foster_start as "fosterStartDate",
23+ null as "fosterEndDate",
24+ shelterluv.latest_foster_event as "latestFosterEvent",
25+ volgistics.first_volunteer_date as "firstVolunteerDate",
26+ volgistics.last_shift_date as "lastShiftDate",
27+ volgistics.total_hours as "totalVolunteerHours",
28+ volgistics.volg_ids as "volgisticIds"
3229 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'
30+ select * from pdp_contacts pc where source_type = 'salesforcecontacts'
31+ ) salesforce
3732 left join (
38- select
39- person_id,
40- max(case when event_type=1 then time else null end) * 1000 adopt,
41- max(case when event_type=2 then time else null end) * 1000 foster_out,
42- -- max(case when event_type=3 then time else null end) rto,
43- max(case when event_type=5 then time else null end) * 1000 foster_return
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'
33+ select matching_id, array_agg(distinct v."number"::int) volg_ids, sum(hours) total_hours,
34+ min(from_date) first_volunteer_date, max(from_date) last_shift_date
35+ from volgistics v
36+ left join volgisticsshifts v2 on v2.volg_id::varchar = v.number
37+ inner join pdp_contacts pc on pc.source_id = v.number::varchar and pc.source_type = 'volgistics'
38+ group by matching_id
39+ ) volgistics on volgistics.matching_id = salesforce.matching_id
4840 left join (
4941 select
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- group by sf.source_id
42+ matching_id, array_agg(distinct p.internal_id) as person_ids ,
43+ min(case when event_type in (2,5) then to_timestamp(time) else null end) foster_start ,
44+ max(case when event_type in (2,5) then to_timestamp(time) else null end) latest_foster_event
45+ from shelterluvpeople p
46+ left join sl_animal_events sae on sae.person_id::varchar = p.internal_id
47+ inner join pdp_contacts pc on pc.source_id = p.internal_id
48+ group by matching_id
49+ ) shelterluv on shelterluv .matching_id = salesforce.matching_id
50+ where volgistics.matching_id is not null or shelterluv.matching_id is not null
5951 ) upd;
6052 """
6153
6254 with Session () as session :
6355 result = session .execute (qry )
6456 sfdata = result .fetchone ()[0 ]
6557 if sfdata :
66- logger .debug (sfdata )
6758 logger .debug ("Query for Salesforce update returned %d records" , len (sfdata ))
6859 return sfdata
0 commit comments