Skip to content

Commit

Permalink
Determine Agreement status based on end date
Browse files Browse the repository at this point in the history
  • Loading branch information
RobNicholsGDS committed Jan 7, 2025
1 parent 8946b96 commit 7bec2e2
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rubyonrails.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:

# Configure Ruby and Rails environment
- name: Install Ruby and gems
uses: ruby/setup-ruby@250fcd6a742febb1123a77a841497ccaa8b9e939 # v1.152.0
uses: ruby/setup-ruby@v1
with:
bundler-cache: true

Expand All @@ -60,7 +60,7 @@ jobs:
- name: Checkout code
uses: actions/checkout@v3
- name: Install Ruby and gems
uses: ruby/setup-ruby@250fcd6a742febb1123a77a841497ccaa8b9e939 # v1.152.0
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
# Add or replace any other lints here
Expand Down
20 changes: 14 additions & 6 deletions app/models/agreement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ class Agreement < DataTable
has_many :agreement_processors, dependent: :delete_all
has_many :processors, through: :agreement_processors

def self.isa_statuses
pluck(Arel.sql("fields -> 'isa_status'")).uniq
end

def self.find_by_id!(id)
find_by!("(fields ->> 'id')::Integer = ?", id.to_i)
class << self
def isa_statuses
pluck(Arel.sql("fields -> 'isa_status'")).uniq
end

def find_by_id!(id)
find_by!("(fields ->> 'id')::Integer = ?", id.to_i)
end

def before_populate_save(instance)
end_date = instance.fields["end_date"].presence
status = end_date && (Time.zone.parse(end_date) < Time.zone.now) ? "Complete" : "Active"
instance.fields["isa_status"] = status
end
end

def id_and_name
Expand Down
6 changes: 6 additions & 0 deletions app/models/data_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,19 @@ def populate
instance.name = name.strip

instance.fields = SourceRecordCleaner.clean(record)
before_populate_save(instance)
instance.save!
instance.id
end
# Remove records that no longer match any on the source system(assume deleted)
where.not(id: ids_of_created).destroy_all
end

def before_populate_save(instance)
# By default do nothing
# Override in child classes to add modifications specific to that class
end

def data_from_source
air_table_data_source? ? data_from_air_table : data_from_rapid
end
Expand Down
44 changes: 44 additions & 0 deletions spec/models/agreement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,50 @@
expect(described_class.last.name).to eq("Bar")
end
end

context "with dates" do
let(:data) do
{
1 => {
id: 1,
agreement_name: "Foo",
start_date: 3.days.ago.strftime("%Y-%m-%d"),

},
2 => {
id: 2,
agreement_name: "Bar",
start_date: 3.days.ago.strftime("%Y-%m-%d"),
end_date: 2.days.ago.strftime("%Y-%m-%d"),

},
3 => {
id: 3,
agreement_name: "Other",
start_date: 3.days.ago.strftime("%Y-%m-%d"),
end_date: 2.days.from_now.strftime("%Y-%m-%d"),
},
}
end

it "sets status to active if no end date" do
populate
agreement = described_class.find_by_id!(1)
expect(agreement.fields["isa_status"]).to eq("Active")
end

it "sets status to active if end date in past" do
populate
agreement = described_class.find_by_id!(2)
expect(agreement.fields["isa_status"]).to eq("Complete")
end

it "sets status to active if end date in future" do
populate
agreement = described_class.find_by_id!(3)
expect(agreement.fields["isa_status"]).to eq("Active")
end
end
end

describe "#id_and_name" do
Expand Down
2 changes: 1 addition & 1 deletion spec/shared_examples/is_data_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
record = described_class.last
expect(record.name).to eq(name)
expect(record.record_id).to eq(id)
expect(record.fields).to eq(fields.stringify_keys)
expect(record.fields["foo"]).to eq("bar")
end

context "when keys are not downcase" do
Expand Down

0 comments on commit 7bec2e2

Please sign in to comment.