Skip to content

Commit

Permalink
fix(events): [PPT-1473] return most detailed metadata (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
chillfox authored Dec 18, 2024
1 parent 50edc8f commit 5ae5218
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
66 changes: 66 additions & 0 deletions spec/controllers/events_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,72 @@ describe Events do
body = client.get("#{EVENTS_BASE}/?period_start=#{now}&period_end=#{later}", headers: headers).body
body.includes?(%("recurring_master_id": "#{master_event_id}"))
end

it "#index should return a list of events with the most detailed metadata" do
WebMock.stub(:get, "https://graph.microsoft.com/v1.0/users/dev%40acaprojects.onmicrosoft.com/calendars")
.to_return(body: File.read("./spec/fixtures/calendars/o365/show.json"))

WebMock.stub(:get, "#{ENV["PLACE_URI"]}/api/engine/v2/systems?limit=1000&offset=0&zone_id=z1")
.to_return(body: File.read("./spec/fixtures/placeos/systems.json"))
WebMock.stub(:post, "https://graph.microsoft.com/v1.0/%24batch")
.to_return(body: File.read("./spec/fixtures/events/o365/batch_index.json"))

# from ./spec/fixtures/events/o365/batch_index.json
ical_uid = "040000008200E00074C5B7101A82E008000000008CD0441F4E7FD60100000000000000001000000087A54520ECE5BD4AA552D826F3718E7F"
event_id = "AAMkADE3YmQxMGQ2LTRmZDgtNDljYy1hNDg1LWM0NzFmMGI0ZTQ3YgBGAAAAAADFYQb3DJ_xSJHh14kbXHWhBwB08dwEuoS_QYSBDzuv558sAAAAAAENAAB08dwEuoS_QYSBDzuv558sAAB8_ORMAAA="

tenant = get_tenant
event_calendar = "[email protected]"
event_start = Random.new.rand(5..19).minutes.from_now.to_unix
event_end = Random.new.rand(25..79).minutes.from_now.to_unix

# 3 metadata records are created for shared events.
# 1. with no extension data
# 2. with extension data
# 3. with no extension data but with a different room email

metadata = EventMetadatasHelper.create_event(
tenant_id: tenant.id,
id: event_id,
event_start: event_start,
event_end: event_end,
room_email: event_calendar,
ext_data: nil,
ical_uid: ical_uid
)

metadata_with_ext_data = EventMetadatasHelper.create_event(
tenant_id: tenant.id,
id: event_id,
event_start: event_start,
event_end: event_end,
system_id: metadata.system_id,
room_email: event_calendar,
host: metadata.host_email,
ext_data: JSON.parse(%({"shared_event": true, "attendance_type": "ONSITE"})),
ical_uid: ical_uid,
permission: metadata.permission
)

metadata_room_calendar = EventMetadatasHelper.create_event(
tenant_id: tenant.id,
id: event_id,
event_start: event_start,
event_end: event_end,
system_id: metadata.system_id,
room_email: "[email protected]",
host: metadata.host_email,
ext_data: nil,
ical_uid: ical_uid,
permission: metadata.permission
)

body = JSON.parse(client.get("#{EVENTS_BASE}?zone_ids=z1&period_start=#{event_start}&period_end=#{event_end}", headers: headers).body).as_a

body.size.should eq(1)
body.first["ical_uid"].should eq(ical_uid)
body.first["extension_data"].should eq(JSON.parse(%({"shared_event": true, "attendance_type": "ONSITE"})))
end
end

describe "#create" do
Expand Down
14 changes: 13 additions & 1 deletion src/controllers/events.cr
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,20 @@ class Events < Application
# where there might be multiple resource calendars on the event we want to pick
# metadatas that most closely match the request
# and has the most information
#
# meta.ext_data is a JSON::Any | Nil type which can contain either nil or a hash.
# This means that nil checks can be unreliable and we should use the `==` or `!=` operator
# instead of `meta.ext_data.nil?` or `meta.ext_data`.
#
# meta.ext_data.nil? # => false
# meta.ext_data == nil # => true
# typeof(meta.ext_data) # => (JSON::Any | Nil)
# meta.ext_data.class # => JSON::Any
# meta.ext_data # => nil
#
#
next if (existing = metadatas[meta.ical_uid]?) && calendars[existing.resource_calendar]? &&
!(calendars[meta.resource_calendar]? && meta.ext_data)
!(calendars[meta.resource_calendar]? && meta.ext_data != nil)

metadatas[meta.ical_uid] = meta
if recurring_master_id = meta.recurring_master_id
Expand Down

0 comments on commit 5ae5218

Please sign in to comment.