Skip to content

Commit 2ffc086

Browse files
authored
feat(PPT-1348): Add functionality to fetch list of guests (#514)
1 parent 90ef38d commit 2ffc086

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

drivers/place/attendee_scanner.cr

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
require "placeos-driver"
2+
require "json"
3+
4+
class Place::AttendeeScanner < PlaceOS::Driver
5+
descriptive_name "PlaceOS Room Events"
6+
generic_name :AttendeeScanner
7+
8+
accessor staff_api : StaffAPI_1
9+
10+
default_settings({
11+
internal_domains: [] of String,
12+
})
13+
14+
getter systems : Hash(String, Array(String)) { get_systems_list.not_nil! }
15+
getter internal_domains : Array(String) = [] of String
16+
17+
def on_update
18+
@internal_domains = setting(Array(String), :internal_domains)
19+
end
20+
21+
# Grabs the list of systems in the building
22+
def get_systems_list
23+
building_id = system[:Location].building_id.get
24+
system["StaffAPI"].systems_in_building(building_id).get.as_h.transform_values(&.as_a.map(&.as_s))
25+
rescue error
26+
logger.warn(exception: error) { "unable to obtain list of systems in the building" }
27+
nil
28+
end
29+
30+
# Lists external guests based on email domains
31+
def list_external_guests
32+
values = [] of Hash(String, JSON::Any)
33+
34+
systems.each do |level_id, system_ids|
35+
system_ids.each do |system_id|
36+
period_start = Time.local.to_unix
37+
period_end = Time.local.to_unix + 86400
38+
39+
events = staff_api.query_events(period_start, period_end, systems: [system_id]).get
40+
41+
events.as_a.each do |event|
42+
domain = event.as_h.["host"].as_s.split("@").last
43+
44+
unless internal_domains.includes?(domain)
45+
event_id = event.as_h.["id"].as_s
46+
ical_uid = event.as_h.["ical_uid"].as_s
47+
48+
bookings = staff_api.query_bookings(event_id: event_id, ical_uid: ical_uid).get
49+
50+
attendee_emails = event["attendees"].as_a.map do |attendee|
51+
attendee.as_h.["email"].as_s
52+
end
53+
54+
bookings.as_a.each do |booking|
55+
booking = booking.as_h
56+
57+
booking.["attendees"].as_a.each do |attendee|
58+
attendee_email = attendee.as_h.["email"].as_s
59+
60+
unless attendee_emails.includes?(attendee_email)
61+
staff_api.create_booking(
62+
booking_type: "visitor",
63+
asset_id: attendee_email,
64+
user_id: attendee_email,
65+
user_email: attendee_email,
66+
user_name: booking["user_name"].as_s,
67+
zones: booking["zones"].as_a?,
68+
booking_start: booking["booking_start"].as_s?,
69+
booking_end: booking["booking_end"].as_s?,
70+
checked_in: booking["checked_in"].as_bool?,
71+
approved: booking["approved"].as_bool?,
72+
title: booking["title"].as_s?,
73+
description: booking["description"].as_s?,
74+
time_zone: booking["time_zone"].as_s?,
75+
extension_data: booking["extension_data"]?,
76+
utm_source: nil,
77+
limit_override: nil,
78+
event_id: event_id,
79+
ical_uid: ical_uid,
80+
attendees: nil
81+
).get
82+
83+
values.push({
84+
"event_id" => event.as_h.["id"],
85+
"ical_uid" => event.as_h.["ical_uid"],
86+
"booking" => JSON::Any.new(booking),
87+
})
88+
end
89+
end
90+
end
91+
end
92+
end
93+
end
94+
end
95+
96+
values
97+
end
98+
end

drivers/place/staff_api.cr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,8 @@ class Place::StaffAPI < PlaceOS::Driver
548548
extension_data : JSON::Any? = nil,
549549
utm_source : String? = nil,
550550
limit_override : Int64? = nil,
551+
event_id : String? = nil,
552+
ical_uid : String? = nil,
551553
attendees : Array(PlaceCalendar::Event::Attendee)? = nil
552554
)
553555
now = time_zone ? Time.local(Time::Location.load(time_zone)) : Time.local
@@ -561,6 +563,8 @@ class Place::StaffAPI < PlaceOS::Driver
561563
params = URI::Params.build do |form|
562564
form.add "utm_source", utm_source.to_s unless utm_source.nil?
563565
form.add "limit_override", limit_override.to_s unless limit_override.nil?
566+
form.add "event_id", event_id.to_s unless event_id.nil?
567+
form.add "ical_uid", ical_uid.to_s unless ical_uid.nil?
564568
end
565569

566570
response = post("/api/staff/v1/bookings?#{params}", headers: authentication, body: {
@@ -693,6 +697,8 @@ class Place::StaffAPI < PlaceOS::Driver
693697
user : String? = nil,
694698
email : String? = nil,
695699
state : String? = nil,
700+
event_id : String? = nil,
701+
ical_uid : String? = nil,
696702
created_before : Int64? = nil,
697703
created_after : Int64? = nil,
698704
approved : Bool? = nil,
@@ -719,6 +725,8 @@ class Place::StaffAPI < PlaceOS::Driver
719725
form.add "approved", approved.to_s unless approved.nil?
720726
form.add "rejected", rejected.to_s unless rejected.nil?
721727
form.add "checked_in", checked_in.to_s unless checked_in.nil?
728+
form.add "event_id", event_id.to_s if event_id.presence
729+
form.add "ical_uid", ical_uid.to_s if ical_uid.presence
722730
form.add "include_checked_out", include_checked_out.to_s unless include_checked_out.nil?
723731

724732
if extension_data

0 commit comments

Comments
 (0)