diff --git a/packages/core/src/common/constants.ts b/packages/core/src/common/constants.ts index c5154c5f74..87dd2fb4c7 100644 --- a/packages/core/src/common/constants.ts +++ b/packages/core/src/common/constants.ts @@ -76,3 +76,10 @@ export function getAllPermissionsMetadata(customPermissions: PermissionDefinitio const allPermissions = [...DEFAULT_PERMISSIONS, ...customPermissions]; return allPermissions.reduce((all, def) => [...all, ...def.getMetadata()], [] as PermissionMetadata[]); } + +export const CacheKey = { + GlobalSettings: 'GlobalSettings', + AllZones: 'AllZones', + ActiveTaxZone: 'ActiveTaxZone', + ActiveTaxZone_PPA: 'ActiveTaxZone_PPA', +}; diff --git a/packages/core/src/service/helpers/order-calculator/order-calculator.ts b/packages/core/src/service/helpers/order-calculator/order-calculator.ts index 70f5ffb061..b8ee2609c7 100644 --- a/packages/core/src/service/helpers/order-calculator/order-calculator.ts +++ b/packages/core/src/service/helpers/order-calculator/order-calculator.ts @@ -4,6 +4,7 @@ import { AdjustmentType } from '@vendure/common/lib/generated-types'; import { RequestContext } from '../../../api/common/request-context'; import { RequestContextCacheService } from '../../../cache/request-context-cache.service'; +import { CacheKey } from '../../../common/constants'; import { InternalServerError } from '../../../common/error/errors'; import { idsAreEqual } from '../../../common/utils'; import { ConfigService } from '../../../config/config.service'; @@ -58,7 +59,7 @@ export class OrderCalculator { // must be revalidated on any changes to an Order. order.promotions = []; const zones = await this.zoneService.getAllWithMembers(ctx); - const activeTaxZone = await this.requestContextCache.get(ctx, 'activeTaxZone', () => + const activeTaxZone = await this.requestContextCache.get(ctx, CacheKey.ActiveTaxZone, () => taxZoneStrategy.determineTaxZone(ctx, zones, ctx.channel, order), ); diff --git a/packages/core/src/service/helpers/product-price-applicator/product-price-applicator.ts b/packages/core/src/service/helpers/product-price-applicator/product-price-applicator.ts index 2ba1bc5d3f..4f9633701b 100644 --- a/packages/core/src/service/helpers/product-price-applicator/product-price-applicator.ts +++ b/packages/core/src/service/helpers/product-price-applicator/product-price-applicator.ts @@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common'; import { RequestContext } from '../../../api/common/request-context'; import { RequestContextCacheService } from '../../../cache/request-context-cache.service'; +import { CacheKey } from '../../../common/constants'; import { InternalServerError } from '../../../common/error/errors'; import { ConfigService } from '../../../config/config.service'; import { Order } from '../../../entity/order/order.entity'; @@ -73,10 +74,10 @@ export class ProductPriceApplicator { }); } const { taxZoneStrategy } = this.configService.taxOptions; - const zones = await this.requestCache.get(ctx, 'allZones', () => + const zones = await this.requestCache.get(ctx, CacheKey.AllZones, () => this.zoneService.getAllWithMembers(ctx), ); - const activeTaxZone = await this.requestCache.get(ctx, 'activeTaxZone-ppa', () => + const activeTaxZone = await this.requestCache.get(ctx, CacheKey.ActiveTaxZone_PPA, () => taxZoneStrategy.determineTaxZone(ctx, zones, ctx.channel, order), ); if (!activeTaxZone) { diff --git a/packages/core/src/service/services/global-settings.service.ts b/packages/core/src/service/services/global-settings.service.ts index 459087ee4a..01956da8d8 100644 --- a/packages/core/src/service/services/global-settings.service.ts +++ b/packages/core/src/service/services/global-settings.service.ts @@ -3,6 +3,7 @@ import { UpdateGlobalSettingsInput } from '@vendure/common/lib/generated-types'; import { RequestContext } from '../../api/common/request-context'; import { RequestContextCacheService } from '../../cache/request-context-cache.service'; +import { CacheKey } from '../../common/constants'; import { InternalServerError } from '../../common/error/errors'; import { ConfigService } from '../../config/config.service'; import { TransactionalConnection } from '../../connection/transactional-connection'; @@ -58,7 +59,7 @@ export class GlobalSettingsService { * Returns the GlobalSettings entity. */ async getSettings(ctx: RequestContext): Promise { - const settings = await this.requestCache.get(ctx, 'globalSettings', () => + const settings = await this.requestCache.get(ctx, CacheKey.GlobalSettings, () => this.connection .getRepository(ctx, GlobalSettings) .createQueryBuilder('global_settings') diff --git a/packages/core/src/service/services/order.service.ts b/packages/core/src/service/services/order.service.ts index 4d4f7dcc1b..c08b5246a5 100644 --- a/packages/core/src/service/services/order.service.ts +++ b/packages/core/src/service/services/order.service.ts @@ -46,6 +46,7 @@ import { FindOptionsUtils } from 'typeorm/find-options/FindOptionsUtils'; import { RequestContext } from '../../api/common/request-context'; import { RelationPaths } from '../../api/decorators/relations.decorator'; import { RequestContextCacheService } from '../../cache/request-context-cache.service'; +import { CacheKey } from '../../common/constants'; import { ErrorResultUnion, isGraphQlErrorResult } from '../../common/error/error-result'; import { EntityNotFoundError, InternalServerError, UserInputError } from '../../common/error/errors'; import { @@ -855,7 +856,8 @@ export class OrderService { // Since a changed ShippingAddress could alter the activeTaxZone, // we will remove any cached activeTaxZone, so it can be re-calculated // as needed. - this.requestCache.set(ctx, 'activeTaxZone', undefined); + this.requestCache.set(ctx, CacheKey.ActiveTaxZone, undefined); + this.requestCache.set(ctx, CacheKey.ActiveTaxZone_PPA, undefined); return this.applyPriceAdjustments(ctx, order, order.lines); } @@ -878,7 +880,8 @@ export class OrderService { // Since a changed BillingAddress could alter the activeTaxZone, // we will remove any cached activeTaxZone, so it can be re-calculated // as needed. - this.requestCache.set(ctx, 'activeTaxZone', undefined); + this.requestCache.set(ctx, CacheKey.ActiveTaxZone, undefined); + this.requestCache.set(ctx, CacheKey.ActiveTaxZone_PPA, undefined); return this.applyPriceAdjustments(ctx, order, order.lines); }