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
22 changes: 22 additions & 0 deletions spec/booking_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,28 @@ module PlaceOS::Model
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
it "hydrate_parents returns bookings that have no parent unchanged" do
tenant_id = Generator.tenant.id.not_nil!
user_email = "one@place.tech"
booking = Booking.new(
booking_type: "asset-request",
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_email),
user_name: "One",
booked_by_email: PlaceOS::Model::Email.new(user_email),
booked_by_name: "One",
tenant_id: tenant_id,
booked_by_id: "user-1",
history: [] of Booking::History,
).save!

hydrated = Booking.hydrate_parents([Booking.find(booking.id)])
hydrated.map(&.id).should eq([booking.id])
JSON.parse(hydrated[0].to_json).as_h["linked_parent_booking"]?.try(&.raw).should be_nil
end

it "excludes deleted recurring instances from expand_bookings" do
tenant_id = Generator.tenant.id.not_nil!
user_email = "steve@place.tech"
Expand Down
2 changes: 2 additions & 0 deletions src/placeos-models/booking.cr
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,8 @@ module PlaceOS::Model

def self.hydrate_parents(bookings : Array(Booking))
parent_ids = bookings.compact_map(&.parent_id).uniq!
# an empty list renders as `IN ()`, which Postgres rejects
return bookings if parent_ids.empty?
parents = Booking.where(id: parent_ids).to_a
parents_by_id = parents.index_by(&.id)
bookings.each do |booking|
Expand Down
Loading