-
-
Notifications
You must be signed in to change notification settings - Fork 479
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add timing metadata for case contacts (#5919)
- Loading branch information
1 parent
4ff3d5c
commit ed7b83f
Showing
6 changed files
with
241 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class CaseContactUpdateService | ||
attr_reader :case_contact | ||
|
||
def initialize(case_contact) | ||
@case_contact = case_contact | ||
end | ||
|
||
def update_attrs(new_attrs) | ||
old_attrs = case_contact.as_json | ||
|
||
result = case_contact.update(new_attrs) | ||
update_status_metadata(old_attrs, new_attrs) if result | ||
|
||
result | ||
end | ||
|
||
private | ||
|
||
def update_status_metadata(old_attrs, new_attrs) | ||
return if old_attrs[:status] == new_attrs[:status] | ||
|
||
metadata = case_contact.metadata | ||
metadata["status"] ||= {} | ||
metadata["status"][new_attrs[:status]] = Time.zone.now | ||
|
||
case_contact.update(metadata: metadata) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddMetadataToCaseContacts < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :case_contacts, :metadata, :jsonb, default: {} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe CaseContactUpdateService do | ||
let(:updater) { described_class.new(case_contact) } | ||
let(:case_contact) { create(:case_contact) } | ||
|
||
let!(:now) { Time.zone.now } | ||
let!(:one_day_ago) { 1.day.ago } | ||
let!(:two_days_ago) { 2.days.ago } | ||
|
||
before { travel_to one_day_ago } | ||
after { travel_back } | ||
|
||
describe "#update_attributes" do | ||
context "case is in details status" do | ||
let!(:case_contact) { create(:case_contact, status: "details", created_at: two_days_ago) } | ||
|
||
context "status is not updated" do | ||
before { updater.update_attrs({notes: "stuff"}) } | ||
|
||
it { expect(case_contact.metadata).to eq({}) } | ||
it { expect(updater.update_attrs({notes: "stuff"})).to be true } | ||
end | ||
|
||
it "does not update metadata if attrs are invalid" do | ||
result = updater.update_attrs({occurred_at: 50.years.ago}) | ||
expect(case_contact.metadata).to eq({}) | ||
expect(result).to be false | ||
end | ||
|
||
context "gets updated to details" do | ||
before { updater.update_attrs({status: "details"}) } | ||
|
||
it "updates details metadata to current date" do | ||
date = case_contact.metadata.dig("status", "details") | ||
expect(DateTime.parse(date)).to eq(Time.zone.now) | ||
end | ||
it { expect(case_contact.status).to eq("details") } | ||
end | ||
|
||
context "gets updated to expenses" do | ||
before { updater.update_attrs({status: "expenses"}) } | ||
|
||
it "updates expenses metadata to current date" do | ||
date = case_contact.metadata.dig("status", "expenses") | ||
expect(DateTime.parse(date)).to eq(Time.zone.now) | ||
end | ||
it { expect(case_contact.status).to eq("expenses") } | ||
end | ||
|
||
context "gets updated to active" do | ||
before { updater.update_attrs({status: "active"}) } | ||
|
||
it "updates active metadata to current date" do | ||
date = case_contact.metadata.dig("status", "active") | ||
expect(DateTime.parse(date)).to eq(Time.zone.now) | ||
end | ||
it { expect(case_contact.status).to eq("active") } | ||
end | ||
end | ||
|
||
context "case is in expenses status" do | ||
let!(:case_contact) { create(:case_contact, status: "expenses", created_at: two_days_ago) } | ||
|
||
context "status is not updated" do | ||
before { updater.update_attrs({notes: "stuff"}) } | ||
|
||
it { expect(case_contact.metadata).to eq({}) } | ||
it { expect(updater.update_attrs({notes: "stuff"})).to be true } | ||
end | ||
|
||
it "does not update metadata if attrs are invalid" do | ||
result = updater.update_attrs({occurred_at: 50.years.ago}) | ||
expect(case_contact.metadata).to eq({}) | ||
expect(result).to be false | ||
end | ||
|
||
context "gets updated to details" do | ||
before { updater.update_attrs({status: "details"}) } | ||
|
||
it "updates details metadata to current date" do | ||
date = case_contact.metadata.dig("status", "details") | ||
expect(DateTime.parse(date)).to eq(Time.zone.now) | ||
end | ||
it { expect(case_contact.status).to eq("details") } | ||
end | ||
|
||
context "gets updated to expenses" do | ||
before { updater.update_attrs({status: "expenses"}) } | ||
|
||
it "updates expenses metadata to current date" do | ||
date = case_contact.metadata.dig("status", "expenses") | ||
expect(DateTime.parse(date)).to eq(Time.zone.now) | ||
end | ||
it { expect(case_contact.status).to eq("expenses") } | ||
end | ||
|
||
context "gets updated to active" do | ||
before { updater.update_attrs({status: "active"}) } | ||
|
||
it "updates active metadata to current date" do | ||
date = case_contact.metadata.dig("status", "active") | ||
expect(DateTime.parse(date)).to eq(Time.zone.now) | ||
end | ||
it { expect(case_contact.status).to eq("active") } | ||
end | ||
end | ||
|
||
context "case is in active status" do | ||
let!(:case_contact) { create(:case_contact, status: "active", created_at: two_days_ago) } | ||
|
||
context "status is not updated" do | ||
before { updater.update_attrs({notes: "stuff"}) } | ||
|
||
it { expect(case_contact.metadata).to eq({}) } | ||
it { expect(updater.update_attrs({notes: "stuff"})).to be true } | ||
end | ||
|
||
it "does not update metadata if attrs are invalid" do | ||
result = updater.update_attrs({occurred_at: 50.years.ago}) | ||
expect(case_contact.metadata).to eq({}) | ||
expect(result).to be false | ||
end | ||
|
||
context "gets updated to details" do | ||
before { updater.update_attrs({status: "details"}) } | ||
|
||
it "updates details metadata to current date" do | ||
date = case_contact.metadata.dig("status", "details") | ||
expect(DateTime.parse(date)).to eq(Time.zone.now) | ||
end | ||
it { expect(case_contact.status).to eq("details") } | ||
end | ||
|
||
context "gets updated to expenses" do | ||
before { updater.update_attrs({status: "expenses"}) } | ||
|
||
it "updates expenses metadata to current date" do | ||
date = case_contact.metadata.dig("status", "expenses") | ||
expect(DateTime.parse(date)).to eq(Time.zone.now) | ||
end | ||
it { expect(case_contact.status).to eq("expenses") } | ||
end | ||
|
||
context "gets updated to active" do | ||
before { updater.update_attrs({status: "active"}) } | ||
|
||
it "updates active metadata to current date" do | ||
date = case_contact.metadata.dig("status", "active") | ||
expect(DateTime.parse(date)).to eq(Time.zone.now) | ||
end | ||
it { expect(case_contact.status).to eq("active") } | ||
end | ||
end | ||
|
||
context "case is in started status" do | ||
let!(:case_contact) { create(:case_contact, status: "started", created_at: two_days_ago) } | ||
|
||
context "status is not updated" do | ||
before { updater.update_attrs({notes: "stuff"}) } | ||
|
||
it { expect(case_contact.metadata).to eq({}) } | ||
it { expect(updater.update_attrs({notes: "stuff"})).to be true } | ||
end | ||
|
||
it "does not update metadata if attrs are invalid" do | ||
result = updater.update_attrs({occurred_at: 50.years.ago}) | ||
expect(case_contact.metadata).to eq({}) | ||
expect(result).to be false | ||
end | ||
|
||
context "gets updated to details" do | ||
before { updater.update_attrs({status: "details"}) } | ||
|
||
it "updates details metadata to current date" do | ||
date = case_contact.metadata.dig("status", "details") | ||
expect(DateTime.parse(date)).to eq(Time.zone.now) | ||
end | ||
it { expect(case_contact.status).to eq("details") } | ||
end | ||
|
||
context "gets updated to expenses" do | ||
before { updater.update_attrs({status: "expenses"}) } | ||
|
||
it "updates expenses metadata to current date" do | ||
date = case_contact.metadata.dig("status", "expenses") | ||
expect(DateTime.parse(date)).to eq(Time.zone.now) | ||
end | ||
it { expect(case_contact.status).to eq("expenses") } | ||
end | ||
|
||
context "gets updated to active" do | ||
before { updater.update_attrs({status: "active"}) } | ||
|
||
it "updates active metadata to current date" do | ||
date = case_contact.metadata.dig("status", "active") | ||
expect(DateTime.parse(date)).to eq(Time.zone.now) | ||
end | ||
it { expect(case_contact.status).to eq("active") } | ||
end | ||
end | ||
end | ||
end |