Skip to content

Commit

Permalink
MAP-1582 - Add PER/YRA events to all feed for AP (#2307)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thource authored Aug 22, 2024
1 parent cf3b994 commit 8833a77
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 2 deletions.
26 changes: 26 additions & 0 deletions app/services/feeds/all.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Feeds
class All < Feeds::Jpc
private

def event_feed
super.tap do |feed|
profiles.find_each do |profile|
profile.person_escort_record&.generic_events&.find_each do |event|
feed << event.for_feed.to_json
end
profile.youth_risk_assessment&.generic_events&.find_each do |event|
feed << event.for_feed.to_json
end
end
moves.find_each do |move|
move.person_escort_record&.generic_events&.find_each do |event|
feed << event.for_feed.to_json
end
move.youth_risk_assessment&.generic_events&.find_each do |event|
feed << event.for_feed.to_json
end
end
end
end
end
end
6 changes: 5 additions & 1 deletion app/services/feeds/jpc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def journeys
.or(::Journey.where('moves.date' => ...@date_from, 'moves.updated_at' => @date_from..@date_to))
end

def profiles
::Profile.updated_at_range(@date_from, @date_to)
end

def move_feed
[].tap do |feed|
moves.find_each do |move|
Expand Down Expand Up @@ -64,7 +68,7 @@ def event_feed

def profile_feed
[].tap do |feed|
::Profile.updated_at_range(@date_from, @date_to).find_each do |profile|
profiles.find_each do |profile|
feed << profile.for_feed.to_json
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/workers/feeds/all_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def perform(date)
Sidekiq.logger.info('Generating analytics feeds...')
time_since = TimeSince.new

feeds = Feeds::Jpc.new(date.beginning_of_day, date.end_of_day).call
feeds = Feeds::All.new(date.beginning_of_day, date.end_of_day).call
feeds.each do |feed_name, feed_data|
CloudData::AnalyticalPlatformFeed.new.write(feed_data, feed_name.to_s.pluralize, date)
end
Expand Down
107 changes: 107 additions & 0 deletions spec/services/feeds/all_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
require 'rails_helper'

RSpec.describe Feeds::All do
subject(:feed) { described_class.new(date_from, date_to) }

let(:date_from) { Time.zone.yesterday.beginning_of_day }
let(:date_to) { Time.zone.yesterday.end_of_day }

describe '#call' do
let!(:on_start_profile) { create(:profile) }
let!(:on_end_profile) { create(:profile) }
let!(:other_profile) { create(:profile) }

let!(:scheduled_on_date_move) { create(:move, date: date_from, profile: on_start_profile) }
let!(:updated_on_date_move) { create(:move, date: date_from.yesterday, profile: on_end_profile) }
let!(:other_date_move) { create(:move, date: date_from.tomorrow, profile: other_profile) }

let!(:journey1) { create(:journey, move: scheduled_on_date_move) }
let!(:journey2) { create(:journey, move: updated_on_date_move) }
let!(:journey3) { create(:journey, move: other_date_move) }

let!(:per1) { create(:person_escort_record, :without_move, move: scheduled_on_date_move, profile: scheduled_on_date_move.profile) }
let!(:per2) { create(:person_escort_record, :without_move, move: other_date_move, profile: other_date_move.profile) }
let!(:yra1) { create(:person_escort_record, :without_move, move: updated_on_date_move, profile: updated_on_date_move.profile) }

let!(:event1) { create(:event_move_cancel, eventable: scheduled_on_date_move) }
let!(:event2) { create(:event_move_cancel, eventable: updated_on_date_move) }
let!(:event3) { create(:event_move_cancel, eventable: other_date_move) }
let!(:event4) { create(:event_journey_cancel, eventable: journey1) }
let!(:event5) { create(:event_journey_cancel, eventable: journey2) }
let!(:event6) { create(:event_journey_cancel, eventable: journey3) }
let!(:event7) { create(:event_per_completion, eventable: per1) }
let!(:event8) { create(:event_per_completion, eventable: per2) }
let!(:event9) { create(:event_per_completion, eventable: yra1) }

let!(:on_start_person) { create(:person) }
let!(:on_end_person) { create(:person) }
let!(:other_person) { create(:person) }

let(:expected_moves) do
[scheduled_on_date_move, updated_on_date_move].sort_by(&:id).map { |move| JSON.parse(move.for_feed.to_json) }
end

let(:expected_journeys) do
[journey1, journey2].sort_by(&:id).map { |journey| JSON.parse(journey.for_feed.to_json) }
end

let(:expected_events) do
[event1, event2, event4, event5, event7, event9].map { |event| JSON.parse(event.for_feed.to_json) }
end

let(:unexpected_events) do
[event3, event6, event8].map { |event| JSON.parse(event.for_feed.to_json) }
end

let(:expected_profiles) do
[on_start_profile, on_end_profile].sort_by(&:id).map { |profile| JSON.parse(profile.for_feed.to_json) }
end

let(:expected_people) do
[on_start_person, on_end_person].sort_by(&:id).map { |person| JSON.parse(person.for_feed.to_json) }
end

# rubocop:disable Rails/SkipsModelValidations
before do
scheduled_on_date_move.update_attribute('updated_at', date_from.yesterday)
updated_on_date_move.update_attribute('updated_at', date_from)
other_date_move.update_attribute('updated_at', date_from.days_ago(2))
on_start_profile.update_attribute('updated_at', date_from)
on_end_profile.update_attribute('updated_at', date_to)
other_profile.update_attribute('updated_at', date_from.days_ago(2))
on_start_person.update_attribute('updated_at', date_from)
on_end_person.update_attribute('updated_at', date_to)
other_person.update_attribute('updated_at', date_from.days_ago(2))
end
# rubocop:enable Rails/SkipsModelValidations

it 'returns correctly formatted move feed' do
actual = feed.call[:move].split("\n").map { |move| JSON.parse(move) }

expect(actual).to include_json(expected_moves)
end

it 'returns correctly formatted journey feed' do
actual = feed.call[:journey].split("\n").map { |journey| JSON.parse(journey) }

expect(actual).to include_json(expected_journeys)
end

it 'returns correctly formatted event feed' do
expected_events.each { |event| expect(feed.call[:event]).to include(event.to_json) }
unexpected_events.each { |event| expect(feed.call[:event]).not_to include(event.to_json) }
end

it 'returns correctly formatted profile feed' do
actual = feed.call[:profile].split("\n").map { |profile| JSON.parse(profile) }

expect(actual).to include_json(expected_profiles)
end

it 'returns correctly formatted person feed' do
actual = feed.call[:person].split("\n").map { |person| JSON.parse(person) }

expect(actual).to include_json(expected_people)
end
end
end

0 comments on commit 8833a77

Please sign in to comment.