Skip to content

Commit

Permalink
feat(booking_approver): improve logging and add fallback
Browse files Browse the repository at this point in the history
fallback ensures any missed events are processed
  • Loading branch information
stakach committed Aug 22, 2023
1 parent 77fefdd commit d1349fe
Showing 1 changed file with 48 additions and 5 deletions.
53 changes: 48 additions & 5 deletions drivers/place/booking_approver.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit d1349fe

Please sign in to comment.