Skip to content

Commit

Permalink
invite user after order is paid
Browse files Browse the repository at this point in the history
see #3
  • Loading branch information
dedekm committed May 12, 2022
1 parent 3561eef commit 40adec3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
12 changes: 9 additions & 3 deletions app/controllers/boutique/go_pay_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions app/models/boutique/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions test/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 { "[email protected]" }
first_name { "John" }
Expand Down
8 changes: 8 additions & 0 deletions test/models/boutique/order_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down

0 comments on commit 40adec3

Please sign in to comment.