Skip to content

Commit cb3b0c1

Browse files
committed
Update CSV filenames for weekly submission batches
Update the filenames for CSV attached to weekly batch emails to include both the start and end date of the batch.
1 parent daa0d62 commit cb3b0c1

File tree

4 files changed

+107
-37
lines changed

4 files changed

+107
-37
lines changed

app/lib/submission_filename_generator.rb

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class << self
44
PREFIX = "govuk_forms_".freeze
55
CSV_EXTENSION = ".csv".freeze
66
JSON_EXTENSION = ".json".freeze
7+
DATE_FORMAT = "%Y-%m-%d".freeze
78

89
def csv_filename(form_name:, submission_reference:)
910
filename(form_name:, submission_reference:, extension: CSV_EXTENSION)
@@ -17,23 +18,35 @@ def filename(form_name:, submission_reference:, extension:)
1718
reference_part = "_#{submission_reference}"
1819

1920
name_part_max_length = MAX_LENGTH - extension.length - PREFIX.length - reference_part.length
21+
name_part = name_part(form_name, name_part_max_length)
2022

21-
name_part = form_name
22-
.parameterize(separator: "_")
23-
.truncate(name_part_max_length, separator: "_", omission: "")
2423
"#{PREFIX}#{name_part}#{reference_part}#{extension}"
2524
end
2625

27-
def batch_csv_filename(form_name:, date:, mode:, form_version:)
28-
date_part = date.strftime("_%Y-%m-%d")
29-
mode_prefix = mode.preview? ? "test_" : ""
30-
version_part = form_version.presence ? "_#{form_version}" : ""
26+
def daily_batch_csv_filename(form_name:, mode:, csv_version:, date:)
27+
date_part = "_#{date.strftime(DATE_FORMAT)}"
28+
batch_csv_filename(form_name:, mode:, csv_version:, date_part:)
29+
end
3130

32-
name_part_max_length = MAX_LENGTH - CSV_EXTENSION.length - mode_prefix.length - PREFIX.length - date_part.length - version_part.length
31+
def weekly_batch_csv_filename(form_name:, mode:, csv_version:, begin_date:, end_date:)
32+
date_part = "_#{begin_date.strftime(DATE_FORMAT)}-#{end_date.strftime(DATE_FORMAT)}"
33+
batch_csv_filename(form_name:, mode:, csv_version:, date_part:)
34+
end
3335

34-
name_part = form_name
36+
private
37+
38+
def name_part(form_name, name_part_max_length)
39+
form_name
3540
.parameterize(separator: "_")
3641
.truncate(name_part_max_length, separator: "_", omission: "")
42+
end
43+
44+
def batch_csv_filename(form_name:, mode:, csv_version:, date_part:)
45+
mode_prefix = mode.preview? ? "test_" : ""
46+
version_part = csv_version.presence ? "_#{csv_version}" : ""
47+
48+
name_part_max_length = MAX_LENGTH - CSV_EXTENSION.length - mode_prefix.length - PREFIX.length - date_part.length - version_part.length
49+
name_part = name_part(form_name, name_part_max_length)
3750

3851
"#{mode_prefix}#{PREFIX}#{name_part}#{date_part}#{version_part}#{CSV_EXTENSION}"
3952
end

app/services/aws_ses_submission_batch_service.rb

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ def initialize(submissions_query:, form:, mode:)
66
end
77

88
def send_daily_batch(date:)
9-
files = csv_attachments(date)
9+
files = build_csv_files do |csv_version|
10+
SubmissionFilenameGenerator.daily_batch_csv_filename(
11+
form_name: @form.name,
12+
mode: @mode,
13+
csv_version:,
14+
date:,
15+
)
16+
end
1017

1118
mail = AwsSesSubmissionBatchMailer.daily_submission_batch_email(form: @form, date:, mode: @mode, files:).deliver_now
1219

@@ -15,7 +22,15 @@ def send_daily_batch(date:)
1522
end
1623

1724
def send_weekly_batch(begin_date:, end_date:)
18-
files = csv_attachments(end_date)
25+
files = build_csv_files do |csv_version|
26+
SubmissionFilenameGenerator.weekly_batch_csv_filename(
27+
form_name: @form.name,
28+
mode: @mode,
29+
csv_version:,
30+
begin_date: begin_date,
31+
end_date: end_date,
32+
)
33+
end
1934

2035
mail = AwsSesSubmissionBatchMailer.weekly_submission_batch_email(
2136
form: @form,
@@ -31,19 +46,13 @@ def send_weekly_batch(begin_date:, end_date:)
3146

3247
private
3348

34-
def csv_attachments(date_for_filename)
49+
def build_csv_files(&block)
3550
files = {}
3651

3752
csvs = CsvGenerator.generate_batched_submissions(submissions_query: @submissions_query, is_s3_submission: false)
3853
csvs.each.with_index(1) do |csv, index|
3954
csv_version = csvs.size > 1 ? index : nil
40-
filename = SubmissionFilenameGenerator.batch_csv_filename(
41-
form_name: @form.name,
42-
date: date_for_filename,
43-
mode: @mode,
44-
form_version: csv_version,
45-
)
46-
55+
filename = block.call(csv_version)
4756
files[filename] = csv
4857
end
4958

spec/lib/submission_filename_generator_spec.rb

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,17 @@
4545
end
4646
end
4747

48-
describe "#batch_csv_filename" do
48+
describe "#daily_batch_csv_filename" do
49+
subject(:filename) { described_class.daily_batch_csv_filename(form_name:, mode:, csv_version:, date:) }
50+
4951
let(:date) { Time.zone.local(2024, 6, 1) }
5052
let(:mode) { instance_double(Mode, preview?: false) }
51-
let(:form_version) { "1" }
53+
let(:csv_version) { "1" }
5254

5355
context "when there is a long form name that would cause the filename to be longer than 100 characters" do
5456
let(:form_name) { "A form name that will cause the filename to be truncated to obey the limit" }
5557

5658
it "truncates the form name in the filename" do
57-
filename = described_class.batch_csv_filename(form_name:, date:, mode:, form_version:)
5859
expect(filename).to eq("govuk_forms_a_form_name_that_will_cause_the_filename_to_be_truncated_to_obey_the_2024-06-01_1.csv")
5960
end
6061
end
@@ -63,17 +64,15 @@
6364
let(:form_name) { "Form name that will cause the filename to be 100 characters long exact" }
6465

6566
it "does not truncate the form name in the filename" do
66-
filename = described_class.batch_csv_filename(form_name:, date:, mode:, form_version:)
6767
expect(filename).to eq("govuk_forms_form_name_that_will_cause_the_filename_to_be_100_characters_long_exact_2024-06-01_1.csv")
6868
end
6969
end
7070

7171
context "when there is no form version provided" do
7272
let(:form_name) { "Form name" }
73-
let(:form_version) { nil }
73+
let(:csv_version) { nil }
7474

7575
it "does not include the form version in the filename" do
76-
filename = described_class.batch_csv_filename(form_name:, date:, mode:, form_version:)
7776
expect(filename).to eq("govuk_forms_form_name_2024-06-01.csv")
7877
end
7978
end
@@ -83,18 +82,67 @@
8382
let(:form_name) { "Form name" }
8483

8584
it "includes 'test_' in the filename" do
86-
filename = described_class.batch_csv_filename(form_name:, date:, mode:, form_version:)
8785
expect(filename).to eq("test_govuk_forms_form_name_2024-06-01_1.csv")
8886
end
8987

9088
context "when the form name would cause the filename to be over 100 characters long including the 'test_' prefix" do
9189
let(:form_name) { "A form name that will cause the filename to be truncated to obey the limit" }
9290

9391
it "truncates the form name in the filename" do
94-
filename = described_class.batch_csv_filename(form_name:, date:, mode:, form_version:)
9592
expect(filename).to eq("test_govuk_forms_a_form_name_that_will_cause_the_filename_to_be_truncated_to_obey_2024-06-01_1.csv")
9693
end
9794
end
9895
end
9996
end
97+
98+
describe "#weekly_batch_csv_filename" do
99+
subject(:filename) { described_class.weekly_batch_csv_filename(form_name:, mode:, csv_version:, begin_date:, end_date:) }
100+
101+
let(:begin_date) { Time.zone.local(2024, 6, 1) }
102+
let(:end_date) { Time.zone.local(2024, 6, 7) }
103+
let(:mode) { instance_double(Mode, preview?: false) }
104+
let(:csv_version) { "1" }
105+
106+
context "when there is a long form name that would cause the filename to be longer than 100 characters" do
107+
let(:form_name) { "A form name that will cause the filename to be truncated to obey" }
108+
109+
it "truncates the form name in the filename" do
110+
expect(filename).to eq("govuk_forms_a_form_name_that_will_cause_the_filename_to_be_truncated_to_2024-06-01-2024-06-07_1.csv")
111+
end
112+
end
113+
114+
context "when the form name would cause the filename to be exactly 100 characters long" do
115+
let(:form_name) { "Form name that will cause the filename to be 100 characterss" }
116+
117+
it "does not truncate the form name in the filename" do
118+
expect(filename).to eq("govuk_forms_form_name_that_will_cause_the_filename_to_be_100_characterss_2024-06-01-2024-06-07_1.csv")
119+
end
120+
end
121+
122+
context "when there is no form version provided" do
123+
let(:form_name) { "Form name" }
124+
let(:csv_version) { nil }
125+
126+
it "does not include the form version in the filename" do
127+
expect(filename).to eq("govuk_forms_form_name_2024-06-01-2024-06-07.csv")
128+
end
129+
end
130+
131+
context "when the mode is preview" do
132+
let(:mode) { instance_double(Mode, preview?: true) }
133+
let(:form_name) { "Form name" }
134+
135+
it "includes 'test_' in the filename" do
136+
expect(filename).to eq("test_govuk_forms_form_name_2024-06-01-2024-06-07_1.csv")
137+
end
138+
139+
context "when the form name would cause the filename to be over 100 characters long including the 'test_' prefix" do
140+
let(:form_name) { "A form name that will cause the filename to be truncated" }
141+
142+
it "truncates the form name in the filename" do
143+
expect(filename).to eq("test_govuk_forms_a_form_name_that_will_cause_the_filename_to_be_2024-06-01-2024-06-07_1.csv")
144+
end
145+
end
146+
end
147+
end
100148
end

spec/services/aws_ses_submission_batch_service_spec.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
let(:date) { Date.new(2024, 6, 1) }
1313

1414
before do
15-
allow(SubmissionFilenameGenerator).to receive(:batch_csv_filename).and_return("filename.csv")
15+
allow(SubmissionFilenameGenerator).to receive(:daily_batch_csv_filename).and_return("filename.csv")
1616
allow(CsvGenerator).to receive(:generate_batched_submissions).and_return(%w[csv-content])
1717
create_list(:submission, 3, form_document:)
1818
end
1919

2020
it "calls the SubmissionFilenameGenerator with a nil form version" do
21-
expect(SubmissionFilenameGenerator).to receive(:batch_csv_filename).with(form_name: form.name, date:, mode:, form_version: nil)
21+
expect(SubmissionFilenameGenerator).to receive(:daily_batch_csv_filename).with(form_name: form.name, date:, mode:, csv_version: nil)
2222
service.send_daily_batch(date:)
2323
end
2424

@@ -46,12 +46,12 @@
4646
context "when the csv generator returns multiple csv versions" do
4747
before do
4848
allow(CsvGenerator).to receive(:generate_batched_submissions).and_return(%w[csv-content csv-content-2])
49-
allow(SubmissionFilenameGenerator).to receive(:batch_csv_filename).and_return("filename.csv", "filename-2.csv")
49+
allow(SubmissionFilenameGenerator).to receive(:daily_batch_csv_filename).and_return("filename.csv", "filename-2.csv")
5050
end
5151

5252
it "calls the SubmissionFilenameGenerator to generate a filename for each form version" do
53-
expect(SubmissionFilenameGenerator).to receive(:batch_csv_filename).with(form_name: form.name, date:, mode:, form_version: 1)
54-
expect(SubmissionFilenameGenerator).to receive(:batch_csv_filename).with(form_name: form.name, date:, mode:, form_version: 2)
53+
expect(SubmissionFilenameGenerator).to receive(:daily_batch_csv_filename).with(form_name: form.name, date:, mode:, csv_version: 1)
54+
expect(SubmissionFilenameGenerator).to receive(:daily_batch_csv_filename).with(form_name: form.name, date:, mode:, csv_version: 2)
5555
service.send_daily_batch(date:)
5656
end
5757

@@ -74,13 +74,13 @@
7474
let(:end_date) { Date.new(2024, 6, 7) }
7575

7676
before do
77-
allow(SubmissionFilenameGenerator).to receive(:batch_csv_filename).and_return("filename.csv")
77+
allow(SubmissionFilenameGenerator).to receive(:weekly_batch_csv_filename).and_return("filename.csv")
7878
allow(CsvGenerator).to receive(:generate_batched_submissions).and_return(%w[csv-content])
7979
create_list(:submission, 3, form_document:)
8080
end
8181

8282
it "calls the SubmissionFilenameGenerator with a nil form version" do
83-
expect(SubmissionFilenameGenerator).to receive(:batch_csv_filename).with(form_name: form.name, date: end_date, mode:, form_version: nil)
83+
expect(SubmissionFilenameGenerator).to receive(:weekly_batch_csv_filename).with(form_name: form.name, mode:, csv_version: nil, begin_date:, end_date:)
8484
service.send_weekly_batch(begin_date:, end_date:)
8585
end
8686

@@ -109,12 +109,12 @@
109109
context "when the csv generator returns multiple csv versions" do
110110
before do
111111
allow(CsvGenerator).to receive(:generate_batched_submissions).and_return(%w[csv-content csv-content-2])
112-
allow(SubmissionFilenameGenerator).to receive(:batch_csv_filename).and_return("filename.csv", "filename-2.csv")
112+
allow(SubmissionFilenameGenerator).to receive(:weekly_batch_csv_filename).and_return("filename.csv", "filename-2.csv")
113113
end
114114

115115
it "calls the SubmissionFilenameGenerator to generate a filename for each form version" do
116-
expect(SubmissionFilenameGenerator).to receive(:batch_csv_filename).with(form_name: form.name, date: end_date, mode:, form_version: 1)
117-
expect(SubmissionFilenameGenerator).to receive(:batch_csv_filename).with(form_name: form.name, date: end_date, mode:, form_version: 2)
116+
expect(SubmissionFilenameGenerator).to receive(:weekly_batch_csv_filename).with(form_name: form.name, mode:, csv_version: 1, begin_date:, end_date:)
117+
expect(SubmissionFilenameGenerator).to receive(:weekly_batch_csv_filename).with(form_name: form.name, mode:, csv_version: 2, begin_date:, end_date:)
118118
service.send_weekly_batch(begin_date:, end_date:)
119119
end
120120

0 commit comments

Comments
 (0)