From 4ad04f32534e4a314f9d176dc3b8631015d11d25 Mon Sep 17 00:00:00 2001 From: Krishnan Subramanian Date: Wed, 23 Oct 2024 18:33:39 +0530 Subject: [PATCH 01/16] Adding to cache --- vehicles/src/app.service.ts | 9 ++++ vehicles/src/common/enum/cache-key.enum.ts | 1 + .../response/read-policy-configuration.dto.ts | 33 +++++++++++++++ .../entities/policy-configuration.entity.ts | 42 +++++++++++++++++++ vehicles/src/modules/policy/policy.module.ts | 11 +++++ vehicles/src/modules/policy/policy.service.ts | 29 +++++++++++++ 6 files changed, 125 insertions(+) create mode 100644 vehicles/src/modules/policy/dto/response/read-policy-configuration.dto.ts create mode 100644 vehicles/src/modules/policy/entities/policy-configuration.entity.ts create mode 100644 vehicles/src/modules/policy/policy.module.ts create mode 100644 vehicles/src/modules/policy/policy.service.ts diff --git a/vehicles/src/app.service.ts b/vehicles/src/app.service.ts index bcee88529..ba08c8c6d 100644 --- a/vehicles/src/app.service.ts +++ b/vehicles/src/app.service.ts @@ -12,6 +12,7 @@ import { PaymentService } from './modules/permit-application-payment/payment/pay import { LogAsyncMethodExecution } from './common/decorator/log-async-method-execution.decorator'; import { FeatureFlagsService } from './modules/feature-flags/feature-flags.service'; import { ApplicationService } from './modules/permit-application-payment/application/application.service'; +import { PolicyService } from './modules/policy/policy.service'; @Injectable() export class AppService { @@ -27,6 +28,7 @@ export class AppService { private paymentService: PaymentService, private featureFlagsService: FeatureFlagsService, private applicationService: ApplicationService, + private policyService: PolicyService ) {} getHello(): string { @@ -128,6 +130,13 @@ export class AppService { createCacheMap(permitApprovalSource, 'id', 'code'), ); + const policyConfigs = await this.policyService.findAllActive(); + await addToCache( + this.cacheManager, + CacheKey.POLICY_CONFIGURATIONS, + createCacheMap(policyConfigs, '??', 'policyJson'), + ); + const endDateTime = new Date(); const processingTime = endDateTime.getTime() - startDateTime.getTime(); this.logger.log( diff --git a/vehicles/src/common/enum/cache-key.enum.ts b/vehicles/src/common/enum/cache-key.enum.ts index 82100c8cc..e114b56b7 100644 --- a/vehicles/src/common/enum/cache-key.enum.ts +++ b/vehicles/src/common/enum/cache-key.enum.ts @@ -20,4 +20,5 @@ export enum CacheKey { FEATURE_FLAG_TYPE = 'FEATURE_FLAG_TYPE', PERMIT_APPLICATION_ORIGIN = 'PERMIT_APPLICATION_ORIGIN', PERMIT_APPROVAL_SOURCE = 'PERMIT_APPROVAL_SOURCE', + POLICY_CONFIGURATIONS = 'POLICY_CONFIGURATIONS', } diff --git a/vehicles/src/modules/policy/dto/response/read-policy-configuration.dto.ts b/vehicles/src/modules/policy/dto/response/read-policy-configuration.dto.ts new file mode 100644 index 000000000..076e99328 --- /dev/null +++ b/vehicles/src/modules/policy/dto/response/read-policy-configuration.dto.ts @@ -0,0 +1,33 @@ +import { AutoMap } from '@automapper/classes'; +import { ApiProperty } from '@nestjs/swagger'; + +export class ReadPolicyConfigurationDto { + @AutoMap() + @ApiProperty({ + description: 'Id of the policy configuration.', + example: 74, + }) + policyConfigurationId: number; + + @AutoMap() + @ApiProperty({ + description: 'The policy config json.', + required: false, + }) + policyJson: JSON; + + @AutoMap() + @ApiProperty({ + description: 'The effective date of the policy config.', + required: false, + }) + effectiveDate: string; + + @AutoMap() + @ApiProperty({ + description: 'Boolean indicator on whether the policy config is a draft.', + example: false, + required: false, + }) + isDraft: boolean; +} diff --git a/vehicles/src/modules/policy/entities/policy-configuration.entity.ts b/vehicles/src/modules/policy/entities/policy-configuration.entity.ts new file mode 100644 index 000000000..be1696b1e --- /dev/null +++ b/vehicles/src/modules/policy/entities/policy-configuration.entity.ts @@ -0,0 +1,42 @@ +import { AutoMap } from '@automapper/classes'; +import { ApiProperty } from '@nestjs/swagger'; +import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; +import { Base } from '../../common/entities/base.entity'; + +@Entity({ name: 'ORBC_POLICY_CONFIGURATION' }) +export class PolicyConfiguration extends Base { + @AutoMap() + @ApiProperty({ + example: '1', + description: 'Unique identifier for the policy configuration.', + }) + @PrimaryGeneratedColumn({ type: 'int', name: 'POLICY_CONFIGURATION_ID' }) + policyConfigurationId: string; + + @AutoMap() + @Column({ length: 8000, name: 'POLICY_JSON', nullable: true }) + policyJson: string; + + @AutoMap() + @Column({ + insert: false, + update: false, + name: 'EFFECTIVE_DATE', + nullable: true, + type: 'date', + }) + effectiveDate: string; + + @AutoMap() + @Column({ + type: 'char', + name: 'IS_DRAFT', + nullable: false, + default: true, + transformer: { + to: (value: boolean): string => (value ? 'Y' : 'N'), // Converts the boolean value to 'Y' or 'N' for storage. + from: (value: string): boolean => value === 'Y', // Converts the stored string back to a boolean. + }, + }) + isDraft: boolean; +} diff --git a/vehicles/src/modules/policy/policy.module.ts b/vehicles/src/modules/policy/policy.module.ts new file mode 100644 index 000000000..db6787114 --- /dev/null +++ b/vehicles/src/modules/policy/policy.module.ts @@ -0,0 +1,11 @@ +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { PolicyConfiguration } from './entities/policy-configuration.entity'; +import { PolicyService } from './policy.service'; + +@Module({ + imports: [TypeOrmModule.forFeature([PolicyConfiguration])], + providers: [PolicyService], + exports: [PolicyService], +}) +export class PolicyConfigurationModule {} diff --git a/vehicles/src/modules/policy/policy.service.ts b/vehicles/src/modules/policy/policy.service.ts new file mode 100644 index 000000000..e95ce4ca1 --- /dev/null +++ b/vehicles/src/modules/policy/policy.service.ts @@ -0,0 +1,29 @@ +import { Mapper } from '@automapper/core'; +import { InjectMapper } from '@automapper/nestjs'; +import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; +import { LogAsyncMethodExecution } from '../../common/decorator/log-async-method-execution.decorator'; +import { ReadPolicyConfigurationDto } from './dto/response/read-policy-configuration.dto'; +import { PolicyConfiguration } from './entities/policy-configuration.entity'; + +@Injectable() +export class PolicyService { + constructor( + @InjectRepository(PolicyConfiguration) + private policyConfigurationRepository: Repository, + @InjectMapper() private readonly classMapper: Mapper, + ) {} + + @LogAsyncMethodExecution() + async findAllActive(): Promise { + return this.classMapper.mapArrayAsync( + await this.policyConfigurationRepository.find({ + select: { policyJson: true }, + where: { isDraft: false }, + }), + PolicyConfiguration, + ReadPolicyConfigurationDto, + ); + } +} From dc3610dddd4ba1ced1b41d1f4cf18f407305e4b8 Mon Sep 17 00:00:00 2001 From: Krishnan Subramanian Date: Tue, 29 Oct 2024 18:05:52 +0530 Subject: [PATCH 02/16] Replace policy service with http call to policy --- vehicles/src/app.service.ts | 10 ++-- vehicles/src/common/constants/api.constant.ts | 3 +- vehicles/src/common/enum/cache-key.enum.ts | 1 + .../common/enum/gov-common-services.enum.ts | 1 + .../helper/gov-common-services.helper.ts | 7 ++- .../helper/permit-application.helper.ts | 9 ++-- .../src/common/helper/permit-fee.helper.ts | 4 +- .../src/common/helper/policy-engine.helper.ts | 30 +++++++++++ .../application/application.service.ts | 4 +- .../payment/payment.service.ts | 11 ++-- .../dto/response/read-policy-config.dto.ts | 53 +++++++++++++++++++ .../response/read-policy-configuration.dto.ts | 33 ------------ .../entities/policy-configuration.entity.ts | 42 --------------- vehicles/src/modules/policy/policy.module.ts | 11 ---- vehicles/src/modules/policy/policy.service.ts | 29 ---------- 15 files changed, 117 insertions(+), 131 deletions(-) create mode 100644 vehicles/src/modules/policy/dto/response/read-policy-config.dto.ts delete mode 100644 vehicles/src/modules/policy/dto/response/read-policy-configuration.dto.ts delete mode 100644 vehicles/src/modules/policy/entities/policy-configuration.entity.ts delete mode 100644 vehicles/src/modules/policy/policy.module.ts delete mode 100644 vehicles/src/modules/policy/policy.service.ts diff --git a/vehicles/src/app.service.ts b/vehicles/src/app.service.ts index ba08c8c6d..d2c29221c 100644 --- a/vehicles/src/app.service.ts +++ b/vehicles/src/app.service.ts @@ -12,7 +12,8 @@ import { PaymentService } from './modules/permit-application-payment/payment/pay import { LogAsyncMethodExecution } from './common/decorator/log-async-method-execution.decorator'; import { FeatureFlagsService } from './modules/feature-flags/feature-flags.service'; import { ApplicationService } from './modules/permit-application-payment/application/application.service'; -import { PolicyService } from './modules/policy/policy.service'; +import { HttpService } from '@nestjs/axios'; +import { getActivePolicyDefinitions } from './common/helper/policy-engine.helper'; @Injectable() export class AppService { @@ -28,7 +29,7 @@ export class AppService { private paymentService: PaymentService, private featureFlagsService: FeatureFlagsService, private applicationService: ApplicationService, - private policyService: PolicyService + private readonly httpService: HttpService, ) {} getHello(): string { @@ -130,7 +131,10 @@ export class AppService { createCacheMap(permitApprovalSource, 'id', 'code'), ); - const policyConfigs = await this.policyService.findAllActive(); + const policyConfigs = await getActivePolicyDefinitions( + this.httpService, + this.cacheManager, + ); await addToCache( this.cacheManager, CacheKey.POLICY_CONFIGURATIONS, diff --git a/vehicles/src/common/constants/api.constant.ts b/vehicles/src/common/constants/api.constant.ts index b15b80cbe..ffcbeb052 100644 --- a/vehicles/src/common/constants/api.constant.ts +++ b/vehicles/src/common/constants/api.constant.ts @@ -8,6 +8,5 @@ export const CRYPTO_ALGORITHM_MD5 = 'md5'; export const CRYPTO_ALGORITHM_SHA256 = 'sha256'; export const TOKEN_EXPIRY_BUFFER = 15; export const PERMISSIONS_KEY = 'permissions'; -export const TIMEZONE_PACIFIC = "America/Vancouver"; +export const TIMEZONE_PACIFIC = 'America/Vancouver'; export const GL_PROJ_CODE_PLACEHOLDER = 'PROJECT'; - diff --git a/vehicles/src/common/enum/cache-key.enum.ts b/vehicles/src/common/enum/cache-key.enum.ts index e114b56b7..fd58b6416 100644 --- a/vehicles/src/common/enum/cache-key.enum.ts +++ b/vehicles/src/common/enum/cache-key.enum.ts @@ -21,4 +21,5 @@ export enum CacheKey { PERMIT_APPLICATION_ORIGIN = 'PERMIT_APPLICATION_ORIGIN', PERMIT_APPROVAL_SOURCE = 'PERMIT_APPROVAL_SOURCE', POLICY_CONFIGURATIONS = 'POLICY_CONFIGURATIONS', + ORBC_SERVICE_ACCOUNT_ACCESS_TOKEN = 'ORBC_SERVICE_ACCOUNT_ACCESS_TOKEN', } diff --git a/vehicles/src/common/enum/gov-common-services.enum.ts b/vehicles/src/common/enum/gov-common-services.enum.ts index 6a44a473f..acc92f93d 100644 --- a/vehicles/src/common/enum/gov-common-services.enum.ts +++ b/vehicles/src/common/enum/gov-common-services.enum.ts @@ -2,4 +2,5 @@ export enum GovCommonServices { COMMON_HOSTED_EMAIL_SERVICE = 'CHES', COMMON_DOCUMENT_GENERATION_SERVICE = 'CDOGS', CREDIT_ACCOUNT_SERVICE = 'CREDIT_ACCOUNT', + ORBC_SERVICE_ACCOUNT = 'SA', } diff --git a/vehicles/src/common/helper/gov-common-services.helper.ts b/vehicles/src/common/helper/gov-common-services.helper.ts index be81e942b..d453be518 100644 --- a/vehicles/src/common/helper/gov-common-services.helper.ts +++ b/vehicles/src/common/helper/gov-common-services.helper.ts @@ -124,7 +124,12 @@ function getTokenCredentials(govCommonServices: GovCommonServices): { username = process.env.CFS_CREDIT_ACCOUNT_CLIENT_ID; password = process.env.CFS_CREDIT_ACCOUNT_CLIENT_SECRET; break; - + case GovCommonServices.ORBC_SERVICE_ACCOUNT: + tokenCacheKey = CacheKey.ORBC_SERVICE_ACCOUNT_ACCESS_TOKEN; + tokenUrl = process.env.ORBC_SERVICE_ACCOUNT_TOKEN_URL; + username = process.env.ORBC_SERVICE_ACCOUNT_CLIENT_ID; + password = process.env.ORBC_SERVICE_ACCOUNT_CLIENT_SECRET; + break; default: break; } diff --git a/vehicles/src/common/helper/permit-application.helper.ts b/vehicles/src/common/helper/permit-application.helper.ts index b572f57e9..96d3b53e4 100644 --- a/vehicles/src/common/helper/permit-application.helper.ts +++ b/vehicles/src/common/helper/permit-application.helper.ts @@ -268,9 +268,12 @@ export const isPermitTypeEligibleForQueue = ( return PERMIT_TYPES_FOR_QUEUE.includes(permitType); }; -export const validApplicationDates = (application: Permit, timezone: string): boolean => { +export const validApplicationDates = ( + application: Permit, + timezone: string, +): boolean => { const todayUTC = dayjs(new Date()); - const todayPacific = todayUTC.tz(timezone).format("YYYY-MM-DD"); + const todayPacific = todayUTC.tz(timezone).format('YYYY-MM-DD'); const { startDate, expiryDate } = application.permitData; return startDate >= todayPacific && startDate <= expiryDate; -} +}; diff --git a/vehicles/src/common/helper/permit-fee.helper.ts b/vehicles/src/common/helper/permit-fee.helper.ts index 8e2314c02..dc14bf979 100644 --- a/vehicles/src/common/helper/permit-fee.helper.ts +++ b/vehicles/src/common/helper/permit-fee.helper.ts @@ -205,7 +205,7 @@ export const validAmount = ( calculatedAmount: number, receivedAmount: number, transactionType: TransactionType, -): boolean =>{ +): boolean => { const isAmountValid = receivedAmount.toFixed(2) === Math.abs(calculatedAmount).toFixed(2); @@ -218,4 +218,4 @@ export const validAmount = ( isAmountValid && (isRefundValid || transactionType !== TransactionType.REFUND) ); -} +}; diff --git a/vehicles/src/common/helper/policy-engine.helper.ts b/vehicles/src/common/helper/policy-engine.helper.ts index fcf3fd3ba..88ec6459a 100644 --- a/vehicles/src/common/helper/policy-engine.helper.ts +++ b/vehicles/src/common/helper/policy-engine.helper.ts @@ -1,6 +1,12 @@ import { Permit } from 'src/modules/permit-application-payment/permit/entities/permit.entity'; import { PolicyApplication } from '../interface/policy-application.interface'; import { PermitData } from '../interface/permit.template.interface'; +import { getAccessToken } from './gov-common-services.helper'; +import { GovCommonServices } from '../enum/gov-common-services.enum'; +import { HttpService } from '@nestjs/axios'; +import { Cache } from 'cache-manager'; +import { AxiosResponse } from 'axios'; +import { ReadPolicyConfigDto } from '../../modules/policy/dto/response/read-policy-config.dto'; export const convertToPolicyApplication = ( application: Permit, @@ -10,3 +16,27 @@ export const convertToPolicyApplication = ( permitData: JSON.parse(application.permitData.permitData) as PermitData, }; }; + +export const getActivePolicyDefinitions = async ( + httpService: HttpService, + cacheManager: Cache, +) => { + const token = await getAccessToken( + GovCommonServices.ORBC_SERVICE_ACCOUNT, + httpService, + cacheManager, + ); + const response = await httpService.axiosRef.get< + // eslint-disable-next-line @typescript-eslint/no-explicit-any + any, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + AxiosResponse, + Request + >(process.env.ORBC_POLICY_URL + '/policy-configurations', { + headers: { + Authorization: `Bearer ${token}`, + 'Content-Type': 'application/json', + }, + }); + return (await response.data.json()) as ReadPolicyConfigDto[]; +}; diff --git a/vehicles/src/modules/permit-application-payment/application/application.service.ts b/vehicles/src/modules/permit-application-payment/application/application.service.ts index 81f29aef6..5cde7daa3 100644 --- a/vehicles/src/modules/permit-application-payment/application/application.service.ts +++ b/vehicles/src/modules/permit-application-payment/application/application.service.ts @@ -1236,8 +1236,8 @@ export class ApplicationService { company: { companyId: permit.company.companyId }, }, }); - if(loaDetails.length != loaIdsToInsert.length) - throw new BadRequestException('One or more loa(s) does not exist') + if (loaDetails.length != loaIdsToInsert.length) + throw new BadRequestException('One or more loa(s) does not exist'); // Transform the permit LOA IDs from an array of numbers into individual records. const singlePermitLoa = loaIdsToInsert.map((loaId) => ({ permitId, diff --git a/vehicles/src/modules/permit-application-payment/payment/payment.service.ts b/vehicles/src/modules/permit-application-payment/payment/payment.service.ts index e4a061f1a..e511f1ced 100644 --- a/vehicles/src/modules/permit-application-payment/payment/payment.service.ts +++ b/vehicles/src/modules/permit-application-payment/payment/payment.service.ts @@ -51,7 +51,10 @@ import { } from 'src/common/helper/permit-fee.helper'; import { CfsTransactionDetail } from './entities/cfs-transaction.entity'; import { CfsFileStatus } from 'src/common/enum/cfs-file-status.enum'; -import { isAmendmentApplication, validApplicationDates } from '../../../common/helper/permit-application.helper'; +import { + isAmendmentApplication, + validApplicationDates, +} from '../../../common/helper/permit-application.helper'; import { isCfsPaymentMethodType } from 'src/common/helper/payment.helper'; import { PgApprovesStatus } from 'src/common/enum/pg-approved-status-type.enum'; import { CACHE_MANAGER } from '@nestjs/cache-manager'; @@ -513,8 +516,10 @@ export class PaymentService { // Calculate and add amount for each requested application, as per the available backend data. for (const application of applications) { //Check if each application has a valid start date and valid expiry date. - if (isCVClientUser && !validApplicationDates(application, TIMEZONE_PACIFIC)) - { + if ( + isCVClientUser && + !validApplicationDates(application, TIMEZONE_PACIFIC) + ) { throw new UnprocessableEntityException( `Atleast one of the application has invalid startDate or expiryDate.`, ); diff --git a/vehicles/src/modules/policy/dto/response/read-policy-config.dto.ts b/vehicles/src/modules/policy/dto/response/read-policy-config.dto.ts new file mode 100644 index 000000000..2f43db29c --- /dev/null +++ b/vehicles/src/modules/policy/dto/response/read-policy-config.dto.ts @@ -0,0 +1,53 @@ +import { AutoMap } from '@automapper/classes'; +import { ApiProperty } from '@nestjs/swagger'; + +export class ReadPolicyConfigDto { + /** + * Unique identifier for the policy configuration. + */ + @AutoMap() + @ApiProperty({ + example: '1', + description: 'Unique identifier for the policy configuration.', + }) + policyConfigId: number; + + /** + * JSON data representing the policy configuration. + */ + @AutoMap() + @ApiProperty({ + description: 'Policy configuration in JSON format.', + }) + policy: JSON; + + /** + * Configuration effective date. + */ + @AutoMap() + @ApiProperty({ + example: '2023-07-13T17:31:17.470Z', + description: 'Policy Configuration effective date.', + }) + effectiveDate: string; + + /** + * Indicates if the configuration is currently a draft version. + */ + @AutoMap() + @ApiProperty({ + example: true, + description: 'Indicates if the configuration is currently a draft.', + }) + isDraft: boolean; + + /** + * Description of changes made in the configuration. + */ + @AutoMap() + @ApiProperty({ + example: 'Initial release of policy configuration with updated rules', + description: 'Description of changes made in the configuration.', + }) + changeDescription: string; +} diff --git a/vehicles/src/modules/policy/dto/response/read-policy-configuration.dto.ts b/vehicles/src/modules/policy/dto/response/read-policy-configuration.dto.ts deleted file mode 100644 index 076e99328..000000000 --- a/vehicles/src/modules/policy/dto/response/read-policy-configuration.dto.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { AutoMap } from '@automapper/classes'; -import { ApiProperty } from '@nestjs/swagger'; - -export class ReadPolicyConfigurationDto { - @AutoMap() - @ApiProperty({ - description: 'Id of the policy configuration.', - example: 74, - }) - policyConfigurationId: number; - - @AutoMap() - @ApiProperty({ - description: 'The policy config json.', - required: false, - }) - policyJson: JSON; - - @AutoMap() - @ApiProperty({ - description: 'The effective date of the policy config.', - required: false, - }) - effectiveDate: string; - - @AutoMap() - @ApiProperty({ - description: 'Boolean indicator on whether the policy config is a draft.', - example: false, - required: false, - }) - isDraft: boolean; -} diff --git a/vehicles/src/modules/policy/entities/policy-configuration.entity.ts b/vehicles/src/modules/policy/entities/policy-configuration.entity.ts deleted file mode 100644 index be1696b1e..000000000 --- a/vehicles/src/modules/policy/entities/policy-configuration.entity.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { AutoMap } from '@automapper/classes'; -import { ApiProperty } from '@nestjs/swagger'; -import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; -import { Base } from '../../common/entities/base.entity'; - -@Entity({ name: 'ORBC_POLICY_CONFIGURATION' }) -export class PolicyConfiguration extends Base { - @AutoMap() - @ApiProperty({ - example: '1', - description: 'Unique identifier for the policy configuration.', - }) - @PrimaryGeneratedColumn({ type: 'int', name: 'POLICY_CONFIGURATION_ID' }) - policyConfigurationId: string; - - @AutoMap() - @Column({ length: 8000, name: 'POLICY_JSON', nullable: true }) - policyJson: string; - - @AutoMap() - @Column({ - insert: false, - update: false, - name: 'EFFECTIVE_DATE', - nullable: true, - type: 'date', - }) - effectiveDate: string; - - @AutoMap() - @Column({ - type: 'char', - name: 'IS_DRAFT', - nullable: false, - default: true, - transformer: { - to: (value: boolean): string => (value ? 'Y' : 'N'), // Converts the boolean value to 'Y' or 'N' for storage. - from: (value: string): boolean => value === 'Y', // Converts the stored string back to a boolean. - }, - }) - isDraft: boolean; -} diff --git a/vehicles/src/modules/policy/policy.module.ts b/vehicles/src/modules/policy/policy.module.ts deleted file mode 100644 index db6787114..000000000 --- a/vehicles/src/modules/policy/policy.module.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { Module } from '@nestjs/common'; -import { TypeOrmModule } from '@nestjs/typeorm'; -import { PolicyConfiguration } from './entities/policy-configuration.entity'; -import { PolicyService } from './policy.service'; - -@Module({ - imports: [TypeOrmModule.forFeature([PolicyConfiguration])], - providers: [PolicyService], - exports: [PolicyService], -}) -export class PolicyConfigurationModule {} diff --git a/vehicles/src/modules/policy/policy.service.ts b/vehicles/src/modules/policy/policy.service.ts deleted file mode 100644 index e95ce4ca1..000000000 --- a/vehicles/src/modules/policy/policy.service.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { Mapper } from '@automapper/core'; -import { InjectMapper } from '@automapper/nestjs'; -import { Injectable } from '@nestjs/common'; -import { InjectRepository } from '@nestjs/typeorm'; -import { Repository } from 'typeorm'; -import { LogAsyncMethodExecution } from '../../common/decorator/log-async-method-execution.decorator'; -import { ReadPolicyConfigurationDto } from './dto/response/read-policy-configuration.dto'; -import { PolicyConfiguration } from './entities/policy-configuration.entity'; - -@Injectable() -export class PolicyService { - constructor( - @InjectRepository(PolicyConfiguration) - private policyConfigurationRepository: Repository, - @InjectMapper() private readonly classMapper: Mapper, - ) {} - - @LogAsyncMethodExecution() - async findAllActive(): Promise { - return this.classMapper.mapArrayAsync( - await this.policyConfigurationRepository.find({ - select: { policyJson: true }, - where: { isDraft: false }, - }), - PolicyConfiguration, - ReadPolicyConfigurationDto, - ); - } -} From 6d98b4126b6a63974ae02670713d8e937a58d607 Mon Sep 17 00:00:00 2001 From: Krishnan Subramanian Date: Tue, 29 Oct 2024 21:11:27 +0530 Subject: [PATCH 03/16] Rough structure for validation --- vehicles/package-lock.json | 47 +++++++++++++++++++ vehicles/package.json | 3 +- .../src/common/helper/policy-engine.helper.ts | 15 ++++++ .../dto/response/read-policy-config.dto.ts | 15 +++++- vehicles/tsconfig.json | 2 +- 5 files changed, 79 insertions(+), 3 deletions(-) diff --git a/vehicles/package-lock.json b/vehicles/package-lock.json index 7e112c88a..6a33b528d 100644 --- a/vehicles/package-lock.json +++ b/vehicles/package-lock.json @@ -39,6 +39,7 @@ "nest-winston": "^1.10.0", "nestjs-cls": "^4.4.1", "nestjs-typeorm-paginate": "^4.0.4", + "onroute-policy-engine": "0.2.1", "passport": "^0.7.0", "passport-jwt": "^4.0.1", "response-time": "^2.3.2", @@ -5540,6 +5541,11 @@ "node": ">=6" } }, + "node_modules/eventemitter2": { + "version": "6.4.9", + "resolved": "https://registry.npmjs.org/eventemitter2/-/eventemitter2-6.4.9.tgz", + "integrity": "sha512-JEPTiaOt9f04oa6NOkc4aH+nVp5I3wEjpHbIPqfgCdD5v5bUzy7xQqwcVO2aDQgOWhI28da57HksMrzK9HlRxg==" + }, "node_modules/eventemitter3": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-5.0.1.tgz", @@ -6436,6 +6442,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/hash-it": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/hash-it/-/hash-it-6.0.0.tgz", + "integrity": "sha512-KHzmSFx1KwyMPw0kXeeUD752q/Kfbzhy6dAZrjXV9kAIXGqzGvv8vhkUqj+2MGZldTo0IBpw6v7iWE7uxsvH0w==" + }, "node_modules/hasown": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.0.tgz", @@ -7822,6 +7833,25 @@ "resolved": "https://registry.npmjs.org/json-parse-even-better-errors/-/json-parse-even-better-errors-2.3.1.tgz", "integrity": "sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w==" }, + "node_modules/json-rules-engine": { + "version": "6.6.0", + "resolved": "https://registry.npmjs.org/json-rules-engine/-/json-rules-engine-6.6.0.tgz", + "integrity": "sha512-jJ4eVCPnItetPiU3fTIzrrl3d2zeIXCcCy11dwWhN72YXBR2mByV1Vfbrvt6y2n+VFmxc6rtL/XhDqLKIwBx6g==", + "dependencies": { + "clone": "^2.1.2", + "eventemitter2": "^6.4.4", + "hash-it": "^6.0.0", + "jsonpath-plus": "^7.2.0" + } + }, + "node_modules/json-rules-engine/node_modules/clone": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/clone/-/clone-2.1.2.tgz", + "integrity": "sha512-3Pe/CF1Nn94hyhIYpjtiLhdCoEoz0DqQ+988E9gmeEdQZlojxnOb74wctFyuwWQHzqyf9X7C7MG8juUpqBJT8w==", + "engines": { + "node": ">=0.8" + } + }, "node_modules/json-schema-traverse": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz", @@ -7860,6 +7890,14 @@ "graceful-fs": "^4.1.6" } }, + "node_modules/jsonpath-plus": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/jsonpath-plus/-/jsonpath-plus-7.2.0.tgz", + "integrity": "sha512-zBfiUPM5nD0YZSBT/o/fbCUlCcepMIdP0CJZxM1+KgA4f2T206f6VAg9e7mX35+KlMaIc5qXW34f3BnwJ3w+RA==", + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/jsonwebtoken": { "version": "9.0.2", "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz", @@ -9118,6 +9156,15 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/onroute-policy-engine": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/onroute-policy-engine/-/onroute-policy-engine-0.2.1.tgz", + "integrity": "sha512-AGsIrq7uggF2+TsieXZ1i0vCz1mybiECVTTCfTz3WR+o9gll3rQ2ZjcDdEeoPvTYgLa0MTW0xbRs1AInBePOLg==", + "dependencies": { + "dayjs": "^1.11.10", + "json-rules-engine": "^6.5.0" + } + }, "node_modules/optionator": { "version": "0.9.3", "resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.3.tgz", diff --git a/vehicles/package.json b/vehicles/package.json index 2e9e7fa73..2132a635e 100644 --- a/vehicles/package.json +++ b/vehicles/package.json @@ -78,7 +78,8 @@ "swagger-ui-express": "^5.0.1", "typeorm": "^0.3.20", "uuid": "^9.0.1", - "winston": "^3.14.2" + "winston": "^3.14.2", + "onroute-policy-engine": "0.2.1" }, "devDependencies": { "@golevelup/ts-jest": "^0.5.4", diff --git a/vehicles/src/common/helper/policy-engine.helper.ts b/vehicles/src/common/helper/policy-engine.helper.ts index 88ec6459a..3716baece 100644 --- a/vehicles/src/common/helper/policy-engine.helper.ts +++ b/vehicles/src/common/helper/policy-engine.helper.ts @@ -7,6 +7,7 @@ import { HttpService } from '@nestjs/axios'; import { Cache } from 'cache-manager'; import { AxiosResponse } from 'axios'; import { ReadPolicyConfigDto } from '../../modules/policy/dto/response/read-policy-config.dto'; +import { Policy, ValidationResults } from 'onroute-policy-engine'; export const convertToPolicyApplication = ( application: Permit, @@ -40,3 +41,17 @@ export const getActivePolicyDefinitions = async ( }); return (await response.data.json()) as ReadPolicyConfigDto[]; }; + +export const validateWithPolicyEngine = async ( + permitApplication: unknown, + cacheManager: Cache, +): Promise => { + const policyDefinitions: ReadPolicyConfigDto[] = await cacheManager.get( + 'active-policy-definitions', + ); + const policy = new Policy(policyDefinitions); + const validationResults: ValidationResults = + await policy.validate(permitApplication); + + return validationResults.violations.length > 0; +}; diff --git a/vehicles/src/modules/policy/dto/response/read-policy-config.dto.ts b/vehicles/src/modules/policy/dto/response/read-policy-config.dto.ts index 2f43db29c..246117a02 100644 --- a/vehicles/src/modules/policy/dto/response/read-policy-config.dto.ts +++ b/vehicles/src/modules/policy/dto/response/read-policy-config.dto.ts @@ -1,5 +1,7 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import { AutoMap } from '@automapper/classes'; import { ApiProperty } from '@nestjs/swagger'; +// import { PolicyDefinition } from 'onroute-policy-engine/types'; export class ReadPolicyConfigDto { /** @@ -19,7 +21,18 @@ export class ReadPolicyConfigDto { @ApiProperty({ description: 'Policy configuration in JSON format.', }) - policy: JSON; + policy: { + version: string; + geographicRegions: any; + permitTypes: any[]; + commonRules: any[]; + globalWeightDefaults: any; + globalSizeDefaults: any; + vehicleCategories: any; + vehicleTypes: any; + commodities: any[]; + rangeMatrices?: any[]; + }; /** * Configuration effective date. diff --git a/vehicles/tsconfig.json b/vehicles/tsconfig.json index fd39c3271..c153b4495 100644 --- a/vehicles/tsconfig.json +++ b/vehicles/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "module": "commonjs", + "module": "CommonJS", "declaration": true, "removeComments": true, "emitDecoratorMetadata": true, From e30d6fd254ddad07c602707441d3766a55e4b857 Mon Sep 17 00:00:00 2001 From: Krishnan Subramanian Date: Tue, 5 Nov 2024 14:55:39 -0800 Subject: [PATCH 04/16] With correct import --- .../dto/response/read-policy-config.dto.ts | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/vehicles/src/modules/policy/dto/response/read-policy-config.dto.ts b/vehicles/src/modules/policy/dto/response/read-policy-config.dto.ts index 246117a02..21a3bf049 100644 --- a/vehicles/src/modules/policy/dto/response/read-policy-config.dto.ts +++ b/vehicles/src/modules/policy/dto/response/read-policy-config.dto.ts @@ -1,7 +1,6 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ import { AutoMap } from '@automapper/classes'; import { ApiProperty } from '@nestjs/swagger'; -// import { PolicyDefinition } from 'onroute-policy-engine/types'; +import { PolicyDefinition } from 'onroute-policy-engine/dist/types'; export class ReadPolicyConfigDto { /** @@ -21,18 +20,7 @@ export class ReadPolicyConfigDto { @ApiProperty({ description: 'Policy configuration in JSON format.', }) - policy: { - version: string; - geographicRegions: any; - permitTypes: any[]; - commonRules: any[]; - globalWeightDefaults: any; - globalSizeDefaults: any; - vehicleCategories: any; - vehicleTypes: any; - commodities: any[]; - rangeMatrices?: any[]; - }; + policy: PolicyDefinition; /** * Configuration effective date. From 0130db672b91284e296b611ef261c77a7333c839 Mon Sep 17 00:00:00 2001 From: Krishnan Subramanian Date: Wed, 6 Nov 2024 20:20:05 -0800 Subject: [PATCH 05/16] Upgraded version of policy --- vehicles/package-lock.json | 8 ++++---- vehicles/package.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/vehicles/package-lock.json b/vehicles/package-lock.json index 6a33b528d..9e090b108 100644 --- a/vehicles/package-lock.json +++ b/vehicles/package-lock.json @@ -39,7 +39,7 @@ "nest-winston": "^1.10.0", "nestjs-cls": "^4.4.1", "nestjs-typeorm-paginate": "^4.0.4", - "onroute-policy-engine": "0.2.1", + "onroute-policy-engine": "0.4.2", "passport": "^0.7.0", "passport-jwt": "^4.0.1", "response-time": "^2.3.2", @@ -9157,9 +9157,9 @@ } }, "node_modules/onroute-policy-engine": { - "version": "0.2.1", - "resolved": "https://registry.npmjs.org/onroute-policy-engine/-/onroute-policy-engine-0.2.1.tgz", - "integrity": "sha512-AGsIrq7uggF2+TsieXZ1i0vCz1mybiECVTTCfTz3WR+o9gll3rQ2ZjcDdEeoPvTYgLa0MTW0xbRs1AInBePOLg==", + "version": "0.4.2", + "resolved": "https://registry.npmjs.org/onroute-policy-engine/-/onroute-policy-engine-0.4.2.tgz", + "integrity": "sha512-LTkCy8omP/UHO+Qw0UowiDGDBfqCPTXNmrXeYftkCQ9QBDulBQiHDLFP+W569mFsFqz4I/Cy0I40NdoiP5IUqA==", "dependencies": { "dayjs": "^1.11.10", "json-rules-engine": "^6.5.0" diff --git a/vehicles/package.json b/vehicles/package.json index 2132a635e..3505bb3f3 100644 --- a/vehicles/package.json +++ b/vehicles/package.json @@ -79,7 +79,7 @@ "typeorm": "^0.3.20", "uuid": "^9.0.1", "winston": "^3.14.2", - "onroute-policy-engine": "0.2.1" + "onroute-policy-engine": "0.4.2" }, "devDependencies": { "@golevelup/ts-jest": "^0.5.4", From abbc60c70db5fa42227143fbbf6f35f52fd95de8 Mon Sep 17 00:00:00 2001 From: Krishnan Subramanian Date: Mon, 9 Dec 2024 13:50:55 -0800 Subject: [PATCH 06/16] Add back orbc policy engine in deps --- vehicles/package-lock.json | 8 ++++---- vehicles/package.json | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/vehicles/package-lock.json b/vehicles/package-lock.json index 1dcdf3a2b..99abb3fad 100644 --- a/vehicles/package-lock.json +++ b/vehicles/package-lock.json @@ -39,7 +39,7 @@ "nest-winston": "^1.10.0", "nestjs-cls": "^4.5.0", "nestjs-typeorm-paginate": "^4.0.4", - "onroute-policy-engine": "0.4.2", + "onroute-policy-engine": "^0.7.1", "passport": "^0.7.0", "passport-jwt": "^4.0.1", "response-time": "^2.3.3", @@ -9265,9 +9265,9 @@ } }, "node_modules/onroute-policy-engine": { - "version": "0.4.2", - "resolved": "https://registry.npmjs.org/onroute-policy-engine/-/onroute-policy-engine-0.4.2.tgz", - "integrity": "sha512-LTkCy8omP/UHO+Qw0UowiDGDBfqCPTXNmrXeYftkCQ9QBDulBQiHDLFP+W569mFsFqz4I/Cy0I40NdoiP5IUqA==", + "version": "0.7.1", + "resolved": "https://registry.npmjs.org/onroute-policy-engine/-/onroute-policy-engine-0.7.1.tgz", + "integrity": "sha512-6kgEQp4/JKmU5DJzeh+Kplth4g15DuRR37/9c4yMqgCKs1Y0rEzfbsnm4z4odNia58c/9zCNQyupqIOuT9/9fQ==", "dependencies": { "dayjs": "^1.11.10", "json-rules-engine": "^6.5.0" diff --git a/vehicles/package.json b/vehicles/package.json index 17542cb4c..82b7340a7 100644 --- a/vehicles/package.json +++ b/vehicles/package.json @@ -71,6 +71,7 @@ "nest-winston": "^1.10.0", "nestjs-cls": "^4.5.0", "nestjs-typeorm-paginate": "^4.0.4", + "onroute-policy-engine": "^0.7.1", "passport": "^0.7.0", "passport-jwt": "^4.0.1", "response-time": "^2.3.3", From 629b4c1ec45f8552ccef8d8b35f51c6751198d76 Mon Sep 17 00:00:00 2001 From: Krishnan Subramanian Date: Mon, 13 Jan 2025 08:37:25 -0800 Subject: [PATCH 07/16] Upgrade policy engine version --- vehicles/package-lock.json | 71 ++++++++++++++++++++++++++++++-------- vehicles/package.json | 2 +- 2 files changed, 58 insertions(+), 15 deletions(-) diff --git a/vehicles/package-lock.json b/vehicles/package-lock.json index 99abb3fad..87e0268a1 100644 --- a/vehicles/package-lock.json +++ b/vehicles/package-lock.json @@ -39,7 +39,7 @@ "nest-winston": "^1.10.0", "nestjs-cls": "^4.5.0", "nestjs-typeorm-paginate": "^4.0.4", - "onroute-policy-engine": "^0.7.1", + "onroute-policy-engine": "^1.4.1", "passport": "^0.7.0", "passport-jwt": "^4.0.1", "response-time": "^2.3.3", @@ -1913,6 +1913,28 @@ "resolved": "https://registry.npmjs.org/@js-joda/core/-/core-5.6.1.tgz", "integrity": "sha512-Xla/d7ZMMR6+zRd6lTio0wRZECfcfFJP7GGe9A9L4tDOlD5CX4YcZ4YZle9w58bBYzssojVapI84RraKWDQZRg==" }, + "node_modules/@jsep-plugin/assignment": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/@jsep-plugin/assignment/-/assignment-1.3.0.tgz", + "integrity": "sha512-VVgV+CXrhbMI3aSusQyclHkenWSAm95WaiKrMxRFam3JSUiIaQjoMIw2sEs/OX4XifnqeQUN4DYbJjlA8EfktQ==", + "engines": { + "node": ">= 10.16.0" + }, + "peerDependencies": { + "jsep": "^0.4.0||^1.0.0" + } + }, + "node_modules/@jsep-plugin/regex": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@jsep-plugin/regex/-/regex-1.0.4.tgz", + "integrity": "sha512-q7qL4Mgjs1vByCaTnDFcBnV9HS7GVPJX5vyVoCgZHNSC9rjwIlmbXG5sUuorR5ndfHAIlJ8pVStxvjXHbNvtUg==", + "engines": { + "node": ">= 10.16.0" + }, + "peerDependencies": { + "jsep": "^0.4.0||^1.0.0" + } + }, "node_modules/@ljharb/through": { "version": "2.3.13", "resolved": "https://registry.npmjs.org/@ljharb/through/-/through-2.3.13.tgz", @@ -7909,6 +7931,14 @@ "resolved": "https://registry.npmjs.org/jsbi/-/jsbi-4.3.0.tgz", "integrity": "sha512-SnZNcinB4RIcnEyZqFPdGPVgrg2AcnykiBy0sHVJQKHYeaLUvi3Exj+iaPpLnFVkDPZIV4U0yvgC9/R4uEAZ9g==" }, + "node_modules/jsep": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/jsep/-/jsep-1.4.0.tgz", + "integrity": "sha512-B7qPcEVE3NVkmSJbaYxvv4cHkVW7DQsZz13pUMrfS8z8Q/BuShN+gcTXrUlPiGqM2/t/EEaI030bpxMqY8gMlw==", + "engines": { + "node": ">= 10.16.0" + } + }, "node_modules/jsesc": { "version": "2.5.2", "resolved": "https://registry.npmjs.org/jsesc/-/jsesc-2.5.2.tgz", @@ -7933,14 +7963,17 @@ "integrity": "sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w==" }, "node_modules/json-rules-engine": { - "version": "6.6.0", - "resolved": "https://registry.npmjs.org/json-rules-engine/-/json-rules-engine-6.6.0.tgz", - "integrity": "sha512-jJ4eVCPnItetPiU3fTIzrrl3d2zeIXCcCy11dwWhN72YXBR2mByV1Vfbrvt6y2n+VFmxc6rtL/XhDqLKIwBx6g==", + "version": "7.3.0", + "resolved": "https://registry.npmjs.org/json-rules-engine/-/json-rules-engine-7.3.0.tgz", + "integrity": "sha512-Ng8Nq9sXID2h92gk3gTCB6bYK6GvQOPgxHLOIl6dEL+PE4+jvTltSOKtfYkVScTR2wL/+ts5gaQqoBFl0zK4/g==", "dependencies": { "clone": "^2.1.2", "eventemitter2": "^6.4.4", "hash-it": "^6.0.0", - "jsonpath-plus": "^7.2.0" + "jsonpath-plus": "^10.2.0" + }, + "engines": { + "node": ">=18.0.0" } }, "node_modules/json-rules-engine/node_modules/clone": { @@ -7990,11 +8023,20 @@ } }, "node_modules/jsonpath-plus": { - "version": "7.2.0", - "resolved": "https://registry.npmjs.org/jsonpath-plus/-/jsonpath-plus-7.2.0.tgz", - "integrity": "sha512-zBfiUPM5nD0YZSBT/o/fbCUlCcepMIdP0CJZxM1+KgA4f2T206f6VAg9e7mX35+KlMaIc5qXW34f3BnwJ3w+RA==", + "version": "10.2.0", + "resolved": "https://registry.npmjs.org/jsonpath-plus/-/jsonpath-plus-10.2.0.tgz", + "integrity": "sha512-T9V+8iNYKFL2n2rF+w02LBOT2JjDnTjioaNFrxRy0Bv1y/hNsqR/EBK7Ojy2ythRHwmz2cRIls+9JitQGZC/sw==", + "dependencies": { + "@jsep-plugin/assignment": "^1.3.0", + "@jsep-plugin/regex": "^1.0.4", + "jsep": "^1.4.0" + }, + "bin": { + "jsonpath": "bin/jsonpath-cli.js", + "jsonpath-plus": "bin/jsonpath-cli.js" + }, "engines": { - "node": ">=12.0.0" + "node": ">=18.0.0" } }, "node_modules/jsonwebtoken": { @@ -9265,12 +9307,13 @@ } }, "node_modules/onroute-policy-engine": { - "version": "0.7.1", - "resolved": "https://registry.npmjs.org/onroute-policy-engine/-/onroute-policy-engine-0.7.1.tgz", - "integrity": "sha512-6kgEQp4/JKmU5DJzeh+Kplth4g15DuRR37/9c4yMqgCKs1Y0rEzfbsnm4z4odNia58c/9zCNQyupqIOuT9/9fQ==", + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/onroute-policy-engine/-/onroute-policy-engine-1.4.1.tgz", + "integrity": "sha512-NSlwb9j2IK6hSk2LOiP02pno51h19t/sCYNUsEpzRww3CPOVOeDw8N4zer7D3tbwDCA9EBkvaLKQh7c8v8nqFQ==", "dependencies": { - "dayjs": "^1.11.10", - "json-rules-engine": "^6.5.0" + "dayjs": "^1.11.13", + "json-rules-engine": "^7.2.1", + "semver": "^7.6.3" } }, "node_modules/optionator": { diff --git a/vehicles/package.json b/vehicles/package.json index 82b7340a7..7b6c0a13a 100644 --- a/vehicles/package.json +++ b/vehicles/package.json @@ -71,7 +71,7 @@ "nest-winston": "^1.10.0", "nestjs-cls": "^4.5.0", "nestjs-typeorm-paginate": "^4.0.4", - "onroute-policy-engine": "^0.7.1", + "onroute-policy-engine": "^1.4.1", "passport": "^0.7.0", "passport-jwt": "^4.0.1", "response-time": "^2.3.3", From 7d4f36801dd71cd50c7ab80ec9ddb0c79f805228 Mon Sep 17 00:00:00 2001 From: Krishnan Subramanian Date: Mon, 13 Jan 2025 15:32:11 -0800 Subject: [PATCH 08/16] Templated code for policy engine validation --- .../dbo.ORBC_FEATURE_FLAG.Table.sql | 23 ++++++++++++- vehicles/src/app.service.ts | 2 +- .../src/common/helper/policy-engine.helper.ts | 34 +++++++------------ .../payment/payment.service.ts | 32 +++++++++++++++++ 4 files changed, 67 insertions(+), 24 deletions(-) diff --git a/database/mssql/scripts/sampledata/dbo.ORBC_FEATURE_FLAG.Table.sql b/database/mssql/scripts/sampledata/dbo.ORBC_FEATURE_FLAG.Table.sql index e66c2e2d9..d6d7d1b85 100644 --- a/database/mssql/scripts/sampledata/dbo.ORBC_FEATURE_FLAG.Table.sql +++ b/database/mssql/scripts/sampledata/dbo.ORBC_FEATURE_FLAG.Table.sql @@ -301,7 +301,28 @@ VALUES N'dbo', GETUTCDATE() ); - +INSERT INTO + [dbo].[ORBC_FEATURE_FLAG] ( + [FEATURE_ID], + [FEATURE_KEY], + [FEATURE_VALUE], + [CONCURRENCY_CONTROL_NUMBER], + [DB_CREATE_USERID], + [DB_CREATE_TIMESTAMP], + [DB_LAST_UPDATE_USERID], + [DB_LAST_UPDATE_TIMESTAMP] + ) +VALUES + ( + '14', + 'VALIDATE-WITH-POLICY-ENGINE', + 'ENABLED', + NULL, + N'dbo', + GETUTCDATE(), + N'dbo', + GETUTCDATE() + ); SET IDENTITY_INSERT [dbo].[ORBC_FEATURE_FLAG] OFF GO \ No newline at end of file diff --git a/vehicles/src/app.service.ts b/vehicles/src/app.service.ts index d2c29221c..9a8224a62 100644 --- a/vehicles/src/app.service.ts +++ b/vehicles/src/app.service.ts @@ -138,7 +138,7 @@ export class AppService { await addToCache( this.cacheManager, CacheKey.POLICY_CONFIGURATIONS, - createCacheMap(policyConfigs, '??', 'policyJson'), + JSON.stringify(policyConfigs), ); const endDateTime = new Date(); diff --git a/vehicles/src/common/helper/policy-engine.helper.ts b/vehicles/src/common/helper/policy-engine.helper.ts index 3716baece..3b4a3213f 100644 --- a/vehicles/src/common/helper/policy-engine.helper.ts +++ b/vehicles/src/common/helper/policy-engine.helper.ts @@ -1,13 +1,12 @@ -import { Permit } from 'src/modules/permit-application-payment/permit/entities/permit.entity'; -import { PolicyApplication } from '../interface/policy-application.interface'; -import { PermitData } from '../interface/permit.template.interface'; -import { getAccessToken } from './gov-common-services.helper'; -import { GovCommonServices } from '../enum/gov-common-services.enum'; import { HttpService } from '@nestjs/axios'; -import { Cache } from 'cache-manager'; import { AxiosResponse } from 'axios'; +import { Cache } from 'cache-manager'; +import { Permit } from 'src/modules/permit-application-payment/permit/entities/permit.entity'; import { ReadPolicyConfigDto } from '../../modules/policy/dto/response/read-policy-config.dto'; -import { Policy, ValidationResults } from 'onroute-policy-engine'; +import { GovCommonServices } from '../enum/gov-common-services.enum'; +import { PermitData } from '../interface/permit.template.interface'; +import { PolicyApplication } from '../interface/policy-application.interface'; +import { getAccessToken } from './gov-common-services.helper'; export const convertToPolicyApplication = ( application: Permit, @@ -39,19 +38,10 @@ export const getActivePolicyDefinitions = async ( 'Content-Type': 'application/json', }, }); - return (await response.data.json()) as ReadPolicyConfigDto[]; -}; - -export const validateWithPolicyEngine = async ( - permitApplication: unknown, - cacheManager: Cache, -): Promise => { - const policyDefinitions: ReadPolicyConfigDto[] = await cacheManager.get( - 'active-policy-definitions', - ); - const policy = new Policy(policyDefinitions); - const validationResults: ValidationResults = - await policy.validate(permitApplication); - - return validationResults.violations.length > 0; + const policyConfigArray = + (await response.data.json()) as ReadPolicyConfigDto[]; + if (!policyConfigArray.length) { + return {}; + } + return policyConfigArray[0]; }; diff --git a/vehicles/src/modules/permit-application-payment/payment/payment.service.ts b/vehicles/src/modules/permit-application-payment/payment/payment.service.ts index 11653208f..a0ace3694 100644 --- a/vehicles/src/modules/permit-application-payment/payment/payment.service.ts +++ b/vehicles/src/modules/permit-application-payment/payment/payment.service.ts @@ -71,6 +71,8 @@ import { PermitData } from 'src/common/interface/permit.template.interface'; import { isValidLoa } from 'src/common/helper/validate-loa.helper'; import { PermitHistoryDto } from '../permit/dto/response/permit-history.dto'; import { SpecialAuthService } from 'src/modules/special-auth/special-auth.service'; +import { ReadPolicyConfigDto } from '../../policy/dto/response/read-policy-config.dto'; +import { Policy, ValidationResults } from 'onroute-policy-engine/.'; @Injectable() export class PaymentService { @@ -257,6 +259,24 @@ export class PaymentService { ); } + /** + * Validates with policy engine. + * @param permitApplication + * @returns + */ + private async validateWithPolicyEngine( + permitApplication: Permit, + ): Promise { + const policyDefinitions: ReadPolicyConfigDto = await this.cacheManager.get( + CacheKey.POLICY_CONFIGURATIONS, + ); + const policy = new Policy(policyDefinitions.policy); + const validationResults: ValidationResults = + await policy.validate(permitApplication); + + return validationResults.violations.length > 0; + } + /** * Creates a Transaction in ORBC System. * @param currentUser - The current user object of type {@link IUserJWT} @@ -282,6 +302,10 @@ export class PaymentService { createTransactionDto.transactionTypeId == TransactionType.REFUND || createTransactionDto.paymentMethodTypeCode === PaymentMethodTypeEnum.NO_PAYMENT; + const isPolicyEngineEnabled = + featureFlags?.['VALIDATE-WITH-POLICY-ENGINE'] && + (featureFlags['VALIDATE-WITH-POLICY-ENGINE'] as FeatureFlagValue) === + FeatureFlagValue.ENABLED; // If the user is a staff user, // transacation is NOT a refund or no payment and STAFF-CAN-PAY is disabled, @@ -364,6 +388,14 @@ export class PaymentService { if (permitData.loas) { await isValidLoa(application, queryRunner, this.classMapper); } + if (isPolicyEngineEnabled) { + const isValid = await this.validateWithPolicyEngine(application); + if (!isValid) { + throw new BadRequestException( + 'Application data does not meet policy engine requirements.', + ); + } + } } const totalTransactionAmount = await this.validateApplicationAndPayment( createTransactionDto, From 8b42fc6da5c6752ca101f3c4f68c0ba9d7c52b32 Mon Sep 17 00:00:00 2001 From: Krishnan Subramanian Date: Wed, 15 Jan 2025 15:59:35 -0800 Subject: [PATCH 09/16] Remove cache from start up to on demand --- vehicles/src/app.service.ts | 11 -------- .../src/common/helper/policy-engine.helper.ts | 2 +- .../payment/payment.service.ts | 27 +++++++++++++++++-- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/vehicles/src/app.service.ts b/vehicles/src/app.service.ts index 9a8224a62..582ba8c2d 100644 --- a/vehicles/src/app.service.ts +++ b/vehicles/src/app.service.ts @@ -13,7 +13,6 @@ import { LogAsyncMethodExecution } from './common/decorator/log-async-method-exe import { FeatureFlagsService } from './modules/feature-flags/feature-flags.service'; import { ApplicationService } from './modules/permit-application-payment/application/application.service'; import { HttpService } from '@nestjs/axios'; -import { getActivePolicyDefinitions } from './common/helper/policy-engine.helper'; @Injectable() export class AppService { @@ -131,16 +130,6 @@ export class AppService { createCacheMap(permitApprovalSource, 'id', 'code'), ); - const policyConfigs = await getActivePolicyDefinitions( - this.httpService, - this.cacheManager, - ); - await addToCache( - this.cacheManager, - CacheKey.POLICY_CONFIGURATIONS, - JSON.stringify(policyConfigs), - ); - const endDateTime = new Date(); const processingTime = endDateTime.getTime() - startDateTime.getTime(); this.logger.log( diff --git a/vehicles/src/common/helper/policy-engine.helper.ts b/vehicles/src/common/helper/policy-engine.helper.ts index 3b4a3213f..8a65e1cf9 100644 --- a/vehicles/src/common/helper/policy-engine.helper.ts +++ b/vehicles/src/common/helper/policy-engine.helper.ts @@ -41,7 +41,7 @@ export const getActivePolicyDefinitions = async ( const policyConfigArray = (await response.data.json()) as ReadPolicyConfigDto[]; if (!policyConfigArray.length) { - return {}; + return null; } return policyConfigArray[0]; }; diff --git a/vehicles/src/modules/permit-application-payment/payment/payment.service.ts b/vehicles/src/modules/permit-application-payment/payment/payment.service.ts index a0ace3694..a3721c5a4 100644 --- a/vehicles/src/modules/permit-application-payment/payment/payment.service.ts +++ b/vehicles/src/modules/permit-application-payment/payment/payment.service.ts @@ -52,6 +52,7 @@ import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Cache } from 'cache-manager'; import { CacheKey } from 'src/common/enum/cache-key.enum'; import { + addToCache, getFromCache, getMapFromCache, } from '../../../common/helper/cache.helper'; @@ -73,12 +74,15 @@ import { PermitHistoryDto } from '../permit/dto/response/permit-history.dto'; import { SpecialAuthService } from 'src/modules/special-auth/special-auth.service'; import { ReadPolicyConfigDto } from '../../policy/dto/response/read-policy-config.dto'; import { Policy, ValidationResults } from 'onroute-policy-engine/.'; +import { getActivePolicyDefinitions } from '../../../common/helper/policy-engine.helper'; +import { HttpService } from '@nestjs/axios'; @Injectable() export class PaymentService { private readonly logger = new Logger(PaymentService.name); constructor( private dataSource: DataSource, + private readonly httpService: HttpService, @InjectRepository(Transaction) private transactionRepository: Repository, @InjectRepository(Receipt) @@ -267,10 +271,29 @@ export class PaymentService { private async validateWithPolicyEngine( permitApplication: Permit, ): Promise { - const policyDefinitions: ReadPolicyConfigDto = await this.cacheManager.get( + const policyDefinitions: string = await this.cacheManager.get( CacheKey.POLICY_CONFIGURATIONS, ); - const policy = new Policy(policyDefinitions.policy); + if (!policyDefinitions) { + const policyDefinitions = await getActivePolicyDefinitions( + this.httpService, + this.cacheManager, + ); + if (!policyDefinitions) { + throw new InternalServerErrorException( + 'Policy engine is not available', + ); + } + await addToCache( + this.cacheManager, + CacheKey.POLICY_CONFIGURATIONS, + JSON.stringify(policyDefinitions), + ); + } + const activePolicyDefintion = JSON.parse( + policyDefinitions, + ) as ReadPolicyConfigDto; + const policy = new Policy(activePolicyDefintion.policy); const validationResults: ValidationResults = await policy.validate(permitApplication); From 1020b1fc1cf9903f50344ce5be84fde91ecb9f04 Mon Sep 17 00:00:00 2001 From: Krishnan Subramanian Date: Thu, 23 Jan 2025 12:19:40 -0800 Subject: [PATCH 10/16] Adding a common service --- .../dbo.ORBC_FEATURE_FLAG.Table.sql | 16 +---- vehicles/package-lock.json | 8 +-- vehicles/package.json | 2 +- vehicles/src/common/logger/logger.config.ts | 2 +- vehicles/src/modules/common/common.service.ts | 66 ++++++++++++++++++- .../payment/payment.service.ts | 46 ++----------- 6 files changed, 77 insertions(+), 63 deletions(-) diff --git a/database/mssql/scripts/sampledata/dbo.ORBC_FEATURE_FLAG.Table.sql b/database/mssql/scripts/sampledata/dbo.ORBC_FEATURE_FLAG.Table.sql index d6d7d1b85..c1e1caceb 100644 --- a/database/mssql/scripts/sampledata/dbo.ORBC_FEATURE_FLAG.Table.sql +++ b/database/mssql/scripts/sampledata/dbo.ORBC_FEATURE_FLAG.Table.sql @@ -63,8 +63,8 @@ INSERT INTO VALUES ( '3', - 'POLICY-CONFIG', - 'ENABLED', + 'VALIDATE-WITH-POLICY-ENGINE', + 'DISABLED', NULL, N'dbo', GETUTCDATE(), @@ -301,18 +301,6 @@ VALUES N'dbo', GETUTCDATE() ); -INSERT INTO - [dbo].[ORBC_FEATURE_FLAG] ( - [FEATURE_ID], - [FEATURE_KEY], - [FEATURE_VALUE], - [CONCURRENCY_CONTROL_NUMBER], - [DB_CREATE_USERID], - [DB_CREATE_TIMESTAMP], - [DB_LAST_UPDATE_USERID], - [DB_LAST_UPDATE_TIMESTAMP] - ) -VALUES ( '14', 'VALIDATE-WITH-POLICY-ENGINE', diff --git a/vehicles/package-lock.json b/vehicles/package-lock.json index 87e0268a1..20015ebae 100644 --- a/vehicles/package-lock.json +++ b/vehicles/package-lock.json @@ -39,7 +39,7 @@ "nest-winston": "^1.10.0", "nestjs-cls": "^4.5.0", "nestjs-typeorm-paginate": "^4.0.4", - "onroute-policy-engine": "^1.4.1", + "onroute-policy-engine": "^1.5.0", "passport": "^0.7.0", "passport-jwt": "^4.0.1", "response-time": "^2.3.3", @@ -9307,9 +9307,9 @@ } }, "node_modules/onroute-policy-engine": { - "version": "1.4.1", - "resolved": "https://registry.npmjs.org/onroute-policy-engine/-/onroute-policy-engine-1.4.1.tgz", - "integrity": "sha512-NSlwb9j2IK6hSk2LOiP02pno51h19t/sCYNUsEpzRww3CPOVOeDw8N4zer7D3tbwDCA9EBkvaLKQh7c8v8nqFQ==", + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/onroute-policy-engine/-/onroute-policy-engine-1.5.0.tgz", + "integrity": "sha512-MAbbwtyJUGssrSOepZT0XpyfIKDaCDcNH3QxeybfvnGdd4Ly0sc+WawZoxraZJyo0dVDqt/5tFs8JAdGQQCdYw==", "dependencies": { "dayjs": "^1.11.13", "json-rules-engine": "^7.2.1", diff --git a/vehicles/package.json b/vehicles/package.json index 7b6c0a13a..e37d32d51 100644 --- a/vehicles/package.json +++ b/vehicles/package.json @@ -71,7 +71,7 @@ "nest-winston": "^1.10.0", "nestjs-cls": "^4.5.0", "nestjs-typeorm-paginate": "^4.0.4", - "onroute-policy-engine": "^1.4.1", + "onroute-policy-engine": "^1.5.0", "passport": "^0.7.0", "passport-jwt": "^4.0.1", "response-time": "^2.3.3", diff --git a/vehicles/src/common/logger/logger.config.ts b/vehicles/src/common/logger/logger.config.ts index b4f2604bd..6a0a933c5 100644 --- a/vehicles/src/common/logger/logger.config.ts +++ b/vehicles/src/common/logger/logger.config.ts @@ -7,7 +7,7 @@ const correlationIdFormat = winston.format((info) => { const cls = ClsServiceManager.getClsService(); const correlationId = cls.getId(); if (correlationId) { - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-base-to-string info.message = `[${correlationId}] ${info.message}`; } return info; diff --git a/vehicles/src/modules/common/common.service.ts b/vehicles/src/modules/common/common.service.ts index 0185d60a5..f6448c2fa 100644 --- a/vehicles/src/modules/common/common.service.ts +++ b/vehicles/src/modules/common/common.service.ts @@ -1,9 +1,22 @@ -import { Injectable } from '@nestjs/common'; +import { + Inject, + Injectable, + InternalServerErrorException, +} from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { Country } from './entities/country.entity'; import { Province } from './entities/province.entity'; import { LogAsyncMethodExecution } from '../../common/decorator/log-async-method-execution.decorator'; +import { Permit } from '../permit-application-payment/permit/entities/permit.entity'; +import { Policy, ValidationResults } from 'onroute-policy-engine/.'; +import { ReadPolicyConfigDto } from '../policy/dto/response/read-policy-config.dto'; +import { CACHE_MANAGER } from '@nestjs/cache-manager'; +import { Cache } from 'cache-manager'; +import { HttpService } from '@nestjs/axios'; +import { CacheKey } from '../../common/enum/cache-key.enum'; +import { getActivePolicyDefinitions } from '../../common/helper/policy-engine.helper'; +import { addToCache } from '../../common/helper/cache.helper'; @Injectable() export class CommonService { @@ -12,6 +25,9 @@ export class CommonService { private countryRepository: Repository, @InjectRepository(Province) private provinceRepository: Repository, + private readonly httpService: HttpService, + @Inject(CACHE_MANAGER) + private readonly cacheManager: Cache, ) {} @LogAsyncMethodExecution() @@ -37,4 +53,52 @@ export class CommonService { async findAllProvinces(): Promise { return await this.provinceRepository.find({}); } + + /** + * Validates a permit application using the policy engine. + * + * This method retrieves the active policy definitions to validate + * the given permit application and returns the validation results. + * + * The policy definitions are fetched from the cache if available; + * otherwise, it retrieves them from the policy service and stores + * them in the cache for future requests. + * If the policy engine is unavailable, an InternalServerErrorException + * is thrown. + * + * @param {Permit} permitApplication - The permit application to be validated. + * @returns {Promise} - The results of the validation process. + * @throws {InternalServerErrorException} - If the policy engine is not available. + */ + @LogAsyncMethodExecution() + async validateWithPolicyEngine( + permitApplication: Permit, + ): Promise { + const policyDefinitions: string = await this.cacheManager.get( + CacheKey.POLICY_CONFIGURATIONS, + ); + if (!policyDefinitions) { + const policyDefinitions = await getActivePolicyDefinitions( + this.httpService, + this.cacheManager, + ); + if (!policyDefinitions) { + throw new InternalServerErrorException( + 'Policy engine is not available', + ); + } + await addToCache( + this.cacheManager, + CacheKey.POLICY_CONFIGURATIONS, + JSON.stringify(policyDefinitions), + ); + } + const activePolicyDefintion = JSON.parse( + policyDefinitions, + ) as ReadPolicyConfigDto; + const policy = new Policy(activePolicyDefintion.policy); + const validationResults: ValidationResults = + await policy.validate(permitApplication); + return validationResults; + } } diff --git a/vehicles/src/modules/permit-application-payment/payment/payment.service.ts b/vehicles/src/modules/permit-application-payment/payment/payment.service.ts index 3b05576c3..b40db4ad8 100644 --- a/vehicles/src/modules/permit-application-payment/payment/payment.service.ts +++ b/vehicles/src/modules/permit-application-payment/payment/payment.service.ts @@ -52,7 +52,6 @@ import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Cache } from 'cache-manager'; import { CacheKey } from 'src/common/enum/cache-key.enum'; import { - addToCache, getFromCache, getMapFromCache, } from '../../../common/helper/cache.helper'; @@ -72,10 +71,8 @@ import { PermitData } from 'src/common/interface/permit.template.interface'; import { isValidLoa } from 'src/common/helper/validate-loa.helper'; import { PermitHistoryDto } from '../permit/dto/response/permit-history.dto'; import { SpecialAuthService } from 'src/modules/special-auth/special-auth.service'; -import { ReadPolicyConfigDto } from '../../policy/dto/response/read-policy-config.dto'; -import { Policy, ValidationResults } from 'onroute-policy-engine/.'; -import { getActivePolicyDefinitions } from '../../../common/helper/policy-engine.helper'; import { HttpService } from '@nestjs/axios'; +import { CommonService } from '../../common/common.service'; @Injectable() export class PaymentService { @@ -97,6 +94,7 @@ export class PaymentService { @InjectMapper() private readonly classMapper: Mapper, @Inject(CACHE_MANAGER) private readonly cacheManager: Cache, + private commonService: CommonService, ) {} private generateHashExpiry = (currDate?: Date) => { @@ -263,43 +261,6 @@ export class PaymentService { ); } - /** - * Validates with policy engine. - * @param permitApplication - * @returns - */ - private async validateWithPolicyEngine( - permitApplication: Permit, - ): Promise { - const policyDefinitions: string = await this.cacheManager.get( - CacheKey.POLICY_CONFIGURATIONS, - ); - if (!policyDefinitions) { - const policyDefinitions = await getActivePolicyDefinitions( - this.httpService, - this.cacheManager, - ); - if (!policyDefinitions) { - throw new InternalServerErrorException( - 'Policy engine is not available', - ); - } - await addToCache( - this.cacheManager, - CacheKey.POLICY_CONFIGURATIONS, - JSON.stringify(policyDefinitions), - ); - } - const activePolicyDefintion = JSON.parse( - policyDefinitions, - ) as ReadPolicyConfigDto; - const policy = new Policy(activePolicyDefintion.policy); - const validationResults: ValidationResults = - await policy.validate(permitApplication); - - return validationResults.violations.length > 0; - } - /** * Creates a Transaction in ORBC System. * @param currentUser - The current user object of type {@link IUserJWT} @@ -412,7 +373,8 @@ export class PaymentService { await isValidLoa(application, queryRunner, this.classMapper); } if (isPolicyEngineEnabled) { - const isValid = await this.validateWithPolicyEngine(application); + const isValid = + await this.commonService.validateWithPolicyEngine(application); if (!isValid) { throw new BadRequestException( 'Application data does not meet policy engine requirements.', From a7ff5c4885c9b25ca051fa29aeca5d726e1fbfd3 Mon Sep 17 00:00:00 2001 From: Krishnan Subramanian Date: Thu, 23 Jan 2025 12:21:47 -0800 Subject: [PATCH 11/16] Correcting query in sample data --- .../scripts/sampledata/dbo.ORBC_FEATURE_FLAG.Table.sql | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/database/mssql/scripts/sampledata/dbo.ORBC_FEATURE_FLAG.Table.sql b/database/mssql/scripts/sampledata/dbo.ORBC_FEATURE_FLAG.Table.sql index c1e1caceb..bbe9a4f38 100644 --- a/database/mssql/scripts/sampledata/dbo.ORBC_FEATURE_FLAG.Table.sql +++ b/database/mssql/scripts/sampledata/dbo.ORBC_FEATURE_FLAG.Table.sql @@ -301,16 +301,6 @@ VALUES N'dbo', GETUTCDATE() ); - ( - '14', - 'VALIDATE-WITH-POLICY-ENGINE', - 'ENABLED', - NULL, - N'dbo', - GETUTCDATE(), - N'dbo', - GETUTCDATE() - ); SET IDENTITY_INSERT [dbo].[ORBC_FEATURE_FLAG] OFF GO \ No newline at end of file From 9393a212fc746a5b348c0cef0828f26d1c0b8687 Mon Sep 17 00:00:00 2001 From: Krishnan Subramanian Date: Thu, 23 Jan 2025 12:25:04 -0800 Subject: [PATCH 12/16] Some corrections --- vehicles/src/app.service.ts | 2 -- .../permit-application-payment/payment/payment.service.ts | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/vehicles/src/app.service.ts b/vehicles/src/app.service.ts index 582ba8c2d..bcee88529 100644 --- a/vehicles/src/app.service.ts +++ b/vehicles/src/app.service.ts @@ -12,7 +12,6 @@ import { PaymentService } from './modules/permit-application-payment/payment/pay import { LogAsyncMethodExecution } from './common/decorator/log-async-method-execution.decorator'; import { FeatureFlagsService } from './modules/feature-flags/feature-flags.service'; import { ApplicationService } from './modules/permit-application-payment/application/application.service'; -import { HttpService } from '@nestjs/axios'; @Injectable() export class AppService { @@ -28,7 +27,6 @@ export class AppService { private paymentService: PaymentService, private featureFlagsService: FeatureFlagsService, private applicationService: ApplicationService, - private readonly httpService: HttpService, ) {} getHello(): string { diff --git a/vehicles/src/modules/permit-application-payment/payment/payment.service.ts b/vehicles/src/modules/permit-application-payment/payment/payment.service.ts index b40db4ad8..3d8642770 100644 --- a/vehicles/src/modules/permit-application-payment/payment/payment.service.ts +++ b/vehicles/src/modules/permit-application-payment/payment/payment.service.ts @@ -373,9 +373,9 @@ export class PaymentService { await isValidLoa(application, queryRunner, this.classMapper); } if (isPolicyEngineEnabled) { - const isValid = + const validationResults = await this.commonService.validateWithPolicyEngine(application); - if (!isValid) { + if (validationResults?.violations?.length > 0) { throw new BadRequestException( 'Application data does not meet policy engine requirements.', ); From 88f0b4426ee5ee78250cbbafa3684b29b15317ae Mon Sep 17 00:00:00 2001 From: Krishnan Subramanian Date: Thu, 23 Jan 2025 12:25:51 -0800 Subject: [PATCH 13/16] Remove httpservice from common service --- .../permit-application-payment/payment/payment.service.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/vehicles/src/modules/permit-application-payment/payment/payment.service.ts b/vehicles/src/modules/permit-application-payment/payment/payment.service.ts index 3d8642770..245f12db5 100644 --- a/vehicles/src/modules/permit-application-payment/payment/payment.service.ts +++ b/vehicles/src/modules/permit-application-payment/payment/payment.service.ts @@ -71,7 +71,6 @@ import { PermitData } from 'src/common/interface/permit.template.interface'; import { isValidLoa } from 'src/common/helper/validate-loa.helper'; import { PermitHistoryDto } from '../permit/dto/response/permit-history.dto'; import { SpecialAuthService } from 'src/modules/special-auth/special-auth.service'; -import { HttpService } from '@nestjs/axios'; import { CommonService } from '../../common/common.service'; @Injectable() @@ -79,7 +78,6 @@ export class PaymentService { private readonly logger = new Logger(PaymentService.name); constructor( private dataSource: DataSource, - private readonly httpService: HttpService, @InjectRepository(Transaction) private transactionRepository: Repository, @InjectRepository(Receipt) From a2350c1402680bc028e2bafe6f00b68acb216276 Mon Sep 17 00:00:00 2001 From: Krishnan Subramanian Date: Thu, 23 Jan 2025 12:34:16 -0800 Subject: [PATCH 14/16] Correcting import type --- vehicles/src/modules/common/common.service.ts | 3 ++- .../permit-application-payment/payment/payment.service.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/vehicles/src/modules/common/common.service.ts b/vehicles/src/modules/common/common.service.ts index f6448c2fa..8a2cb5221 100644 --- a/vehicles/src/modules/common/common.service.ts +++ b/vehicles/src/modules/common/common.service.ts @@ -9,7 +9,8 @@ import { Country } from './entities/country.entity'; import { Province } from './entities/province.entity'; import { LogAsyncMethodExecution } from '../../common/decorator/log-async-method-execution.decorator'; import { Permit } from '../permit-application-payment/permit/entities/permit.entity'; -import { Policy, ValidationResults } from 'onroute-policy-engine/.'; +import { ValidationResults } from 'onroute-policy-engine/dist/validation-results'; +import { Policy } from 'onroute-policy-engine/dist/policy-engine'; import { ReadPolicyConfigDto } from '../policy/dto/response/read-policy-config.dto'; import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Cache } from 'cache-manager'; diff --git a/vehicles/src/modules/permit-application-payment/payment/payment.service.ts b/vehicles/src/modules/permit-application-payment/payment/payment.service.ts index 245f12db5..55fc905f8 100644 --- a/vehicles/src/modules/permit-application-payment/payment/payment.service.ts +++ b/vehicles/src/modules/permit-application-payment/payment/payment.service.ts @@ -92,7 +92,7 @@ export class PaymentService { @InjectMapper() private readonly classMapper: Mapper, @Inject(CACHE_MANAGER) private readonly cacheManager: Cache, - private commonService: CommonService, + private readonly commonService: CommonService, ) {} private generateHashExpiry = (currDate?: Date) => { From e854eb84d3f11c2eeff98d00e729042d5707bd8a Mon Sep 17 00:00:00 2001 From: Krishnan Subramanian Date: Thu, 23 Jan 2025 12:39:26 -0800 Subject: [PATCH 15/16] Correct import again --- vehicles/src/modules/common/common.service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vehicles/src/modules/common/common.service.ts b/vehicles/src/modules/common/common.service.ts index 8a2cb5221..3169ee10a 100644 --- a/vehicles/src/modules/common/common.service.ts +++ b/vehicles/src/modules/common/common.service.ts @@ -9,8 +9,8 @@ import { Country } from './entities/country.entity'; import { Province } from './entities/province.entity'; import { LogAsyncMethodExecution } from '../../common/decorator/log-async-method-execution.decorator'; import { Permit } from '../permit-application-payment/permit/entities/permit.entity'; -import { ValidationResults } from 'onroute-policy-engine/dist/validation-results'; -import { Policy } from 'onroute-policy-engine/dist/policy-engine'; +import { ValidationResults } from 'onroute-policy-engine/dist/validation-results.d'; +import { Policy } from 'onroute-policy-engine/dist/policy-engine.d'; import { ReadPolicyConfigDto } from '../policy/dto/response/read-policy-config.dto'; import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Cache } from 'cache-manager'; From fbb6d9ae52919888798c4fbf3b8e05307ef113fe Mon Sep 17 00:00:00 2001 From: Krishnan Subramanian Date: Thu, 23 Jan 2025 12:44:52 -0800 Subject: [PATCH 16/16] Trying again --- vehicles/src/modules/common/common.service.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/vehicles/src/modules/common/common.service.ts b/vehicles/src/modules/common/common.service.ts index 3169ee10a..0a90c5745 100644 --- a/vehicles/src/modules/common/common.service.ts +++ b/vehicles/src/modules/common/common.service.ts @@ -9,8 +9,7 @@ import { Country } from './entities/country.entity'; import { Province } from './entities/province.entity'; import { LogAsyncMethodExecution } from '../../common/decorator/log-async-method-execution.decorator'; import { Permit } from '../permit-application-payment/permit/entities/permit.entity'; -import { ValidationResults } from 'onroute-policy-engine/dist/validation-results.d'; -import { Policy } from 'onroute-policy-engine/dist/policy-engine.d'; +import { Policy, ValidationResults } from 'onroute-policy-engine'; import { ReadPolicyConfigDto } from '../policy/dto/response/read-policy-config.dto'; import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Cache } from 'cache-manager';