Skip to content

Commit

Permalink
Remove cache from start up to on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
krishnan-aot committed Jan 15, 2025
1 parent 19f459b commit 8b42fc6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
11 changes: 0 additions & 11 deletions vehicles/src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion vehicles/src/common/helper/policy-engine.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<Transaction>,
@InjectRepository(Receipt)
Expand Down Expand Up @@ -267,10 +271,29 @@ export class PaymentService {
private async validateWithPolicyEngine(
permitApplication: Permit,
): Promise<boolean> {
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);

Expand Down

0 comments on commit 8b42fc6

Please sign in to comment.