## 概要 [Task2465: QueryBuilderのままにするか、service層でソートするか検討する](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/2465) [タスク 2404: controller.tsに不要なログが残っている](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/OMDSDictation/_sprints/taskboard/OMDSDictation%20%E3%83%81%E3%83%BC%E3%83%A0/OMDSDictation/%E3%82%B9%E3%83%97%E3%83%AA%E3%83%B3%E3%83%88%2016-1?workitem=2404) - 元PBI or タスクへのリンク(内容・目的などはそちらにあるはず) - 何をどう変更したか、追加したライブラリなど 割り当て可能ライセンス取得APIのQueryBuilder使用部分に対してコメント文を追加した。 ついでに過去MISOチームで実装したcontrollerの処理で残っていた不要なデバッグログを削除した。 - このPull Requestでの対象/対象外 - 影響範囲(他の機能にも影響があるか) なし ## レビューポイント - 特にレビューしてほしい箇所 - 軽微なものや自明なものは記載不要 - 修正範囲が大きい場合などに記載 - 全体的にや仕様を満たしているか等は本当に必要な時のみ記載 ## UIの変更 - Before/Afterのスクショなど - スクショ置き場 ## 動作確認状況 - ローカルで確認、develop環境で確認など ## 補足 - 相談、参考資料などがあれば
572 lines
16 KiB
TypeScript
572 lines
16 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
HttpStatus,
|
|
Post,
|
|
Get,
|
|
Req,
|
|
UseGuards,
|
|
Param,
|
|
} from '@nestjs/common';
|
|
import {
|
|
ApiOperation,
|
|
ApiResponse,
|
|
ApiTags,
|
|
ApiBearerAuth,
|
|
} from '@nestjs/swagger';
|
|
import { ErrorResponse } from '../../common/error/types/types';
|
|
import { Request } from 'express';
|
|
import { AccountsService } from './accounts.service';
|
|
import {
|
|
CreateAccountRequest,
|
|
CreateAccountResponse,
|
|
GetLicenseSummaryRequest,
|
|
GetLicenseSummaryResponse,
|
|
GetMyAccountResponse,
|
|
GetTypistGroupsResponse,
|
|
GetTypistsResponse,
|
|
CreatePartnerAccountRequest,
|
|
CreatePartnerAccountResponse,
|
|
GetPartnerLicensesRequest,
|
|
GetPartnerLicensesResponse,
|
|
GetOrderHistoriesRequest,
|
|
GetOrderHistoriesResponse,
|
|
IssueLicenseRequest,
|
|
IssueLicenseResponse,
|
|
GetDealersResponse,
|
|
CreateTypistGroupResponse,
|
|
CreateTypistGroupRequest,
|
|
GetTypistGroupResponse,
|
|
GetTypistGroupRequest,
|
|
UpdateTypistGroupRequest,
|
|
UpdateTypistGroupRequestParam,
|
|
} from './types/types';
|
|
import { USER_ROLES, ADMIN_ROLES, TIERS } from '../../constants';
|
|
import { AuthGuard } from '../../common/guards/auth/authguards';
|
|
import { RoleGuard } from '../../common/guards/role/roleguards';
|
|
import { retrieveAuthorizationToken } from '../../common/http/helper';
|
|
import { AccessToken } from '../../common/token';
|
|
import jwt from 'jsonwebtoken';
|
|
import { makeContext } from '../../common/log';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
@ApiTags('accounts')
|
|
@Controller('accounts')
|
|
export class AccountsController {
|
|
constructor(
|
|
private readonly accountService: AccountsService, //private readonly cryptoService: CryptoService,
|
|
) {}
|
|
|
|
@Post()
|
|
@ApiResponse({
|
|
status: HttpStatus.OK,
|
|
type: CreateAccountResponse,
|
|
description: '成功時のレスポンス',
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.BAD_REQUEST,
|
|
description: '登録済みユーザーからの登録など',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
description: '想定外のサーバーエラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiOperation({ operationId: 'createAccount' })
|
|
async createAccount(
|
|
@Body() body: CreateAccountRequest,
|
|
): Promise<CreateAccountResponse> {
|
|
const {
|
|
companyName,
|
|
country,
|
|
dealerAccountId,
|
|
adminMail,
|
|
adminPassword,
|
|
adminName,
|
|
acceptedTermsVersion,
|
|
} = body;
|
|
const role = USER_ROLES.NONE;
|
|
|
|
const context = makeContext(uuidv4());
|
|
|
|
await this.accountService.createAccount(
|
|
context,
|
|
companyName,
|
|
country,
|
|
dealerAccountId,
|
|
adminMail,
|
|
adminPassword,
|
|
adminName,
|
|
role,
|
|
acceptedTermsVersion,
|
|
);
|
|
|
|
return {};
|
|
}
|
|
|
|
@ApiResponse({
|
|
status: HttpStatus.OK,
|
|
type: GetLicenseSummaryResponse,
|
|
description: '成功時のレスポンス',
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.UNAUTHORIZED,
|
|
description: '認証エラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
description: '想定外のサーバーエラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiOperation({
|
|
operationId: 'getLicenseSummary',
|
|
description: '指定したアカウントのライセンス集計情報を取得します',
|
|
})
|
|
@ApiBearerAuth()
|
|
@UseGuards(AuthGuard)
|
|
@UseGuards(RoleGuard.requireds({ roles: [ADMIN_ROLES.ADMIN] }))
|
|
@Post('licenses/summary')
|
|
async getLicenseSummary(
|
|
@Req() req: Request,
|
|
@Body() body: GetLicenseSummaryRequest,
|
|
): Promise<GetLicenseSummaryResponse> {
|
|
const response = await this.accountService.getLicenseSummary(
|
|
body.accountId,
|
|
);
|
|
return response;
|
|
}
|
|
|
|
@ApiResponse({
|
|
status: HttpStatus.OK,
|
|
type: GetMyAccountResponse,
|
|
description: '成功時のレスポンス',
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.BAD_REQUEST,
|
|
description: '該当アカウントがDBに存在しない場合',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.UNAUTHORIZED,
|
|
description: '認証エラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
description: '想定外のサーバーエラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiOperation({
|
|
operationId: 'getMyAccount',
|
|
description: 'ログインしているユーザーのアカウント情報を取得します',
|
|
})
|
|
@ApiBearerAuth()
|
|
@UseGuards(AuthGuard)
|
|
@UseGuards(RoleGuard.requireds({ roles: [ADMIN_ROLES.ADMIN] }))
|
|
@Get('me')
|
|
async getMyAccount(@Req() req: Request): Promise<GetMyAccountResponse> {
|
|
// アクセストークン取得
|
|
const accessToken = retrieveAuthorizationToken(req);
|
|
const payload = jwt.decode(accessToken, { json: true }) as AccessToken;
|
|
//アカウントID取得処理
|
|
const accountInfo = await this.accountService.getMyAccountInfo(payload);
|
|
return accountInfo;
|
|
}
|
|
|
|
@ApiResponse({
|
|
status: HttpStatus.OK,
|
|
type: GetTypistsResponse,
|
|
description: '成功時のレスポンス',
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.UNAUTHORIZED,
|
|
description: '認証エラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
description: '想定外のサーバーエラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiOperation({
|
|
operationId: 'getTypists',
|
|
description:
|
|
'ログインしているユーザーのアカウント配下のタイピスト一覧を取得します',
|
|
})
|
|
@ApiBearerAuth()
|
|
@UseGuards(AuthGuard)
|
|
@Get('typists')
|
|
async getTypists(@Req() req: Request): Promise<GetTypistsResponse> {
|
|
const accessToken = retrieveAuthorizationToken(req);
|
|
const payload = jwt.decode(accessToken, { json: true }) as AccessToken;
|
|
|
|
const typists = await this.accountService.getTypists(payload.userId);
|
|
|
|
return { typists };
|
|
}
|
|
|
|
@ApiResponse({
|
|
status: HttpStatus.OK,
|
|
type: GetTypistGroupsResponse,
|
|
description: '成功時のレスポンス',
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.UNAUTHORIZED,
|
|
description: '認証エラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
description: '想定外のサーバーエラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiOperation({
|
|
operationId: 'getTypistGroups',
|
|
description:
|
|
'ログインしているユーザーのアカウント配下のタイピストグループ一覧を取得します',
|
|
})
|
|
@ApiBearerAuth()
|
|
@UseGuards(AuthGuard)
|
|
@Get('typist-groups')
|
|
async getTypistGroups(@Req() req: Request): Promise<GetTypistGroupsResponse> {
|
|
const accessToken = retrieveAuthorizationToken(req);
|
|
const payload = jwt.decode(accessToken, { json: true }) as AccessToken;
|
|
|
|
const typistGroups = await this.accountService.getTypistGroups(
|
|
payload.userId,
|
|
);
|
|
|
|
return { typistGroups };
|
|
}
|
|
|
|
@ApiResponse({
|
|
status: HttpStatus.OK,
|
|
type: GetTypistGroupsResponse,
|
|
description: '成功時のレスポンス',
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.BAD_REQUEST,
|
|
description: 'グループが存在しない場合',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.UNAUTHORIZED,
|
|
description: '認証エラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
description: '想定外のサーバーエラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiOperation({
|
|
operationId: 'getTypistGroup',
|
|
description:
|
|
'ログインしているユーザーのアカウント配下でIDで指定されたタイピストグループを取得します',
|
|
})
|
|
@ApiBearerAuth()
|
|
@UseGuards(AuthGuard)
|
|
@UseGuards(RoleGuard.requireds({ roles: [ADMIN_ROLES.ADMIN] }))
|
|
@Get('typist-groups/:typistGroupId')
|
|
async getTypistGroup(
|
|
@Req() req: Request,
|
|
@Param() param: GetTypistGroupRequest,
|
|
): Promise<GetTypistGroupResponse> {
|
|
console.log(req.header('Authorization'));
|
|
// アクセストークン取得
|
|
const accessToken = retrieveAuthorizationToken(req);
|
|
const payload = jwt.decode(accessToken, { json: true }) as AccessToken;
|
|
|
|
console.log(param.typistGroupId);
|
|
|
|
return { typistGroupName: '', typistIds: [] };
|
|
}
|
|
|
|
@ApiResponse({
|
|
status: HttpStatus.OK,
|
|
type: CreateTypistGroupResponse,
|
|
description: '成功時のレスポンス',
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.BAD_REQUEST,
|
|
description: 'グループ名が空の場合/ユーザーが存在しない場合',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.UNAUTHORIZED,
|
|
description: '認証エラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
description: '想定外のサーバーエラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiOperation({
|
|
operationId: 'createTypistGroup',
|
|
description:
|
|
'ログインしているユーザーのアカウント配下にタイピストグループを追加します',
|
|
})
|
|
@ApiBearerAuth()
|
|
@UseGuards(AuthGuard)
|
|
@UseGuards(RoleGuard.requireds({ roles: [ADMIN_ROLES.ADMIN] }))
|
|
@Post('typist-groups')
|
|
async createTypistGroup(
|
|
@Req() req: Request,
|
|
@Body() body: CreateTypistGroupRequest,
|
|
): Promise<CreateTypistGroupResponse> {
|
|
const { typistGroupName, typistIds } = body;
|
|
// アクセストークン取得
|
|
const accessToken = retrieveAuthorizationToken(req);
|
|
const { userId } = jwt.decode(accessToken, { json: true }) as AccessToken;
|
|
const context = makeContext(userId);
|
|
await this.accountService.createTypistGroup(
|
|
context,
|
|
userId,
|
|
typistGroupName,
|
|
typistIds,
|
|
);
|
|
return {};
|
|
}
|
|
|
|
@ApiResponse({
|
|
status: HttpStatus.OK,
|
|
type: CreateTypistGroupResponse,
|
|
description: '成功時のレスポンス',
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.BAD_REQUEST,
|
|
description: 'グループ名が空の場合/ユーザーが存在しない場合',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.UNAUTHORIZED,
|
|
description: '認証エラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
description: '想定外のサーバーエラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiOperation({
|
|
operationId: 'updateTypistGroup',
|
|
description:
|
|
'ログインしているユーザーのアカウント配下でIDで指定されたタイピストグループを編集します',
|
|
})
|
|
@ApiBearerAuth()
|
|
@UseGuards(AuthGuard)
|
|
@UseGuards(RoleGuard.requireds({ roles: [ADMIN_ROLES.ADMIN] }))
|
|
@Post('typist-groups/:typistGroupId')
|
|
async updateTypistGroup(
|
|
@Req() req: Request,
|
|
@Body() body: UpdateTypistGroupRequest,
|
|
@Param() param: UpdateTypistGroupRequestParam,
|
|
): Promise<CreateTypistGroupResponse> {
|
|
// アクセストークン取得
|
|
const accessToken = retrieveAuthorizationToken(req);
|
|
const payload = jwt.decode(accessToken, { json: true }) as AccessToken;
|
|
|
|
console.log(param.typistGroupId);
|
|
console.log(body.typistGroupName);
|
|
console.log(body.typistIds);
|
|
|
|
return {};
|
|
}
|
|
|
|
@Post('partner')
|
|
@ApiResponse({
|
|
status: HttpStatus.OK,
|
|
type: CreatePartnerAccountResponse,
|
|
description: '成功時のレスポンス',
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.UNAUTHORIZED,
|
|
description: '認証エラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.BAD_REQUEST,
|
|
description: '登録済みユーザーからの登録など',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
description: '想定外のサーバーエラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiOperation({ operationId: 'createPartnerAccount' })
|
|
@ApiBearerAuth()
|
|
@UseGuards(AuthGuard)
|
|
@UseGuards(
|
|
RoleGuard.requireds({
|
|
roles: [ADMIN_ROLES.ADMIN],
|
|
tiers: [TIERS.TIER1, TIERS.TIER2, TIERS.TIER3],
|
|
}),
|
|
)
|
|
async createPartnerAccount(
|
|
@Req() req: Request,
|
|
@Body() body: CreatePartnerAccountRequest,
|
|
): Promise<CreatePartnerAccountResponse> {
|
|
const { companyName, country, email, adminName } = body;
|
|
const accessToken = retrieveAuthorizationToken(req);
|
|
const payload = jwt.decode(accessToken, { json: true }) as AccessToken;
|
|
|
|
const context = makeContext(payload.userId);
|
|
|
|
await this.accountService.createPartnerAccount(
|
|
context,
|
|
companyName,
|
|
country,
|
|
email,
|
|
adminName,
|
|
payload.userId,
|
|
payload.tier,
|
|
);
|
|
|
|
return {};
|
|
}
|
|
|
|
@Post('partner-licenses')
|
|
@ApiResponse({
|
|
status: HttpStatus.OK,
|
|
type: GetPartnerLicensesResponse,
|
|
description: '成功時のレスポンス',
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.UNAUTHORIZED,
|
|
description: '認証エラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
description: '想定外のサーバーエラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiOperation({ operationId: 'getPartnerLicenses' })
|
|
@ApiBearerAuth()
|
|
@UseGuards(AuthGuard)
|
|
@UseGuards(
|
|
RoleGuard.requireds({
|
|
roles: [ADMIN_ROLES.ADMIN],
|
|
tiers: [TIERS.TIER1, TIERS.TIER2, TIERS.TIER3, TIERS.TIER4],
|
|
}),
|
|
)
|
|
async getPartnerLicenses(
|
|
@Req() req: Request,
|
|
@Body() body: GetPartnerLicensesRequest,
|
|
): Promise<GetPartnerLicensesResponse> {
|
|
const { limit, offset, accountId } = body;
|
|
|
|
const getPartnerLicensesResponse =
|
|
await this.accountService.getPartnerLicenses(limit, offset, accountId);
|
|
|
|
return getPartnerLicensesResponse;
|
|
}
|
|
|
|
@Post('order-histories')
|
|
@ApiResponse({
|
|
status: HttpStatus.OK,
|
|
type: GetOrderHistoriesResponse,
|
|
description: '成功時のレスポンス',
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.UNAUTHORIZED,
|
|
description: '認証エラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
description: '想定外のサーバーエラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiOperation({ operationId: 'getOrderHistories' })
|
|
@ApiBearerAuth()
|
|
@UseGuards(AuthGuard)
|
|
@UseGuards(
|
|
RoleGuard.requireds({
|
|
roles: [ADMIN_ROLES.ADMIN],
|
|
}),
|
|
)
|
|
async getOrderHistories(
|
|
@Req() req: Request,
|
|
@Body() body: GetOrderHistoriesRequest,
|
|
): Promise<GetOrderHistoriesResponse> {
|
|
const { limit, offset, accountId } = body;
|
|
|
|
const getOrderHistoriesResponse =
|
|
await this.accountService.getOrderHistories(limit, offset, accountId);
|
|
|
|
return getOrderHistoriesResponse;
|
|
}
|
|
|
|
@Post('/licenses/issue')
|
|
@ApiResponse({
|
|
status: HttpStatus.OK,
|
|
type: IssueLicenseResponse,
|
|
description: '成功時のレスポンス',
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.BAD_REQUEST,
|
|
description:
|
|
'自身のライセンス数が不足している場合/すでに対象注文が発行済の場合',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.UNAUTHORIZED,
|
|
description: '認証エラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
description: '想定外のサーバーエラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiOperation({ operationId: 'issueLicense' })
|
|
@ApiBearerAuth()
|
|
@UseGuards(AuthGuard)
|
|
@UseGuards(
|
|
RoleGuard.requireds({
|
|
roles: [ADMIN_ROLES.ADMIN],
|
|
tiers: [TIERS.TIER1, TIERS.TIER2, TIERS.TIER3, TIERS.TIER4],
|
|
}),
|
|
)
|
|
async issueLicense(
|
|
@Req() req: Request,
|
|
@Body() body: IssueLicenseRequest,
|
|
): Promise<IssueLicenseResponse> {
|
|
const { orderedAccountId, poNumber } = body;
|
|
|
|
const token = retrieveAuthorizationToken(req);
|
|
const accessToken = jwt.decode(token, { json: true }) as AccessToken;
|
|
|
|
const context = makeContext(accessToken.userId);
|
|
await this.accountService.issueLicense(
|
|
context,
|
|
orderedAccountId,
|
|
accessToken.userId,
|
|
accessToken.tier,
|
|
poNumber,
|
|
);
|
|
return {};
|
|
}
|
|
|
|
@Get('/dealers')
|
|
@ApiResponse({
|
|
status: HttpStatus.OK,
|
|
type: GetDealersResponse,
|
|
description: '成功時のレスポンス',
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
description: '想定外のサーバーエラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiOperation({ operationId: 'getDealers' })
|
|
async getDealers(): Promise<GetDealersResponse> {
|
|
return await this.accountService.getDealers();
|
|
}
|
|
}
|