Skip to content

Commit

Permalink
check for active clients before deleting provider. #229
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Apr 10, 2019
1 parent 3231416 commit e44ab77
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 8 additions & 0 deletions app/controllers/concerns/countable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ def client_count(provider_id: nil)
response.results.total > 0 ? facet_by_cumulative_year(response.response.aggregations.cumulative_years.buckets) : []
end

# count active clients by provider. Provider can only be deleted when there are no active clients.
def active_client_count(provider_id: nil)
return 0 unless provider_id.present?

response = Client.query(nil, provider_id: provider_id, page: { number: 1, size: 0 })
response.results.total
end

# show provider count for admin
# count until the previous year if provider has been deleted
def provider_count(provider_id: nil)
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ def totals
# a provider with clients or prefixes can't be deleted
def destroy
logger = Logger.new(STDOUT)
if @provider.client_count.present?
message = "Can't delete provider that has clients."
if @provider.active_client_count > 0
message = "Can't delete provider that has active clients."
status = 400
logger.warn message
render json: { errors: [{ status: status.to_s, title: message }] }.to_json, status: status
Expand Down

0 comments on commit e44ab77

Please sign in to comment.