Class: Kidsmin::Api::V1::Admin::FamiliesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/kidsmin/api/v1/admin/families_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /api/v1/admin/families Quick-creates a family + children + invitation, optionally sends SMS.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/controllers/kidsmin/api/v1/admin/families_controller.rb', line 87

def create
  if family_params[:address].blank?
    return render json: { error: "Home address is required", code: "validation_error" },
                  status: :unprocessable_entity
  end

  family = build_family
  family.save!

  build_children(family)
  build_guardians(family)

  invitation    = Invitation.create!(family: family)
  invite_method = send_invite(family, invitation)

  # Push to PCO in the background only if already connected
  pco_connected   = ChurchIntegration.current.access_token.present? &&
                    (ChurchIntegration.current.expires_at.nil? ||
                     ChurchIntegration.current.expires_at > Time.current)
  pco_sync_queued = pco_connected
  PcoCreatePersonJob.perform_later(family.id) if pco_sync_queued

  render json: {
    family:          FamilyBlueprint.render_as_hash(family, view: :with_children),
    invite_url:      invitation.invite_url,
    invite_method:   invite_method,   # "sms" | "email" | "none"
    sms_sent:        invite_method == "sms",
    pco_sync_queued: pco_sync_queued,
  }, status: :created

rescue ActiveRecord::RecordInvalid => e
  render json: { error: e.message, code: "validation_error" },
         status: :unprocessable_entity
end

#indexObject

GET /api/v1/admin/families



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
# File 'app/controllers/kidsmin/api/v1/admin/families_controller.rb', line 9

def index
  families = Family.includes(:children, :account)
                   .order(created_at: :desc)
                   .page(params[:page]).per(50)

  render json: {
    families: families.map { |f|
      {
        id:                         f.id,
        family_name:                f.family_name,
        primary_contact_first_name: f.primary_contact_first_name,
        primary_contact_last_name:  f.primary_contact_last_name,
        email:                      f.email,
        phone:                      f.phone,
        address:                    f.address,
        children_count:             f.children.size,
        account_linked:             f..present?,
        pco_synced:                 f.pco_last_synced_at.present?,
        created_at:                 f.created_at,
      }
    },
    meta: {
      total_count:  families.total_count,
      current_page: families.current_page,
      total_pages:  families.total_pages,
    },
  }
end

#inviteObject

POST /api/v1/admin/families/:id/invite Creates a fresh invitation (or returns the existing active one) and optionally re-sends via SMS.



73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/kidsmin/api/v1/admin/families_controller.rb', line 73

def invite
  family = Family.find(params[:id])

  # Expire any old invitations so we start clean
  Invitation.where(family: family, accepted_at: nil).update_all(expires_at: Time.current)

  invitation    = Invitation.create!(family: family)
  invite_method = send_invite(family, invitation)

  render json: { invite_url: invitation.invite_url, invite_method: invite_method }
end

#showObject

GET /api/v1/admin/families/:id



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
# File 'app/controllers/kidsmin/api/v1/admin/families_controller.rb', line 39

def show
  family = Family.includes(:children, :guardians, :account).find(params[:id])
  pending_invite = Invitation.where(family: family, accepted_at: nil)
                             .where("expires_at > ?", Time.current)
                             .order(created_at: :desc).first

  render json: {
    id:                         family.id,
    family_name:                family.family_name,
    primary_contact_first_name: family.primary_contact_first_name,
    primary_contact_last_name:  family.primary_contact_last_name,
    email:                      family.email,
    phone:                      family.phone,
    address:                    family.address,
    account_linked:             family..present?,
    pco_person_id:              family.pco_person_id,
    pco_household_id:           family.pco_household_id,
    pco_last_synced_at:         family.pco_last_synced_at,
    created_at:                 family.created_at,
    children:   family.children.map { |c|
      { id: c.id, first_name: c.first_name, last_name: c.last_name,
        grade_display: c.grade_display, age: c.age, notes: c.notes }
    },
    guardians:  family.guardians.map { |g|
      { id: g.id, first_name: g.first_name, last_name: g.last_name,
        phone: g.phone, email: g.email, relationship: g.relationship }
    },
    invite_url: pending_invite&.invite_url,
  }
end