diff --git a/app/controllers/spree/api/v2/storefront/id_cards_controller.rb b/app/controllers/spree/api/v2/storefront/id_cards_controller.rb index 588a15ca7..b2a89a1e4 100644 --- a/app/controllers/spree/api/v2/storefront/id_cards_controller.rb +++ b/app/controllers/spree/api/v2/storefront/id_cards_controller.rb @@ -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) } @@ -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 diff --git a/app/controllers/spree/api/v2/storefront/template_guests_controller.rb b/app/controllers/spree/api/v2/storefront/template_guests_controller.rb deleted file mode 100644 index 30858dd41..000000000 --- a/app/controllers/spree/api/v2/storefront/template_guests_controller.rb +++ /dev/null @@ -1,79 +0,0 @@ -module Spree - module Api - module V2 - module Storefront - class TemplateGuestsController < ::Spree::Api::V2::ResourceController - before_action :require_spree_current_user - - # override - def model_class - SpreeCmCommissioner::TemplateGuest - end - - # override - def collection - spree_current_user.template_guests - end - - # override - def collection_serializer - SpreeCmCommissioner::V2::Storefront::TemplateGuestSerializer - end - - # override - def resource_serializer - SpreeCmCommissioner::V2::Storefront::TemplateGuestSerializer - end - - def create - resource = spree_current_user.template_guests.create(template_guest_params) - - if resource.save - render_serialized_payload(201) { serialize_resource(resource) } - else - render_error_payload(resource.errors, 400) - end - end - - def update - resource = spree_current_user.template_guests.find(params[:id]) - resource.assign_attributes(template_guest_params) - - if resource.save - render_serialized_payload { serialize_resource(resource) } - else - render_error_payload(resource.errors, 400) - end - end - - def destroy - resource = spree_current_user.template_guests.find(params[:id]) - - if resource.destroy - render json: { message: 'Template guest deleted successfully' }, status: :ok - else - render_error_payload(resource.errors, 400) - end - end - - private - - def template_guest_params - params.permit( - :first_name, - :last_name, - :dob, - :phone_number, - :emergency_contact, - :gender, - :occupation_id, - :other_occupation, - :nationality_id, - :is_default - ) - end - end - end - end - end -end diff --git a/app/interactors/spree_cm_commissioner/guest_id_card_manager.rb b/app/interactors/spree_cm_commissioner/guest_id_card_manager.rb index 5f776325e..af5379b78 100644 --- a/app/interactors/spree_cm_commissioner/guest_id_card_manager.rb +++ b/app/interactors/spree_cm_commissioner/guest_id_card_manager.rb @@ -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 diff --git a/app/interactors/spree_cm_commissioner/id_card_manager.rb b/app/interactors/spree_cm_commissioner/id_card_manager.rb deleted file mode 100644 index d750cf56f..000000000 --- a/app/interactors/spree_cm_commissioner/id_card_manager.rb +++ /dev/null @@ -1,65 +0,0 @@ -module SpreeCmCommissioner - class IdCardManager < BaseInteractor - delegate :card_type, :front_image_url, :back_image_url, :guestable_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 - guestable - 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 - - def guestable - context.fail!(message: 'Image url can not be empty') - end - end -end diff --git a/app/interactors/spree_cm_commissioner/template_guest_id_card_manager.rb b/app/interactors/spree_cm_commissioner/template_guest_id_card_manager.rb deleted file mode 100644 index 289321a5b..000000000 --- a/app/interactors/spree_cm_commissioner/template_guest_id_card_manager.rb +++ /dev/null @@ -1,7 +0,0 @@ -module SpreeCmCommissioner - class TemplateGuestIdCardManager < IdCardManager - def guestable - @guestable ||= SpreeCmCommissioner::TemplateGuest.find(context.guestable_id) - end - end -end diff --git a/app/models/spree_cm_commissioner/guest.rb b/app/models/spree_cm_commissioner/guest.rb index f4f408393..9df071087 100644 --- a/app/models/spree_cm_commissioner/guest.rb +++ b/app/models/spree_cm_commissioner/guest.rb @@ -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 diff --git a/app/models/spree_cm_commissioner/id_card.rb b/app/models/spree_cm_commissioner/id_card.rb index 46ee84e3c..d306c927a 100644 --- a/app/models/spree_cm_commissioner/id_card.rb +++ b/app/models/spree_cm_commissioner/id_card.rb @@ -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' diff --git a/app/models/spree_cm_commissioner/template_guest.rb b/app/models/spree_cm_commissioner/template_guest.rb deleted file mode 100644 index 951b34293..000000000 --- a/app/models/spree_cm_commissioner/template_guest.rb +++ /dev/null @@ -1,25 +0,0 @@ -module SpreeCmCommissioner - class TemplateGuest < SpreeCmCommissioner::Base - acts_as_paranoid - - enum :gender, { :other => 0, :male => 1, :female => 2 } - - has_one :id_card, as: :id_cardable, class_name: 'SpreeCmCommissioner::IdCard', dependent: :destroy - - belongs_to :user, class_name: 'Spree::User' - belongs_to :occupation, class_name: 'Spree::Taxon' - belongs_to :nationality, class_name: 'Spree::Taxon' - - before_save :ensure_single_default - - private - - def ensure_single_default - return unless is_default && is_default_changed? - - user.template_guests.where.not(id: id).find_each do |template_guest| - template_guest.update!(is_default: false) - end - end - end -end diff --git a/app/models/spree_cm_commissioner/user_decorator.rb b/app/models/spree_cm_commissioner/user_decorator.rb index 6e1346e5d..f76d91d6f 100644 --- a/app/models/spree_cm_commissioner/user_decorator.rb +++ b/app/models/spree_cm_commissioner/user_decorator.rb @@ -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' diff --git a/app/serializers/spree_cm_commissioner/v2/storefront/template_guest_serializer.rb b/app/serializers/spree_cm_commissioner/v2/storefront/template_guest_serializer.rb deleted file mode 100644 index 178252be1..000000000 --- a/app/serializers/spree_cm_commissioner/v2/storefront/template_guest_serializer.rb +++ /dev/null @@ -1,16 +0,0 @@ -module SpreeCmCommissioner - module V2 - module Storefront - class TemplateGuestSerializer < BaseSerializer - attributes :first_name, :last_name, :dob, :gender, :is_default, :phone_number, :emergency_contact, - :occupation_id, :other_occupation, :nationality_id, :user_id, - :deleted_at, :created_at, :updated_at - - belongs_to :occupation, serializer: Spree::V2::Storefront::TaxonSerializer - belongs_to :nationality, serializer: Spree::V2::Storefront::TaxonSerializer - - has_one :id_card, serializer: Spree::V2::Storefront::IdCardSerializer - end - end - end -end diff --git a/config/routes.rb b/config/routes.rb index 490d46ea3..9c1157e77 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20240926100138_create_spree_cm_commissioner_template_guests.rb b/db/migrate/20240926100138_create_spree_cm_commissioner_template_guests.rb deleted file mode 100644 index 53ccc0a11..000000000 --- a/db/migrate/20240926100138_create_spree_cm_commissioner_template_guests.rb +++ /dev/null @@ -1,20 +0,0 @@ -class CreateSpreeCmCommissionerTemplateGuests < ActiveRecord::Migration[7.0] - def change - create_table :cm_template_guests, if_not_exists: true do |t| - t.string :first_name - t.string :last_name - t.date :dob - t.integer :gender, default: 0 - t.string :phone_number, limit: 50 - t.string :emergency_contact, limit: 50 - t.boolean :is_default, default: false - t.references :user, foreign_key: { to_table: :spree_users } - t.references :nationality, foreign_key: { to_table: :spree_taxons } - t.references :occupation, foreign_key: { to_table: :spree_taxons } - t.string :other_occupation - t.datetime :deleted_at, index: true - - t.timestamps - end - end -end diff --git a/db/migrate/20240926120947_add_template_guest_id_to_spree_cm_commissioner_guest.rb b/db/migrate/20240926120947_add_template_guest_id_to_spree_cm_commissioner_guest.rb deleted file mode 100644 index dc647ef64..000000000 --- a/db/migrate/20240926120947_add_template_guest_id_to_spree_cm_commissioner_guest.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddTemplateGuestIdToSpreeCmCommissionerGuest < ActiveRecord::Migration[7.0] - def change - add_reference :cm_guests, :template_guest, foreign_key: { to_table: :cm_template_guests }, index: true, if_not_exists: true - end -end diff --git a/db/migrate/20240927082749_migrate_guest_id_to_polymorphic_in_cm_id_cards.rb b/db/migrate/20240927082749_migrate_guest_id_to_polymorphic_in_cm_id_cards.rb deleted file mode 100644 index 25dda50b7..000000000 --- a/db/migrate/20240927082749_migrate_guest_id_to_polymorphic_in_cm_id_cards.rb +++ /dev/null @@ -1,9 +0,0 @@ -class MigrateGuestIdToPolymorphicInCmIdCards < ActiveRecord::Migration[7.0] - def up - add_reference :cm_id_cards, :id_cardable, polymorphic: true, index: true - end - - def down - remove_reference :cm_id_cards, :id_cardable, polymorphic: true, index: true - end -end diff --git a/lib/spree_cm_commissioner/test_helper/factories/id_card_factory.rb b/lib/spree_cm_commissioner/test_helper/factories/id_card_factory.rb index 95e1dc5b2..f1e5dce51 100644 --- a/lib/spree_cm_commissioner/test_helper/factories/id_card_factory.rb +++ b/lib/spree_cm_commissioner/test_helper/factories/id_card_factory.rb @@ -1,8 +1,8 @@ FactoryBot.define do factory :id_card , class: SpreeCmCommissioner::IdCard do card_type { 1 } - id_cardable { create(:guest) } + guest { create(:guest) } front_image { create(:cm_front_image) } back_image { create(:cm_back_image) } end -end +end \ No newline at end of file diff --git a/lib/tasks/migrate_id_cards.rake b/lib/tasks/migrate_id_cards.rake deleted file mode 100644 index 3c7d41eee..000000000 --- a/lib/tasks/migrate_id_cards.rake +++ /dev/null @@ -1,9 +0,0 @@ -# recommend to be used in schedule.yml & manually access in /sidekiq/cron -namespace :spree_cm_commissioner do - desc 'Migrate guest_id to id_cardable in CmIdCards' - task migrate_id_cards: :environment do - SpreeCmCommissioner::IdCard.where.not(guest_id: nil).find_each do |id_card| - id_card.update(id_cardable_type: 'SpreeCmCommissioner::Guest', id_cardable_id: id_card.guest_id) - end - end -end diff --git a/spec/interactors/spree_cm_commissioner/id_card_manager_spec.rb b/spec/interactors/spree_cm_commissioner/guest_id_card_manager_spec.rb similarity index 51% rename from spec/interactors/spree_cm_commissioner/id_card_manager_spec.rb rename to spec/interactors/spree_cm_commissioner/guest_id_card_manager_spec.rb index 35a31c024..55a66e0ff 100644 --- a/spec/interactors/spree_cm_commissioner/id_card_manager_spec.rb +++ b/spec/interactors/spree_cm_commissioner/guest_id_card_manager_spec.rb @@ -1,32 +1,31 @@ require 'spec_helper' -RSpec.describe SpreeCmCommissioner::IdCardManager do - let(:guest) { create(:guest) } - let(:id_card) { create(:id_card, card_type: 'passport', id_cardable: guest) } +RSpec.describe SpreeCmCommissioner::GuestIdCardManager do + let(:id_card) { create(:id_card, card_type: 'passport') } - let(:guest_with_id_card) { create(:guest, id_card: id_card) } + let(:guest_with_id_card) { create(:guest , id_card: id_card ) } + let(:guest) { create(:guest) } let(:card_type) { 'student_id_card' } - let(:front_image_url) { "https://example.com/front_image.jpg" } - let(:back_image_url) { "https://example.com/back_image.jpg" } + let(:front_image_url) { "https://example/front_image.jpg" } + let(:back_image_url) { "https://example/front_image.jpg" } describe '.call' do context 'create_guest_id_card' do - it 'returns error if front_image_url is nil' do - context = described_class.call(card_type: card_type, guest_id: guest.id) + it 'return error if front_image_url is nil' do + context = described_class.call(card_type: card_type, guest_id: guest.id) - expect(context.success?).to eq false - expect(context.message).to eq 'Image url can not be empty' + expect(context.success?).to eq false + expect(context.message).to eq 'Image url can not be empty' end - it 'returns id_card record if create is successful' do - allow_any_instance_of(described_class).to receive(:guestable).and_return(guest) + it 'return id_card record if create success' do context = described_class.call( - card_type: card_type, - guestable_id: guest.id, - front_image_url: front_image_url, - back_image_url: back_image_url - ) + card_type: card_type, + guest_id: guest.id, + front_image_url: front_image_url, + back_image_url: front_image_url + ) allow(context).to receive(:manage_front_image).and_return(instance_double("context", success?: true)) allow(context).to receive(:manage_back_image).and_return(instance_double("context", success?: true)) @@ -35,24 +34,25 @@ end context 'update_guest_id_card' do - it 'updates card type when present' do - allow_any_instance_of(described_class).to receive(:guestable).and_return(guest_with_id_card) - described_class.call(card_type: card_type, guestable: guest_with_id_card.id) + it 'update card type when present' do + described_class.call(card_type: card_type , guest_id: guest_with_id_card.id) expect(id_card.reload.card_type).to eq(card_type) end - it 'updates front_image when present' do + it 'update front_image when present' do + allow(SpreeCmCommissioner::IdCardImageUpdater).to receive(:call).with( id_card: id_card, - image_url: front_image_url, + image_url: front_image_url , type: 'front_image' ).and_return(instance_double("context", success?: true)) described_class.call(front_image_url: front_image_url, guest_id: guest_with_id_card.id) end - it 'updates back_image when present' do + it 'update back_image when present' do + allow(SpreeCmCommissioner::IdCardImageUpdater).to receive(:call).with( id_card: id_card, image_url: back_image_url,