Class: Stay::Bookings::CreateBookingService

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

Instance Method Summary collapse

Constructor Details

#initialize(booking_query) ⇒ CreateBookingService

Returns a new instance of CreateBookingService.



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

def initialize(booking_query)
  @booking_query = booking_query
end

Instance Method Details

#performObject



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

def perform
  user = @booking_query.user
  property = @booking_query.property
  room_numbers = @booking_query&.query_for.present? ? @booking_query.query_for["room_number"] : @booking_query.property.rooms.pluck(:id)
  line_items_attributes = build_line_items(room_numbers)
  @booking = Stay::Booking.new(
    user: user,
    property: property,
    check_in_date: @booking_query.check_in_date,
    check_out_date: @booking_query.check_out_date,
    number_of_guests: @booking_query.guest_count, 
    line_items_attributes: line_items_attributes,
  )
  @booking.chat = @booking_query.chat
  if @booking.save
    @booking.calculate_totals
    @booking_query.update(booking: @booking)
    { success: true, booking: @booking }
  else
    { success: false, errors: @booking.errors.full_messages }
  end
end