Skip to content

Commit

Permalink
Revert "close #1968 implement CRUD template_guests with softdelete (#…
Browse files Browse the repository at this point in the history
…1981)" (#2102)

This reverts commit 8a2f51b.
  • Loading branch information
theachoem authored Nov 28, 2024
1 parent 03578be commit c648106
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 292 deletions.
35 changes: 15 additions & 20 deletions app/controllers/spree/api/v2/storefront/id_cards_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@ module V2
module Storefront
class IdCardsController < ::Spree::Api::V2::ResourceController
def create
context = id_card_manager_context
context = SpreeCmCommissioner::GuestIdCardManager.call(
card_type: id_card_params[:card_type],
front_image_url: id_card_params[:front_image_url],
back_image_url: id_card_params[:back_image_url],
guest_id: id_card_params[:guest_id]
)

if context&.success?
if context.success?
render_serialized_payload { serialize_resource(context.result) }
else
render_error_payload(context&.message || 'Failed to create ID card')
render_error_payload(context.message)
end
end

def update
context = id_card_manager_context
context = SpreeCmCommissioner::GuestIdCardManager.call(
card_type: id_card_params[:card_type],
front_image_url: id_card_params[:front_image_url],
back_image_url: id_card_params[:back_image_url],
guest_id: id_card_params[:guest_id]
)

if context.success?
render_serialized_payload { serialize_resource(context.result) }
Expand All @@ -36,22 +46,7 @@ def destroy
private

def id_card_params
params.permit(:card_type, :front_image_url, :back_image_url, :guest_id, :template_guest_id)
end

def id_card_manager_context
options = {
card_type: id_card_params[:card_type],
front_image_url: id_card_params[:front_image_url],
back_image_url: id_card_params[:back_image_url]
}
if id_card_params[:guest_id].present?
id_card_options = options.merge(guestable_id: id_card_params[:guest_id])
SpreeCmCommissioner::GuestIdCardManager.call(**id_card_options)
elsif id_card_params[:template_guest_id].present?
id_card_options = options.merge(guestable_id: id_card_params[:template_guest_id])
SpreeCmCommissioner::TemplateGuestIdCardManager.call(**id_card_options)
end
params.permit(:card_type, :front_image_url, :back_image_url, :guest_id)
end

def model_class
Expand Down

This file was deleted.

60 changes: 57 additions & 3 deletions app/interactors/spree_cm_commissioner/guest_id_card_manager.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,61 @@
module SpreeCmCommissioner
class GuestIdCardManager < IdCardManager
def guestable
@guestable ||= SpreeCmCommissioner::Guest.find(context.guestable_id)
class GuestIdCardManager < BaseInteractor
delegate :card_type, :front_image_url, :back_image_url, :guest_id, to: :context

def call
if guest.id_card.blank? && front_image_url.present?
create_guest_id_card
elsif guest.id_card.present?
update_guest_id_card
else
context.fail!(message: 'Image url can not be empty')
end
end

def create_guest_id_card
id_card = guest.build_id_card(card_type: card_type)

manage_front_image(id_card)
manage_back_image(id_card) if back_image_url.present?

context.fail!(message: 'Failed to create Id card') unless context.success? && id_card.save
context.result = guest.id_card
end

def update_guest_id_card
manage_card_type(guest.id_card) if card_type.present?
manage_front_image(guest.id_card) if front_image_url.present?
manage_back_image(guest.id_card) if back_image_url.present?

context.fail!(message: 'Failed to update Id card') unless context.success? && guest.id_card.save
context.result = guest.id_card
end

def guest
SpreeCmCommissioner::Guest.find(guest_id)
end

def manage_card_type(id_card)
id_card.update(card_type: card_type)
end

def manage_front_image(id_card)
context = SpreeCmCommissioner::IdCardImageUpdater.call(
id_card: id_card,
image_url: front_image_url,
type: 'front_image'
)

context.fail!(message: 'Failed to update front image') unless context.success?
end

def manage_back_image(id_card)
context = SpreeCmCommissioner::IdCardImageUpdater.call(
id_card: id_card,
image_url: back_image_url,
type: 'back_image'
)
context.fail!(message: 'Failed to update back image') unless context.success?
end
end
end
65 changes: 0 additions & 65 deletions app/interactors/spree_cm_commissioner/id_card_manager.rb

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion app/models/spree_cm_commissioner/guest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Guest < SpreeCmCommissioner::Base

belongs_to :event, class_name: 'Spree::Taxon'

has_one :id_card, as: :id_cardable, class_name: 'SpreeCmCommissioner::IdCard', dependent: :destroy
has_one :id_card, class_name: 'SpreeCmCommissioner::IdCard', dependent: :destroy
has_one :check_in, class_name: 'SpreeCmCommissioner::CheckIn'

preference :telegram_user_id, :string
Expand Down
5 changes: 1 addition & 4 deletions app/models/spree_cm_commissioner/id_card.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
module SpreeCmCommissioner
class IdCard < SpreeCmCommissioner::Base
# Polymorphic association
belongs_to :id_cardable, polymorphic: true

enum card_type: { :national_id_card => 0, :passport => 1, :student_id_card => 2 }

# belongs_to :guest, class_name: 'SpreeCmCommissioner::Guest'
belongs_to :guest, class_name: 'SpreeCmCommissioner::Guest'

has_one :front_image, as: :viewable, dependent: :destroy, class_name: 'SpreeCmCommissioner::FrontImage'
has_one :back_image, as: :viewable, dependent: :destroy, class_name: 'SpreeCmCommissioner::BackImage'
Expand Down
25 changes: 0 additions & 25 deletions app/models/spree_cm_commissioner/template_guest.rb

This file was deleted.

1 change: 0 additions & 1 deletion app/models/spree_cm_commissioner/user_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def self.prepended(base)
base.has_many :user_events, class_name: 'SpreeCmCommissioner::UserEvent'
base.has_many :events, through: :user_events, class_name: 'Spree::Taxon', source: 'taxon'
base.has_many :guests, class_name: 'SpreeCmCommissioner::Guest', dependent: :destroy
base.has_many :template_guests, class_name: 'SpreeCmCommissioner::TemplateGuest', dependent: :destroy

base.has_many :wished_items, class_name: 'Spree::WishedItem', through: :wishlists
base.has_many :promotions, through: :promotion_rules, class_name: 'Spree::Promotion'
Expand Down

This file was deleted.

3 changes: 0 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,6 @@
resources :guests, only: %i[create update show] do
resources :id_cards
end
resources :template_guests, only: %i[index create show update destroy] do
resources :id_cards
end
resources :user_guests do
resources :id_cards
end
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit c648106

Please sign in to comment.