Skip to content

Commit

Permalink
Add scope to vendor api tokens for support dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
CatalinVoineag committed Jan 23, 2025
1 parent bd41b2a commit ff6077c
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/controllers/support_interface/api_tokens_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def index
VendorAPIToken.arel_table[:last_used_at].desc.nulls_last,
created_at: :desc,
)
@api_tokens_last_3_months_count = @api_tokens.where('last_used_at >= ?', 3.months.ago).count
@api_tokens_last_3_months_count = VendorAPIToken.used_in_last_3_months.count
end

def new
Expand Down
2 changes: 2 additions & 0 deletions app/models/vendor_api_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class VendorAPIToken < ApplicationRecord

audited associated_with: :provider

scope :used_in_last_3_months, -> { where('last_used_at >= ?', 3.months.ago) }

def self.create_with_random_token!(provider:)
unhashed_token, hashed_token = Devise.token_generator.generate(VendorAPIToken, :hashed_token)
create!(hashed_token:, provider:)
Expand Down
2 changes: 0 additions & 2 deletions app/views/support_interface/api_tokens/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
count: @api_tokens.count,
label: 'API tokens issued',
colour: :blue,
href: '#not-connected',
) %>
</div>
<div class="govuk-grid-column-one-half">
<%= render SupportInterface::TileComponent.new(
count: @api_tokens_last_3_months_count,
label: 'API tokens used in the last 3 months',
colour: :blue,
href: '#not-synced',
) %>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions spec/system/support_interface/api_tokens/view_tokens_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
given_i_am_signed_in
and_api_tokens_exist
when_i_visit_the_tokens_page
then_i_see_all_the_providers_with_api_tokens
then_i_see_the_count_of_providers_with_api_tokens
end

def given_i_am_signed_in
Expand All @@ -27,7 +27,7 @@ def when_i_visit_the_tokens_page
visit support_interface_api_tokens_path
end

def then_i_see_all_the_providers_with_api_tokens
def then_i_see_the_count_of_providers_with_api_tokens
expect(page).to have_content '2 API tokens issued'
expect(page).to have_content '1 API tokens used in the last 3 months'

Expand Down

0 comments on commit ff6077c

Please sign in to comment.