diff --git a/dictation_server/src/api/odms/openapi.json b/dictation_server/src/api/odms/openapi.json index bf171db..9931cf2 100644 --- a/dictation_server/src/api/odms/openapi.json +++ b/dictation_server/src/api/odms/openapi.json @@ -2837,9 +2837,21 @@ "type": "object", "properties": { "accountId": { "type": "number" }, - "companyName": { "type": "string" } + "companyName": { "type": "string" }, + "tier": { "type": "number" }, + "country": { "type": "string" }, + "parentAccountId": { "type": "number" }, + "delegationPermission": { "type": "boolean" }, + "primaryAdminUserId": { "type": "number" }, + "secondryAdminUserId": { "type": "number" } }, - "required": ["accountId", "companyName"] + "required": [ + "accountId", + "companyName", + "tier", + "country", + "delegationPermission" + ] }, "GetMyAccountResponse": { "type": "object", diff --git a/dictation_server/src/features/accounts/accounts.controller.ts b/dictation_server/src/features/accounts/accounts.controller.ts index 88cd108..27a3980 100644 --- a/dictation_server/src/features/accounts/accounts.controller.ts +++ b/dictation_server/src/features/accounts/accounts.controller.ts @@ -190,8 +190,12 @@ export class AccountsController { // アクセストークン取得 const accessToken = retrieveAuthorizationToken(req); const payload = jwt.decode(accessToken, { json: true }) as AccessToken; + const context = makeContext(payload.userId); //アカウントID取得処理 - const accountInfo = await this.accountService.getMyAccountInfo(payload); + const accountInfo = await this.accountService.getAccountInfo( + context, + payload.userId, + ); return accountInfo; } diff --git a/dictation_server/src/features/accounts/accounts.service.spec.ts b/dictation_server/src/features/accounts/accounts.service.spec.ts index f83453c..2e30743 100644 --- a/dictation_server/src/features/accounts/accounts.service.spec.ts +++ b/dictation_server/src/features/accounts/accounts.service.spec.ts @@ -4757,3 +4757,56 @@ describe('パートナー一覧取得', () => { expect(partners.total).toBe(0); }); }); + +describe('getAccountInfo', () => { + 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('パラメータのユーザに対応するアカウント情報を取得できる', async () => { + const module = await makeTestingModule(source); + // 第五階層のアカウント作成 + const { account, admin } = await makeTestAccount(source, { + parent_account_id: 123, + }); + + const service = module.get(AccountsService); + const context = makeContext(admin.external_id); + + const accountResponse = await service.getAccountInfo( + context, + admin.external_id, + ); + + //実行結果を確認 + { + expect(accountResponse.account.accountId).toBe(account.id); + expect(accountResponse.account.companyName).toBe(account.company_name); + expect(accountResponse.account.country).toBe(account.country); + expect(accountResponse.account.delegationPermission).toBe( + account.delegation_permission, + ); + expect(accountResponse.account.parentAccountId).toBe( + account.parent_account_id, + ); + expect(accountResponse.account.primaryAdminUserId).toBe( + account.primary_admin_user_id, + ); + expect(accountResponse.account.secondryAdminUserId).toBe(undefined); + expect(accountResponse.account.tier).toBe(account.tier); + } + }); +}); diff --git a/dictation_server/src/features/accounts/accounts.service.ts b/dictation_server/src/features/accounts/accounts.service.ts index acb6308..024089f 100644 --- a/dictation_server/src/features/accounts/accounts.service.ts +++ b/dictation_server/src/features/accounts/accounts.service.ts @@ -364,38 +364,47 @@ export class AccountsService { } /** - * アクセストークンからアカウント情報を取得する - * @param token - * @returns accountId + * パラメータのユーザIDからアカウント情報を取得する + * @param externalId + * @returns GetMyAccountResponse */ - async getMyAccountInfo(token: AccessToken): Promise { - this.logger.log(`[IN] ${this.getMyAccountInfo.name}`); - - let userInfo: User; + async getAccountInfo( + context: Context, + externalId: string, + ): Promise { + this.logger.log( + `[IN] [${context.trackingId}] ${this.getAccountInfo.name} | params: { ` + + `name: ${externalId}, };`, + ); try { - userInfo = await this.usersRepository.findUserByExternalId(token.userId); + let userInfo: User; + userInfo = await this.usersRepository.findUserByExternalId(externalId); + + let accountInfo: Account; + accountInfo = await this.accountRepository.findAccountById( + userInfo.account_id, + ); + + return { + account: { + accountId: userInfo.account_id, + companyName: accountInfo.company_name, + tier: accountInfo.tier, + country: accountInfo.country, + parentAccountId: accountInfo.parent_account_id ?? undefined, + delegationPermission: accountInfo.delegation_permission, + primaryAdminUserId: accountInfo.primary_admin_user_id ?? undefined, + secondryAdminUserId: accountInfo.secondary_admin_user_id ?? undefined, + }, + }; } catch (e) { + this.logger.error(`[${context.trackingId}] error=${e}`); switch (e.constructor) { case UserNotFoundError: throw new HttpException( makeErrorResponse('E010204'), HttpStatus.BAD_REQUEST, ); - default: - throw new HttpException( - makeErrorResponse('E009999'), - HttpStatus.INTERNAL_SERVER_ERROR, - ); - } - } - - let accountInfo: Account; - try { - accountInfo = await this.accountRepository.findAccountById( - userInfo.account_id, - ); - } catch (e) { - switch (e.constructor) { case AccountNotFoundError: throw new HttpException( makeErrorResponse('E010501'), @@ -407,15 +416,11 @@ export class AccountsService { HttpStatus.INTERNAL_SERVER_ERROR, ); } + } finally { + this.logger.log( + `[OUT] [${context.trackingId}] ${this.getAccountInfo.name}`, + ); } - - this.logger.log(`[OUT] ${this.getMyAccountInfo.name}`); - return { - account: { - accountId: userInfo.account_id, - companyName: accountInfo.company_name, - }, - }; } async getTypistGroups(externalId: string): Promise { diff --git a/dictation_server/src/features/accounts/types/types.ts b/dictation_server/src/features/accounts/types/types.ts index e67cb65..99672e3 100644 --- a/dictation_server/src/features/accounts/types/types.ts +++ b/dictation_server/src/features/accounts/types/types.ts @@ -103,8 +103,27 @@ export class GetLicenseSummaryResponse { export class Account { @ApiProperty() accountId: number; + @ApiProperty() companyName: string; + + @ApiProperty() + tier: number; + + @ApiProperty() + country: string; + + @ApiProperty({ required: false }) + parentAccountId?: number | undefined; + + @ApiProperty() + delegationPermission: boolean; + + @ApiProperty({ required: false }) + primaryAdminUserId?: number | undefined; + + @ApiProperty({ required: false }) + secondryAdminUserId?: number | undefined; } export class GetMyAccountResponse {