class Events

Defined in:

controllers/events.cr

Constant Summary

AFTER = {} of Nil => Nil
AROUND = {} of Nil => Nil
BEFORE = {determine_tenant_from_domain => {nil, nil}, configure_request_logging => {nil, nil}, check_jwt_scope => {nil, nil}} of Nil => Nil
FORCE = {} of Nil => Nil
LOCAL_AFTER = {} of Nil => Nil

function => options

LOCAL_AROUND = {} of Nil => Nil

function => options

LOCAL_BEFORE = {} of Nil => Nil

function => options

LOCAL_FORCE = {} of Nil => Nil

function => options

LOCAL_RESCUE = {} of Nil => Nil

function => options

LOCAL_ROUTES = {approve => {"post", "/:id/approve", nil, do update_status("accepted") end, false}, reject => {"post", "/:id/reject", nil, do update_status("declined") end, false}, guest_list => {"get", "/:id/guests", nil, do event_id = route_params["id"] if query_params["calendar"]? render(json: [] of Nil) end system_id = query_params["system_id"]? if system_id else render(:bad_request, json: {error: "missing system_id param"}) end cal_id = (get_placeos_client.systems.fetch(system_id)).email if cal_id else render(json: [] of Nil) end event = client.get_event(user.email, id: event_id, calendar_id: cal_id) if event else head(:not_found) end if (client.client_id == (:office365)) && (event.host != cal_id) event = get_hosts_event(event) event_id = event.id end metadata = get_event_metadata(event, system_id) parent_meta = metadata && (metadata.event_id != event.id) if metadata else render(json: [] of Nil) end visitors = metadata.attendees.to_a if visitors.empty? render(json: [] of Nil) end guests = visitors.each_with_object({} of String => Guest) do |visitor, obj| obj[visitor.guest.email.not_nil!] = visitor.guest end visitors = visitors.map do |visitor| attending_guest(visitor, guests[visitor.guest.email]?, parent_meta) end render(json: visitors) end, false}, guest_checkin => {"post", "/:id/guests/:guest_id/checkin", nil, do checkin = (query_params["state"]? || "true") == "true" event_id = route_params["id"] guest_id = route_params["guest_id"].downcase host_mailbox = query_params["host_mailbox"]?.try(&.downcase) if user_token.scope.includes?("guest") guest_event_id, system_id = user.roles guest_token_email = user.email.downcase if (event_id == guest_event_id) && (guest_id == guest_token_email) else head(:forbidden) end else system_id = query_params["system_id"]? if system_id else render(:bad_request, json: {error: "missing system_id param"}) end end guest_email = if guest_id.includes?('@') guest_id.strip.downcase else ((Guest.query.by_tenant(tenant.id)).find!(guest_id.to_i64)).email end system = get_placeos_client.systems.fetch(system_id) sys_email = system.email if sys_email else render(:not_found, json: {error: "system #{system_id} missing resource email"}) end cal_id = host_mailbox || sys_email event = client.get_event(user.email, id: event_id, calendar_id: cal_id) if event.nil? render(:not_found, json: {error: "event #{event_id} not found in #{cal_id}"}) end attendee = event.attendees.find do |attending| attending.email.downcase == guest_email end if attendee else head(:not_found) end guest = begin (Guest.query.by_tenant(tenant.id)).find!({email: guest_email}) rescue Clear::SQL::RecordNotFoundError g = Guest.new({tenant_id: tenant.id, email: guest_email, name: attendee.name, banned: false, dangerous: false, extension_data: JSON::Any.new({} of String => JSON::Any)}) if !g.save render(:unprocessable_entity, json: g.errors.map(&.to_s)) end g end if (client.client_id == (:office365)) && (event.host != cal_id) event = get_hosts_event(event) event_id = event.id.not_nil! end eventmeta = (get_migrated_metadata(event, system_id)) || (EventMetadata.create!({system_id: system.id.not_nil!, event_id: event.id.not_nil!, recurring_master_id: ( if event.recurring event.recurring_event_id || event.id end), event_start: event.event_start.not_nil!.to_unix, event_end: event.event_end.not_nil!.to_unix, resource_calendar: sys_email, host_email: event.host.not_nil!, tenant_id: tenant.id, ical_uid: event.ical_uid.not_nil!})) if attendee = (Attendee.query.by_tenant(tenant.id)).find({guest_id: guest.id, event_id: eventmeta.not_nil!.id}) attendee.update!({checked_in: checkin}) else attendee = Attendee.create!({event_id: eventmeta.id.not_nil!, guest_id: guest.id, visit_expected: true, checked_in: checkin, tenant_id: tenant.id}) end if event && (event.status != "cancelled") else render(:not_found, json: {error: "the event #{event_id} in the hosts calendar #{event.host} is cancelled"}) end spawn do get_placeos_client.root.signal("staff/guest/checkin", {action: :checkin, checkin: checkin, system_id: system_id, event_id: event_id, host: event.host, resource: eventmeta.resource_calendar, event_summary: event.not_nil!.body, event_starting: eventmeta.event_start, attendee_name: guest.name, attendee_email: guest.email, ext_data: eventmeta.ext_data}) end render(json: attending_guest(attendee, attendee.guest)) end, false}} of Nil => Nil

function => options

LOCAL_SKIP = {check_jwt_scope => {[show, guest_checkin], nil}} of Nil => Nil

function => options

NAMESPACE = ["/api/staff/v1/events"]

default namespace based on class

RESCUE = {Error::Unauthorized => {error_unauthorized, do |error| Log.debug do error.message end head(:unauthorized) end}, Error::Forbidden => {error_forbidden, do |error| Log.debug do error.inspect_with_backtrace end head(:forbidden) end}, Clear::SQL::RecordNotFoundError => {clear_sql_record_not_found_error, do |error| Log.debug do error.message end head(:not_found) end}, Clear::SQL::Error => {clear_sql_error, do |error| render_error(HTTP::Status::INTERNAL_SERVER_ERROR, error) end}, KeyError => {key_error, do |error| if error.message.try(&.includes?("param")) else raise(error) end respond_with(:bad_request) do text(error.message) json({error: error.message}) end end}, JSON::MappingError => {json_mapping_error, do |error| respond_with(:bad_request) do text(error.inspect_with_backtrace) json({error: error.message, backtrace: error.backtrace?}) end end}, ::PlaceOS::Client::API::Error => {_place_os_client_api_error, do |error| render_error(HTTP::Status::NOT_FOUND, error) end}, ::Enumerable::EmptyError => {_enumerable_empty_error, do |error| render_error(HTTP::Status::NOT_FOUND, error) end}, PlaceCalendar::Exception => {place_calendar_exception, do |error| render_error(HTTP::Status::INTERNAL_SERVER_ERROR, error, "#{error.http_body} \n #{error.inspect_with_backtrace}") end}} of Nil => Nil
ROUTES = {approve => {"post", "/:id/approve", nil, do update_status("accepted") end, false}, reject => {"post", "/:id/reject", nil, do update_status("declined") end, false}, guest_list => {"get", "/:id/guests", nil, do event_id = route_params["id"] if query_params["calendar"]? render(json: [] of Nil) end system_id = query_params["system_id"]? if system_id else render(:bad_request, json: {error: "missing system_id param"}) end cal_id = (get_placeos_client.systems.fetch(system_id)).email if cal_id else render(json: [] of Nil) end event = client.get_event(user.email, id: event_id, calendar_id: cal_id) if event else head(:not_found) end if (client.client_id == (:office365)) && (event.host != cal_id) event = get_hosts_event(event) event_id = event.id end metadata = get_event_metadata(event, system_id) parent_meta = metadata && (metadata.event_id != event.id) if metadata else render(json: [] of Nil) end visitors = metadata.attendees.to_a if visitors.empty? render(json: [] of Nil) end guests = visitors.each_with_object({} of String => Guest) do |visitor, obj| obj[visitor.guest.email.not_nil!] = visitor.guest end visitors = visitors.map do |visitor| attending_guest(visitor, guests[visitor.guest.email]?, parent_meta) end render(json: visitors) end, false}, guest_checkin => {"post", "/:id/guests/:guest_id/checkin", nil, do checkin = (query_params["state"]? || "true") == "true" event_id = route_params["id"] guest_id = route_params["guest_id"].downcase host_mailbox = query_params["host_mailbox"]?.try(&.downcase) if user_token.scope.includes?("guest") guest_event_id, system_id = user.roles guest_token_email = user.email.downcase if (event_id == guest_event_id) && (guest_id == guest_token_email) else head(:forbidden) end else system_id = query_params["system_id"]? if system_id else render(:bad_request, json: {error: "missing system_id param"}) end end guest_email = if guest_id.includes?('@') guest_id.strip.downcase else ((Guest.query.by_tenant(tenant.id)).find!(guest_id.to_i64)).email end system = get_placeos_client.systems.fetch(system_id) sys_email = system.email if sys_email else render(:not_found, json: {error: "system #{system_id} missing resource email"}) end cal_id = host_mailbox || sys_email event = client.get_event(user.email, id: event_id, calendar_id: cal_id) if event.nil? render(:not_found, json: {error: "event #{event_id} not found in #{cal_id}"}) end attendee = event.attendees.find do |attending| attending.email.downcase == guest_email end if attendee else head(:not_found) end guest = begin (Guest.query.by_tenant(tenant.id)).find!({email: guest_email}) rescue Clear::SQL::RecordNotFoundError g = Guest.new({tenant_id: tenant.id, email: guest_email, name: attendee.name, banned: false, dangerous: false, extension_data: JSON::Any.new({} of String => JSON::Any)}) if !g.save render(:unprocessable_entity, json: g.errors.map(&.to_s)) end g end if (client.client_id == (:office365)) && (event.host != cal_id) event = get_hosts_event(event) event_id = event.id.not_nil! end eventmeta = (get_migrated_metadata(event, system_id)) || (EventMetadata.create!({system_id: system.id.not_nil!, event_id: event.id.not_nil!, recurring_master_id: ( if event.recurring event.recurring_event_id || event.id end), event_start: event.event_start.not_nil!.to_unix, event_end: event.event_end.not_nil!.to_unix, resource_calendar: sys_email, host_email: event.host.not_nil!, tenant_id: tenant.id, ical_uid: event.ical_uid.not_nil!})) if attendee = (Attendee.query.by_tenant(tenant.id)).find({guest_id: guest.id, event_id: eventmeta.not_nil!.id}) attendee.update!({checked_in: checkin}) else attendee = Attendee.create!({event_id: eventmeta.id.not_nil!, guest_id: guest.id, visit_expected: true, checked_in: checkin, tenant_id: tenant.id}) end if event && (event.status != "cancelled") else render(:not_found, json: {error: "the event #{event_id} in the hosts calendar #{event.host} is cancelled"}) end spawn do get_placeos_client.root.signal("staff/guest/checkin", {action: :checkin, checkin: checkin, system_id: system_id, event_id: event_id, host: event.host, resource: eventmeta.resource_calendar, event_summary: event.not_nil!.body, event_starting: eventmeta.event_start, attendee_name: guest.name, attendee_email: guest.email, ext_data: eventmeta.ext_data}) end render(json: attending_guest(attendee, attendee.guest)) end, false}, index => {"get", "/", nil, nil, false}, create => {"post", "/", nil, nil, false}, update => {"patch", "/:id", nil, nil, false}, show => {"get", "/:id", nil, nil, false}, destroy => {"delete", "/:id", nil, nil, false}} of Nil => Nil
SKIP = {check_jwt_scope => {[show, guest_checkin], nil}} of Nil => Nil

Class Method Summary

Instance Method Summary

Macro Summary

Class methods inherited from class Application

context(method : String, route : String, route_params : Hash(String, String) | Nil = nil, headers : Hash(String, String) | Nil = nil, body : String | Bytes | IO | Nil = nil, &) context

Instance methods inherited from module Utils::MultiTenant

client : PlaceCalendar::Client client, tenant tenant

Instance methods inherited from module Utils::CurrentUser

authorize! authorize!, check_admin check_admin, check_support check_support, is_admin? is_admin?, is_support? is_support?, user user, user_token : UserJWT user_token

Instance methods inherited from module Utils::PlaceOSHelpers

check_access(groups : Array(String), check : Array(String)) check_access, get_placeos_client : PlaceOS::Client get_placeos_client, get_user_calendars get_user_calendars, matching_calendar_ids(allow_default = false) matching_calendar_ids

Class Method Detail

def self.__init_routes__(router) #

Routes call the functions generated above


def self.__route_list__ #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.approve(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Helper methods for performing redirect_to calls


def self.base_route #

Helper for obtaining base route


def self.context(method : String, route : String, route_params : Hash(String, String) | Nil = nil, headers : Hash(String, String) | Nil = nil, body : String | Bytes | IO | Nil = nil, &) #

[View source]
def self.create(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.delete__api_staff_v1_events__id(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.destroy(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.get__api_staff_v1_events_(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.get__api_staff_v1_events__id(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.get__api_staff_v1_events__id_guests(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.guest_checkin(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.guest_list(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.index(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.patch__api_staff_v1_events__id(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.post__api_staff_v1_events_(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.post__api_staff_v1_events__id_approve(context, head_request) #

Generate functions for each route


def self.post__api_staff_v1_events__id_guests__guest_id_checkin(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.post__api_staff_v1_events__id_reject(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.reject(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.show(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.update(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


Instance Method Detail

def _enumerable_empty_error(error) #

def _place_os_client_api_error(error) #

def approve #

Create functions for named routes Skip the CRUD verbs, as they are already defined Annotations defined via the annotations argument are applied here


def base_route(*args, **options) #

Helper for obtaining base route


def base_route(*args, **options, &) #

Helper for obtaining base route


def clear_sql_error(error) #

def clear_sql_record_not_found_error(error) #

def create #

ameba:disable Metrics/CyclomaticComplexity


[View source]
def destroy #

ameba:disable Metrics/CyclomaticComplexity


[View source]
def error_forbidden(error) #

def error_unauthorized(error) #

Create functions as required for errors Skip the generating methods for existing handlers


def guest_checkin #

def guest_list #

def index #

ameba:disable Metrics/CyclomaticComplexity


[View source]
def json_mapping_error(error) #

def key_error(error) #

def place_calendar_exception(error) #

def reject #

def show #

[View source]
def update #

ameba:disable Metrics/CyclomaticComplexity


[View source]

Macro Detail

macro __inherit_after_filters__ #

macro __inherit_around_filters__ #

macro __inherit_before_filters__ #

macro __inherit_force_filters__ #

macro __inherit_rescue_filters__ #

macro __inherit_routes_filters__ #

macro __inherit_skip_filters__ #