From 33c68b28b6abaa54389b50f44ab06ab5bcba2d8f Mon Sep 17 00:00:00 2001 From: Jan Cernik <66427541+jancernik@users.noreply.github.com> Date: Fri, 5 Jul 2024 00:00:24 -0300 Subject: [PATCH] DEV: Use serializers for `user_notification_schedule` and `featured_topic` (#27719) Co-authored-by: Alan Guo Xiang Tan --- app/serializers/current_user_serializer.rb | 2 +- app/serializers/user_card_serializer.rb | 2 +- .../user_notification_schedule_serializer.rb | 21 +++++++++++++++++ app/serializers/user_serializer.rb | 6 ++++- .../current_user_serializer_spec.rb | 21 +++++++++++++++++ spec/serializers/user_card_serializer_spec.rb | 23 +++++++++++++++++++ spec/serializers/user_serializer_spec.rb | 15 ++++++++++++ 7 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 app/serializers/user_notification_schedule_serializer.rb diff --git a/app/serializers/current_user_serializer.rb b/app/serializers/current_user_serializer.rb index fb0433f44df..c9007caa33f 100644 --- a/app/serializers/current_user_serializer.rb +++ b/app/serializers/current_user_serializer.rb @@ -294,7 +294,7 @@ class CurrentUserSerializer < BasicUserSerializer end def featured_topic - object.user_profile.featured_topic + BasicTopicSerializer.new(object.user_profile.featured_topic, scope: scope, root: false).as_json end def has_topic_draft diff --git a/app/serializers/user_card_serializer.rb b/app/serializers/user_card_serializer.rb index 006f310ea4e..69cdddf370b 100644 --- a/app/serializers/user_card_serializer.rb +++ b/app/serializers/user_card_serializer.rb @@ -214,7 +214,7 @@ class UserCardSerializer < BasicUserSerializer end def featured_topic - object.user_profile.featured_topic + BasicTopicSerializer.new(object.user_profile.featured_topic, scope: scope, root: false).as_json end def include_timezone? diff --git a/app/serializers/user_notification_schedule_serializer.rb b/app/serializers/user_notification_schedule_serializer.rb new file mode 100644 index 00000000000..94b3d470506 --- /dev/null +++ b/app/serializers/user_notification_schedule_serializer.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +class UserNotificationScheduleSerializer < ApplicationSerializer + attributes :id, + :user_id, + :enabled, + :day_0_start_time, + :day_0_end_time, + :day_1_start_time, + :day_1_end_time, + :day_2_start_time, + :day_2_end_time, + :day_3_start_time, + :day_3_end_time, + :day_4_start_time, + :day_4_end_time, + :day_5_start_time, + :day_5_end_time, + :day_6_start_time, + :day_6_end_time +end diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb index 070f57e3e44..e4826bb727d 100644 --- a/app/serializers/user_serializer.rb +++ b/app/serializers/user_serializer.rb @@ -77,7 +77,11 @@ class UserSerializer < UserCardSerializer ### # def user_notification_schedule - object.user_notification_schedule || UserNotificationSchedule::DEFAULT + UserNotificationScheduleSerializer.new( + object.user_notification_schedule, + scope: scope, + root: false, + ).as_json || UserNotificationSchedule::DEFAULT end def mailing_list_posts_per_day diff --git a/spec/serializers/current_user_serializer_spec.rb b/spec/serializers/current_user_serializer_spec.rb index 5fa3074f6c8..edb3b2999c6 100644 --- a/spec/serializers/current_user_serializer_spec.rb +++ b/spec/serializers/current_user_serializer_spec.rb @@ -352,4 +352,25 @@ RSpec.describe CurrentUserSerializer do expect(initial_count).to eq(final_count) end end + + describe "#featured_topic" do + fab!(:featured_topic) { Fabricate(:topic) } + + before { user.user_profile.update!(featured_topic_id: featured_topic.id) } + + it "includes the featured topic" do + payload = serializer.as_json + + expect(payload[:featured_topic]).to_not be_nil + expect(payload[:featured_topic][:id]).to eq(featured_topic.id) + expect(payload[:featured_topic][:title]).to eq(featured_topic.title) + expect(payload[:featured_topic].keys).to contain_exactly( + :id, + :title, + :fancy_title, + :slug, + :posts_count, + ) + end + end end diff --git a/spec/serializers/user_card_serializer_spec.rb b/spec/serializers/user_card_serializer_spec.rb index 2b04a13303b..3dafe539f89 100644 --- a/spec/serializers/user_card_serializer_spec.rb +++ b/spec/serializers/user_card_serializer_spec.rb @@ -126,4 +126,27 @@ RSpec.describe UserCardSerializer do expect(json.keys).not_to include :status end end + + describe "#featured_topic" do + fab!(:user) + fab!(:featured_topic) { Fabricate(:topic) } + + before { user.user_profile.update(featured_topic_id: featured_topic.id) } + + it "includes the featured topic" do + serializer = described_class.new(user, scope: Guardian.new(user), root: false) + json = serializer.as_json + + expect(json[:featured_topic]).to_not be_nil + expect(json[:featured_topic][:id]).to eq(featured_topic.id) + expect(json[:featured_topic][:title]).to eq(featured_topic.title) + expect(json[:featured_topic].keys).to contain_exactly( + :id, + :title, + :fancy_title, + :slug, + :posts_count, + ) + end + end end diff --git a/spec/serializers/user_serializer_spec.rb b/spec/serializers/user_serializer_spec.rb index b5c5b173d4a..c9289fe11bd 100644 --- a/spec/serializers/user_serializer_spec.rb +++ b/spec/serializers/user_serializer_spec.rb @@ -339,6 +339,21 @@ RSpec.describe UserSerializer do end end end + + describe "with a custom notification schedule" do + let(:schedule) do + UserNotificationSchedule.create({ user: user }.merge(UserNotificationSchedule::DEFAULT)) + end + let(:scope) { Guardian.new(user) } + + it "includes the serialized schedule" do + expect(json[:user_notification_schedule][:enabled]).to eq(schedule[:enabled]) + expect(json[:user_notification_schedule][:day_0_start_time]).to eq( + schedule[:day_0_start_time], + ) + expect(json[:user_notification_schedule][:day_6_end_time]).to eq(schedule[:day_6_end_time]) + end + end end context "with custom_fields" do