Class: PartnerService::Restaurant::Create

Inherits:
ApplicationService show all
Defined in:
app/services/partner_service/restaurant/create.rb

Instance Attribute Summary

Attributes inherited from ApplicationService

#object

Instance Method Summary collapse

Methods inherited from ApplicationService

#execute

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
  message = nil

  ActiveRecord::Base.transaction do
    # Create Owner
    if Owner.find_by(email: owner_params[:email])
      message = "Email #{I18n.t('errors.messages.taken')}"
      raise ActiveRecord::Rollback
    end
    @owner = Owner.new(owner_params)
    unless @owner.save(validate: false) && @owner.confirm
      message = @owner.errors.full_messages.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
      message = @staff.errors.full_messages.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.primary_tags.build(restaurant_tag: cuisine_tag) if cuisine_tag.present?
    @restaurant.primary_tags.build(restaurant_tag: dining_style_tag) if dining_style_tag.present?
    @restaurant.primary_tags.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
      message = @restaurant.errors.full_messages.to_sentence
      raise ActiveRecord::Rollback
    end

    # Create Staff role
    @staff_role = StaffRole.new(restaurant: @restaurant, staff: @staff, role: :owner)
    unless @staff_role.save
      message = @staff_role.errors.full_messages.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: message, errors: [message]
  end
rescue StandardError => e
  APMErrorHandler.report(e)
  ServiceResult.new message: 'Something went wrong', errors: [e.message]
rescue InvalidPackageData => e
  ServiceResult.new message: e.message, errors: [e.message]
end