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 { //アクセストークンからユーザー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, ); } } } }