Skip to content

Commit 0ca3e48

Browse files
authored
Merge pull request #1205 from Crown-Commercial-Service/feature/nrmi-177-delete-validation-failed-submission-entries-when-resolved
NRMI-177
2 parents 410e947 + 91089f1 commit 0ca3e48

4 files changed

Lines changed: 32 additions & 4 deletions

File tree

app/models/submission.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
class Submission < ApplicationRecord
22
ERRORED_ROW_LIMIT = 10
3+
REPLACEMENT_STATES = %i[validation_failed ingest_failed in_review completed].freeze
4+
5+
after_save :cleanup_prev_failed_entries, if: :state_changed_to_replacement?
36

47
include AASM
58

@@ -123,6 +126,17 @@ def call_workday(workday_reference)
123126

124127
private
125128

129+
def cleanup_prev_failed_entries
130+
task.submissions.where(aasm_state: 'validation_failed').where.not(id: id).find_each do |s|
131+
s.entries.destroy_all
132+
s.staging_entries.destroy_all
133+
end
134+
end
135+
136+
def state_changed_to_replacement?
137+
saved_change_to_aasm_state? && REPLACEMENT_STATES.include?(aasm_state.to_sym)
138+
end
139+
126140
def enqueue_reversal_invoice_creation_job(user)
127141
SubmissionReversalInvoiceCreationJob.perform_later(self, user)
128142
end

docker-compose.test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ services:
2727
db-test:
2828
image: postgres
2929
volumes:
30-
- pg_test_data:/var/lib/postgresql/data/:cached
30+
- test_data:/var/lib/postgresql/data/:cached
3131
networks:
3232
- tests
3333
restart: on-failure
@@ -39,5 +39,5 @@ networks:
3939
tests:
4040

4141
volumes:
42-
pg_test_data: {}
42+
test_data: {}
4343
node_modules:

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ services:
5656
db:
5757
image: postgres
5858
volumes:
59-
- pg_data:/var/lib/postgresql/data/:cached
59+
- data:/var/lib/postgresql/data/:cached
6060
restart: on-failure
6161
container_name: "data-submission-service-api_db"
6262
networks:
@@ -72,7 +72,7 @@ services:
7272
private:
7373

7474
volumes:
75-
pg_data: {}
75+
data: {}
7676
node_modules:
7777

7878
networks:

spec/models/submission_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@
1010
it { is_expected.to have_many(:files) }
1111
it { is_expected.to have_many(:entries) }
1212

13+
describe 'replacing a validation_failed submission' do
14+
let(:task) { FactoryBot.create(:task) }
15+
let!(:failed_submission) { FactoryBot.create(:submission_with_invalid_entries, task: task) }
16+
17+
Submission::REPLACEMENT_STATES.each do |new_state|
18+
it "deletes entries and staging entries for the failed submission when the new state is #{new_state}" do
19+
FactoryBot.create(:submission, aasm_state: new_state, task: task)
20+
21+
expect(failed_submission.entries.reload).to be_empty
22+
expect(failed_submission.staging_entries.reload).to be_empty
23+
end
24+
end
25+
end
26+
1327
describe '#ready_for_review state machine event' do
1428
it 'transitions from processing to in_review, with a valid submission' do
1529
submission = FactoryBot.create(:submission_with_validated_entries, aasm_state: :processing)

0 commit comments

Comments
 (0)