diff --git a/dictation_server/src/common/test/modules.ts b/dictation_server/src/common/test/modules.ts index ed77ffc..b3a2eef 100644 --- a/dictation_server/src/common/test/modules.ts +++ b/dictation_server/src/common/test/modules.ts @@ -30,6 +30,8 @@ import { NotificationhubService } from '../../gateways/notificationhub/notificat import { FilesService } from '../../features/files/files.service'; import { LicensesService } from '../../features/licenses/licenses.service'; import { TasksService } from '../../features/tasks/tasks.service'; +import { TemplatesService } from '../../features/templates/templates.service'; +import { TemplatesModule } from '../../features/templates/templates.module'; export const makeTestingModule = async ( datasource: DataSource, @@ -50,6 +52,7 @@ export const makeTestingModule = async ( UsersModule, SendGridModule, LicensesModule, + TemplatesModule, AccountsRepositoryModule, UsersRepositoryModule, LicensesRepositoryModule, @@ -74,6 +77,7 @@ export const makeTestingModule = async ( FilesService, TasksService, LicensesService, + TemplatesService, ], }) .useMocker(async (token) => { diff --git a/dictation_server/src/features/templates/templates.controller.ts b/dictation_server/src/features/templates/templates.controller.ts index f850067..6d85968 100644 --- a/dictation_server/src/features/templates/templates.controller.ts +++ b/dictation_server/src/features/templates/templates.controller.ts @@ -47,11 +47,11 @@ export class TemplatesController { @Get() async getTemplates(@Req() req: Request): Promise { const token = retrieveAuthorizationToken(req); - const accessToken = jwt.decode(token, { json: true }) as AccessToken; + const { userId } = jwt.decode(token, { json: true }) as AccessToken; - const context = makeContext(accessToken.userId); - console.log(context.trackingId); + const context = makeContext(userId); + const templates = await this.templatesService.getTemplates(context, userId); - return { templates: [] }; + return { templates }; } } diff --git a/dictation_server/src/features/templates/templates.module.ts b/dictation_server/src/features/templates/templates.module.ts index 76aa447..ac14580 100644 --- a/dictation_server/src/features/templates/templates.module.ts +++ b/dictation_server/src/features/templates/templates.module.ts @@ -1,9 +1,11 @@ import { Module } from '@nestjs/common'; import { TemplatesController } from './templates.controller'; import { TemplatesService } from './templates.service'; +import { UsersRepositoryModule } from '../../repositories/users/users.repository.module'; +import { TemplateFilesRepositoryModule } from '../../repositories/template_files/template_files.repository.module'; @Module({ - imports: [], + imports: [UsersRepositoryModule, TemplateFilesRepositoryModule], providers: [TemplatesService], controllers: [TemplatesController], }) diff --git a/dictation_server/src/features/templates/templates.service.spec.ts b/dictation_server/src/features/templates/templates.service.spec.ts new file mode 100644 index 0000000..c29b168 --- /dev/null +++ b/dictation_server/src/features/templates/templates.service.spec.ts @@ -0,0 +1,108 @@ +import { DataSource } from 'typeorm'; +import { makeTestingModule } from '../../common/test/modules'; +import { TemplatesService } from './templates.service'; +import { createTemplateFile } from './test/utility'; +import { makeTestAccount } from '../../common/test/utility'; +import { makeContext } from '../../common/log'; +import { TemplateFilesRepositoryService } from '../../repositories/template_files/template_files.repository.service'; +import { HttpException, HttpStatus } from '@nestjs/common'; +import { makeErrorResponse } from '../../common/error/makeErrorResponse'; + +describe('getTemplates', () => { + 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 service = module.get(TemplatesService); + // 第五階層のアカウント作成 + const { account, admin } = await makeTestAccount(source, { tier: 5 }); + const context = makeContext(admin.external_id); + + const template1 = await createTemplateFile( + source, + account.id, + 'test1', + 'https://url1/test1', + ); + const template2 = await createTemplateFile( + source, + account.id, + 'test2', + 'https://url2/test2', + ); + + // 作成したデータを確認 + { + expect(template1.file_name).toBe('test1'); + expect(template2.file_name).toBe('test2'); + } + + const templates = await service.getTemplates(context, admin.external_id); + + //実行結果を確認 + { + expect(templates.length).toBe(2); + expect(templates[0].id).toBe(template1.id); + expect(templates[0].name).toBe(template1.file_name); + expect(templates[1].id).toBe(template2.id); + expect(templates[1].name).toBe(template2.file_name); + } + }); + + it('テンプレートファイル一覧を取得できる(0件)', async () => { + const module = await makeTestingModule(source); + const service = module.get(TemplatesService); + // 第五階層のアカウント作成 + const { admin } = await makeTestAccount(source, { tier: 5 }); + const context = makeContext(admin.external_id); + + const templates = await service.getTemplates(context, admin.external_id); + + //実行結果を確認 + { + expect(templates.length).toBe(0); + } + }); + + it('テンプレートファイル一覧の取得に失敗した場合、エラーとなること', async () => { + const module = await makeTestingModule(source); + const service = module.get(TemplatesService); + // 第五階層のアカウント作成 + const { admin } = await makeTestAccount(source, { tier: 5 }); + const context = makeContext(admin.external_id); + + //DBアクセスに失敗するようにする + const typistGroupService = module.get( + TemplateFilesRepositoryService, + ); + typistGroupService.getTemplateFiles = jest + .fn() + .mockRejectedValue('DB failed'); + + try { + await service.getTemplates(context, admin.external_id); + } catch (e) { + if (e instanceof HttpException) { + expect(e.getStatus()).toEqual(HttpStatus.INTERNAL_SERVER_ERROR); + expect(e.getResponse()).toEqual(makeErrorResponse('E009999')); + } else { + fail(); + } + } + }); +}); diff --git a/dictation_server/src/features/templates/templates.service.ts b/dictation_server/src/features/templates/templates.service.ts index cf1d193..d2a88c4 100644 --- a/dictation_server/src/features/templates/templates.service.ts +++ b/dictation_server/src/features/templates/templates.service.ts @@ -1,7 +1,56 @@ -import { Injectable, Logger } from '@nestjs/common'; +import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common'; +import { UsersRepositoryService } from '../../repositories/users/users.repository.service'; +import { TemplateFile } from './types/types'; +import { Context } from '../../common/log'; +import { makeErrorResponse } from '../../common/error/makeErrorResponse'; +import { TemplateFilesRepositoryService } from '../../repositories/template_files/template_files.repository.service'; @Injectable() export class TemplatesService { private readonly logger = new Logger(TemplatesService.name); - constructor() {} + constructor( + private readonly usersRepository: UsersRepositoryService, + private readonly templateFilesRepository: TemplateFilesRepositoryService, + ) {} + + /** + * アカウント内のテンプレートファイルの一覧を取得する + * @param context + * @param externalId + * @returns templates + */ + async getTemplates( + context: Context, + externalId: string, + ): Promise { + this.logger.log( + `[IN] [${context.trackingId}] ${this.getTemplates.name} | params: { externalId: ${externalId} };`, + ); + + try { + const { account_id: accountId } = + await this.usersRepository.findUserByExternalId(externalId); + + const templateFileRecords = + await this.templateFilesRepository.getTemplateFiles(accountId); + + // DBから取得したテンプレートファイルのレコードをレスポンス用に整形する + const resTemplates = templateFileRecords.map((templateFile) => ({ + id: templateFile.id, + name: templateFile.file_name, + })); + + return resTemplates; + } catch (e) { + this.logger.error(`error=${e}`); + throw new HttpException( + makeErrorResponse('E009999'), + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } finally { + this.logger.log( + `[OUT] [${context.trackingId}] ${this.getTemplates.name}`, + ); + } + } } diff --git a/dictation_server/src/features/templates/test/utility.ts b/dictation_server/src/features/templates/test/utility.ts new file mode 100644 index 0000000..2643e88 --- /dev/null +++ b/dictation_server/src/features/templates/test/utility.ts @@ -0,0 +1,28 @@ +import { DataSource } from 'typeorm'; +import { TemplateFile } from '../../../repositories/template_files/entity/template_file.entity'; + +export const createTemplateFile = async ( + datasource: DataSource, + accountId: number, + name: string, + url: string, +): Promise => { + const { identifiers } = await datasource.getRepository(TemplateFile).insert({ + account_id: accountId, + file_name: name, + url: url, + created_by: 'test_runner', + created_at: new Date(), + updated_by: 'updater', + updated_at: new Date(), + }); + + const template = identifiers.pop() as TemplateFile; + const templateFile = await datasource.getRepository(TemplateFile).findOne({ + where: { + id: template.id, + }, + }); + + return templateFile; +}; diff --git a/dictation_server/src/repositories/template_files/entity/template_file.entity.ts b/dictation_server/src/repositories/template_files/entity/template_file.entity.ts index 5c45d2f..41d6738 100644 --- a/dictation_server/src/repositories/template_files/entity/template_file.entity.ts +++ b/dictation_server/src/repositories/template_files/entity/template_file.entity.ts @@ -18,8 +18,6 @@ export class TemplateFile { url: string; @Column() file_name: string; - @Column({ nullable: true }) - deleted_at?: Date; @Column() created_by: string; @CreateDateColumn() diff --git a/dictation_server/src/repositories/template_files/template_files.repository.service.ts b/dictation_server/src/repositories/template_files/template_files.repository.service.ts index b35c191..ffe3b81 100644 --- a/dictation_server/src/repositories/template_files/template_files.repository.service.ts +++ b/dictation_server/src/repositories/template_files/template_files.repository.service.ts @@ -1,7 +1,25 @@ import { Injectable } from '@nestjs/common'; import { DataSource } from 'typeorm'; +import { TemplateFile } from './entity/template_file.entity'; @Injectable() export class TemplateFilesRepositoryService { constructor(private dataSource: DataSource) {} + + /** + * アカウント内のテンプレートファイルの一覧を取得する + * @param accountId + * @returns template files + */ + async getTemplateFiles(accountId: number): Promise { + return await this.dataSource.transaction(async (entityManager) => { + const templateFilesRepo = entityManager.getRepository(TemplateFile); + + const templates = await templateFilesRepo.find({ + where: { account_id: accountId }, + }); + + return templates; + }); + } }