Browse Source

Allow optional merge date in merge orgs task

pull/2003/head
Kat 3 years ago
parent
commit
dfc8b875c8
  1. 2
      app/services/merge/merge_organisations_service.rb
  2. 13
      lib/tasks/merge_organisations.rake
  3. 20
      spec/lib/tasks/merge_organisations_spec.rb
  4. 2
      spec/services/merge/merge_organisations_service_spec.rb

2
app/services/merge/merge_organisations_service.rb

@ -2,7 +2,7 @@ class Merge::MergeOrganisationsService
def initialize(absorbing_organisation_id:, merging_organisation_ids:, merge_date: Time.zone.today)
@absorbing_organisation = Organisation.find(absorbing_organisation_id)
@merging_organisations = Organisation.find(merging_organisation_ids)
@merge_date = merge_date
@merge_date = merge_date || Time.zone.today
end
def call

13
lib/tasks/merge_organisations.rake

@ -1,12 +1,19 @@
namespace :merge do
desc "Merge organisations into one"
task :merge_organisations, %i[absorbing_organisation_id merging_organisation_ids] => :environment do |_task, args|
task :merge_organisations, %i[absorbing_organisation_id merging_organisation_ids merge_date] => :environment do |_task, args|
absorbing_organisation_id = args[:absorbing_organisation_id]
merging_organisation_ids = args[:merging_organisation_ids]&.split(" ")&.map(&:to_i)
begin
merge_date = args[:merge_date].present? ? Date.parse(args[:merge_date]) : nil
rescue StandardError
raise "Usage: rake merge:merge_organisations[absorbing_organisation_id, merging_organisation_ids, merge_date]. Merge date must be in format YYYY-MM-DD"
end
raise "Usage: rake merge:merge_organisations[absorbing_organisation_id, merging_organisation_ids]" if merging_organisation_ids.blank? || absorbing_organisation_id.blank?
if merging_organisation_ids.blank? || absorbing_organisation_id.blank?
raise "Usage: rake merge:merge_organisations[absorbing_organisation_id, merging_organisation_ids, merge_date]"
end
service = Merge::MergeOrganisationsService.new(absorbing_organisation_id:, merging_organisation_ids:)
service = Merge::MergeOrganisationsService.new(absorbing_organisation_id:, merging_organisation_ids:, merge_date:)
service.call
end
end

20
spec/lib/tasks/merge_organisations_spec.rb

@ -20,22 +20,32 @@ RSpec.describe "emails" do
context "when the rake task is run" do
it "raises an error when no parameters are given" do
expect { task.invoke(nil) }.to raise_error(RuntimeError, "Usage: rake merge:merge_organisations[absorbing_organisation_id, merging_organisation_ids]")
expect { task.invoke(nil) }.to raise_error(RuntimeError, "Usage: rake merge:merge_organisations[absorbing_organisation_id, merging_organisation_ids, merge_date]")
end
it "raises an error when only absorbing organisation is given" do
expect { task.invoke(1, nil) }.to raise_error(RuntimeError, "Usage: rake merge:merge_organisations[absorbing_organisation_id, merging_organisation_ids]")
expect { task.invoke(1, nil) }.to raise_error(RuntimeError, "Usage: rake merge:merge_organisations[absorbing_organisation_id, merging_organisation_ids, merge_date]")
end
it "raises an error when only merging organisations are given" do
expect { task.invoke(nil, "1 2") }.to raise_error(RuntimeError, "Usage: rake merge:merge_organisations[absorbing_organisation_id, merging_organisation_ids]")
expect { task.invoke(nil, "1 2") }.to raise_error(RuntimeError, "Usage: rake merge:merge_organisations[absorbing_organisation_id, merging_organisation_ids, merge_date]")
end
it "raises runs the service with correct organisation IDs" do
expect(Merge::MergeOrganisationsService).to receive(:new).with(absorbing_organisation_id: 1, merging_organisation_ids: [2, 3]).once
it "raises an error when the merge date given is not valid" do
expect { task.invoke("3", "1 2", "invalid_date") }.to raise_error(RuntimeError, "Usage: rake merge:merge_organisations[absorbing_organisation_id, merging_organisation_ids, merge_date]. Merge date must be in format YYYY-MM-DD")
end
it "runs the service with correct organisation IDs" do
expect(Merge::MergeOrganisationsService).to receive(:new).with(absorbing_organisation_id: 1, merging_organisation_ids: [2, 3], merge_date: nil).once
expect(merge_organisations_service).to receive(:call).once
task.invoke(1, "2 3")
end
it "runs the service with correct date when date is given" do
expect(Merge::MergeOrganisationsService).to receive(:new).with(absorbing_organisation_id: 1, merging_organisation_ids: [2, 3], merge_date: Time.zone.local(2021, 1, 13)).once
expect(merge_organisations_service).to receive(:call).once
task.invoke(1, "2 3", "2021-01-13")
end
end
end
end

2
spec/services/merge/merge_organisations_service_spec.rb

@ -1,7 +1,7 @@
require "rails_helper"
RSpec.describe Merge::MergeOrganisationsService do
subject(:merge_organisations_service) { described_class.new(absorbing_organisation_id: absorbing_organisation.id, merging_organisation_ids: [merging_organisation_ids]) }
subject(:merge_organisations_service) { described_class.new(absorbing_organisation_id: absorbing_organisation.id, merging_organisation_ids: [merging_organisation_ids], merge_date: nil) }
let(:absorbing_organisation) { create(:organisation, holds_own_stock: false) }
let(:absorbing_organisation_user) { create(:user, organisation: absorbing_organisation) }

Loading…
Cancel
Save