Skip to content

Commit

Permalink
Fix migration and add contact search capability
Browse files Browse the repository at this point in the history
Fix migration file numbers naming.

Add search cabaility to oye contacts GET http call.
Use it to search for members by phone number.
Update method namings and descriptions
  • Loading branch information
DeeTheDev committed Jul 29, 2024
1 parent 0aa5a00 commit 23ab676
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 24 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/suma/api/preferences.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def update_preferences(member)
subscr = member.preferences!.subscription(k)
invalid!("subscription #{k} is invalid") if subscr.nil?
subscr.set_from_opted_in(optin)
subscr.sync_oye_contact_marketing_preferences
subscr.update_oye_contact_marketing_preferences
end
member.preferences.save_changes
end
Expand Down
25 changes: 9 additions & 16 deletions lib/suma/member/oye_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,16 @@ def initialize(member)
@marketing_key = Suma::Oye.sms_marketing_preferences_key
end

# If contact ID is missing, add it to member. Always update oye contact
# sms preferences so that member sms preferences stay in sync.
def upsert_sms_status
# If contact ID is missing, attempt to find and add it to member.
# Only sync/update contact status when contact ID is set.
def update_contact_sms_status
return unless Suma::Oye.configured?
self._add_contact_id if self.contact_id.blank?
self._update_contact_status
end

def _add_contact_id
contacts = Suma::Oye.get_contacts
return unless (contact = contacts.find { |c| Suma::PhoneNumber::US.normalize(c.fetch("number")) === @member.phone })
@member.update(oye_contact_id: contact.fetch("id").to_s)
end

def _update_contact_status
marketing_subscr = self.marketing_subscription
oye_sms_status = Suma::Oye::STATUS_MATCH.invert.fetch(marketing_subscr[:opted_in])
if self.contact_id.blank?
contacts = Suma::Oye.get_contacts(Suma::PhoneNumber.format_e164(@member.phone))
return if contacts.empty?
@member.update(oye_contact_id: contacts.first.fetch("id").to_s)
end
oye_sms_status = Suma::Oye::STATUS_MATCH.invert.fetch(self.marketing_subscription[:opted_in])
Suma::Oye.bulk_update_contacts(contacts: [{id: self.contact_id, status: oye_sms_status}])
end

Expand Down
4 changes: 2 additions & 2 deletions lib/suma/message/preferences.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def set_from_opted_in(optin)
self.model.set(self.optout_field => !optin)
end

def sync_oye_contact_marketing_preferences
self.model.member.oye.upsert_sms_status if self.key === Suma::Oye.sms_marketing_preferences_key
def update_oye_contact_marketing_preferences
self.model.member.oye.update_contact_sms_status if self.key === Suma::Oye.sms_marketing_preferences_key
end
end

Expand Down
9 changes: 6 additions & 3 deletions lib/suma/oye.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ def self.api_headers
}
end

def self.get_contacts
# Query contacts by phone number, first name or last name
def self.get_contacts(search=nil)
query = {}
query = query.merge(search:) unless search.nil?
response = Suma::Http.get(
self.api_root + "/contacts", headers: self.api_headers, logger: self.logger,
self.api_root + "/contacts", query, headers: self.api_headers, logger: self.logger,
)
return response.parsed_response
end
Expand All @@ -55,7 +58,7 @@ def self.sync_contact_sms_preferences
contacts = self.get_contacts
contacts.each do |c|
member = Suma::Member[oye_contact_id: c.fetch("id")]
unless member
if member.nil?
phone = Suma::PhoneNumber::US.normalize(c.fetch("number"))
next unless Suma::PhoneNumber::US.valid_normalized?(phone)
next unless (member = Suma::Member[phone:])
Expand Down
17 changes: 17 additions & 0 deletions spec/data/oye/contacts_search_get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"id": "1",
"number": "+12223334444",
"first_name": "Hola",
"last_name": "Adios",
"created_at": "Mon, 03 Jun 2024 16:20:39.567013000 EDT -04:00",
"updated_at": "Mon, 03 Jun 2024 16:20:39.567013000 EDT -04:00",
"organization_id": "1",
"status": "inactive",
"state": null,
"aasm_state": null,
"state_metadata": null,
"language": "",
"eid": ""
}
]
6 changes: 4 additions & 2 deletions spec/suma/api/preferences_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
Suma::Oye.auth_token = "fake token"
member.update(oye_contact_id: "1")
contact_status_update_req = stub_request(:put, "https://app.oyetext.org/api/v1/contacts/bulk_update").
with(body: {contacts: [{id: "1", status: "inactive"}]}).
to_return(fixture_response("oye/bulk_update_contacts"), status: 200)

post "/v1/preferences/public",
Expand All @@ -72,9 +73,10 @@

it "updates contact id on member if it is blank" do
Suma::Oye.auth_token = "fake token"
member.update(phone: "12223334444", oye_contact_id: "")
member.update(oye_contact_id: "")
get_contacts_req = stub_request(:get, "https://app.oyetext.org/api/v1/contacts").
to_return(fixture_response("oye/contacts_get"), status: 200)
with(query: {search: Suma::PhoneNumber.format_e164(member.phone)}).
to_return(fixture_response("oye/contacts_search_get"), status: 200)
status_update_req = stub_request(:put, "https://app.oyetext.org/api/v1/contacts/bulk_update").
to_return(fixture_response("oye/bulk_update_contacts"), status: 200)

Expand Down

0 comments on commit 23ab676

Please sign in to comment.