Check if account can initiate relationship
This commit is contained in:
parent
a8a5119dd8
commit
9da7505338
2 changed files with 152 additions and 1 deletions
|
@ -4,7 +4,7 @@ class AcceptAsSupporter
|
|||
include Interactor
|
||||
|
||||
around :wrap_into_transaction
|
||||
|
||||
before :validate_account
|
||||
after :reload_records
|
||||
|
||||
def call
|
||||
|
@ -27,4 +27,9 @@ private
|
|||
def reload_records
|
||||
context.person.reload
|
||||
end
|
||||
|
||||
def validate_account
|
||||
context.fail! unless context.initiator_account.can_initiate_relationship? \
|
||||
context.regional_office
|
||||
end
|
||||
end
|
||||
|
|
|
@ -36,4 +36,150 @@ RSpec.describe AcceptAsSupporter do
|
|||
regional_secretary_flag: nil,
|
||||
)
|
||||
end
|
||||
|
||||
context 'when initiator is a federal secretary' do
|
||||
let!(:initiator_account) { create :personal_account }
|
||||
|
||||
before do
|
||||
create :federal_secretary_relationship, person: initiator_account.person
|
||||
end
|
||||
|
||||
specify { expect(subject).to be_success }
|
||||
|
||||
specify do
|
||||
expect { subject }.to change(Relationship, :count).by(1)
|
||||
end
|
||||
|
||||
specify do
|
||||
expect { subject }.to \
|
||||
change(person.all_relationships, :count).from(0).to(1)
|
||||
end
|
||||
|
||||
specify do
|
||||
expect { subject }.to change(person, :current_relationship).from(nil)
|
||||
|
||||
expect(person.current_relationship).to have_attributes(
|
||||
regional_office: regional_office,
|
||||
initiator_account: initiator_account,
|
||||
from_date: Time.zone.today,
|
||||
status: 'supporter',
|
||||
role: nil,
|
||||
federal_secretary_flag: nil,
|
||||
regional_secretary_flag: nil,
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when initiator is a regional secretary with the same region' do
|
||||
let!(:initiator_account) { create :personal_account }
|
||||
|
||||
before do
|
||||
create :regional_secretary_relationship,
|
||||
person: initiator_account.person,
|
||||
regional_office: regional_office
|
||||
end
|
||||
|
||||
specify { expect(subject).to be_success }
|
||||
|
||||
specify do
|
||||
expect { subject }.to change(Relationship, :count).by(1)
|
||||
end
|
||||
|
||||
specify do
|
||||
expect { subject }.to \
|
||||
change(person.all_relationships, :count).from(0).to(1)
|
||||
end
|
||||
|
||||
specify do
|
||||
expect { subject }.to change(person, :current_relationship).from(nil)
|
||||
|
||||
expect(person.current_relationship).to have_attributes(
|
||||
regional_office: regional_office,
|
||||
initiator_account: initiator_account,
|
||||
from_date: Time.zone.today,
|
||||
status: 'supporter',
|
||||
role: nil,
|
||||
federal_secretary_flag: nil,
|
||||
regional_secretary_flag: nil,
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when initiator is a regional secretary with different region' do
|
||||
let!(:initiator_account) { create :personal_account }
|
||||
|
||||
before do
|
||||
create :regional_secretary_relationship, person: initiator_account.person
|
||||
end
|
||||
|
||||
specify { expect(subject).to be_failure }
|
||||
|
||||
specify do
|
||||
expect { subject }.not_to change(Relationship, :count)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when initiator is a federal manager' do
|
||||
let!(:initiator_account) { create :personal_account }
|
||||
|
||||
before do
|
||||
create :federal_manager_relationship,
|
||||
person: initiator_account.person,
|
||||
regional_office: regional_office
|
||||
end
|
||||
|
||||
specify { expect(subject).to be_failure }
|
||||
|
||||
specify do
|
||||
expect { subject }.not_to change(Relationship, :count)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when initiator is a federal supervisor' do
|
||||
let!(:initiator_account) { create :personal_account }
|
||||
|
||||
before do
|
||||
create :federal_supervisor_relationship,
|
||||
person: initiator_account.person,
|
||||
regional_office: regional_office
|
||||
end
|
||||
|
||||
specify { expect(subject).to be_failure }
|
||||
|
||||
specify do
|
||||
expect { subject }.not_to change(Relationship, :count)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when initiator is a regional manager' do
|
||||
let!(:initiator_account) { create :personal_account }
|
||||
|
||||
before do
|
||||
create :regional_manager_relationship,
|
||||
person: initiator_account.person,
|
||||
regional_office: regional_office
|
||||
end
|
||||
|
||||
specify { expect(subject).to be_failure }
|
||||
|
||||
specify do
|
||||
expect { subject }.not_to change(Relationship, :count)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when initiator is a regional supervisor' do
|
||||
let!(:initiator_account) { create :personal_account }
|
||||
|
||||
before do
|
||||
create :regional_supervisor_relationship,
|
||||
person: initiator_account.person,
|
||||
regional_office: regional_office
|
||||
end
|
||||
|
||||
specify { expect(subject).to be_failure }
|
||||
|
||||
specify do
|
||||
expect { subject }.not_to change(Relationship, :count)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Reference in a new issue