Skip to content

Commit 1398a36

Browse files
committed
Add is_prescriber?
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 1363156 commit 1398a36

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
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.is_admin? || cis2_info.is_nurse? ||
53-
cis2_info.is_healthcare_assistant? || cis2_info.is_superuser?
53+
cis2_info.is_healthcare_assistant? || cis2_info.is_superuser? ||
54+
cis2_info.is_prescriber?
5455
end
5556

5657
def storable_location?

app/models/cis2_info.rb

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

99
SUPERUSER_WORKGROUP = "mavissuperusers"
1010

11+
INDEPENDENT_PRESCRIBING_ACTIVITY_CODE = "B0420"
1112
PERSONAL_MEDICATION_ADMINISTRATION_ACTIVITY_CODE = "B0428"
1213

1314
attribute :organisation_name
@@ -50,6 +51,10 @@ def is_healthcare_assistant?
5051
activity_codes.include?(PERSONAL_MEDICATION_ADMINISTRATION_ACTIVITY_CODE)
5152
end
5253

54+
def is_prescriber?
55+
activity_codes.include?(INDEPENDENT_PRESCRIBING_ACTIVITY_CODE)
56+
end
57+
5358
def is_superuser?
5459
workgroups.include?(SUPERUSER_WORKGROUP)
5560
end

app/models/user.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ def role_description
126126
role =
127127
if is_healthcare_assistant?
128128
"Healthcare Assistant"
129+
elsif is_prescriber?
130+
"Prescriber"
129131
elsif is_nurse?
130132
"Nurse"
131133
else
@@ -151,6 +153,10 @@ def is_healthcare_assistant?
151153
end
152154
end
153155

156+
def is_prescriber?
157+
cis2_enabled? ? cis2_info.is_prescriber? : fallback_role_prescriber?
158+
end
159+
154160
def is_superuser?
155161
cis2_enabled? ? cis2_info.is_superuser? : fallback_role_superuser?
156162
end

spec/factories/users.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@
100100
fallback_role { :healthcare_assistant }
101101
end
102102

103+
trait :prescriber do
104+
sequence(:email) { |n| "prescriber-#{n}@example.com" }
105+
role_code { nil }
106+
activity_codes { [CIS2Info::INDEPENDENT_PRESCRIBING_ACTIVITY_CODE] }
107+
fallback_role { :prescriber }
108+
end
109+
103110
trait :signed_in do
104111
current_sign_in_at { Time.current }
105112
current_sign_in_ip { "127.0.0.1" }
@@ -108,5 +115,6 @@
108115

109116
factory :admin, parent: :user, traits: %i[admin]
110117
factory :healthcare_assistant, parent: :user, traits: %i[healthcare_assistant]
118+
factory :prescriber, parent: :user, traits: %i[prescriber]
111119
factory :superuser, parent: :user, traits: %i[superuser]
112120
end

spec/models/user_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,50 @@
196196
end
197197
end
198198

199+
describe "#is_prescriber?" do
200+
subject { user.is_prescriber? }
201+
202+
context "cis2 is enabled", cis2: :enabled do
203+
context "when the user is a nurse" do
204+
let(:user) { build(:nurse) }
205+
206+
it { should be(true) }
207+
end
208+
209+
context "when the user is admin staff" do
210+
let(:user) { build(:admin) }
211+
212+
it { should be(false) }
213+
end
214+
215+
context "when the user is a prescriber" do
216+
let(:user) { build(:prescriber) }
217+
218+
it { should be(true) }
219+
end
220+
end
221+
222+
context "cis2 is disabled", cis2: :disabled do
223+
context "when the user is a nurse" do
224+
let(:user) { build(:nurse) }
225+
226+
it { should be(true) }
227+
end
228+
229+
context "when the user is admin staff" do
230+
let(:user) { build(:admin) }
231+
232+
it { should be(false) }
233+
end
234+
235+
context "when the user is a prescriber" do
236+
let(:user) { build(:prescriber) }
237+
238+
it { should be(true) }
239+
end
240+
end
241+
end
242+
199243
describe "#is_superuser?" do
200244
subject { user.is_superuser? }
201245

0 commit comments

Comments
 (0)