From 712efb8bb777bf273550142d9be92d668faa7e18 Mon Sep 17 00:00:00 2001 From: "saito.k" Date: Tue, 8 Aug 2023 00:28:27 +0000 Subject: [PATCH] =?UTF-8?q?Merged=20PR=20309:=20Dealer=E5=8F=96=E5=BE=97AP?= =?UTF-8?q?I=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 概要 [Task2348: Dealer取得API実装](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/2348) - アカウント登録画面で呼ぶDealer取得APIを実装 ## レビューポイント - 取得条件は正しいか - テストケースは足りているか - DBエラーによるエラーケースは作成しても効果が薄いと思ったため作成していません ## UIの変更 ## 動作確認状況 - ローカルで確認 ## 補足 - 相談、参考資料などがあれば --- .../features/accounts/accounts.controller.ts | 10 +-- .../accounts/accounts.service.spec.ts | 72 +++++++++++++++++++ .../src/features/accounts/accounts.service.ts | 33 ++++++++- .../accounts/accounts.repository.service.ts | 15 ++++ 4 files changed, 120 insertions(+), 10 deletions(-) diff --git a/dictation_server/src/features/accounts/accounts.controller.ts b/dictation_server/src/features/accounts/accounts.controller.ts index ddc8e53..70dbc76 100644 --- a/dictation_server/src/features/accounts/accounts.controller.ts +++ b/dictation_server/src/features/accounts/accounts.controller.ts @@ -423,14 +423,6 @@ export class AccountsController { }) @ApiOperation({ operationId: 'getDealers' }) async getDealers(): Promise { - return { - dealers: [ - { - id: 1, - name: 'Dealer1', - country: 'US', - }, - ], - }; + return await this.accountService.getDealers(); } } diff --git a/dictation_server/src/features/accounts/accounts.service.spec.ts b/dictation_server/src/features/accounts/accounts.service.spec.ts index 2e4f66e..c4925e8 100644 --- a/dictation_server/src/features/accounts/accounts.service.spec.ts +++ b/dictation_server/src/features/accounts/accounts.service.spec.ts @@ -18,6 +18,7 @@ import { import { DataSource } from 'typeorm'; import { makeTestingModule } from '../../common/test/modules'; import { AccountsService } from './accounts.service'; +import { TIERS } from '../../constants'; describe('AccountsService', () => { it('アカウントに紐づくライセンス情報を取得する', async () => { @@ -549,3 +550,74 @@ describe('getOrderHistories', () => { ); }); }); + +describe('getDealers', () => { + let source: DataSource = null; + beforeEach(async () => { + source = new DataSource({ + type: 'sqlite', + database: ':memory:', + logging: false, + entities: [__dirname + '/../../**/*.entity{.ts,.js}'], + synchronize: true, // trueにすると自動的にmigrationが行われるため注意 + }); + return source.initialize(); + }); + + afterEach(async () => { + await source.destroy(); + source = null; + }); + + it('Dealerを取得できる', async () => { + const module = await makeTestingModule(source); + const { accountId: accountId_1 } = await createAccount( + source, + 1, + TIERS.TIER4, + 'DEALER_1', + ); + const { accountId: accountId_2 } = await createAccount( + source, + 2, + TIERS.TIER4, + 'DEALER_2', + ); + const { accountId: accountId_3 } = await createAccount( + source, + 3, + TIERS.TIER4, + 'DEALER_3', + ); + const service = module.get(AccountsService); + + expect(await service.getDealers()).toEqual({ + dealers: [ + { + country: 'JP', + id: accountId_1, + name: 'DEALER_1', + }, + { + country: 'JP', + id: accountId_2, + name: 'DEALER_2', + }, + { + country: 'JP', + id: accountId_3, + name: 'DEALER_3', + }, + ], + }); + }); + it('0件でもDealerを取得できる', async () => { + const module = await makeTestingModule(source); + + const service = module.get(AccountsService); + + expect(await service.getDealers()).toEqual({ + dealers: [], + }); + }); +}); diff --git a/dictation_server/src/features/accounts/accounts.service.ts b/dictation_server/src/features/accounts/accounts.service.ts index fb97a50..9995b94 100644 --- a/dictation_server/src/features/accounts/accounts.service.ts +++ b/dictation_server/src/features/accounts/accounts.service.ts @@ -22,6 +22,8 @@ import { PartnerLicenseInfo, GetOrderHistoriesResponse, LicenseOrder, + GetDealersResponse, + Dealer, GetMyAccountResponse, } from './types/types'; import { DateWithZeroTime } from '../licenses/types/types'; @@ -263,7 +265,7 @@ export class AccountsService { account: { accountId: userInfo.account_id, companyName: accountInfo.company_name, - } + }, }; } @@ -584,4 +586,33 @@ export class AccountsService { this.logger.log(`[OUT] ${this.getOrderHistories.name}`); } } + + // dealersのアカウント情報を取得する + async getDealers(): Promise { + this.logger.log(`[IN] ${this.getDealers.name}`); + + try { + const dealerAccounts = await this.accountRepository.findDealerAccounts(); + + const dealers: GetDealersResponse = { + dealers: dealerAccounts.map((dealerAccount): Dealer => { + return { + id: dealerAccount.id, + name: dealerAccount.company_name, + country: dealerAccount.country, + }; + }), + }; + + return dealers; + } catch (e) { + this.logger.error(e); + throw new HttpException( + makeErrorResponse('E009999'), + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } finally { + this.logger.log(`[OUT] ${this.getDealers.name}`); + } + } } diff --git a/dictation_server/src/repositories/accounts/accounts.repository.service.ts b/dictation_server/src/repositories/accounts/accounts.repository.service.ts index c195fbf..2aabfff 100644 --- a/dictation_server/src/repositories/accounts/accounts.repository.service.ts +++ b/dictation_server/src/repositories/accounts/accounts.repository.service.ts @@ -21,6 +21,7 @@ import { import { LICENSE_ALLOCATED_STATUS, LICENSE_STATUS_ISSUE_REQUESTING, + TIERS, } from '../../constants'; import { LicenseSummaryInfo, @@ -551,4 +552,18 @@ export class AccountsRepositoryService { }; }); } + + /** + * Dealer(Tier4)アカウント情報を取得する + * @returns dealer accounts + */ + async findDealerAccounts(): Promise { + const accounts = await this.dataSource.getRepository(Account).find({ + where: { + tier: TIERS.TIER4, + }, + }); + + return accounts; + } }