From 40adec32adf7829dfcea70b0c1886408b1c8e713 Mon Sep 17 00:00:00 2001 From: dedekm Date: Thu, 12 May 2022 10:17:35 +0200 Subject: [PATCH] invite user after order is paid see #3 --- app/controllers/boutique/go_pay_controller.rb | 12 +++++++++--- app/models/boutique/order.rb | 12 ++++++++++++ test/dummy/config/routes.rb | 2 +- test/factories.rb | 4 ++++ test/models/boutique/order_test.rb | 8 ++++++++ 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/app/controllers/boutique/go_pay_controller.rb b/app/controllers/boutique/go_pay_controller.rb index 7b3c00ec..2efd96b7 100644 --- a/app/controllers/boutique/go_pay_controller.rb +++ b/app/controllers/boutique/go_pay_controller.rb @@ -6,12 +6,18 @@ class Boutique::GoPayController < Boutique::ApplicationController def comeback if @payment.paid? flash[:success] = t(".success") + + if @payment.order.user.invitation_accepted_at.nil? + session[:folio_user_invited_email] = @payment.order.user.email + redirect_to main_app.user_invitation_path + else + redirect_to order_path(@payment.order.secret_hash) + end else flash[:alert] = t(".failure") - end - # TODO: redirect to confirmation page for unregistered user - redirect_to order_path(@payment.order.secret_hash) + redirect_to order_path(@payment.order.secret_hash) + end end def notify diff --git a/app/models/boutique/order.rb b/app/models/boutique/order.rb index 330186ca..2d9b818d 100644 --- a/app/models/boutique/order.rb +++ b/app/models/boutique/order.rb @@ -64,12 +64,24 @@ class Boutique::Order < Boutique::ApplicationRecord before do set_numbers imprint_prices + + self.email ||= user.try(:email) end end event :pay do transitions from: :confirmed, to: :paid + after do + if user.nil? + self.user = Folio::User.invite!(email:, + first_name:, + last_name:) + update_columns(folio_user_id: user.id, + updated_at:) + end + end + after_commit do Boutique::OrderMailer.paid(self).deliver_later end diff --git a/test/dummy/config/routes.rb b/test/dummy/config/routes.rb index 534c5427..efe57d95 100644 --- a/test/dummy/config/routes.rb +++ b/test/dummy/config/routes.rb @@ -10,7 +10,7 @@ omniauth_providers: Rails.application.config.folio_users_omniauth_providers devise_scope :user do - get "/users/invitation", to: "dummy/users/invitations#show", as: nil + get "/users/invitation", to: "dummy/folio/users/invitations#show", as: nil get "/users/auth/conflict", to: "dummy/folio/users/omniauth_callbacks#conflict" get "/users/auth/resolve_conflict", to: "dummy/folio/users/omniauth_callbacks#resolve_conflict" get "/users/auth/new_user", to: "dummy/folio/users/omniauth_callbacks#new_user" diff --git a/test/factories.rb b/test/factories.rb index c7b53586..cc46294a 100644 --- a/test/factories.rb +++ b/test/factories.rb @@ -32,6 +32,10 @@ line_items_count { 0 } end + trait :with_user do + association :user, factory: :folio_user + end + trait :ready_to_be_confirmed do email { "order@email.email" } first_name { "John" } diff --git a/test/models/boutique/order_test.rb b/test/models/boutique/order_test.rb index 2a6a5996..00800e6f 100644 --- a/test/models/boutique/order_test.rb +++ b/test/models/boutique/order_test.rb @@ -51,6 +51,14 @@ def setup_emails setup_emails order = create(:boutique_order, :confirmed) + assert_difference("ActionMailer::Base.deliveries.size", 2) do + perform_enqueued_jobs do + order.pay! + end + end + + order = create(:boutique_order, :confirmed, :with_user) + assert_difference("ActionMailer::Base.deliveries.size", 1) do perform_enqueued_jobs do order.pay!