Skip to content

Commit

Permalink
fix(core): Correctly update cache in customerGroup promo condition
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Nov 9, 2020
1 parent 6676335 commit 8df4fec
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/core/src/common/ttl-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class TtlCache<K, V> {
const hit = this.cache.get(key);
const now = new Date().getTime();
if (hit) {
if (hit.expires < now) {
if (now < hit.expires) {
return hit.value;
} else {
this.cache.delete(key);
Expand All @@ -42,6 +42,10 @@ export class TtlCache<K, V> {
});
}

delete(key: K) {
this.cache.delete(key);
}

private first() {
return this.cache.keys().next().value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { LanguageCode } from '@vendure/common/lib/generated-types';
import { ID } from '@vendure/common/lib/shared-types';
import { Subscription } from 'rxjs';

import { TtlCache } from '../../../common/ttl-cache';
import { idsAreEqual } from '../../../common/utils';
import { EventBus } from '../../../event-bus/event-bus';
import { CustomerGroupEvent } from '../../../event-bus/events/customer-group-event';
import { PromotionCondition } from '../promotion-condition';

let customerService: import('../../../service/services/customer.service').CustomerService;
let subscription: Subscription;

const fiveMinutes = 5 * 60 * 1000;
const cache = new TtlCache<ID, ID[]>({ ttl: fiveMinutes });
Expand All @@ -24,6 +28,19 @@ export const customerGroup = new PromotionCondition({
// Lazily-imported to avoid circular dependency issues.
const { CustomerService } = await import('../../../service/services/customer.service');
customerService = injector.get(CustomerService);
subscription = injector
.get(EventBus)
.ofType(CustomerGroupEvent)
.subscribe(event => {
// When a customer is added to or removed from a group, we need
// to invalidate the cache for that customer id
for (const customer of event.customers) {
cache.delete(customer.id);
}
});
},
destroy() {
subscription.unsubscribe();
},
async check(ctx, order, args) {
if (!order.customer) {
Expand Down

0 comments on commit 8df4fec

Please sign in to comment.