Refresh wordings etc... when receiving a webhook
This commit is contained in:
parent
3e4d1bff26
commit
66ac5bd519
1
TODO.md
1
TODO.md
|
@ -10,7 +10,6 @@
|
||||||
## Short term
|
## Short term
|
||||||
|
|
||||||
- [Feat] [caching] Use getURLs for precaching + precache everything
|
- [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] Make sure uploads name are slug-like and with an extension.
|
||||||
- [Bugs] Nyupun can't upload subtitles files
|
- [Bugs] Nyupun can't upload subtitles files
|
||||||
- [Bugs] https://v3.accords-library.com/en/collectibles/dod-original-soundtrack/scans obi is way too big
|
- [Bugs] https://v3.accords-library.com/en/collectibles/dod-original-soundtrack/scans obi is way too big
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
import type { APIRoute } from "astro";
|
import type { APIRoute } from "astro";
|
||||||
import type { AfterOperationWebHookMessage } from "src/shared/payload/payload-sdk";
|
import { Collections, type AfterOperationWebHookMessage } from "src/shared/payload/payload-sdk";
|
||||||
import { invalidateDataCache } from "src/utils/payload";
|
import {
|
||||||
|
invalidateDataCache,
|
||||||
|
refreshCurrencies,
|
||||||
|
refreshLocales,
|
||||||
|
refreshWebsiteConfig,
|
||||||
|
refreshWordings,
|
||||||
|
} from "src/utils/payload";
|
||||||
|
|
||||||
export const POST: APIRoute = async ({ request }) => {
|
export const POST: APIRoute = async ({ request }) => {
|
||||||
const auth = request.headers.get("Authorization");
|
const auth = request.headers.get("Authorization");
|
||||||
|
@ -12,10 +18,35 @@ export const POST: APIRoute = async ({ request }) => {
|
||||||
const message = (await request.json()) as AfterOperationWebHookMessage;
|
const message = (await request.json()) as AfterOperationWebHookMessage;
|
||||||
console.log("[Webhook] Received messages from CMS:", message);
|
console.log("[Webhook] Received messages from CMS:", message);
|
||||||
|
|
||||||
await invalidateDataCache(
|
// Not awaiting on purpose to respond with a 202 and not block the CMS
|
||||||
[...(message.id ? [message.id] : []), ...message.addedDependantIds],
|
handleWebHookMessage(message);
|
||||||
message.urls
|
|
||||||
);
|
|
||||||
|
|
||||||
return new Response(null, { status: 202, statusText: "Accepted" });
|
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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
|
@ -1279,6 +1279,7 @@ export enum AttributeTypes {
|
||||||
/* WEB HOOKS */
|
/* WEB HOOKS */
|
||||||
|
|
||||||
export type AfterOperationWebHookMessage = {
|
export type AfterOperationWebHookMessage = {
|
||||||
|
collection: Collections;
|
||||||
id?: string;
|
id?: string;
|
||||||
addedDependantIds: string[];
|
addedDependantIds: string[];
|
||||||
urls: string[];
|
urls: string[];
|
||||||
|
|
Loading…
Reference in New Issue