Skip to content

Commit

Permalink
refactor(core): Use constant for common cache keys
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Apr 3, 2024
1 parent 5641b48 commit 969b72c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
7 changes: 7 additions & 0 deletions packages/core/src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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')
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/service/services/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down

0 comments on commit 969b72c

Please sign in to comment.