Skip to content

Commit 0042ebe

Browse files
committed
Add can_prescribe?
This adds a method to `User` and `CIS2Info` which can be used to determine with a particular user has permission to prescribe a vaccination using either a PSD or the national protocol. Jira-Issue: MAV-1400
1 parent 4ef36b4 commit 0042ebe

File tree

5 files changed

+70
-2
lines changed

5 files changed

+70
-2
lines changed

app/controllers/concerns/authentication_concern.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def selected_cis2_workgroup_is_valid?
5050

5151
def selected_cis2_role_is_valid?
5252
cis2_info.can_view? || cis2_info.can_supply_using_pgd? ||
53-
cis2_info.can_perform_local_admin_tasks? ||
53+
cis2_info.can_prescribe?
54+
cis2_info.can_perform_local_admin_tasks? ||
5455
cis2_info.can_access_sensitive_records?
5556
end
5657

app/models/cis2_info.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class CIS2Info
88

99
SUPERUSER_WORKGROUP = "mavissuperusers"
1010

11+
INDEPENDENT_PRESCRIBING_ACTIVITY_CODE = "B0420"
12+
1113
attribute :organisation_name
1214
attribute :organisation_code
1315
attribute :role_name
@@ -44,6 +46,11 @@ def can_supply_using_pgd?
4446
role_code == NURSE_ROLE
4547
end
4648

49+
def can_prescribe?
50+
role_code == NURSE_ROLE ||
51+
activity_codes.include?(INDEPENDENT_PRESCRIBING_ACTIVITY_CODE)
52+
end
53+
4754
def can_perform_local_admin_tasks?
4855
in_superuser_workgroup?
4956
end

app/models/user.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ def can_supply_using_pgd?
139139
cis2_enabled? ? cis2_info.can_supply_using_pgd? : fallback_role_nurse?
140140
end
141141

142+
def can_prescribe?
143+
if cis2_enabled?
144+
cis2_info.can_prescribe?
145+
else
146+
fallback_role_nurse? || fallback_role_prescriber?
147+
end
148+
end
149+
142150
def can_perform_local_admin_tasks?
143151
if cis2_enabled?
144152
cis2_info.can_perform_local_admin_tasks?

spec/factories/users.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,19 @@
9393
fallback_role { :healthcare_assistant }
9494
end
9595

96+
trait :prescriber do
97+
sequence(:email) { |n| "prescriber-#{n}@example.com" }
98+
role_code { nil }
99+
activity_codes { [CIS2Info::INDEPENDENT_PRESCRIBING_ACTIVITY_CODE] }
100+
fallback_role { :prescriber }
101+
end
102+
96103
trait :signed_in do
97104
current_sign_in_at { Time.current }
98105
current_sign_in_ip { "127.0.0.1" }
99106
end
100107
end
101108

102109
factory :admin, parent: :user, traits: %i[admin]
110+
factory :prescriber, parent: :user, traits: %i[prescriber]
103111
end

spec/models/user_spec.rb

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
end
120120
end
121121

122-
describe "#can_supply_using_pgd??" do
122+
describe "#can_supply_using_pgd?" do
123123
subject { user.can_supply_using_pgd? }
124124

125125
context "cis2 is enabled", cis2: :enabled do
@@ -151,6 +151,50 @@
151151
end
152152
end
153153

154+
describe "#can_prescribe?" do
155+
subject { user.can_prescribe? }
156+
157+
context "cis2 is enabled", cis2: :enabled do
158+
context "when the user is a nurse" do
159+
let(:user) { build(:nurse) }
160+
161+
it { should be(true) }
162+
end
163+
164+
context "when the user is admin staff" do
165+
let(:user) { build(:admin) }
166+
167+
it { should be(false) }
168+
end
169+
170+
context "when the user is a prescriber" do
171+
let(:user) { build(:prescriber) }
172+
173+
it { should be(true) }
174+
end
175+
end
176+
177+
context "cis2 is disabled", cis2: :disabled do
178+
context "when the user is a nurse" do
179+
let(:user) { build(:nurse) }
180+
181+
it { should be(true) }
182+
end
183+
184+
context "when the user is admin staff" do
185+
let(:user) { build(:admin) }
186+
187+
it { should be(false) }
188+
end
189+
190+
context "when the user is a prescriber" do
191+
let(:user) { build(:prescriber) }
192+
193+
it { should be(true) }
194+
end
195+
end
196+
end
197+
154198
describe "#can_perform_local_admin_tasks?" do
155199
subject { user.can_perform_local_admin_tasks? }
156200

0 commit comments

Comments
 (0)