Skip to content

Commit f3c2eac

Browse files
committed
feat(placeos/visitor-deleter): new driver deletes Guests, Visitor Bookings
1 parent 0230e9c commit f3c2eac

3 files changed

Lines changed: 68 additions & 0 deletions

File tree

drivers/place/staff_api.cr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,15 @@ class Place::StaffAPI < PlaceOS::Driver
221221
raise "failed to update guest #{id}: #{response.status_code}" unless response.success?
222222
end
223223

224+
@[Security(Level::Support)]
225+
def delete_guest(id : Int64 | String) : Nil
226+
response = delete("/api/staff/v1/guests/#{id}", headers: authentication(HTTP::Headers{
227+
"Content-Type" => "application/json",
228+
}))
229+
230+
raise "failed to delete guest #{id}: #{response.status_code}" unless response.success?
231+
end
232+
224233
@[Security(Level::Support)]
225234
def query_guests(period_start : Int64, period_end : Int64, zones : Array(String))
226235
params = URI::Params.build do |form|

drivers/place/visitor_deleter.cr

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
require "placeos-driver"
2+
3+
class Place::VisitorDeleter < PlaceOS::Driver
4+
descriptive_name "PlaceOS Visitor Deleter"
5+
generic_name :VisitorDeleter
6+
description %(Delete guests / visitor bookings n days after their visit. For use with Trigger on cron. Requires Support permissions)
7+
8+
default_settings({
9+
building_zone_id: "required",
10+
debug: false,
11+
})
12+
13+
accessor staff_api : StaffAPI_1
14+
15+
@building_zone_id : String = "required"
16+
17+
def on_load
18+
on_update
19+
end
20+
21+
def on_update
22+
@building_zone_id = setting(String, :building_zone_id)
23+
end
24+
25+
@[Security(Level::Support)]
26+
def find_and_delete(past_days_to_search : UInt32 = 70_u32,
27+
days_after_visit_until_visitor_deletion : UInt32 = 60_u32,
28+
delete_guests : Bool = true,
29+
delete_visitor_bookings : Bool = true) : Nil
30+
now = Time.utc.to_unix
31+
from_epoch = now - past_days_to_search.days.to_i
32+
til_epoch = now - days_after_visit_until_visitor_deletion.days.to_i
33+
34+
find_and_delete_guests(from_epoch, til_epoch) if delete_guests
35+
find_and_delete_visitor_bookings(from_epoch, til_epoch) if delete_visitor_bookings
36+
end
37+
38+
private def find_and_delete_guests(from_epoch : Int64, til_epoch : Int64)
39+
guests = staff_api.query_guests(from_epoch, til_epoch, [@building_zone_id]).get.as_a
40+
guests.each { |g| delete_guest(g["id"].as_i) }
41+
end
42+
43+
private def find_and_delete_visitor_bookings(from_epoch : Int64, til_epoch : Int64)
44+
visitor_bookings = staff_api.query_bookings("visitor", from_epoch, til_epoch, [@building_zone_id]).get.as_a
45+
visitor_bookings.each { |b| delete_visitor_booking(b["id"].as_i) }
46+
end
47+
48+
private def delete_guest(id : Int32)
49+
staff_api.delete_guest(id)
50+
end
51+
52+
private def delete_visitor_booking(id : Int32)
53+
staff_api.booking_delete(id)
54+
end
55+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require "placeos-driver/spec"
2+
3+
DriverSpecs.mock_driver "Place::VisitorDeleter" do
4+
end

0 commit comments

Comments
 (0)