DEV: Use serializers for user_notification_schedule and featured_topic (#27719)

Co-authored-by: Alan Guo Xiang Tan <gxtan1990@gmail.com>
This commit is contained in:
Jan Cernik 2024-07-05 00:00:24 -03:00 committed by GitHub
parent 906da0f3d1
commit 33c68b28b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 87 additions and 3 deletions

View File

@ -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

View File

@ -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?

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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