diff --git a/app/models/group.rb b/app/models/group.rb index 8a53ae8b..1670b7dd 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -10,6 +10,19 @@ def sponsor members.find(&:sponsor?) end + def barred? + members.any?(&:barred?) + end + + def status + Patron::PATRON_STANDING.fetch(standing, '') + end + + def standing + possible_standings = Patron::PATRON_STANDING.keys + members.map(&:standing).min_by { |s| possible_standings.index(s) || Float::INFINITY } + end + def checkouts @checkouts ||= member_list.flat_map(&:checkouts) end diff --git a/app/models/patron.rb b/app/models/patron.rb index 3db85b51..ec86deb5 100644 --- a/app/models/patron.rb +++ b/app/models/patron.rb @@ -7,11 +7,11 @@ class Patron CHARGE_LIMIT_THRESHOLD = 25_000 PATRON_STANDING = { - 'COLLECTION' => 'Blocked', 'BARRED' => 'Contact us', + 'COLLECTION' => 'Blocked', 'BLOCKED' => 'Blocked', - 'OK' => 'OK', - 'DELINQUENT' => 'OK' + 'DELINQUENT' => 'OK', + 'OK' => 'OK' }.freeze USER_PROFILE = { @@ -36,8 +36,24 @@ def barcode def status if expired? 'Expired' + elsif proxy_borrower? + # proxy borrowers inherit from the group + group.status else - PATRON_STANDING.fetch(fields['standing']['key'], '') + PATRON_STANDING.fetch(standing, '') + end + end + + def standing + fields.dig('standing', 'key') + end + + def barred? + # proxy borrowers inherit from the group + if proxy_borrower? + group.standing == 'BARRED' + else + standing == 'BARRED' end end @@ -129,10 +145,6 @@ def group? group.member_list.any? end - def barred? - fields.dig('standing', 'key') == 'BARRED' - end - def to_partial_path 'patron/patron' end diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb index 888e64fa..1d3e123c 100644 --- a/spec/models/group_spec.rb +++ b/spec/models/group_spec.rb @@ -108,6 +108,61 @@ end end + describe '#barred?' do + context 'when a member of the group is barred' do + let(:member_list) do + [ + { key: '1', fields: {} }, + { key: '2', fields: { groupSettings: { + fields: { responsibility: { key: 'SPONSOR' } } + } } }, + { key: '3', fields: { standing: { key: 'BARRED' } } }, + { key: '411612', fields: { firstName: 'Mark (P=Wangchuk)' } } + ] + end + + it 'is barred' do + expect(group).to be_barred + end + end + + context 'with all members in good standing' do + it 'is not barred' do + expect(group).not_to be_barred + end + end + end + + describe '#standing' do + let(:member_list) do + [ + { key: '1', fields: {} }, + { key: '2', fields: { standing: { key: 'OK' } } }, + { key: '3', fields: { standing: { key: 'BARRED' } } }, + { key: '411612', fields: { standing: { key: 'OK' } } } + ] + end + + it 'is the worst possible standing of the members of the group' do + expect(group.standing).to eq 'BARRED' + end + end + + describe '#status' do + let(:member_list) do + [ + { key: '1', fields: {} }, + { key: '2', fields: { standing: { key: 'OK' } } }, + { key: '3', fields: { standing: { key: 'DELINQUENT' } } }, + { key: '411612', fields: { standing: { key: 'BLOCKED' } } } + ] + end + + it 'is the worst possible status of the members of the group' do + expect(group.status).to eq 'Blocked' + end + end + describe '#checkouts' do let(:member_list) do [fields: { diff --git a/spec/models/patron_spec.rb b/spec/models/patron_spec.rb index 428f1aaf..4d5c3508 100644 --- a/spec/models/patron_spec.rb +++ b/spec/models/patron_spec.rb @@ -47,9 +47,7 @@ fields: { group: { fields: { - memberList: [ - key: '521187' - ] + memberList: member_list } } } @@ -57,6 +55,8 @@ } end + let(:member_list) { [key: '521187'] } + it 'has a key' do expect(patron.key).to eq '1' end @@ -202,7 +202,16 @@ context 'with a proxy borrower' do before do - fields[:groupSettings] = { fields: { responsibility: { key: 'PROXY' } } } + fields[:groupSettings] = { + fields: { + responsibility: { key: 'PROXY' }, + group: { + fields: { + memberList: member_list + } + } + } + } fields[:firstName] = 'Second (P=FirstProxyLN)' end @@ -217,6 +226,40 @@ expect(patron.proxy_borrower_name).to eq 'Proxy FirstProxyLN' end end + + describe '#status' do + context 'when the group is blocked' do + let(:member_list) do + [ + { key: '1', fields: {} }, + { key: '2', fields: { standing: { key: 'OK' } } }, + { key: '3', fields: { standing: { key: 'DELINQUENT' } } }, + { key: '411612', fields: { standing: { key: 'BLOCKED' } } } + ] + end + + it 'inherits the group status' do + expect(patron.status).to eq 'Blocked' + end + end + end + + describe '#barred?' do + context 'when the group is barred' do + let(:member_list) do + [ + { key: '1', fields: {} }, + { key: '2', fields: { standing: { key: 'OK' } } }, + { key: '3', fields: { standing: { key: 'DELINQUENT' } } }, + { key: '411612', fields: { standing: { key: 'BARRED' } } } + ] + end + + it 'inherits the group barred status' do + expect(patron).to be_barred + end + end + end end it 'is not a sponsor' do