Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(placeos/visitor-deleter): new driver deletes Guests, Visitor Bookings #463

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading