Refresh wordings etc... when receiving a webhook

This commit is contained in:
DrMint 2024-06-19 11:14:49 +02:00
parent 3e4d1bff26
commit 66ac5bd519
3 changed files with 38 additions and 7 deletions

View File

@ -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

View File

@ -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;
}
};

View File

@ -1279,6 +1279,7 @@ export enum AttributeTypes {
/* WEB HOOKS */
export type AfterOperationWebHookMessage = {
collection: Collections;
id?: string;
addedDependantIds: string[];
urls: string[];