saleor-apps-redis_apl/apps/search/scripts/migrations/update-webhooks.ts
Krzysztof Wolski 7e0755ec9e
Webhook helpers (#949)
* WIP

* Added script and making implementation more roboust

* Added rollback on issues with the migration

* Cleanup the code

* Use allSettled instead of all

* Do not check spelling in schema files.
Schema is pulled from the API and is not controlled by our team

* Update the pkg json

* Fix typo on log message

* Add dedupe to the ignored words.
Its used by codegen

* Add changesets
2023-09-07 13:04:23 +02:00

47 lines
1.3 KiB
TypeScript

/* eslint-disable turbo/no-undeclared-env-vars */
import { createGraphQLClient } from "@saleor/apps-shared";
import { AuthData } from "@saleor/app-sdk/APL";
import { webhookMigrationRunner } from "@saleor/webhook-utils";
import { appWebhooks } from "../../webhooks";
export const updateWebhooksScript = async ({
authData,
dryRun,
}: {
authData: AuthData;
dryRun: boolean;
}) => {
console.log("Working on env: ", authData.saleorApiUrl);
const client = createGraphQLClient({
saleorApiUrl: authData.saleorApiUrl,
token: authData.token,
});
await webhookMigrationRunner({
client,
dryRun,
getManifests: async ({ appDetails }) => {
const webhooks = appDetails.webhooks;
if (!webhooks?.length) {
console.info("The environment does not have any webhooks, skipping");
return [];
}
// All webhooks in this application are turned on or off. If any of them is enabled, we enable all of them.
const enabled = webhooks.some((w) => w.isActive);
const targetUrl = appDetails.appUrl;
if (!targetUrl?.length) {
throw new Error("App has no defined appUrl, skipping");
}
const baseUrl = new URL(targetUrl).origin;
return appWebhooks.map((w) => ({ ...w.getWebhookManifest(baseUrl), enabled }));
},
});
};