Merged PR 589: ライセンスチェック処理をlicensesまたはusers配下に移動して共通部品化する
## 概要 [Task3084: ライセンスチェック処理をlicensesまたはusers配下に移動して共通部品化する](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/3084) - "チェック"する関数でなく、ライセンスの割当状態を取得する関数に変更 - 利用側では、割当状態を元にして必要なら例外をthrowする実装に変更 - 関数の機能を `LicensesRepository` が提供する形に変更 ## レビューポイント - 関数のシグネチャは問題なさそうか ## 動作確認状況 - テストは通過
This commit is contained in:
parent
68aaa0e548
commit
f38733f9b2
@ -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],
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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' };
|
||||
}
|
||||
}
|
||||
|
||||
@ -648,24 +648,6 @@ export class UsersRepositoryService {
|
||||
return originAccount.delegation_permission;
|
||||
});
|
||||
}
|
||||
/**
|
||||
* ユーザーに割り当てられているライセンスを取得する
|
||||
* @param userId ユーザーID
|
||||
* @returns License
|
||||
*/
|
||||
async findLicenseByUserId(userId: number): Promise<License | null> {
|
||||
const allocatedLicense = await this.dataSource
|
||||
.getRepository(License)
|
||||
.findOne({
|
||||
where: {
|
||||
allocated_user_id: userId,
|
||||
status: LICENSE_ALLOCATED_STATUS.ALLOCATED,
|
||||
},
|
||||
});
|
||||
|
||||
return allocatedLicense;
|
||||
}
|
||||
|
||||
/**
|
||||
* ユーザーに紐づく各種情報を取得する
|
||||
* @param userId
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user