Skip to content

Instantly share code, notes, and snippets.

@zarazan
Last active July 15, 2020 22:08
Show Gist options
  • Save zarazan/a7531326c307aa7221f489ef97d41f96 to your computer and use it in GitHub Desktop.
Save zarazan/a7531326c307aa7221f489ef97d41f96 to your computer and use it in GitHub Desktop.
MASTER_FACILITY_ID = 200
SOURCE_ID = 2
STORE_NUMBERS = ['107','117','310','316','317','318','320','326','367','368','369','370','443','512','514','518','519','550','555','556','587','589','590','645','649','655','660','661','662','663','664','665','666','667','669','700','706','778','779','780','781','830','831','835','843','844','845','846','847','848','849','850','851','852','853','854','855','862','863','864','865','877','878','879','889','901','5006','5040','5062','5063','5066','5067','5071','5142','5170','5205','5206','5208','5220','5222','5223','5394','5412','5413','5536','5538','5539','5540','5581','5583','5584','5596','5610','5615','5616','5617','5633','5637','5732','5733','5740','5785','5786','5787','5790','5800','5814','5815','5816','5817','5818','5819','5834','5839','5843','5850','5890','5990','5995','6049','6069','6095','6109','6190','6258','6271','6272','6277','6368','6369','6681','6683','6700','6714','6715','6716','6717','6718','6719','6720','6721','6722','6723','6724','6725','6764','6774','6787','6788','6938','6939','6940','6941','6942','6943','6944','6945','6946','6947','6948','6949','6950','6951','6952','6953','6977','6978','6979','6980','6981','6982','6983','6984','6985','6986','6987','6988']
def update_phnkey!(facility)
# retrieve phnkey numbers
phnkey = PhnkeyApi.new(facility.phnkey_api_key, facility.phnkey_auth_token, Rails.logger, ApplicationConfig[:phnkey_url])
sms_phone = nil
sms_phone = phnkey.phones['phones'].first if phnkey.phones
if !sms_phone
puts "No phone found for store #{facility.store_number}"
return
end
facility.sms_phone = sms_phone['Phone']
facility.save!
# update phnkey
phn_nums = [{ 'ApiKey' => sms_phone['ApiKey'], 'source_id' => SOURCE_ID }]
RefreshPhnkeyPhoneService.process(facility: facility, phone_numbers: phn_nums)
# update twilio
current_number = TwilioClient.api.account.incoming_phone_numbers.list({phone_number: facility.sms_phone}).first
current_number.update(sms_application_sid: ApplicationConfig[:twilio_sms_app_id])
end
# Can only have one sms turned-on at a time for these template contexts, so first turn them all off
def turn_off_sms_for_contexts!(facility, template_contexts)
facility.templates.joins(:template_contexts).sms.where(template_contexts: {type: template_contexts.pluck(:type)}).each do |template|
if !template.default
template.description = "#{template.description} (OLD)"
template.save!
end
template.turn_off!
end
end
def copy_template!(facility, template_to_copy)
template = template_to_copy.dup
template.facility = facility
template.template_contexts = template_to_copy.template_contexts.map do |template_context|
facility.template_context_for(template_context.class)
end
template.save!
end
# Copy the templates from the master store
def copy_templates!(facility, templates_to_copy)
templates_to_copy.each do |template_to_copy|
if template_to_copy.default
matching_default_template = facility.templates.sms.find_by(description: template_to_copy.description)
if matching_default_template
matching_default_template.turn_on!
next
end
end
copy_template!(facility, template_to_copy)
end
end
templates_to_copy = Facility.find(MASTER_FACILITY_ID).templates.sms.turned_on
template_contexts = templates_to_copy.map(&:template_contexts).flatten
STORE_NUMBERS.each do |store_number|
facility = Facility.find_by(store_number: store_number)
if !facility
puts "Failed to find facility for store number #{store_number}"
next
end
update_phnkey!(facility)
turn_off_sms_for_contexts!(facility, template_contexts)
copy_templates!(facility, templates_to_copy)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment