]> gitweb.mndrdr.org Git - arelpe.git/commitdiff
Added resync from buttondown. Testing in prod like a pro
authorAidan Cornelius-Bell <[email protected]>
Tue, 8 Oct 2024 08:12:16 +0000 (18:42 +1030)
committerAidan Cornelius-Bell <[email protected]>
Tue, 8 Oct 2024 08:12:16 +0000 (18:42 +1030)
app/controllers/mailing_lists_controller.rb
app/services/buttondown_service.rb
app/views/mailing_lists/index.html.erb
config/routes.rb

index 39144be3f226b352407b84256972740a3379612a..60c75c7f7b6b04a5ed3db47c74bb4fc2c5d69511 100644 (file)
@@ -24,6 +24,15 @@ class MailingListsController < ApplicationController
       redirect_to mailing_lists_path, alert: "Failed to unsubscribe. Please try again later."
     end
   end
+  
+  def resync_from_buttondown
+    result = ButtondownService.new.resync_user(current_user)
+    if result
+      redirect_to mailing_lists_path, notice: "Successfully resynced your information from Buttondown."
+    else
+      redirect_to mailing_lists_path, alert: "Failed to resync from Buttondown. Please try again later."
+    end
+  end
 
   def sync_status
     result = ButtondownService.new.update_subscriber(current_user)
index e2b01bbab6f363a31561f3a947563868e948366f..fa166eca12817143ed229947a921f29959056f26 100644 (file)
@@ -44,6 +44,18 @@ class ButtondownService
        user.update(buttondown_status: 'unactivated') if response.success?
        response.success?
   end
+  
+  def resync_user(user)
+         response = self.class.get("/subscribers/#{user.email}", @options)
+         
+         if response.success?
+               subscriber_data = JSON.parse(response.body)
+               update_user_from_buttondown(user, subscriber_data)
+               true
+         else
+               false
+         end
+       end
 
   private
 
@@ -56,6 +68,17 @@ class ButtondownService
          user.buttondown_status
        end
   end
+  
+  def update_user_from_buttondown(user, subscriber_data)
+         user.update(
+               buttondown_status: subscriber_data['type'],
+               # Add any other fields you want to sync from Buttondown
+               # For example:
+               # last_buttondown_sync: Time.current,
+               # buttondown_notes: subscriber_data['notes'],
+               # etc.
+         )
+       end
 
   def update_user_buttondown_status(user, response)
        buttondown_data = JSON.parse(response.body)
index 396172116addbb9ff8b5d58eedfb4e916699415a..617b56daa4eb4c0bbab5725586c2a29ccccac4fd 100644 (file)
@@ -16,6 +16,8 @@
   <% end %>
   
   <%= button_to "Unsubscribe", unsubscribe_mailing_lists_path, method: :delete, class: "button danger", data: { confirm: "Are you sure you want to unsubscribe?" } %>
+  
+  <%= button_to "Resync from Buttondown", resync_from_buttondown_mailing_lists_path, method: :post, class: "button" %>
 <% end %>
 
 <% if @is_paid_user && @buttondown_status != 'gifted' %>
index 22328501893beae1f53eb8fcec288cff3564a9e4..efa8e9f24afed85592a69919fda6bd58e26e40d8 100644 (file)
@@ -10,11 +10,12 @@ Rails.application.routes.draw do
     end
   end
   
-  resources :mailing_lists, only: [:index] do
+resources :mailing_lists, only: [:index] do
     collection do
       post 'subscribe'
       delete 'unsubscribe'
       post 'sync_status'
+      post 'resync_from_buttondown'
     end
   end
   resources :subscriptions, only: [:new, :create, :index]