From 0ef50dd22d7147bd2a602884579bb2e4e3e9636f Mon Sep 17 00:00:00 2001 From: William Le Date: Fri, 22 Sep 2023 01:46:52 -0700 Subject: [PATCH] feat(placeos/visitor-deleter): new driver deletes Guests, Visitor Bookings --- drivers/place/staff_api.cr | 9 +++++ drivers/place/visitor_deleter.cr | 55 +++++++++++++++++++++++++++ drivers/place/visitor_deleter_spec.cr | 4 ++ 3 files changed, 68 insertions(+) create mode 100644 drivers/place/visitor_deleter.cr create mode 100644 drivers/place/visitor_deleter_spec.cr diff --git a/drivers/place/staff_api.cr b/drivers/place/staff_api.cr index 70fe186a08..27c5be20d0 100644 --- a/drivers/place/staff_api.cr +++ b/drivers/place/staff_api.cr @@ -221,6 +221,15 @@ class Place::StaffAPI < PlaceOS::Driver raise "failed to update guest #{id}: #{response.status_code}" unless response.success? end + @[Security(Level::Support)] + def delete_guest(id : Int64 | String) : Nil + response = delete("/api/staff/v1/guests/#{id}", headers: authentication(HTTP::Headers{ + "Content-Type" => "application/json", + })) + + raise "failed to delete guest #{id}: #{response.status_code}" unless response.success? + end + @[Security(Level::Support)] def query_guests(period_start : Int64, period_end : Int64, zones : Array(String)) params = URI::Params.build do |form| diff --git a/drivers/place/visitor_deleter.cr b/drivers/place/visitor_deleter.cr new file mode 100644 index 0000000000..9e714fd783 --- /dev/null +++ b/drivers/place/visitor_deleter.cr @@ -0,0 +1,55 @@ +require "placeos-driver" + +class Place::VisitorDeleter < PlaceOS::Driver + descriptive_name "PlaceOS Visitor Deleter" + generic_name :VisitorDeleter + description %(Delete guests / visitor bookings n days after their visit. For use with Trigger on cron. Requires Support permissions) + + default_settings({ + building_zone_id: "required", + debug: false, + }) + + accessor staff_api : StaffAPI_1 + + @building_zone_id : String = "required" + + def on_load + on_update + end + + def on_update + @building_zone_id = setting(String, :building_zone_id) + end + + @[Security(Level::Support)] + def find_and_delete(past_days_to_search : UInt32 = 70_u32, + days_after_visit_until_visitor_deletion : UInt32 = 60_u32, + delete_guests : Bool = true, + delete_visitor_bookings : Bool = true) : Nil + now = Time.utc.to_unix + from_epoch = now - past_days_to_search.days.to_i + til_epoch = now - days_after_visit_until_visitor_deletion.days.to_i + + find_and_delete_guests(from_epoch, til_epoch) if delete_guests + find_and_delete_visitor_bookings(from_epoch, til_epoch) if delete_visitor_bookings + end + + private def find_and_delete_guests(from_epoch : Int64, til_epoch : Int64) + guests = staff_api.query_guests(from_epoch, til_epoch, [@building_zone_id]).get.as_a + guests.each { |g| delete_guest(g["id"].as_i) } + end + + private def find_and_delete_visitor_bookings(from_epoch : Int64, til_epoch : Int64) + visitor_bookings = staff_api.query_bookings("visitor", from_epoch, til_epoch, [@building_zone_id]).get.as_a + visitor_bookings.each { |b| delete_visitor_booking(b["id"].as_i) } + end + + private def delete_guest(id : Int32) + staff_api.delete_guest(id) + end + + private def delete_visitor_booking(id : Int32) + staff_api.booking_delete(id) + end +end diff --git a/drivers/place/visitor_deleter_spec.cr b/drivers/place/visitor_deleter_spec.cr new file mode 100644 index 0000000000..577e44392a --- /dev/null +++ b/drivers/place/visitor_deleter_spec.cr @@ -0,0 +1,4 @@ +require "placeos-driver/spec" + +DriverSpecs.mock_driver "Place::VisitorDeleter" do +end