Skip to content

Commit e4ff48d

Browse files
authored
feat(booking) [PPT-1942] include parent in json (#289)
1 parent 328974a commit e4ff48d

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

spec/booking_spec.cr

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,4 +572,47 @@ module PlaceOS::Model
572572
child_booking.rejected.should eq(true)
573573
child_booking.rejected_at.should eq(rejected_at_time)
574574
end
575+
576+
it "include parent booking in json" do
577+
tenant_id = Generator.tenant.id
578+
user_one_email = "one@example.com"
579+
580+
# parent booking
581+
parent_booking = Booking.new(
582+
booking_type: "room",
583+
asset_ids: ["room-1"],
584+
booking_start: 1.hour.from_now.to_unix,
585+
booking_end: 2.hours.from_now.to_unix,
586+
user_email: PlaceOS::Model::Email.new(user_one_email),
587+
user_name: "One",
588+
booked_by_email: PlaceOS::Model::Email.new(user_one_email),
589+
booked_by_name: "One",
590+
tenant_id: tenant_id,
591+
booked_by_id: "user-1",
592+
history: [] of Booking::History
593+
).save!
594+
595+
# child booking
596+
child_booking = Booking.new(
597+
booking_type: "asset",
598+
asset_ids: ["laptop-1"],
599+
booking_start: 1.hour.from_now.to_unix,
600+
booking_end: 2.hours.from_now.to_unix,
601+
user_email: PlaceOS::Model::Email.new(user_one_email),
602+
user_name: "One",
603+
booked_by_email: PlaceOS::Model::Email.new(user_one_email),
604+
booked_by_name: "One",
605+
tenant_id: tenant_id,
606+
booked_by_id: "user-1",
607+
history: [] of Booking::History,
608+
parent_id: parent_booking.id
609+
).save!
610+
611+
child_booking = Booking.find(child_booking.id)
612+
child_booking = Booking.hydrate_parents([child_booking])[0]
613+
child_booking_json = JSON.parse(child_booking.to_json).as_h
614+
child_booking_json["parent_id"].should eq(parent_booking.id)
615+
child_booking_json["linked_parent_booking"].as_h.should_not be_nil
616+
child_booking_json["linked_parent_booking"].as_h["id"].should eq(parent_booking.id)
617+
end
575618
end

src/placeos-models/booking.cr

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ module PlaceOS::Model
9898
@[JSON::Field(key: "linked_bookings", ignore_deserialize: true)]
9999
getter(children : Array(Booking)?) { get_children }
100100

101+
@[JSON::Field(key: "linked_parent_booking", ignore_deserialize: true)]
102+
property(parent : Booking?) { get_parent }
103+
101104
@[JSON::Field(key: "attendees", ignore_serialize: true)]
102105
property req_attendees : Array(PlaceCalendar::Event::Attendee)? = nil
103106

@@ -675,6 +678,25 @@ module PlaceOS::Model
675678
Booking.where(parent_id: id).to_a
676679
end
677680

681+
private def get_parent
682+
return nil unless !parent?
683+
Booking.where(id: parent_id).to_a[0]
684+
end
685+
686+
def self.hydrate_parents(bookings : Array(Booking))
687+
parent_ids = bookings.compact_map(&.parent_id).uniq!
688+
parents = Booking.where(id: parent_ids).to_a
689+
parents_by_id = parents.index_by(&.id)
690+
bookings.each do |booking|
691+
next if booking.parent_id.nil?
692+
if parent = parents_by_id[booking.parent_id]?
693+
booking.parent = parent
694+
end
695+
end
696+
697+
bookings
698+
end
699+
678700
# ===
679701
# booking to event relationship
680702
# ===

0 commit comments

Comments
 (0)