Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions spec/booking_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -572,4 +572,47 @@ module PlaceOS::Model
child_booking.rejected.should eq(true)
child_booking.rejected_at.should eq(rejected_at_time)
end

it "include parent booking in json" do
tenant_id = Generator.tenant.id
user_one_email = "[email protected]"

# parent booking
parent_booking = Booking.new(
booking_type: "room",
asset_ids: ["room-1"],
booking_start: 1.hour.from_now.to_unix,
booking_end: 2.hours.from_now.to_unix,
user_email: PlaceOS::Model::Email.new(user_one_email),
user_name: "One",
booked_by_email: PlaceOS::Model::Email.new(user_one_email),
booked_by_name: "One",
tenant_id: tenant_id,
booked_by_id: "user-1",
history: [] of Booking::History
).save!

# child booking
child_booking = Booking.new(
booking_type: "asset",
asset_ids: ["laptop-1"],
booking_start: 1.hour.from_now.to_unix,
booking_end: 2.hours.from_now.to_unix,
user_email: PlaceOS::Model::Email.new(user_one_email),
user_name: "One",
booked_by_email: PlaceOS::Model::Email.new(user_one_email),
booked_by_name: "One",
tenant_id: tenant_id,
booked_by_id: "user-1",
history: [] of Booking::History,
parent_id: parent_booking.id
).save!

child_booking = Booking.find(child_booking.id)
child_booking = Booking.hydrate_parents([child_booking])[0]
child_booking_json = JSON.parse(child_booking.to_json).as_h
child_booking_json["parent_id"].should eq(parent_booking.id)
child_booking_json["linked_parent_booking"].as_h.should_not be_nil
child_booking_json["linked_parent_booking"].as_h["id"].should eq(parent_booking.id)
end
end
22 changes: 22 additions & 0 deletions src/placeos-models/booking.cr
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@
@[JSON::Field(key: "linked_bookings", ignore_deserialize: true)]
getter(children : Array(Booking)?) { get_children }

@[JSON::Field(key: "linked_parent_booking", ignore_deserialize: true)]
property(parent : Booking?) { get_parent }

@[JSON::Field(key: "attendees", ignore_serialize: true)]
property req_attendees : Array(PlaceCalendar::Event::Attendee)? = nil

Expand Down Expand Up @@ -675,6 +678,25 @@
Booking.where(parent_id: id).to_a
end

private def get_parent
return nil unless !parent?

Check notice on line 682 in src/placeos-models/booking.cr

View workflow job for this annotation

GitHub Actions / Ameba

Style/NegatedConditionsInUnless

Avoid negated conditions in unless blocks
Raw output
> return nil unless !parent?
  ^
Booking.where(id: parent_id).to_a[0]
end

def self.hydrate_parents(bookings : Array(Booking))
parent_ids = bookings.compact_map(&.parent_id).uniq!
parents = Booking.where(id: parent_ids).to_a
parents_by_id = parents.index_by(&.id)
bookings.each do |booking|
next if booking.parent_id.nil?
if parent = parents_by_id[booking.parent_id]?
booking.parent = parent
end
end

bookings
end

# ===
# booking to event relationship
# ===
Expand Down
Loading