maruyama.t 890b004a9c Merged PR 124: [PBI1221指摘対応]ライセンス注文APIの中で画面から渡ってきたパラメータのバリデーションを行う
## 概要
[Task1901: [PBI1221指摘対応]ライセンス注文APIの中で画面から渡ってきたパラメータのバリデーションを行う](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/1901)

- バリデーションチェック処理をコントローラに追加
- 未使用のimportを削除
- エラーテストで比較するメッセージが間違っていたので合わせて修正

## レビューポイント
- バリデーションのタイミングは適切か
- バリデーションの範囲に過不足がないか

## UIの変更
- なし

## 動作確認状況
- ローカルで確認

## 補足
- licenses.controller.ts以外もセルフチェックを行いました。
2023-06-05 07:06:20 +00:00

104 lines
2.9 KiB
TypeScript

import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common';
import { makeErrorResponse } from '../../common/error/makeErrorResponse';
import { AccessToken } from 'src/common/token';
import {
UsersRepositoryService,
UserNotFoundError,
} from '../../repositories/users/users.repository.service';
import {
AccountsRepositoryService,
AccountNotFoundError,
} from '../../repositories/accounts/accounts.repository.service';
import {
LicensesRepositoryService,
PoNumberAlreadyExistError,
} from '../../repositories/licenses/licenses.repository.service';
@Injectable()
export class LicensesService {
constructor(
private readonly usersRepository: UsersRepositoryService,
private readonly accountsRepository: AccountsRepositoryService,
private readonly licensesRepository: LicensesRepositoryService,
) {}
private readonly logger = new Logger(LicensesService.name);
/**
* license Orders
* @param token
* @param body
*/
async licenseOrders(
accessToken: AccessToken,
poNumber: string,
orderCount: number,
): Promise<void> {
//アクセストークンからユーザーIDを取得する
this.logger.log(`[IN] ${this.licenseOrders.name}`);
const userId = accessToken.userId;
let myAccountId: number;
let parentAccountId: number;
// ユーザIDからアカウントIDを取得する
try {
myAccountId = (await this.usersRepository.findUserByExternalId(userId))
.account_id;
} catch (e) {
switch (e.constructor) {
case UserNotFoundError:
throw new HttpException(
makeErrorResponse('E010204'),
HttpStatus.BAD_REQUEST,
);
default:
throw new HttpException(
makeErrorResponse('E009999'),
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
// 親アカウントIDを取得
try {
parentAccountId = (
await this.accountsRepository.findAccountById(myAccountId)
).parent_account_id;
} catch (e) {
switch (e.constructor) {
case AccountNotFoundError:
throw new HttpException(
makeErrorResponse('E010501'),
HttpStatus.BAD_REQUEST,
);
default:
throw new HttpException(
makeErrorResponse('E009999'),
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
try {
await this.licensesRepository.order(
poNumber,
myAccountId,
parentAccountId,
orderCount,
);
} catch (e) {
switch (e.constructor) {
case PoNumberAlreadyExistError:
throw new HttpException(
makeErrorResponse('E010401'),
HttpStatus.BAD_REQUEST,
);
default:
throw new HttpException(
makeErrorResponse('E009999'),
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}
}