From 249689a40ed2322cc65e8709285d03155a9c6bb3 Mon Sep 17 00:00:00 2001 From: "maruyama.t" Date: Tue, 22 Aug 2023 12:22:47 +0000 Subject: [PATCH] =?UTF-8?q?Merged=20PR=20347:=20=E5=89=B2=E3=82=8A?= =?UTF-8?q?=E5=BD=93=E3=81=A6=E5=8F=AF=E8=83=BD=E3=83=A9=E3=82=A4=E3=82=BB?= =?UTF-8?q?=E3=83=B3=E3=82=B9=E5=8F=96=E5=BE=97API=E3=82=92QueryBuilder?= =?UTF-8?q?=E3=81=A7=E3=81=AE=E5=AE=9F=E8=A3=85=E3=81=AB=E6=88=BB=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 概要 [Task2466: 割り当て可能ライセンス取得APIをQueryBuilderでの実装に戻す](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/2466) - 元PBI or タスクへのリンク(内容・目的などはそちらにあるはず) - 何をどう変更したか、追加したライブラリなど - EntityManegarで実装した部分について、レビューバック後のPOSTMANによる確認が漏れていました。 結果、MySQLにクエリを投入する際に、typeORMでNULLS FIRSTを使用できない旨のエラーが出てしまいました。 よって、暫定的にQueryBuilderを使用しての実装にしています。 EntityManagarを使用するか、QueryBuilderを使用するかは別途開発メンバで相談して、修正する場合は別タスクで追って対応いたします。 [タスク 2465: QueryBuilderのままにするか、service層でソートするか検討する]() - ライセンス種別の取得について、結果0件時の考慮が出来ていなかったので修正しています - このPull Requestでの対象/対象外 - 影響範囲(他の機能にも影響があるか) ## レビューポイント - 特にレビューしてほしい箇所 - 軽微なものや自明なものは記載不要 - 修正範囲が大きい場合などに記載 - 全体的にや仕様を満たしているか等は本当に必要な時のみ記載 ## UIの変更 - Before/Afterのスクショなど - スクショ置き場 ## 動作確認状況 - ローカルで確認 ## 補足 - 相談、参考資料などがあれば --- .../licenses/licenses.repository.service.ts | 80 ++++++++----------- 1 file changed, 35 insertions(+), 45 deletions(-) diff --git a/dictation_server/src/repositories/licenses/licenses.repository.service.ts b/dictation_server/src/repositories/licenses/licenses.repository.service.ts index 258da24..51d227c 100644 --- a/dictation_server/src/repositories/licenses/licenses.repository.service.ts +++ b/dictation_server/src/repositories/licenses/licenses.repository.service.ts @@ -409,42 +409,28 @@ export class LicensesRepositoryService { myAccountId: number, ): Promise { const nowDate = new DateWithZeroTime(); - - return await this.dataSource.transaction(async (entityManager) => { - const licenseRepo = entityManager.getRepository(License); - const allocatableLicenses = await licenseRepo.find({ - where: [ - { - account_id: myAccountId, - status: In([ - LICENSE_ALLOCATED_STATUS.UNALLOCATED, - LICENSE_ALLOCATED_STATUS.REUSABLE, - ]), - expiry_date: MoreThanOrEqual(nowDate), - }, - { - account_id: myAccountId, - status: In([ - LICENSE_ALLOCATED_STATUS.UNALLOCATED, - LICENSE_ALLOCATED_STATUS.REUSABLE, - ]), - expiry_date: IsNull(), - }, + const licenseRepo = this.dataSource.getRepository(License); + const queryBuilder = licenseRepo + .createQueryBuilder('license') + .where('license.account_id = :accountId', { accountId: myAccountId }) + .andWhere('license.status IN (:...statuses)', { + statuses: [ + LICENSE_ALLOCATED_STATUS.UNALLOCATED, + LICENSE_ALLOCATED_STATUS.REUSABLE, ], - order: { - expiry_date: { - direction: 'DESC', - nulls: 'FIRST', - }, - id: 'ASC', - }, - }); - - return allocatableLicenses.map((license) => ({ - licenseId: license.id, - expiryDate: license.expiry_date, - })); - }); + }) + .andWhere( + 'license.expiry_date >= :nowDate OR license.expiry_date IS NULL', + { nowDate }, + ) + .orderBy('license.expiry_date IS NULL', 'DESC') + .addOrderBy('license.expiry_date', 'DESC') + .addOrderBy('license.id', 'ASC'); + const allocatableLicenses = await queryBuilder.getMany(); + return allocatableLicenses.map((license) => ({ + licenseId: license.id, + expiryDate: license.expiry_date, + })); } /** * ライセンスをユーザーに割り当てる @@ -527,16 +513,20 @@ export class LicensesRepositoryService { }); let switchFromType = ''; - switch (oldLicenseType.license.type) { - case LICENSE_TYPE.CARD: - switchFromType = SWITCH_FROM_TYPE.CARD; - break; - case LICENSE_TYPE.TRIAL: - switchFromType = SWITCH_FROM_TYPE.TRIAL; - break; - default: - switchFromType = SWITCH_FROM_TYPE.NONE; - break; + if (oldLicenseType) { + switch (oldLicenseType.license.type) { + case LICENSE_TYPE.CARD: + switchFromType = SWITCH_FROM_TYPE.CARD; + break; + case LICENSE_TYPE.TRIAL: + switchFromType = SWITCH_FROM_TYPE.TRIAL; + break; + default: + switchFromType = SWITCH_FROM_TYPE.NONE; + break; + } + } else { + switchFromType = SWITCH_FROM_TYPE.NONE; } // ライセンス割り当て履歴テーブルへ登録