diff --git a/app/controllers/rsvps_controller.rb b/app/controllers/rsvps_controller.rb index a8d0987..acf8251 100644 --- a/app/controllers/rsvps_controller.rb +++ b/app/controllers/rsvps_controller.rb @@ -1,6 +1,9 @@ class RsvpsController < ApplicationController def create @rsvp = Rsvp.find_or_create_by_email!(rsvp_params[:email]) + if @rsvp.airtable_record_id.blank? + @rsvp.update!(url_params: rsvp_params[:url_params]) + end flash[:success] = "Thanks for your interest! We'll be in touch soon." redirect_to root_path rescue ActiveRecord::RecordInvalid @@ -11,6 +14,6 @@ class RsvpsController < ApplicationController private def rsvp_params - params.permit(:email) + params.permit(:email, :url_params) end end diff --git a/app/jobs/sync_rsvp_with_airtable_job.rb b/app/jobs/sync_rsvp_with_airtable_job.rb new file mode 100644 index 0000000..a13968b --- /dev/null +++ b/app/jobs/sync_rsvp_with_airtable_job.rb @@ -0,0 +1,8 @@ +class SyncRsvpWithAirtableJob < ApplicationJob + queue_as :default + + def perform(rsvp_id) + rsvp = Rsvp.find(rsvp_id) + rsvp.sync_with_airtable! + end +end diff --git a/app/models/rsvp.rb b/app/models/rsvp.rb index ba335b0..a3114d1 100644 --- a/app/models/rsvp.rb +++ b/app/models/rsvp.rb @@ -1,7 +1,45 @@ class Rsvp < ApplicationRecord validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP } + after_create :enqueue_airtable_sync + def self.find_or_create_by_email!(email) find_or_create_by!(email: email.downcase.strip) end + + def sync_with_airtable! + uri = URI("https://api.airtable.com/v0/appuDQSHCdCHyOrxw/tblhGTc3WX9nYzU18") + + request = Net::HTTP::Patch.new(uri) + request["Authorization"] = "Bearer #{ENV.fetch("AIRTABLE_API_KEY")}" + request["Content-Type"] = "application/json" + request.body = { + performUpsert: { + fieldsToMergeOn: [ "email" ] + }, + records: [ + { + fields: { + email: email, + url_params: url_params + } + } + ] + }.to_json + + response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| + http.request(request) + end + + puts response.body + + self.airtable_record_id = JSON.parse(response.body).dig("records", 0, "id") + save! + end + + private + + def enqueue_airtable_sync + SyncRsvpWithAirtableJob.perform_later(id) + end end diff --git a/app/views/landing/index.html.erb b/app/views/landing/index.html.erb index bfa6413..a0c74ef 100644 --- a/app/views/landing/index.html.erb +++ b/app/views/landing/index.html.erb @@ -22,6 +22,7 @@ <%= form_with url: rsvp_path, method: :post, local: true do |form| %>