3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'app/jobs/kidspire/pco_inbound_people_sync_job.rb', line 3
def perform
return unless SyncSetting.current.inbound_people_sync?
client = PcoClient.new
tagged_hh_ids = tagged_household_ids(client) if ministry_tag.present?
if tagged_hh_ids&.empty?
Rails.logger.warn("[Kidspire] PcoInboundPeopleSyncJob: tag '#{ministry_tag}' matched no people — sync aborted")
return
end
response = client.paginate(
"/people/v2/people",
include: "households,emails,phone_numbers,addresses"
)
people = response["data"]
included = response["included"]
if tagged_hh_ids
people = people.select { |p|
hh_id = p.dig("relationships", "households", "data", 0, "id")
tagged_hh_ids.include?(hh_id)
}
Rails.logger.info("[Kidspire] Tag filter active — #{people.size} people in #{tagged_hh_ids.size} tagged households")
end
households = index_by_id(included, "Household")
emails = group_by_person(included, "Email")
phones = group_by_person(included, "PhoneNumber")
addresses = group_by_person(included, "Address")
adults = people.reject { |p| p.dig("attributes", "child") }
children = people.select { |p| p.dig("attributes", "child") }
adults.each { |p| sync_family(p, households, emails, phones, addresses) }
children.each { |p| sync_child(p, households) }
SyncSetting.current.update!(last_synced_at: Time.current)
Rails.logger.info("[Kidspire] PcoInboundPeopleSyncJob complete — #{adults.size} adults, #{children.size} children")
rescue PcoError => e
Rails.logger.error("[Kidspire] PcoInboundPeopleSyncJob failed: #{e.message}")
raise
end
|