Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(688) Add content block updates to a document's timeline #9754

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
24 changes: 24 additions & 0 deletions app/models/host_content_update_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class HostContentUpdateEvent < Data.define(:author, :created_at, :content_id, :content_title)
def self.all_for_date_window(document:, from:, to:)
events = Services.publishing_api.get_events_for_content_id(document.content_id, {
action: "HostContentUpdateJob",
from:,
to:,
})

events.map do |event|
HostContentUpdateEvent.new(
author: get_user_for_uuid(event["payload"]["source_block"]["updated_by_user_uid"]),
created_at: Time.zone.parse(event["created_at"]),
content_id: event["payload"]["source_block"]["content_id"],
content_title: event["payload"]["source_block"]["title"],
)
end
end

private

def self.get_user_for_uuid(uuid)
User.find_by(uid: uuid)
end
end
77 changes: 77 additions & 0 deletions test/unit/app/models/host_content_update_event_test.rb
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice tests ⭐

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require "test_helper"

class HostContentUpdateEventTest < ActiveSupport::TestCase
extend Minitest::Spec::DSL

let(:document) { create(:document) }
let(:user) { create(:user) }

describe ".all_for_date_window" do
it "returns all HostContentUpdateJobs" do
from = Time.zone.now - 2.months
to = Time.zone.now - 1.month

Services.publishing_api.expects(:get_events_for_content_id).with(
document.content_id, {
action: "HostContentUpdateJob",
from:,
to:,
}
).returns(
[
{
"id" => 1593,
"action" => "HostContentUpdateJob",
"created_at" => "2024-01-01T00:00:00.000Z",
"updated_at" => "2024-01-01T00:00:00.000Z",
"request_id" => SecureRandom.uuid,
"content_id" => document.content_id,
"payload" => {
"title" => "Host content updated by content block update",
"locale" => "en",
"content_id" => document.content_id,
"source_block" => {
"title" => "An exciting piece of content",
"content_id" => "ef224ae6-7a81-4c59-830b-e9884fe57ec8",
"updated_by_user_uid" => user.uid,
},
},
},
{
"id" => 1593,
"action" => "HostContentUpdateJob",
"user_uid" => SecureRandom.uuid,
"created_at" => "2023-12-01T00:00:00.000Z",
"updated_at" => "2023-12-01T00:00:00.000Z",
"request_id" => SecureRandom.uuid,
"content_id" => document.content_id,
"payload" => {
"title" => "Host content updated by content block update",
"locale" => "en",
"content_id" => document.content_id,
"source_block" => {
"title" => "Another exciting piece of content",
"content_id" => "5c5520ce-6677-4a76-bd6e-4515f46a804e",
"updated_by_user_uid" => nil,
},
},
},
],
)

result = HostContentUpdateEvent.all_for_date_window(document:, from:, to:)

assert_equal result.count, 2

assert_equal result.first.author, user
assert_equal result.first.created_at, Time.zone.parse("2024-01-01T00:00:00.000Z")
assert_equal result.first.content_id, "ef224ae6-7a81-4c59-830b-e9884fe57ec8"
assert_equal result.first.content_title, "An exciting piece of content"

assert_nil result.second.author
assert_equal result.second.created_at, Time.zone.parse("2023-12-01T00:00:00.000Z")
assert_equal result.second.content_id, "5c5520ce-6677-4a76-bd6e-4515f46a804e"
assert_equal result.second.content_title, "Another exciting piece of content"
end
end
end