Class: Stay::Bookings::ExistingBookingService

Inherits:
Object
  • Object
show all
Defined in:
app/services/stay/bookings/existing_booking_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(booking, params) ⇒ ExistingBookingService

Returns a new instance of ExistingBookingService.



4
5
6
7
# File 'app/services/stay/bookings/existing_booking_service.rb', line 4

def initialize(booking, params)
  @booking = booking
  @params = params
end

Instance Method Details

#performObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/stay/bookings/existing_booking_service.rb', line 9

def perform
  user = @booking.user
  property = @booking.property
  unless property.shared_property
    room_numbers = @booking.property.rooms.pluck(:id)
  else
    room_numbers = @params[:line_items_attributes].map { |r| r["room_id"] } if @params[:line_items_attributes].any?
  end
  if room_numbers.any?
    line_items_attributes = build_line_items(room_numbers)
    @booking.line_items_attributes = line_items_attributes
  end

  if @booking.update(
      check_in_date: @params[:check_in_date],
      check_out_date: @params[:check_out_date],
      number_of_guests: @params[:number_of_guests]
    )
    @booking.calculate_totals
    { success: true, booking: @booking }

  else
    { success: false, errors: @booking.errors.full_messages }
  end
end