Skip to content

Commit 6ab5aee

Browse files
authored
Merge pull request #1243 from Crown-Commercial-Service/feature/nrmi-252-add-archiving-error-messaging
Feature/nrmi 252 add archiving error messaging
2 parents 2c32693 + 91e7db0 commit 6ab5aee

4 files changed

Lines changed: 86 additions & 4 deletions

File tree

app/controllers/admin/frameworks_controller.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,24 @@ def publish
8080

8181
def archive_confirmation; end
8282

83+
# rubocop:disable Metrics/AbcSize
8384
def archive
84-
if @framework.can_be_archived?
85-
@framework.archive!
85+
@framework.errors.clear
86+
87+
unless @framework.can_be_archived?
88+
flash[:failure] = @framework.errors.full_messages.to_sentence
89+
return redirect_to admin_framework_path(@framework)
90+
end
91+
92+
if @framework.archive
8693
flash[:success] = 'Framework archived successfully.'
8794
else
88-
flash[:failure] = 'Framework cannot be archived. Ensure it is published and has no active agreements.'
95+
flash[:failure] = @framework.errors.full_messages.to_sentence.presence || 'Error archiving framework.'
8996
end
97+
9098
redirect_to admin_framework_path(@framework)
9199
end
100+
# rubocop:enable Metrics/AbcSize
92101

93102
def unarchive_confirmation; end
94103

app/models/framework.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ def lot_has_suppliers_onboarded?(definition_source)
108108
end
109109

110110
def can_be_archived?
111-
published? && agreements.active.none?
111+
errors.add(:base, 'Framework must be published to be archived') unless published?
112+
errors.add(:base, 'Framework cannot be archived while it has active agreements') unless agreements.active.none?
113+
errors.empty?
112114
end
113115
end

spec/models/framework_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,26 @@
171171
expect(framework.definition).to eq(framework.definition)
172172
end
173173
end
174+
175+
describe '#can_be_archived?' do
176+
let(:framework) { create(:framework) }
177+
178+
context 'when not published' do
179+
before { framework.update(aasm_state: 'new') }
180+
181+
it 'returns false and adds an error' do
182+
expect(framework.can_be_archived?).to eq(false)
183+
expect(framework.errors[:base]).to include('Framework must be published to be archived')
184+
end
185+
end
186+
187+
context 'when published and has no active agreements' do
188+
before { framework.update(aasm_state: 'published') }
189+
190+
it 'returns true' do
191+
expect(framework.can_be_archived?).to eq(true)
192+
expect(framework.errors[:base]).to be_empty
193+
end
194+
end
195+
end
174196
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe 'Admin archives a framework', type: :request do
4+
include SingleSignOnHelpers
5+
6+
before do
7+
stub_govuk_bank_holidays_request
8+
mock_sso_with(email: 'admin@example.com')
9+
get '/auth/google_oauth2/callback'
10+
end
11+
12+
let(:definition_source) do
13+
<<~FDL
14+
Framework RM999 {
15+
Name 'Framework to be published'
16+
ManagementCharge 0.5% of 'Supplier Price'
17+
Lots {
18+
'1' -> 'Lot 1'
19+
'2' -> 'Second Lot'
20+
}
21+
InvoiceFields {
22+
InvoiceValue from 'Supplier Price'
23+
}
24+
}
25+
FDL
26+
end
27+
28+
let!(:framework) do
29+
create(:framework, aasm_state: 'published', short_name: 'RM999',
30+
name: 'Framework to be published', definition_source: definition_source)
31+
end
32+
33+
context 'when the framework has an active agreement' do
34+
before do
35+
supplier = create(:supplier, name: 'Test Supplier')
36+
create(:agreement, framework: framework, supplier: supplier, active: true)
37+
end
38+
39+
it 'cannot be archived and shows an error message' do
40+
post archive_admin_framework_path(framework)
41+
42+
expect(response).to redirect_to admin_framework_path(framework)
43+
follow_redirect!
44+
45+
expect(response.body).to include('Framework cannot be archived while it has active agreements')
46+
expect(framework.reload.aasm_state).to eq('published')
47+
end
48+
end
49+
end

0 commit comments

Comments
 (0)