diff --git a/drivers/place/booking_approver.cr b/drivers/place/booking_approver.cr index ac330005a3..fe2958800d 100644 --- a/drivers/place/booking_approver.cr +++ b/drivers/place/booking_approver.cr @@ -9,8 +9,8 @@ class Place::BookingApprover < PlaceOS::Driver accessor staff_api : StaffAPI_1 default_settings({ - # approve_booking_types: ["desk"], Todo: only approve selected booking types - debug: false, + approve_booking_types: ["desk"], + approve_zones: ["zone-12345"], }) def on_load @@ -21,21 +21,64 @@ class Place::BookingApprover < PlaceOS::Driver on_update end - @debug : Bool = false @bookings_approved : Int32 = 0u32 + @approve_zones : Array(String) = [] of String + @approve_booking_types : Array(String) = [] of String def on_update - @debug = setting(Bool, :debug) + @approve_zones = setting?(Array(String), :approve_zones) || [] of String + @approve_booking_types = setting?(Array(String), :approve_booking_types) || [] of String + + schedule.clear + schedule.every(10.minutes) { approve_missed } + end + + # Finds the building ID for the current location services object + def get_building_id + zone_ids = staff_api.zones(tags: "building").get.as_a.map(&.[]("id").as_s) + (zone_ids & system.zones).first + rescue error + logger.warn(exception: error) { "unable to determine building zone id" } + nil end private def approve_booking(booking : Booking) return false unless booking.action == "create" + + if !@approve_zones.empty? + if (booking.zones & @approve_zones).empty? + logger.debug { "Ignoring booking as no booking zone matches #{booking.id}" } + return false + end + end + + if !@approve_booking_types.empty? + if !@approve_booking_types.includes?(booking.booking_type) + logger.debug { "Ignoring booking as booking_type #{booking.booking_type} doesn't match #{booking.id}" } + return false + end + end + staff_api.approve(booking.id).get + logger.debug { "Approved Booking #{booking.id}" } @bookings_approved += 1 - logger.debug { "Approved Booking #{booking.id}" } if @debug true end + def approve_missed + booking_type = @approve_booking_types[0]? || "desk" + bookings = Array(Booking).from_json staff_api.query_bookings( + type: booking_type, + created_after: 12.hours.ago, + zones: [get_building_id], + approved: false + ).get.to_json + bookings.each do |booking| + booking.action = "create" + approve_booking booking + end + end + def status {bookings_approved: @bookings_approved} end