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