Class: ReviewService::Create

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

Constant Summary collapse

REVIEW_TIME_LIMIT =

if customer is trying to make review and current time is more than following :REVIEW_TIME_LIMIT: value, then it should be rejected

30.days

Instance Attribute Summary collapse

Attributes inherited from ApplicationService

#object

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationService

#execute

Constructor Details

#initialize(review, user = nil) ⇒ Create

Returns a new instance of Create.



13
14
15
16
# File 'app/services/review_service/create.rb', line 13

def initialize(review, user = nil)
  self.object = review
  object.user = user unless user.nil?
end

Instance Attribute Details

#disclose_reviewObject

Returns the value of attribute disclose_review.



7
8
9
# File 'app/services/review_service/create.rb', line 7

def disclose_review
  @disclose_review
end

#userObject

Returns the value of attribute user.



7
8
9
# File 'app/services/review_service/create.rb', line 7

def user
  @user
end

Class Method Details

.find_time_limit(dining_time) ⇒ Object

Parameters:

  • dining_time (Time)

    reservation dining time



76
77
78
# File 'app/services/review_service/create.rb', line 76

def self.find_time_limit(dining_time)
  dining_time + REVIEW_TIME_LIMIT
end

.past_review_time_limit?(dining_time, time_zone) ⇒ Boolean

Parameters:

  • dining_time (Time)

    reservation dining time

  • time_zone (String)

    restaurant time zone

Returns:

  • (Boolean)


68
69
70
71
72
73
# File 'app/services/review_service/create.rb', line 68

def self.past_review_time_limit?(dining_time, time_zone)
  current_time = Time.now_in_tz time_zone
  return true if current_time > find_time_limit(dining_time)

  false
end

Instance Method Details

#execute!Object



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
# File 'app/services/review_service/create.rb', line 18

def execute!
  return invalid_rating if refute_rating_rules?
  return invalid_object unless valid_status?
  return invalid_object unless valid_time?

  success = false

  ActiveRecord::Base.transaction do
    setup_disclose_data(object.reservation)

    object.state = ReviewReservationDecorator::SECOND_STEP
    object.save!

    OwnerMailer.report_user_review(object.id).deliver_later! if object.ours?
    store_report_data(object, object.reservation.restaurant)

    if object.reservation.restaurant.eligible_for_rewards?
      GiveReservationRewardWorker.perform_async(object.reservation_id)
    end

    success = true
  end

  raise unless success

  ServiceResult.new(data: object)
end

#valid_status?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
# File 'app/services/review_service/create.rb', line 46

def valid_status?
  reservation = object.reservation
  return true if reservation.present? && reservation.active? && reservation.is_past? && !reservation.no_show?

  object.errors.add :base,
                    'You can not add review if your reservation has been cancelled or marked as no show'
  false
end

#valid_time?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
# File 'app/services/review_service/create.rb', line 55

def valid_time?
  reservation = object.reservation
  return true unless self.class.past_review_time_limit?(
    reservation.reservation_time, object.reservation.restaurant.time_zone
  )

  object.errors.add :base,
                    "Sorry, for this reservation, you only allowed to make review until #{I18n.l(self.class.find_time_limit(reservation.reservation_time))}"
  false
end