Class: PartnerService::Restaurant::Create
- Inherits:
-
ApplicationService
- Object
- ApplicationService
- PartnerService::Restaurant::Create
- Defined in:
- app/services/partner_service/restaurant/create.rb
Instance Attribute Summary
Attributes inherited from ApplicationService
Instance Method Summary collapse
- #execute! ⇒ Object
-
#initialize(params) ⇒ Create
constructor
A new instance of Create.
Methods inherited from ApplicationService
Constructor Details
#initialize(params) ⇒ Create
Returns a new instance of Create.
6 7 8 |
# File 'app/services/partner_service/restaurant/create.rb', line 6 def initialize(params) @params = params end |
Instance Method Details
#execute! ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/services/partner_service/restaurant/create.rb', line 10 def execute! result = false = nil ActiveRecord::Base.transaction do # Create Owner if Owner.find_by(email: owner_params[:email]) = "Email #{I18n.t('errors.messages.taken')}" raise ActiveRecord::Rollback end @owner = Owner.new(owner_params) unless @owner.save(validate: false) && @owner.confirm = @owner.errors..to_sentence raise ActiveRecord::Rollback end # Create Staff @staff = Staff.new(staff_params) @staff.owner = @owner @staff.confirmation_sent_at = Time.current @staff.confirmation_token = Devise.friendly_token unless @staff.save = @staff.errors..to_sentence raise ActiveRecord::Rollback end # Create Restaurant @restaurant = Restaurant.new(restaurant_params) @restaurant.owner_id = @owner.id @restaurant.skip_validation = true # Set primary cuisine, address cuisine_tag = RestaurantTag.find_by(id: params[:primary_cuisine_id]) dining_style_tag = RestaurantTag.find_by(id: params[:primary_dining_style_id]) location_tag = RestaurantTag.find_by(id: params[:primary_location_id]) @restaurant..build(restaurant_tag: cuisine_tag) if cuisine_tag.present? @restaurant..build(restaurant_tag: dining_style_tag) if dining_style_tag.present? @restaurant..build(restaurant_tag: location_tag) if location_tag.present? @restaurant.address_en = params[:address] @restaurant.city_id = params[:city_id] @restaurant.expiry_date = Date.today + 1.year @restaurant.created_by = 'owner' unless @restaurant.save = @restaurant.errors..to_sentence raise ActiveRecord::Rollback end # Create Staff role @staff_role = StaffRole.new(restaurant: @restaurant, staff: @staff, role: :owner) unless @staff_role.save = @staff_role.errors..to_sentence raise ActiveRecord::Rollback end # PrimaryTagCpt::Operations::UpdateRelation.call(id: cuisine_tag.id, restaurant_id: @restaurant.id) if cuisine_tag.present? result = true end if result ServiceResult.new data: { owner: @owner, restaurant: @restaurant, staff: @staff } else ServiceResult.new message: , errors: [] end rescue StandardError => e APMErrorHandler.report(e) ServiceResult.new message: 'Something went wrong', errors: [e.] rescue InvalidPackageData => e ServiceResult.new message: e., errors: [e.] end |