From 5377320cecfa6b28022246eb172d27146d316da8 Mon Sep 17 00:00:00 2001 From: SilasPeters <69711739+SilasPeters@users.noreply.github.com> Date: Sat, 25 May 2024 13:47:39 +0200 Subject: [PATCH] chore: expanded database, processed TODOs and handled exceptiosn --- app/controllers/api/calendars_controller.rb | 33 ++++++++----------- .../src/members/activities/activities.js | 2 +- app/models/activity.rb | 6 ++-- config/locales/en.yml | 2 +- config/locales/nl.yml | 2 +- ...0240521180025_add_calendar_id_to_member.rb | 3 +- lib/icalendar_helper.rb | 5 +-- 7 files changed, 22 insertions(+), 31 deletions(-) diff --git a/app/controllers/api/calendars_controller.rb b/app/controllers/api/calendars_controller.rb index 3141a5f2e..631c67eab 100644 --- a/app/controllers/api/calendars_controller.rb +++ b/app/controllers/api/calendars_controller.rb @@ -2,18 +2,15 @@ class Api::CalendarsController < ActionController::Base # supply the personalised iCal feed from the user - # TODO supporting WebDAV might be nat, but not needed def show - # TODO reservist marker - # TODO only display future activities option - # TODO multilingual/optional description + @member = Member.find_by(calendar_id: params[:calendar_id]) + # member variable will be accessible in other methods as well now - # respond_to do |format| - # format.ics { - # render plain: create_calendar, - # content_type: 'text/calendar' - # } - # end + if not @member # No member with the specified hash was found + render json: { error: "Unkown hash" }, status: :not_found + return + end + respond_to do |format| format.ics { send_data create_personal_calendar, @@ -22,23 +19,21 @@ def show filename: "#{@member.first_name}_activities.ics" } end - - - # send_file calendar_path, type: 'text/calendar', disposition: 'attachment' - # else - # render json: { error: "Unkown hash" }, status: :not_found - # end TODO 500 error if @member is empty end def index - @member = Member.find(current_user.credentials_id) # TODO gives 500 error when not logged in + if current_user.nil? + render json: { error: "Not logged in" }, status: :forbidden + return + end # TODO heb je overal dit soort error handling? + + @member = Member.find(current_user.credentials_id) render plain: "https://koala.svsticky.nl/api/calendar/pull/#{@member.calendar_id}" # TODO can this less hard-coded? end # Not exposed to API directly, but through #show def create_personal_calendar - @member = Member.find_by(calendar_id: params[:calendar_id]) - @locale = I18n.locale # TODO werkt dit? + @locale = I18n.locale # Convert activities to events, and mark activities where the member is # is enrolled as reservist diff --git a/app/javascript/src/members/activities/activities.js b/app/javascript/src/members/activities/activities.js index 500dd141b..0f5bbe9ec 100644 --- a/app/javascript/src/members/activities/activities.js +++ b/app/javascript/src/members/activities/activities.js @@ -29,7 +29,7 @@ function copyPersonalICSToClipboard() { }).catch((error) => { console.log(error) }); -} +} // TODO makes an API call even if the button is not pressed export function get_activity_container() { return $("#activity-container"); diff --git a/app/models/activity.rb b/app/models/activity.rb index 6ea8e23c1..1b728d645 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -360,7 +360,7 @@ def activity_url return "https://koala.svsticky.nl/activities/#{ id }" end - def descrption_localised(locale) + def description_localised(locale) return locale == :nl ? description_nl : description_en end @@ -371,7 +371,7 @@ def google_event(loc = nil) fmt_dt = ->(dt) { dt.utc.strftime('%Y%m%dT%H%M%SZ') } loc = I18n.locale if loc.nil? - description = "#{ activity_url }\n\n#{ descrption_localised(loc) }" + description = "#{ activity_url }\n\n#{ description_localised(loc) }" uri_name = URI.encode_www_form_component(name) uri_description = URI.encode_www_form_component(description) uri_location = URI.encode_www_form_component(location) @@ -398,7 +398,7 @@ def whatsapp_message(loc) location: location, price: pc, url: activity_url, - description: descrption_localised(loc), + description: description_localised(loc), locale: loc) end diff --git a/config/locales/en.yml b/config/locales/en.yml index 62cd87665..18ba86a3c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -35,7 +35,7 @@ en: description_nl: Dutch description end_date: Enddate end_time: Endtime - google_event: Add to calendar + google_event: Copy once to calendar is_alcoholic: Alcoholic(18+) is_enrollable: Enrollable is_freshmans: First year students diff --git a/config/locales/nl.yml b/config/locales/nl.yml index f294877eb..97190916e 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -35,7 +35,7 @@ nl: description_nl: Nederlandse beschrijving end_date: Einddatum end_time: Eindtijd - google_event: Voeg toe aan je agenda + google_event: Kopiƫer eenmalig naar kalender is_alcoholic: Alcohol (18+) is_enrollable: Inschrijfbaar is_freshmans: Eerstejaars diff --git a/db/migrate/20240521180025_add_calendar_id_to_member.rb b/db/migrate/20240521180025_add_calendar_id_to_member.rb index d9fa39ca1..84b51a4fc 100644 --- a/db/migrate/20240521180025_add_calendar_id_to_member.rb +++ b/db/migrate/20240521180025_add_calendar_id_to_member.rb @@ -8,12 +8,11 @@ def up record.update_columns(calendar_id: SecureRandom.uuid) end - # Add a unique index to the UUID column, because why not + # Add an index to the UUID column, because why not add_index :members, :calendar_id, unique: true end def down - # Remove the UUID column remove_column :members, :calendar_id end end diff --git a/lib/icalendar_helper.rb b/lib/icalendar_helper.rb index 9041e1772..7e9a6f1a7 100644 --- a/lib/icalendar_helper.rb +++ b/lib/icalendar_helper.rb @@ -9,7 +9,7 @@ def IcalendarHelper.activityToEvent(activity, locale) event.dtstart = activity.start_date event.dtend = activity.end_date event.summary = activity.name - event.description = activity.descrption_localised(locale) + event.description = activity.description_localised(locale) event.location = activity.location return event end @@ -38,8 +38,5 @@ def IcalendarHelper.createFile(calendar, path) file.write(calendar_string) end end - - # TODO allow updating a calendar file, to preserve uids. - # This enables the calendar client to recognise which events are new end