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

Add rake task to set the status of expired registration exemptions #255

Merged
merged 6 commits into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/models/waste_exemptions_engine/registration_exemption.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

module WasteExemptionsEngine
class RegistrationExemption < ActiveRecord::Base
self.table_name = "registration_exemptions"

include CanDeactivateExemption
include CanBeOrderedByStateAndExemptionId

Expand All @@ -16,7 +18,8 @@ class RegistrationExemption < ActiveRecord::Base
.order(:registered_on, :registration_id)
}

scope :not_expired, -> { where("expires_on > CURRENT_DATE") }
scope :not_expired, -> { where("expires_on >= CURRENT_DATE") }
scope :expired, -> { where("expires_on < CURRENT_DATE") }
cintamani marked this conversation as resolved.
Show resolved Hide resolved

scope :all_active_exemptions, lambda {
active
Expand Down
13 changes: 13 additions & 0 deletions app/services/expired_registrations_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class ExpiredRegistrationsService < ::WasteExemptionsEngine::BaseService
def run
all_expired_registration_exemptions.update_all(state: :expired)
cintamani marked this conversation as resolved.
Show resolved Hide resolved
end

private

def all_expired_registration_exemptions
WasteExemptionsEngine::RegistrationExemption.active.expired
end
end
8 changes: 8 additions & 0 deletions lib/tasks/expire_registration.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

namespace :expire_registration do
desc "Set the status of expired registations to expired"
task run: :environment do
ExpiredRegistrationsService.run
end
end
23 changes: 23 additions & 0 deletions spec/services/expired_registrations_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe ExpiredRegistrationsService do
describe ".run" do
it "sets the status of active registration exemptions that have an expiry date in the past to expired" do
expired_registration_exemption = create(:registration_exemption, expires_on: 1.day.ago)

expect { described_class.run }
.to change { expired_registration_exemption.reload.state }
.from("active")
.to("expired")
end

it "does not change the state of expired registration that are not in an active status" do
expired_registration_exemption = create(:registration_exemption, :ceased, expires_on: 1.day.ago)

expect { described_class.run }
.not_to change { expired_registration_exemption.reload.state }
end
end
end