diff --git a/app/controllers/v1/activities_controller.rb b/app/controllers/v1/activities_controller.rb index f9551525..b51904d5 100644 --- a/app/controllers/v1/activities_controller.rb +++ b/app/controllers/v1/activities_controller.rb @@ -18,13 +18,25 @@ def generate_alias render json: alias_response("#{mail_alias}@csvalpha.nl") end - def ical # rubocop:disable Metrics/AbcSize, Metrics/MethodLength + def ical # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity return head :unauthorized unless authenticate_user_by_ical_secret_key - requested_categories = params[:categories].try(:split, ',') + stored_categories = @user.ical_categories + + requested_categories = params[:categories]&.split(',') + + permitted_categories = [] + + if stored_categories.empty? && requested_categories.present? + # this logic is only to store preferences on first use. this makes it so the end user doesn't notice the change + new_categories_to_store = requested_categories & Activity.categories + @user.update(ical_categories: new_categories_to_store) + permitted_categories = new_categories_to_store + else + permitted_categories = stored_categories & Activity.categories + end + permitted_categories = Activity.categories if permitted_categories.empty? - permitted_categories = (requested_categories & Activity.categories) || - Activity.categories activities_for_ical(permitted_categories).each do |act| calendar.add_event(act.to_ical) end diff --git a/app/controllers/v1/users_controller.rb b/app/controllers/v1/users_controller.rb index ac0e90e3..2f242e68 100644 --- a/app/controllers/v1/users_controller.rb +++ b/app/controllers/v1/users_controller.rb @@ -110,7 +110,7 @@ def context def excluded_display_properties %i[created_at updated_at deleted_at activated_at archived_at password_digest activation_token avatar activation_token_valid_till setup_complete otp_secret_key otp_required - ical_secret_key id] + ical_secret_key ical_categories id] end def otp_already_required_error diff --git a/app/models/activity.rb b/app/models/activity.rb index 6b3de661..083d59e2 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -34,8 +34,8 @@ class Activity < ApplicationRecord after_save :copy_author_and_group_to_form! def self.categories - %w[algemeen societeit vorming dinsdagkring woensdagkring - choose ifes ozon disputen kiemgroepen huizen extern eerstejaars curiositates] + %w[algemeen societeit vorming kring + choose ifes ozon disputen genootschapen huizen extern eerstejaars] end def full_day? diff --git a/app/resources/v1/user_resource.rb b/app/resources/v1/user_resource.rb index 5782f475..cc5dc867 100644 --- a/app/resources/v1/user_resource.rb +++ b/app/resources/v1/user_resource.rb @@ -4,7 +4,7 @@ class V1::UserResource < V1::ApplicationResource # rubocop:disable Metrics/Class :ifes_data_sharing_preference, :info_in_almanak, :almanak_subscription_preference, :digtus_subscription_preference, :email, :birthday, :address, :postcode, :city, :phone_number, :food_preferences, :vegetarian, :study, :start_study, - :picture_publication_preference, :ical_secret_key, + :picture_publication_preference, :ical_secret_key, :ical_categories, :password, :avatar, :avatar_url, :avatar_thumb_url, :user_details_sharing_preference, :allow_sofia_sharing, :trailer_drivers_license, :sidekiq_access, :setup_complete @@ -51,7 +51,8 @@ def fetchable_fields # Relationships allowed_keys += %i[groups active_groups memberships mail_aliases mandates group_mail_aliases permissions photos user_permissions] - allowed_keys += %i[ical_secret_key] if me? + # Ical fields + allowed_keys += %i[ical_secret_key ical_categories] if me? if update_or_me? allowed_keys += %i[login_enabled otp_required activated_at emergency_contact emergency_number ifes_data_sharing_preference info_in_almanak @@ -78,7 +79,8 @@ def self.creatable_fields(context) # rubocop:disable Metrics/MethodLength attributes += %i[otp_required password user_details_sharing_preference allow_sofia_sharing picture_publication_preference info_in_almanak - ifes_data_sharing_preference sidekiq_access setup_complete] + ifes_data_sharing_preference ical_categories sidekiq_access + setup_complete] end if user_can_create_or_update?(context) diff --git a/db/migrate/20251103104056_simplyfing_calender_options.rb b/db/migrate/20251103104056_simplyfing_calender_options.rb new file mode 100644 index 00000000..4a5207ab --- /dev/null +++ b/db/migrate/20251103104056_simplyfing_calender_options.rb @@ -0,0 +1,19 @@ +class SimplyfingCalenderOptions < ActiveRecord::Migration[7.2] + # rubocop:disable Rails/SkipsModelValidations + def up + add_column :users, :ical_categories, :string, array: true, default: [] + Activity.where(category: 'dinsdagkring').update_all(category: 'kring') + Activity.where(category: 'woensdagkring').update_all(category: 'kring') + Activity.where(category: 'kiemgroepen').update_all(category: 'algemeen') + Activity.where(category: 'curiositates').update_all(category: 'algemeen') + end + + def down + remove_column :users, :ical_categories + Activity.where(category: 'kring').update_all(category: 'dinsdagkring') + # NOTE: As mentioned before, reverting 'algemeen' to 'kiemgroepen' or 'curiositates' + # cannot be done reliably with `update_all` without additional information. + # The `down` migration here only addresses the 'kring' category. + end + # rubocop:enable Rails/SkipsModelValidations +end diff --git a/db/schema.rb b/db/schema.rb index 20015070..1812a52a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2025_03_10_235232) do +ActiveRecord::Schema[7.1].define(version: 2025_11_03_104056) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -562,6 +562,7 @@ t.string "nickname" t.boolean "trailer_drivers_license", default: false, null: false t.boolean "setup_complete", default: false, null: false + t.string "ical_categories", default: [], array: true t.index ["deleted_at"], name: "index_users_on_deleted_at" t.index ["email"], name: "index_users_on_email", unique: true t.index ["login_enabled"], name: "index_users_on_login_enabled" diff --git a/spec/factories/activities.rb b/spec/factories/activities.rb index 2d5a5df4..63692b97 100644 --- a/spec/factories/activities.rb +++ b/spec/factories/activities.rb @@ -10,8 +10,8 @@ start_time { Faker::Time.between(from: 1.day.ago, to: Time.zone.today) } end_time { Faker::Time.between(from: 1.day.from_now, to: 2.days.from_now) } category do - %w[algemeen societeit vorming dinsdagkring woensdagkring - choose ifes ozon disputen kiemgroepen huizen extern eerstejaars curiositates].sample + %w[algemeen societeit vorming kring + choose ifes ozon disputen genootschapen huizen extern eerstejaars].sample end publicly_visible { false } diff --git a/spec/models/activity_spec.rb b/spec/models/activity_spec.rb index ff486844..e761f715 100644 --- a/spec/models/activity_spec.rb +++ b/spec/models/activity_spec.rb @@ -179,8 +179,8 @@ context 'when it is another category' do let(:record) do build_stubbed(:activity, - category: %w[algemeen sociƫteit vorming dinsdagkring woensdagkring - disputen kiemgroepen huizen extern curiositates].sample) + category: %w[algemeen sociƫteit vorming kring + disputen genootschapen huizen extern].sample) end it { expect(record.humanized_category).to eq record.category.capitalize } diff --git a/spec/resources/v1/user_resource_spec.rb b/spec/resources/v1/user_resource_spec.rb index 2dd32966..3b3bb74b 100644 --- a/spec/resources/v1/user_resource_spec.rb +++ b/spec/resources/v1/user_resource_spec.rb @@ -99,7 +99,7 @@ let(:another_user) { user } let(:fields) do basic_fields + update_fields + - read_fields + user_details_fields + %i[ical_secret_key] + read_fields + user_details_fields + %i[ical_secret_key ical_categories] end it { expect(resource.fetchable_fields).to match_array(fields) } @@ -155,7 +155,7 @@ context 'when record is current user' do let(:another_user) { user } - it { expect(creatable_fields).to match_array(basic_fields + current_user_fields) } + it { expect(creatable_fields).to match_array(basic_fields + current_user_fields + %i[ical_categories]) } end context 'when with create permisison' do @@ -170,7 +170,7 @@ it { expect(creatable_fields).to match_array(basic_fields + permissible_fields + - current_user_fields - [:login_enabled]) + current_user_fields - [:login_enabled] + %i[ical_categories]) } end end