Browse Source

refactor(core): Use constant for common cache keys

Michael Bromley 1 year ago
parent
commit
969b72ccb4

+ 7 - 0
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',
+};

+ 2 - 1
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),
         );
 

+ 3 - 2
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) {

+ 2 - 1
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<GlobalSettings> {
-        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')

+ 5 - 2
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);
     }