Skip to content

Commit 3fb1667

Browse files
authored
Merge pull request #3590 from nhsuk/mavis-cli
Add "mavis" CLI runner and "generate" commands
2 parents 8ef3958 + 14ba7cb commit 3fb1667

File tree

11 files changed

+305
-3
lines changed

11 files changed

+305
-3
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ gem "config"
3131
gem "csv"
3232
gem "devise"
3333
gem "discard"
34+
gem "dry-cli"
3435
gem "factory_bot_rails"
3536
gem "faker"
3637
gem "faraday"

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ GEM
207207
docile (1.4.0)
208208
domain_name (0.6.20240107)
209209
drb (2.2.3)
210+
dry-cli (1.2.0)
210211
email_validator (2.2.4)
211212
activemodel
212213
erb (5.0.1)
@@ -736,6 +737,7 @@ DEPENDENCIES
736737
debug
737738
devise
738739
discard
740+
dry-cli
739741
factory_bot_instruments
740742
factory_bot_rails
741743
faker

app/lib/mavis_cli.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
module MavisCLI
4+
extend Dry::CLI::Registry
5+
6+
def self.load_rails
7+
require_relative "../../config/environment"
8+
end
9+
end
10+
11+
require_relative "mavis_cli/generate/cohort_imports"
12+
require_relative "mavis_cli/generate/consents"
13+
require_relative "mavis_cli/generate/vaccination_records"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../../mavis_cli"
4+
5+
module MavisCLI
6+
module Generate
7+
class CohortImports < Dry::CLI::Command
8+
desc "Generate cohort imports"
9+
option :patients,
10+
type: :integer,
11+
required: true,
12+
default: 10,
13+
desc: "Number of patients to create"
14+
15+
def call(patients:)
16+
MavisCLI.load_rails
17+
18+
::Generate::CohortImports.call(patient_count: patients.to_i)
19+
end
20+
end
21+
end
22+
23+
register "generate", aliases: ["g"] do |prefix|
24+
prefix.register "cohort-imports", Generate::CohortImports
25+
end
26+
end
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../../mavis_cli"
4+
5+
module MavisCLI
6+
module Generate
7+
class Consents < Dry::CLI::Command
8+
desc "Generate consents"
9+
option :organisation,
10+
aliases: ["-o"],
11+
default: "A9A5A",
12+
desc: "ODS code of organisation to generate consents for"
13+
option :programme_type,
14+
aliases: ["-p"],
15+
default: "hpv",
16+
desc:
17+
"Programme type to generate consents for (hpv, menacwy, td_ipv, etc)"
18+
option :session_id,
19+
aliases: ["-s"],
20+
desc:
21+
"Generate consents for patients in a session, instead of across the entire organisation"
22+
option :given,
23+
default: 0,
24+
aliases: ["-g"],
25+
desc: "Number of given consents to create"
26+
option :needing_triage,
27+
default: 0,
28+
aliases: ["-N"],
29+
desc: "Number of given consents that need triage to create"
30+
option :refused,
31+
default: 0,
32+
aliases: ["-r"],
33+
desc: "Number of refused consents to create"
34+
35+
def call(
36+
organisation:,
37+
programme_type:,
38+
given:,
39+
needing_triage:,
40+
refused:,
41+
session_id: nil,
42+
**
43+
)
44+
MavisCLI.load_rails
45+
46+
session = Session.find(session_id) if session_id
47+
48+
::Generate::Consents.call(
49+
organisation: Organisation.find_by(ods_code: organisation),
50+
programme: Programme.find_by(type: programme_type),
51+
session:,
52+
given: given.to_i,
53+
given_needs_triage: needing_triage.to_i,
54+
refused: refused.to_i
55+
)
56+
end
57+
end
58+
end
59+
60+
register "generate", aliases: ["g"] do |prefix|
61+
prefix.register "consents", Generate::Consents
62+
end
63+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
module MavisCLI
4+
module Generate
5+
class VaccinationRecords < Dry::CLI::Command
6+
desc "Generate vaccination records (and attendances if required)"
7+
option :organisation,
8+
aliases: ["-o"],
9+
default: "A9A5A",
10+
desc: "ODS code of organisation to generate consents for"
11+
option :programme_type,
12+
aliases: ["-p"],
13+
default: "hpv",
14+
desc:
15+
"Programme type to generate consents for (hpv, menacwy, td_ipv, etc)"
16+
option :session_id,
17+
aliases: ["-s"],
18+
desc:
19+
"Generate consents for patients in a session, instead of" \
20+
" across the entire organisation"
21+
option :administered,
22+
default: 0,
23+
aliases: ["-A"],
24+
desc: "Number of administered vaccination records to create"
25+
26+
def call(
27+
organisation:,
28+
programme_type:,
29+
administered:,
30+
session_id: nil,
31+
**
32+
)
33+
MavisCLI.load_rails
34+
35+
session = Session.find(session_id) if session_id
36+
37+
::Generate::VaccinationRecords.call(
38+
organisation: Organisation.find_by(ods_code: organisation),
39+
programme:
40+
Programme.includes(:organisations).find_by(type: programme_type),
41+
session:,
42+
administered: administered.to_i
43+
)
44+
end
45+
end
46+
end
47+
48+
register "generate", aliases: ["g"] do |prefix|
49+
prefix.register "vaccination-records", Generate::VaccinationRecords
50+
end
51+
end

config/initializers/inflections.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
ActiveSupport::Inflector.inflections(:en) do |inflect|
1616
inflect.acronym "API"
1717
inflect.acronym "CIS2"
18+
inflect.acronym "CLI"
1819
inflect.acronym "CSRF"
1920
inflect.acronym "CSV"
2021
inflect.acronym "DPS"

lib/generate/consents.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ def patients
6060
end
6161

6262
def random_patients(count)
63-
patients
64-
.shuffle
65-
.take(count)
63+
@patients_randomised ||= patients.shuffle
64+
@patients_randomised
65+
.shift(count)
6666
.tap do
6767
if it.size < count
6868
raise "Only #{it.size} patients without consent in #{programme.type} programme"

script/mavis

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env ruby
2+
3+
require "bundler/setup"
4+
require "dry/cli"
5+
6+
require_relative "../app/lib/mavis_cli"
7+
8+
Dry::CLI.new(MavisCLI).call
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../../app/lib/mavis_cli"
4+
5+
describe "mavis generate consents" do
6+
it "generates consents" do
7+
given_an_organisation_exists
8+
and_there_are_three_patients_in_a_session
9+
when_i_run_the_generate_consents_command
10+
then_consents_are_created_with_the_given_statuses
11+
end
12+
13+
def given_an_organisation_exists
14+
@organisation = create(:organisation)
15+
@programme = create(:programme, type: "hpv")
16+
end
17+
18+
def and_there_are_three_patients_in_a_session
19+
@session =
20+
create(:session, organisation: @organisation, programmes: [@programme])
21+
parent = create(:parent)
22+
create_list(
23+
:patient,
24+
3,
25+
organisation: @organisation,
26+
session: @session,
27+
programmes: [@programme],
28+
parents: [parent]
29+
)
30+
end
31+
32+
def when_i_run_the_generate_consents_command
33+
@consent_count_before = @organisation.consents.count
34+
35+
Dry::CLI.new(MavisCLI).call(
36+
arguments: [
37+
"generate",
38+
"consents",
39+
"-o",
40+
@organisation.ods_code.to_s,
41+
"-p",
42+
@programme.type,
43+
"-s",
44+
@session.id.to_s,
45+
"-g",
46+
"1",
47+
"-N",
48+
"1",
49+
"-r",
50+
"1"
51+
]
52+
)
53+
end
54+
55+
def then_consents_are_created_with_the_given_statuses
56+
expect(@organisation.consents.count).to eq @consent_count_before + 3
57+
58+
expect(
59+
@organisation
60+
.patient_sessions
61+
.has_consent_status(:given, programme: @programme)
62+
.has_triage_status(:not_required, programme: @programme)
63+
.count
64+
).to eq 1
65+
end
66+
end

0 commit comments

Comments
 (0)