Class: Kidspire::PcoCreatePersonJob
- Inherits:
-
ApplicationJob
- Object
- ActiveJob::Base
- ApplicationJob
- Kidspire::PcoCreatePersonJob
- Defined in:
- app/jobs/kidspire/pco_create_person_job.rb
Overview
Creates a brand-new person (and their children) in Planning Center when a family is quick-added in kidspire and doesn’t already have a PCO ID.
Instance Method Summary collapse
Instance Method Details
#perform(family_id) ⇒ Object
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'app/jobs/kidspire/pco_create_person_job.rb', line 5 def perform(family_id) family = Family.find(family_id) return if family.pco_person_id.present? # already in PCO return unless family.pco_sync_enabled? return unless ChurchIntegration.current.access_token.present? client = PcoClient.new # ── 1. Create the primary contact in PCO ──────────────────────────── person_resp = client.post("/people/v2/people", { data: { type: "Person", attributes: { first_name: family.primary_contact_first_name, last_name: family.primary_contact_last_name, } } }) pco_person_id = person_resp.dig("data", "id") raise "PCO did not return a person id" unless pco_person_id # ── 2. Add email ───────────────────────────────────────────────────── if family.email.present? client.post("/people/v2/people/#{pco_person_id}/emails", { data: { type: "Email", attributes: { address: family.email, primary: true } } }) end # ── 3. Add phone ───────────────────────────────────────────────────── if family.phone.present? client.post("/people/v2/people/#{pco_person_id}/phone_numbers", { data: { type: "PhoneNumber", attributes: { number: family.phone, primary: true, location: "Mobile" } } }) end # ── 4. Apply kidspire tag ───────────────────────────────────────────── tag_name = SyncSetting.current.effective_ministry_tag if tag_name.present? = client.get_all("/people/v2/tags", "where[name]" => tag_name) tag = .find { |t| t.dig("attributes", "name") == tag_name } if tag client.post("/people/v2/tags/#{tag["id"]}/relationships/people", { "data" => [{ "type" => "Person", "id" => pco_person_id }] }) end end # ── 5. Create children ─────────────────────────────────────────────── family.children.each do |child| child_resp = client.post("/people/v2/people", { data: { type: "Person", attributes: { first_name: child.first_name, last_name: child.last_name, child: true, grade: child.grade, birthdate: child.birthdate&.iso8601, medical_notes: child.notes, }.compact } }) child_pco_id = child_resp.dig("data", "id") child.update_column(:pco_person_id, child_pco_id) if child_pco_id end # ── 6. Store PCO IDs on the family ─────────────────────────────────── family.update_columns( pco_person_id: pco_person_id, pco_last_synced_at: Time.current ) Rails.logger.info("[Kidspire] PcoCreatePersonJob: created PCO person #{pco_person_id} for family #{family_id}") rescue ActiveRecord::RecordNotFound Rails.logger.warn("[Kidspire] PcoCreatePersonJob: family #{family_id} not found") rescue PcoError => e Rails.logger.error("[Kidspire] PcoCreatePersonJob failed for family #{family_id}: #{e.}") raise end |