diff --git a/src/bernard/client.ts b/src/bernard/client.ts index a2be2c1..94396c7 100644 --- a/src/bernard/client.ts +++ b/src/bernard/client.ts @@ -15,3 +15,19 @@ export async function sendPathChangedCmd( }; await sendMessage(JSON.stringify(msg), process.env.BERNARD_QUEUE_URL); } + +export async function sendProductUpdatedEvent( + productIds: (string | number)[], + caches: string[], +): Promise { + const msg = { + args: { + name: 'ProductUpdated', + ids: productIds, + caches: caches, + }, + class: 'Bernard:Message:DefaultMessage', + timestamp: Math.round(Date.now() / 1000), // Epoch seconds + }; + await sendMessage(JSON.stringify(msg), process.env.BERNARD_QUEUE_URL); +} diff --git a/src/resolvers/products-resolver.ts b/src/resolvers/products-resolver.ts index 3280ee4..02ffa72 100644 --- a/src/resolvers/products-resolver.ts +++ b/src/resolvers/products-resolver.ts @@ -24,6 +24,7 @@ import { PrintProductDefaults } from '../types/printproduct-defaults-types'; import { GraphQLError } from 'graphql'; import { getOrientationString, isRepeatingPattern } from '../datasources/utils'; import { sendMessage } from '../aws/sqs'; +import { sendProductUpdatedEvent } from '../bernard/client'; const ProductBlacklist = { async market(parent, _args, { dataSources }) { @@ -271,6 +272,7 @@ export const productMutationTypeDefs = { productId, info, ); + await sendProductUpdatedEvent([productId], ['product']); return (dataSources.productApi).getProduct(productId); }, @@ -320,6 +322,10 @@ export const productMutationTypeDefs = { productId, groupIds, ); + + // Clear the cache to remove the old data + await sendProductUpdatedEvent([productId], ['product_groups']); + const product = await (dataSources.productApi).getProduct( productId, ); @@ -554,6 +560,7 @@ export const productMutationTypeDefs = { visible, browsable, ); + await sendProductUpdatedEvent(productIds, ['product']); // Clears cache return { value: 'OK', };