diff --git a/TODO.md b/TODO.md index 8f3d322..2d40642 100644 --- a/TODO.md +++ b/TODO.md @@ -10,7 +10,6 @@ ## Short term - [Feat] [caching] Use getURLs for precaching + precache everything -- [Feat] [caching] Refresh payload cache (wordings, languages...) when receiving webhook - [Bugs] Make sure uploads name are slug-like and with an extension. - [Bugs] Nyupun can't upload subtitles files - [Bugs] https://v3.accords-library.com/en/collectibles/dod-original-soundtrack/scans obi is way too big diff --git a/src/pages/[locale]/api/hooks/collection-operation.ts b/src/pages/[locale]/api/hooks/collection-operation.ts index 6023d2b..7064562 100644 --- a/src/pages/[locale]/api/hooks/collection-operation.ts +++ b/src/pages/[locale]/api/hooks/collection-operation.ts @@ -1,6 +1,12 @@ import type { APIRoute } from "astro"; -import type { AfterOperationWebHookMessage } from "src/shared/payload/payload-sdk"; -import { invalidateDataCache } from "src/utils/payload"; +import { Collections, type AfterOperationWebHookMessage } from "src/shared/payload/payload-sdk"; +import { + invalidateDataCache, + refreshCurrencies, + refreshLocales, + refreshWebsiteConfig, + refreshWordings, +} from "src/utils/payload"; export const POST: APIRoute = async ({ request }) => { const auth = request.headers.get("Authorization"); @@ -12,10 +18,35 @@ export const POST: APIRoute = async ({ request }) => { const message = (await request.json()) as AfterOperationWebHookMessage; console.log("[Webhook] Received messages from CMS:", message); - await invalidateDataCache( - [...(message.id ? [message.id] : []), ...message.addedDependantIds], - message.urls - ); + // Not awaiting on purpose to respond with a 202 and not block the CMS + handleWebHookMessage(message); return new Response(null, { status: 202, statusText: "Accepted" }); }; + +const handleWebHookMessage = async ({ + addedDependantIds, + collection, + urls, + id, +}: AfterOperationWebHookMessage) => { + await invalidateDataCache([...(id ? [id] : []), ...addedDependantIds], urls); + + switch (collection) { + case Collections.Wordings: + await refreshWordings(); + break; + + case Collections.Currencies: + await refreshCurrencies(); + break; + + case Collections.Languages: + await refreshLocales(); + break; + + case Collections.WebsiteConfig: + await refreshWebsiteConfig(); + break; + } +}; diff --git a/src/shared/payload/payload-sdk.ts b/src/shared/payload/payload-sdk.ts index d40d083..b017b02 100644 --- a/src/shared/payload/payload-sdk.ts +++ b/src/shared/payload/payload-sdk.ts @@ -1279,6 +1279,7 @@ export enum AttributeTypes { /* WEB HOOKS */ export type AfterOperationWebHookMessage = { + collection: Collections; id?: string; addedDependantIds: string[]; urls: string[];