-
Notifications
You must be signed in to change notification settings - Fork 166
Add initial Partner API with agencies and partner accounts #5054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| module Agreements | ||
| class AgencyBlueprint < Blueprinter::Base | ||
| identifier :abbreviation | ||
|
|
||
| field :name | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| module Agreements | ||
| class PartnerAccountBlueprint < Blueprinter::Base | ||
| identifier :requesting_agency | ||
|
|
||
| field :name | ||
| field :became_partner, datetime_format: '%Y-%m-%d' | ||
| field :status do |account, _options| | ||
| account.partner_status | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| module Agreements | ||
| module Db | ||
| class AccountsByAgency | ||
| def self.call | ||
| PartnerAccount. | ||
| includes(:agency, :partner_account_status). | ||
| group_by { |pa| pa.agency } | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| module Agreements | ||
| module Reports | ||
| class AgenciesReport < BaseReport | ||
| def initialize(agencies:) | ||
| @agencies = agencies | ||
| end | ||
|
|
||
| def run | ||
| save_report( | ||
| 'agencies', | ||
| AgencyBlueprint.render(agencies, root: :agencies), | ||
| '', | ||
| ) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| attr_reader :agencies | ||
| end | ||
| end | ||
| end |
22 changes: 22 additions & 0 deletions
22
app/services/agreements/reports/agency_partner_accounts_report.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| module Agreements | ||
| module Reports | ||
| class AgencyPartnerAccountsReport < BaseReport | ||
| def initialize(agency:, partner_accounts:) | ||
| @agency = agency.downcase | ||
| @partner_accounts = partner_accounts.sort_by(&:requesting_agency) | ||
| end | ||
|
|
||
| def run | ||
| save_report( | ||
| 'partner_accounts', | ||
| PartnerAccountBlueprint.render(partner_accounts, root: :partner_accounts), | ||
| "agencies/#{agency}/", | ||
| ) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| attr_reader :agency, :partner_accounts | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| module Agreements | ||
| module Reports | ||
| class BaseReport < ::Reports::BaseReport | ||
| def gen_s3_bucket_name | ||
| prefix = IdentityConfig.store.partner_api_bucket_prefix | ||
| "#{prefix}.#{ec2_data.account_id}-#{ec2_data.region}" | ||
| end | ||
|
|
||
| def save_report(report_name, body, path = nil) | ||
| if !IdentityConfig.store.s3_reports_enabled | ||
| logger.info('Not uploading report to S3, s3_reports_enabled is false') | ||
| return body | ||
| end | ||
| upload_file_to_s3(report_name, body, path) | ||
| end | ||
|
|
||
| def upload_file_to_s3(report_name, body, path) | ||
| s3_path = "#{path}#{report_name}" | ||
| upload_file_to_s3_bucket(path: s3_path, body: body, content_type: 'application/json') | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| module Agreements | ||
| module Reports | ||
| class PartnerApiReport | ||
| def run | ||
| return unless IdentityConfig.store.enable_partner_api | ||
|
|
||
| collect_account_data | ||
| upload_json_files | ||
| true | ||
| end | ||
|
|
||
| private | ||
|
|
||
| attr_reader :accounts_by_agency, :agencies | ||
|
|
||
| def collect_account_data | ||
| @accounts_by_agency = Agreements::Db::AccountsByAgency.call | ||
| @agencies = accounts_by_agency.keys | ||
| @accounts_by_agency = accounts_by_agency.transform_keys(&:abbreviation) | ||
| end | ||
|
|
||
| def upload_json_files | ||
| upload_agencies | ||
| upload_accounts | ||
| end | ||
|
|
||
| def upload_agencies | ||
| AgenciesReport.new(agencies: agencies).run | ||
| end | ||
|
|
||
| def upload_accounts | ||
| accounts_by_agency.each do |agency_abbr, accounts| | ||
| AgencyPartnerAccountsReport.new(agency: agency_abbr, partner_accounts: accounts).run | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Blueprinter.configure do |config| | ||
| config.sort_fields_by = :definition | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe Agreements::AgencyBlueprint do | ||
| let(:agency) do | ||
| create(:agency, abbreviation: 'ABC', name: 'Awesome Bureau of Comedy') | ||
| end | ||
| let(:expected) do | ||
| { | ||
| agencies: [ | ||
| { | ||
| abbreviation: 'ABC', | ||
| name: 'Awesome Bureau of Comedy', | ||
| }, | ||
| ], | ||
| }.to_json | ||
| end | ||
|
|
||
| it 'renders the appropriate json' do | ||
| expect(described_class.render([agency], root: :agencies)).to eq(expected) | ||
| end | ||
| end |
36 changes: 36 additions & 0 deletions
36
spec/blueprints/agreements/partner_account_blueprint_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe Agreements::PartnerAccountBlueprint do | ||
| let(:status) do | ||
| create( | ||
| :partner_account_status, | ||
| name: 'secret', | ||
| partner_name: 'public', | ||
| ) | ||
| end | ||
| let(:account) do | ||
| create( | ||
| :partner_account, | ||
| partner_account_status: status, | ||
| requesting_agency: 'ABC-DEF', | ||
| name: 'Department of Energy Fusion', | ||
| became_partner: '2021-01-01', | ||
| ) | ||
| end | ||
| let(:expected) do | ||
| { | ||
| partner_accounts: [ | ||
| { | ||
| requesting_agency: 'ABC-DEF', | ||
| name: 'Department of Energy Fusion', | ||
| became_partner: '2021-01-01', | ||
| status: 'public', | ||
| }, | ||
| ], | ||
| }.to_json | ||
| end | ||
|
|
||
| it 'renders the appropriate json' do | ||
| expect(described_class.render([account], root: :partner_accounts)).to eq(expected) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe Agreements::Db::AccountsByAgency do | ||
| let(:agency1) { create(:agency) } | ||
| let(:agency2) { create(:agency) } | ||
|
|
||
| before { clear_agreements_data } | ||
|
|
||
| describe '.call' do | ||
| it 'returns all partner accounts grouped by agency' do | ||
| account1, account2 = create_pair(:partner_account, agency: agency1) | ||
| account3 = create(:partner_account, agency: agency2) | ||
| expected = { | ||
| agency1 => [account1, account2], | ||
| agency2 => [account3], | ||
| } | ||
|
|
||
| expect(described_class.call).to eq(expected) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe Agreements::Reports::AgenciesReport do | ||
| it 'uploads the JSON serialization of the passed agencies' do | ||
| agency = build(:agency) | ||
| expected = Agreements::AgencyBlueprint.render([agency], root: :agencies) | ||
|
|
||
| expect(described_class.new(agencies: [agency]).run).to eq(expected) | ||
| end | ||
| end |
17 changes: 17 additions & 0 deletions
17
spec/services/agreements/reports/agency_partner_accounts_report_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe Agreements::Reports::AgencyPartnerAccountsReport do | ||
| let(:agency) { create(:agency) } | ||
| let(:partner_accounts) do | ||
| build_pair(:partner_account, agency: agency).sort_by(&:requesting_agency) | ||
| end | ||
|
|
||
| it 'uploads the JSON serialization of the passed partner accounts' do | ||
| expected = Agreements::PartnerAccountBlueprint.render(partner_accounts, root: :partner_accounts) | ||
| report_object = described_class.new( | ||
| agency: agency.abbreviation, | ||
| partner_accounts: partner_accounts, | ||
| ) | ||
| expect(report_object.run).to eq(expected) | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.