Skip to content

Commit 671e705

Browse files
authored
Merge pull request #6651 from NHSDigital/add-careplus-team-scopes
Add careplus_enabled, has_careplus_credentials, eligible_for_automated_careplus_reports scope/predicate
2 parents 07bad44 + 81463cb commit 671e705

13 files changed

Lines changed: 224 additions & 96 deletions

app/lib/mavis_cli/reports/export_automated_careplus.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ def call(
126126
# prevent this tool from creating database entries at all
127127

128128
ActiveRecord::Base.transaction do
129-
careplus_export =
130-
CareplusExport.create!(
129+
careplus_report =
130+
CareplusReport.create!(
131131
team:,
132132
academic_year: academic_year_value,
133133
date_from: parsed_start_date,
@@ -141,10 +141,10 @@ def call(
141141
)
142142

143143
now_iso = now.iso8601(6)
144-
CareplusExportVaccinationRecord.insert_all!(
144+
CareplusReportVaccinationRecord.insert_all!(
145145
records.map do |record|
146146
{
147-
careplus_export_id: careplus_export.id,
147+
careplus_report_id: careplus_report.id,
148148
vaccination_record_id: record.id,
149149
change_type: 0,
150150
created_at: now_iso,
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# == Schema Information
44
#
5-
# Table name: careplus_exports
5+
# Table name: careplus_reports
66
#
77
# id :bigint not null, primary key
88
# academic_year :integer not null
@@ -21,24 +21,24 @@
2121
#
2222
# Indexes
2323
#
24-
# index_careplus_exports_on_programme_types (programme_types) USING gin
25-
# index_careplus_exports_on_status_and_scheduled_at (status,scheduled_at)
26-
# index_careplus_exports_on_team_id (team_id)
27-
# index_careplus_exports_on_team_id_and_academic_year (team_id,academic_year)
24+
# index_careplus_reports_on_programme_types (programme_types) USING gin
25+
# index_careplus_reports_on_status_and_scheduled_at (status,scheduled_at)
26+
# index_careplus_reports_on_team_id (team_id)
27+
# index_careplus_reports_on_team_id_and_academic_year (team_id,academic_year)
2828
#
2929
# Foreign Keys
3030
#
3131
# fk_rails_... (team_id => teams.id)
3232
#
33-
class CareplusExport < ApplicationRecord
33+
class CareplusReport < ApplicationRecord
3434
include HasManyProgrammes
3535

3636
audited associated_with: :team
3737

3838
belongs_to :team
3939

40-
has_many :careplus_export_vaccination_records, dependent: :destroy
41-
has_many :vaccination_records, through: :careplus_export_vaccination_records
40+
has_many :careplus_report_vaccination_records, dependent: :destroy
41+
has_many :vaccination_records, through: :careplus_report_vaccination_records
4242

4343
enum :status, { pending: 0, sending: 1, sent: 2, failed: 3 }, validate: true
4444

app/models/careplus_export_vaccination_record.rb renamed to app/models/careplus_report_vaccination_record.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22

33
# == Schema Information
44
#
5-
# Table name: careplus_export_vaccination_records
5+
# Table name: careplus_report_vaccination_records
66
#
77
# change_type :integer not null
88
# created_at :datetime not null
99
# updated_at :datetime not null
10-
# careplus_export_id :bigint not null, primary key
10+
# careplus_report_id :bigint not null, primary key
1111
# vaccination_record_id :bigint not null, primary key
1212
#
1313
# Indexes
1414
#
15-
# idx_on_careplus_export_id_8ce4ed1ff0 (careplus_export_id)
16-
# idx_on_vaccination_record_id_d4c93aefb7 (vaccination_record_id)
15+
# idx_on_careplus_report_id_98876049c7 (careplus_report_id)
16+
# idx_on_vaccination_record_id_e7f05454ab (vaccination_record_id)
1717
#
1818
# Foreign Keys
1919
#
20-
# fk_rails_... (careplus_export_id => careplus_exports.id) ON DELETE => cascade
20+
# fk_rails_... (careplus_report_id => careplus_reports.id) ON DELETE => cascade
2121
# fk_rails_... (vaccination_record_id => vaccination_records.id)
2222
#
23-
class CareplusExportVaccinationRecord < ApplicationRecord
24-
self.primary_key = %i[careplus_export_id vaccination_record_id]
23+
class CareplusReportVaccinationRecord < ApplicationRecord
24+
self.primary_key = %i[careplus_report_id vaccination_record_id]
2525

26-
belongs_to :careplus_export
26+
belongs_to :careplus_report
2727
belongs_to :vaccination_record
2828

2929
enum :change_type, { created: 0, updated: 1 }, validate: true

app/models/team.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,23 @@ class Team < ApplicationRecord
8585

8686
encrypts :careplus_username, :careplus_password
8787

88+
scope :careplus_enabled,
89+
-> do
90+
where
91+
.not(careplus_staff_code: [nil, ""])
92+
.where.not(careplus_staff_type: [nil, ""])
93+
.where.not(careplus_venue_code: [nil, ""])
94+
end
95+
scope :has_careplus_credentials,
96+
-> do
97+
where
98+
.not(careplus_namespace: [nil, ""])
99+
.where.not(careplus_username: nil)
100+
.where.not(careplus_password: nil)
101+
end
102+
scope :eligible_for_automated_careplus_reports,
103+
-> { careplus_enabled.has_careplus_credentials }
104+
88105
enum :type,
89106
{ point_of_care: 0, national_reporting: 1, support: 2 },
90107
validate: true,
@@ -154,4 +171,9 @@ def careplus_enabled?
154171
careplus_staff_code.present? && careplus_staff_type.present? &&
155172
careplus_venue_code.present?
156173
end
174+
175+
def eligible_for_automated_careplus_reports?
176+
careplus_enabled? && careplus_username.present? &&
177+
careplus_password.present? && careplus_namespace.present?
178+
end
157179
end

config/locales/en.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,13 @@ en:
334334
blank: Choose a programme
335335
activerecord:
336336
attributes:
337-
careplus_export:
337+
careplus_report:
338338
statuses:
339339
pending: Pending
340340
sending: Sending
341341
sent: Sent
342342
failed: Failed
343-
careplus_export_vaccination_record:
343+
careplus_report_vaccination_record:
344344
change_types:
345345
created: Created
346346
updated: Updated
@@ -518,7 +518,7 @@ en:
518518
taken: This batch expiry date already exists for this batch number
519519
number:
520520
blank: Enter a batch number
521-
careplus_export:
521+
careplus_report:
522522
attributes:
523523
academic_year:
524524
blank: Enter an academic year
@@ -532,7 +532,7 @@ en:
532532
blank: Enter a scheduled date
533533
status:
534534
inclusion: Choose a status
535-
careplus_export_vaccination_record:
535+
careplus_report_vaccination_record:
536536
attributes:
537537
change_type:
538538
inclusion: Choose a change type
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+
class RenameCareplusExportsToReports < ActiveRecord::Migration[8.0]
4+
def change
5+
rename_table :careplus_exports, :careplus_reports
6+
7+
rename_table :careplus_export_vaccination_records,
8+
:careplus_report_vaccination_records
9+
rename_column :careplus_report_vaccination_records,
10+
:careplus_export_id,
11+
:careplus_report_id
12+
end
13+
end

db/schema.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[8.1].define(version: 2026_04_07_121005) do
13+
ActiveRecord::Schema[8.1].define(version: 2026_04_19_122121) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "pg_catalog.plpgsql"
1616
enable_extension "pg_trgm"
@@ -102,17 +102,17 @@
102102
t.index ["vaccine_id"], name: "index_batches_on_vaccine_id"
103103
end
104104

105-
create_table "careplus_export_vaccination_records", primary_key: ["careplus_export_id", "vaccination_record_id"], force: :cascade do |t|
106-
t.bigint "careplus_export_id", null: false
105+
create_table "careplus_report_vaccination_records", primary_key: ["careplus_report_id", "vaccination_record_id"], force: :cascade do |t|
106+
t.bigint "careplus_report_id", null: false
107107
t.integer "change_type", null: false
108108
t.datetime "created_at", null: false
109109
t.datetime "updated_at", null: false
110110
t.bigint "vaccination_record_id", null: false
111-
t.index ["careplus_export_id"], name: "idx_on_careplus_export_id_8ce4ed1ff0"
112-
t.index ["vaccination_record_id"], name: "idx_on_vaccination_record_id_d4c93aefb7"
111+
t.index ["careplus_report_id"], name: "idx_on_careplus_report_id_98876049c7"
112+
t.index ["vaccination_record_id"], name: "idx_on_vaccination_record_id_e7f05454ab"
113113
end
114114

115-
create_table "careplus_exports", force: :cascade do |t|
115+
create_table "careplus_reports", force: :cascade do |t|
116116
t.integer "academic_year", null: false
117117
t.datetime "created_at", null: false
118118
t.text "csv_data"
@@ -126,10 +126,10 @@
126126
t.integer "status", default: 0, null: false
127127
t.bigint "team_id", null: false
128128
t.datetime "updated_at", null: false
129-
t.index ["programme_types"], name: "index_careplus_exports_on_programme_types", using: :gin
130-
t.index ["status", "scheduled_at"], name: "index_careplus_exports_on_status_and_scheduled_at"
131-
t.index ["team_id", "academic_year"], name: "index_careplus_exports_on_team_id_and_academic_year"
132-
t.index ["team_id"], name: "index_careplus_exports_on_team_id"
129+
t.index ["programme_types"], name: "index_careplus_reports_on_programme_types", using: :gin
130+
t.index ["status", "scheduled_at"], name: "index_careplus_reports_on_status_and_scheduled_at"
131+
t.index ["team_id", "academic_year"], name: "index_careplus_reports_on_team_id_and_academic_year"
132+
t.index ["team_id"], name: "index_careplus_reports_on_team_id"
133133
end
134134

135135
create_table "class_imports", force: :cascade do |t|
@@ -1087,9 +1087,9 @@
10871087
add_foreign_key "attendance_records", "patients"
10881088
add_foreign_key "batches", "teams"
10891089
add_foreign_key "batches", "vaccines"
1090-
add_foreign_key "careplus_export_vaccination_records", "careplus_exports", on_delete: :cascade
1091-
add_foreign_key "careplus_export_vaccination_records", "vaccination_records"
1092-
add_foreign_key "careplus_exports", "teams"
1090+
add_foreign_key "careplus_report_vaccination_records", "careplus_reports", on_delete: :cascade
1091+
add_foreign_key "careplus_report_vaccination_records", "vaccination_records"
1092+
add_foreign_key "careplus_reports", "teams"
10931093
add_foreign_key "class_imports", "locations"
10941094
add_foreign_key "class_imports", "teams"
10951095
add_foreign_key "class_imports", "users", column: "uploaded_by_user_id"

spec/factories/careplus_export_vaccination_records.rb renamed to spec/factories/careplus_report_vaccination_records.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@
22

33
# == Schema Information
44
#
5-
# Table name: careplus_export_vaccination_records
5+
# Table name: careplus_report_vaccination_records
66
#
77
# change_type :integer not null
88
# created_at :datetime not null
99
# updated_at :datetime not null
10-
# careplus_export_id :bigint not null, primary key
10+
# careplus_report_id :bigint not null, primary key
1111
# vaccination_record_id :bigint not null, primary key
1212
#
1313
# Indexes
1414
#
15-
# idx_on_careplus_export_id_8ce4ed1ff0 (careplus_export_id)
16-
# idx_on_vaccination_record_id_d4c93aefb7 (vaccination_record_id)
15+
# idx_on_careplus_report_id_98876049c7 (careplus_report_id)
16+
# idx_on_vaccination_record_id_e7f05454ab (vaccination_record_id)
1717
#
1818
# Foreign Keys
1919
#
20-
# fk_rails_... (careplus_export_id => careplus_exports.id) ON DELETE => cascade
20+
# fk_rails_... (careplus_report_id => careplus_reports.id) ON DELETE => cascade
2121
# fk_rails_... (vaccination_record_id => vaccination_records.id)
2222
#
2323
FactoryBot.define do
24-
factory :careplus_export_vaccination_record do
25-
careplus_export
24+
factory :careplus_report_vaccination_record do
25+
careplus_report
2626
vaccination_record
2727
change_type { :created }
2828
end
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# == Schema Information
44
#
5-
# Table name: careplus_exports
5+
# Table name: careplus_reports
66
#
77
# id :bigint not null, primary key
88
# academic_year :integer not null
@@ -21,17 +21,17 @@
2121
#
2222
# Indexes
2323
#
24-
# index_careplus_exports_on_programme_types (programme_types) USING gin
25-
# index_careplus_exports_on_status_and_scheduled_at (status,scheduled_at)
26-
# index_careplus_exports_on_team_id (team_id)
27-
# index_careplus_exports_on_team_id_and_academic_year (team_id,academic_year)
24+
# index_careplus_reports_on_programme_types (programme_types) USING gin
25+
# index_careplus_reports_on_status_and_scheduled_at (status,scheduled_at)
26+
# index_careplus_reports_on_team_id (team_id)
27+
# index_careplus_reports_on_team_id_and_academic_year (team_id,academic_year)
2828
#
2929
# Foreign Keys
3030
#
3131
# fk_rails_... (team_id => teams.id)
3232
#
3333
FactoryBot.define do
34-
factory :careplus_export do
34+
factory :careplus_report do
3535
transient { programmes { [Programme.sample] } }
3636

3737
team { association(:team, programmes:) }
@@ -44,7 +44,7 @@
4444
trait :sent do
4545
status { :sent }
4646
sent_at { Time.current }
47-
csv_filename { "careplus_export.csv" }
47+
csv_filename { "careplus_report.csv" }
4848
csv_data { "col1,col2\nval1,val2" }
4949
end
5050

0 commit comments

Comments
 (0)