Merged PR 400: API修正(アカウント情報取得API)
## 概要 [Task2601: API修正(アカウント情報取得API)](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/2601) - 元PBI or タスクへのリンク(内容・目的などはそちらにあるはず) - 何をどう変更したか、追加したライブラリなど - アカウント情報取得APIで返却する値を追加 - テストしやすさを考慮し、getMyAccountInfoのパラメータと関数名を修正 - ログ出力について規約に沿った形に修正 - このPull Requestでの対象/対象外 - 影響範囲(他の機能にも影響があるか) ## レビューポイント - 特にレビューしてほしい箇所 アクセストークンを使ったユニットテストがあれば教えてください。 - 軽微なものや自明なものは記載不要 - 修正範囲が大きい場合などに記載 - 全体的にや仕様を満たしているか等は本当に必要な時のみ記載 ## UIの変更 - Before/Afterのスクショなど - スクショ置き場 ## 動作確認状況 - ローカルで確認 アクセストークンからアカウント情報を取得するAPIであるため、ポストマンで確認しました。 - 確認事項 - 追加したtier、country、parentAccountId、delegationPermission、primaryAdminUserId、secondryAdminUserIdが返却されることを確認。 - 異常系 - MySQLにてusersとaccountsがない場合のエラーメッセージが返却されるかを確認 ## 補足 - 相談、参考資料などがあれば
This commit is contained in:
parent
81d17fb57e
commit
78cbfd15e8
@ -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",
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -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>(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);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@ -364,38 +364,47 @@ export class AccountsService {
|
||||
}
|
||||
|
||||
/**
|
||||
* アクセストークンからアカウント情報を取得する
|
||||
* @param token
|
||||
* @returns accountId
|
||||
* パラメータのユーザIDからアカウント情報を取得する
|
||||
* @param externalId
|
||||
* @returns GetMyAccountResponse
|
||||
*/
|
||||
async getMyAccountInfo(token: AccessToken): Promise<GetMyAccountResponse> {
|
||||
this.logger.log(`[IN] ${this.getMyAccountInfo.name}`);
|
||||
|
||||
let userInfo: User;
|
||||
async getAccountInfo(
|
||||
context: Context,
|
||||
externalId: string,
|
||||
): Promise<GetMyAccountResponse> {
|
||||
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<TypistGroup[]> {
|
||||
|
||||
@ -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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user