Skip to content

Commit

Permalink
feat(placeos/visitor-deleter): new driver deletes Guests, Visitor Boo…
Browse files Browse the repository at this point in the history
…kings
  • Loading branch information
w-le committed Sep 22, 2023
1 parent 31481a3 commit 0ef50dd
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
9 changes: 9 additions & 0 deletions drivers/place/staff_api.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
55 changes: 55 additions & 0 deletions drivers/place/visitor_deleter.cr
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions drivers/place/visitor_deleter_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require "placeos-driver/spec"

DriverSpecs.mock_driver "Place::VisitorDeleter" do
end

0 comments on commit 0ef50dd

Please sign in to comment.