|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'grape' |
| 4 | +require 'time' |
| 5 | + |
| 6 | +class UnitHubApi < Grape::API |
| 7 | + helpers AuthenticationHelpers |
| 8 | + |
| 9 | + helpers do |
| 10 | + def hub_unit! |
| 11 | + UnitHub::Access.units_for(current_user).find(params[:unit_id]) |
| 12 | + end |
| 13 | + |
| 14 | + def manageable_hub_unit! |
| 15 | + unit = hub_unit! |
| 16 | + error!({ error: 'Only teaching staff assigned to this unit can manage its hub.' }, 403) unless UnitHub::Access.manage?(current_user, unit) |
| 17 | + unit |
| 18 | + end |
| 19 | + |
| 20 | + def hub_attributes(key, fields, date_fields: []) |
| 21 | + attributes = declared(params, include_missing: false).fetch(key).slice(*fields).to_h.symbolize_keys |
| 22 | + date_fields.each do |field| |
| 23 | + value = attributes[field] |
| 24 | + next unless value |
| 25 | + |
| 26 | + unless value.match?(/T.*(?:Z|[+-]\d{2}:\d{2})\z/) |
| 27 | + error!({ error: "#{field} must include an explicit time zone offset." }, 400) |
| 28 | + end |
| 29 | + attributes[field] = Time.iso8601(value) |
| 30 | + rescue ArgumentError |
| 31 | + error!({ error: "#{field} must be a valid ISO 8601 date and time." }, 400) |
| 32 | + end |
| 33 | + attributes |
| 34 | + end |
| 35 | + |
| 36 | + params :announcement_fields do |
| 37 | + requires :announcement, type: Hash do |
| 38 | + optional :title, type: String |
| 39 | + optional :body, type: String |
| 40 | + optional :source_url, type: String |
| 41 | + optional :pinned, type: Boolean |
| 42 | + optional :published_at, type: String |
| 43 | + optional :expires_at, type: String |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + params :session_fields do |
| 48 | + requires :session, type: Hash do |
| 49 | + optional :title, type: String |
| 50 | + optional :description, type: String |
| 51 | + optional :kind, type: String, values: UnitLearningSession::KINDS |
| 52 | + optional :start_at, type: String |
| 53 | + optional :end_at, type: String |
| 54 | + optional :timezone, type: String |
| 55 | + optional :location, type: String |
| 56 | + optional :join_url, type: String |
| 57 | + optional :source_url, type: String |
| 58 | + optional :published, type: Boolean |
| 59 | + optional :cancelled, type: Boolean |
| 60 | + optional :recurrence, type: String, values: UnitLearningSession::RECURRENCES |
| 61 | + optional :recurrence_until, type: Date |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + def announcement_attributes |
| 66 | + hub_attributes(:announcement, %i[title body source_url pinned published_at expires_at], date_fields: %i[published_at expires_at]) |
| 67 | + end |
| 68 | + |
| 69 | + def session_attributes |
| 70 | + hub_attributes(:session, %i[title description kind start_at end_at timezone location join_url source_url published cancelled recurrence recurrence_until], date_fields: %i[start_at end_at]) |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + before do |
| 75 | + authenticated? |
| 76 | + header 'Cache-Control', 'private, no-store' |
| 77 | + end |
| 78 | + |
| 79 | + desc 'Published announcements and learning sessions for the current user units' |
| 80 | + get '/unit_hub' do |
| 81 | + units = UnitHub::Access.units_for(current_user).order(:code, :id).to_a |
| 82 | + teams_configuration = UnitHub::Teams::Configuration.new |
| 83 | + announcements = UnitAnnouncement.where(unit_id: units.map(&:id)).visible_at(Time.current).includes(:unit).recent_first.limit(101).to_a |
| 84 | + from = 1.day.ago |
| 85 | + to = 90.days.from_now |
| 86 | + schedules = UnitLearningSession.where(unit_id: units.map(&:id), published: true) |
| 87 | + .where('start_at <= ?', to) |
| 88 | + .where('end_at >= ? OR recurrence_until >= ?', from, from.to_date) |
| 89 | + .includes(:unit) |
| 90 | + sessions = schedules.flat_map do |schedule| |
| 91 | + schedule.occurrences(from: from, to: to).map { |occurrence| UnitHub::Serializer.session(schedule, occurrence) } |
| 92 | + end |
| 93 | + sessions.sort_by! { |occurrence| [Time.iso8601(occurrence[:start_at]), occurrence[:occurrence_id]] } |
| 94 | + |
| 95 | + { |
| 96 | + units: units.map { |unit| { id: unit.id, code: unit.code, name: unit.name, can_manage: UnitHub::Access.manage?(current_user, unit), teams_sync: teams_configuration.configured_for?(unit.id) ? 'configured' : 'not_configured' } }, |
| 97 | + announcements: announcements.first(100).map { |record| UnitHub::Serializer.announcement(record) }, |
| 98 | + sessions: sessions, |
| 99 | + announcements_truncated: announcements.length > 100, |
| 100 | + window_start: from.iso8601, window_end: to.iso8601 |
| 101 | + } |
| 102 | + end |
| 103 | + |
| 104 | + resource :units do |
| 105 | + route_param :unit_id, type: Integer do |
| 106 | + resource :announcements do |
| 107 | + get do |
| 108 | + scope = manageable_hub_unit!.unit_announcements |
| 109 | + records = scope.where(source_provider: 'manual').or(scope.visible_at(Time.current)).includes(:unit).recent_first |
| 110 | + records.map { |record| UnitHub::Serializer.announcement(record) } |
| 111 | + end |
| 112 | + |
| 113 | + params { use :announcement_fields } |
| 114 | + post do |
| 115 | + record = manageable_hub_unit!.unit_announcements.create!(announcement_attributes.merge(author: current_user)) |
| 116 | + UnitHub::Serializer.announcement(record) |
| 117 | + end |
| 118 | + |
| 119 | + route_param :id, type: Integer do |
| 120 | + params { use :announcement_fields } |
| 121 | + put do |
| 122 | + record = manageable_hub_unit!.unit_announcements.find(params[:id]) |
| 123 | + error!({ error: 'Manage this imported announcement in Teams.' }, 403) if record.source_provider == 'microsoft_teams' |
| 124 | + record.update!(announcement_attributes) |
| 125 | + UnitHub::Serializer.announcement(record) |
| 126 | + end |
| 127 | + |
| 128 | + delete do |
| 129 | + record = manageable_hub_unit!.unit_announcements.find(params[:id]) |
| 130 | + error!({ error: 'Manage this imported announcement in Teams.' }, 403) if record.source_provider == 'microsoft_teams' |
| 131 | + record.destroy! |
| 132 | + { success: true } |
| 133 | + end |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + resource :sessions do |
| 138 | + get do |
| 139 | + records = manageable_hub_unit!.unit_learning_sessions.includes(:unit).order(:start_at, :id) |
| 140 | + records.map { |record| UnitHub::Serializer.session(record) } |
| 141 | + end |
| 142 | + |
| 143 | + params { use :session_fields } |
| 144 | + post do |
| 145 | + record = manageable_hub_unit!.unit_learning_sessions.create!(session_attributes.merge(author: current_user)) |
| 146 | + UnitHub::Serializer.session(record) |
| 147 | + end |
| 148 | + |
| 149 | + route_param :id, type: Integer do |
| 150 | + params { use :session_fields } |
| 151 | + put do |
| 152 | + record = manageable_hub_unit!.unit_learning_sessions.find(params[:id]) |
| 153 | + record.update!(session_attributes) |
| 154 | + UnitHub::Serializer.session(record) |
| 155 | + end |
| 156 | + |
| 157 | + delete do |
| 158 | + # Preserve the UID and schedule so subscribed calendars receive an |
| 159 | + # explicit cancellation instead of retaining an old working join link. |
| 160 | + record = manageable_hub_unit!.unit_learning_sessions.find(params[:id]) |
| 161 | + record.update!(cancelled: true) |
| 162 | + { success: true } |
| 163 | + end |
| 164 | + end |
| 165 | + end |
| 166 | + end |
| 167 | + end |
| 168 | +end |
0 commit comments