## 概要 [Task2650: API実装(テンプレートファイル一覧取得API)](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/2650) - テンプレートファイル一覧取得APIとテストを実装しました。 ## レビューポイント - サービスの配置、リポジトリの呼び出しは適切か - テストケースは適切か - テスト用にtemplates配下にテンプレートファイル追加関数を追加したが適切か ## UIの変更 - なし ## 動作確認状況 - ローカルで確認
58 lines
1.9 KiB
TypeScript
58 lines
1.9 KiB
TypeScript
import { Controller, Get, HttpStatus, Req, UseGuards } from '@nestjs/common';
|
|
import {
|
|
ApiBearerAuth,
|
|
ApiOperation,
|
|
ApiResponse,
|
|
ApiTags,
|
|
} from '@nestjs/swagger';
|
|
import jwt from 'jsonwebtoken';
|
|
import { AccessToken } from '../../common/token';
|
|
import { ErrorResponse } from '../../common/error/types/types';
|
|
import { GetTemplatesResponse } from './types/types';
|
|
import { AuthGuard } from '../../common/guards/auth/authguards';
|
|
import { RoleGuard } from '../../common/guards/role/roleguards';
|
|
import { ADMIN_ROLES } from '../../constants';
|
|
import { retrieveAuthorizationToken } from '../../common/http/helper';
|
|
import { Request } from 'express';
|
|
import { makeContext } from '../../common/log';
|
|
import { TemplatesService } from './templates.service';
|
|
|
|
@ApiTags('templates')
|
|
@Controller('templates')
|
|
export class TemplatesController {
|
|
constructor(private readonly templatesService: TemplatesService) {}
|
|
|
|
@ApiResponse({
|
|
status: HttpStatus.OK,
|
|
type: GetTemplatesResponse,
|
|
description: '成功時のレスポンス',
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.UNAUTHORIZED,
|
|
description: '認証エラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiResponse({
|
|
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
description: '想定外のサーバーエラー',
|
|
type: ErrorResponse,
|
|
})
|
|
@ApiOperation({
|
|
operationId: 'getTemplates',
|
|
description: 'アカウント内のテンプレートファイルの一覧を取得します',
|
|
})
|
|
@ApiBearerAuth()
|
|
@UseGuards(AuthGuard)
|
|
@UseGuards(RoleGuard.requireds({ roles: [ADMIN_ROLES.ADMIN] }))
|
|
@Get()
|
|
async getTemplates(@Req() req: Request): Promise<GetTemplatesResponse> {
|
|
const token = retrieveAuthorizationToken(req);
|
|
const { userId } = jwt.decode(token, { json: true }) as AccessToken;
|
|
|
|
const context = makeContext(userId);
|
|
const templates = await this.templatesService.getTemplates(context, userId);
|
|
|
|
return { templates };
|
|
}
|
|
}
|