From f38733f9b2eafd57bd9fb9a129ed0ab23c5a9c3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B9=AF=E6=9C=AC=20=E9=96=8B?= Date: Tue, 21 Nov 2023 10:48:57 +0000 Subject: [PATCH] =?UTF-8?q?Merged=20PR=20589:=20=E3=83=A9=E3=82=A4?= =?UTF-8?q?=E3=82=BB=E3=83=B3=E3=82=B9=E3=83=81=E3=82=A7=E3=83=83=E3=82=AF?= =?UTF-8?q?=E5=87=A6=E7=90=86=E3=82=92licenses=E3=81=BE=E3=81=9F=E3=81=AFu?= =?UTF-8?q?sers=E9=85=8D=E4=B8=8B=E3=81=AB=E7=A7=BB=E5=8B=95=E3=81=97?= =?UTF-8?q?=E3=81=A6=E5=85=B1=E9=80=9A=E9=83=A8=E5=93=81=E5=8C=96=E3=81=99?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 概要 [Task3084: ライセンスチェック処理をlicensesまたはusers配下に移動して共通部品化する](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/3084) - "チェック"する関数でなく、ライセンスの割当状態を取得する関数に変更 - 利用側では、割当状態を元にして必要なら例外をthrowする実装に変更 - 関数の機能を `LicensesRepository` が提供する形に変更 ## レビューポイント - 関数のシグネチャは問題なさそうか ## 動作確認状況 - テストは通過 --- .../src/features/files/files.module.ts | 2 + .../src/features/files/files.service.ts | 86 ++++++------------- .../licenses/licenses.repository.service.ts | 35 ++++++++ .../users/users.repository.service.ts | 18 ---- 4 files changed, 61 insertions(+), 80 deletions(-) diff --git a/dictation_server/src/features/files/files.module.ts b/dictation_server/src/features/files/files.module.ts index d5ca8cc..56ba499 100644 --- a/dictation_server/src/features/files/files.module.ts +++ b/dictation_server/src/features/files/files.module.ts @@ -9,6 +9,7 @@ import { BlobstorageModule } from '../../gateways/blobstorage/blobstorage.module import { TemplateFilesRepositoryModule } from '../../repositories/template_files/template_files.repository.module'; import { UserGroupsRepositoryModule } from '../../repositories/user_groups/user_groups.repository.module'; import { NotificationhubModule } from '../../gateways/notificationhub/notificationhub.module'; +import { LicensesRepositoryModule } from '../../repositories/licenses/licenses.repository.module'; @Module({ imports: [ @@ -20,6 +21,7 @@ import { NotificationhubModule } from '../../gateways/notificationhub/notificati TemplateFilesRepositoryModule, UserGroupsRepositoryModule, NotificationhubModule, + LicensesRepositoryModule, ], providers: [FilesService], controllers: [FilesController], diff --git a/dictation_server/src/features/files/files.service.ts b/dictation_server/src/features/files/files.service.ts index ae441af..0699419 100644 --- a/dictation_server/src/features/files/files.service.ts +++ b/dictation_server/src/features/files/files.service.ts @@ -35,7 +35,7 @@ import { LicenseExpiredError, LicenseNotAllocatedError, } from '../../repositories/licenses/errors/types'; -import { DateWithZeroTime } from '../licenses/types/types'; +import { LicensesRepositoryService } from '../../repositories/licenses/licenses.repository.service'; @Injectable() export class FilesService { @@ -48,6 +48,7 @@ export class FilesService { private readonly blobStorageService: BlobstorageService, private readonly userGroupsRepositoryService: UserGroupsRepositoryService, private readonly notificationhubService: NotificationhubService, + private readonly licensesRepository: LicensesRepositoryService, ) {} /** @@ -295,13 +296,16 @@ export class FilesService { if (user.account.locked) { throw new AccountLockedError('account is locked.'); } - // ライセンスの有効性をチェック - const { licenseError } = await this.checkLicenseValidityByUserId( - context, + + // ライセンスが有効でない場合、エラー + const { state } = await this.licensesRepository.getLicenseState( user.id, ); - if (licenseError) { - throw licenseError; + if (state === 'expired') { + throw new LicenseExpiredError('license is expired.'); + } + if (state === 'inallocated') { + throw new LicenseNotAllocatedError('license is not allocated.'); } } // 国に応じたリージョンのBlobストレージにコンテナが存在するか確認 @@ -379,13 +383,15 @@ export class FilesService { } // 第五階層のみチェック if (user.account.tier === TIERS.TIER5) { - // ライセンスの有効性をチェック - const { licenseError } = await this.checkLicenseValidityByUserId( - context, + // ライセンスが有効でない場合、エラー + const { state } = await this.licensesRepository.getLicenseState( user.id, ); - if (licenseError) { - throw licenseError; + if (state === 'expired') { + throw new LicenseExpiredError('license is expired.'); + } + if (state === 'inallocated') { + throw new LicenseNotAllocatedError('license is not allocated.'); } } accountId = user.account.id; @@ -545,13 +551,15 @@ export class FilesService { } // 第五階層のみチェック if (user.account.tier === TIERS.TIER5) { - // ライセンスの有効性をチェック - const { licenseError } = await this.checkLicenseValidityByUserId( - context, + // ライセンスが有効でない場合、エラー + const { state } = await this.licensesRepository.getLicenseState( user.id, ); - if (licenseError) { - throw licenseError; + if (state === 'expired') { + throw new LicenseExpiredError('license is expired.'); + } + if (state === 'inallocated') { + throw new LicenseNotAllocatedError('license is not allocated.'); } } accountId = user.account_id; @@ -801,50 +809,4 @@ export class FilesService { ); } } - - /** - * ユーザーに割り当てられているライセンスの有効性をチェックする。 - * ライセンスが割り当てられていない場合、またはライセンスが有効期限切れの場合、エラー返却する。 - * @param userId - * @returns licenseError? - */ - // TODO: TASK3084で共通部品化する - private async checkLicenseValidityByUserId( - context: Context, - userId: number, - ): Promise<{ licenseError?: Error }> { - this.logger.log( - `[IN] [${context.getTrackingId()}] ${ - this.checkLicenseValidityByUserId.name - } | params: { userId: ${userId} };`, - ); - try { - const allocatedLicense = await this.usersRepository.findLicenseByUserId( - userId, - ); - - if (!allocatedLicense) { - return { - licenseError: new LicenseNotAllocatedError( - 'license is not allocated.', - ), - }; - } else { - const currentDate = new DateWithZeroTime(); - if ( - allocatedLicense.expiry_date && - allocatedLicense.expiry_date < currentDate - ) { - return { - licenseError: new LicenseExpiredError('license is expired.'), - }; - } - } - - return {}; // エラーがない場合は空のオブジェクトを返す - } catch (e) { - // リポジトリ層のエラーやその他の例外をハンドリング - return e; - } - } } diff --git a/dictation_server/src/repositories/licenses/licenses.repository.service.ts b/dictation_server/src/repositories/licenses/licenses.repository.service.ts index 6f107fc..ddac253 100644 --- a/dictation_server/src/repositories/licenses/licenses.repository.service.ts +++ b/dictation_server/src/repositories/licenses/licenses.repository.service.ts @@ -643,4 +643,39 @@ export class LicensesRepositoryService { await orderRepo.save(targetOrder); }); } + + /** + * ライセンスの割当状態を取得します + * @param userId ユーザーID + * @error { Error } DBアクセス失敗時の例外 + * @returns Promise<{ state: 'allocated' | 'inallocated' | 'expired' }> + */ + async getLicenseState( + userId: number, + ): Promise<{ state: 'allocated' | 'inallocated' | 'expired' }> { + const allocatedLicense = await this.dataSource + .getRepository(License) + .findOne({ + where: { + allocated_user_id: userId, + status: LICENSE_ALLOCATED_STATUS.ALLOCATED, + }, + }); + + // ライセンスが割り当てられていない場合は未割当状態 + if (allocatedLicense == null) { + return { state: 'inallocated' }; + } + + // ライセンスの有効期限が過ぎている場合は期限切れ状態 + const currentDate = new DateWithZeroTime(); + if ( + allocatedLicense.expiry_date && + allocatedLicense.expiry_date < currentDate + ) { + return { state: 'expired' }; + } + + return { state: 'allocated' }; + } } diff --git a/dictation_server/src/repositories/users/users.repository.service.ts b/dictation_server/src/repositories/users/users.repository.service.ts index 97787de..af179e9 100644 --- a/dictation_server/src/repositories/users/users.repository.service.ts +++ b/dictation_server/src/repositories/users/users.repository.service.ts @@ -648,24 +648,6 @@ export class UsersRepositoryService { return originAccount.delegation_permission; }); } - /** - * ユーザーに割り当てられているライセンスを取得する - * @param userId ユーザーID - * @returns License - */ - async findLicenseByUserId(userId: number): Promise { - const allocatedLicense = await this.dataSource - .getRepository(License) - .findOne({ - where: { - allocated_user_id: userId, - status: LICENSE_ALLOCATED_STATUS.ALLOCATED, - }, - }); - - return allocatedLicense; - } - /** * ユーザーに紐づく各種情報を取得する * @param userId