From 6813cccd2bf5408afff0c4adc0e4bac9aa176a4c Mon Sep 17 00:00:00 2001 From: Lukasz Ostrowski Date: Wed, 30 Aug 2023 10:32:44 +0200 Subject: [PATCH] Apply webhook context to all webhook handlers --- .../api/webhooks/saleor/product_created.ts | 1 - .../api/webhooks/saleor/product_deleted.ts | 64 ++++++------------ .../api/webhooks/saleor/product_updated.ts | 65 ++++++------------ .../saleor/product_variant_back_in_stock.ts | 65 ++++++------------ .../saleor/product_variant_created.ts | 67 +++++++------------ .../saleor/product_variant_deleted.ts | 67 +++++++------------ .../saleor/product_variant_out_of_stock.ts | 65 ++++++------------ .../saleor/product_variant_updated.ts | 66 ++++++------------ 8 files changed, 156 insertions(+), 304 deletions(-) diff --git a/apps/search/src/pages/api/webhooks/saleor/product_created.ts b/apps/search/src/pages/api/webhooks/saleor/product_created.ts index 04d914ce2..40790333a 100644 --- a/apps/search/src/pages/api/webhooks/saleor/product_created.ts +++ b/apps/search/src/pages/api/webhooks/saleor/product_created.ts @@ -1,5 +1,4 @@ import { NextWebhookApiHandler } from "@saleor/app-sdk/handlers/next"; -import { createGraphQLClient } from "@saleor/apps-shared"; import { ProductCreated } from "../../../../../generated/graphql"; import { WebhookActivityTogglerService } from "../../../../domain/WebhookActivityToggler.service"; import { createLogger } from "../../../../lib/logger"; diff --git a/apps/search/src/pages/api/webhooks/saleor/product_deleted.ts b/apps/search/src/pages/api/webhooks/saleor/product_deleted.ts index beb9c802c..463469437 100644 --- a/apps/search/src/pages/api/webhooks/saleor/product_deleted.ts +++ b/apps/search/src/pages/api/webhooks/saleor/product_deleted.ts @@ -1,11 +1,9 @@ import { NextWebhookApiHandler } from "@saleor/app-sdk/handlers/next"; -import { ChannelsDocument, ProductDeleted } from "../../../../../generated/graphql"; -import { AlgoliaSearchProvider } from "../../../../lib/algolia/algoliaSearchProvider"; -import { getAlgoliaConfiguration } from "../../../../lib/algolia/getAlgoliaConfiguration"; +import { ProductDeleted } from "../../../../../generated/graphql"; import { WebhookActivityTogglerService } from "../../../../domain/WebhookActivityToggler.service"; import { createLogger } from "../../../../lib/logger"; -import { createGraphQLClient } from "@saleor/apps-shared"; import { webhookProductDeleted } from "../../../../webhooks/definitions/product-deleted"; +import { createWebhookContext } from "../../../../webhooks/webhook-context"; export const config = { api: { @@ -31,52 +29,34 @@ export const handler: NextWebhookApiHandler = async (req, res, c return res.status(200).end(); } - const { settings, errors } = await getAlgoliaConfiguration({ authData }); - const client = createGraphQLClient({ - saleorApiUrl: authData.saleorApiUrl, - token: authData.token, - }); - const { data: channelsData } = await client.query(ChannelsDocument, {}).toPromise(); - const channels = channelsData?.channels || []; - - if (errors?.length || !settings) { - logger.warn("Aborting due to lack of settings"); - logger.debug(errors); - - const errorMessage = (errors && errors.length && errors[0].message) ?? "Unknown error"; - - return res.status(400).json({ - message: errorMessage, - }); - } + try { + const { algoliaClient, apiClient } = await createWebhookContext({ authData }); - const searchProvider = new AlgoliaSearchProvider({ - appId: settings.appId, - apiKey: settings.secretKey, - indexNamePrefix: settings.indexNamePrefix, - channels, - }); + try { + await algoliaClient.deleteProduct(product); - try { - await searchProvider.deleteProduct(product); - } catch (e) { - logger.info(e, "Algolia deleteProduct failed. Webhooks will be disabled"); + res.status(200).end(); + return; + } catch (e) { + logger.info(e, "Algolia deleteProduct failed. Webhooks will be disabled"); - const webhooksToggler = new WebhookActivityTogglerService( - authData.appId, - createGraphQLClient({ saleorApiUrl: authData.saleorApiUrl, token: authData.token }), - ); + const webhooksToggler = new WebhookActivityTogglerService(authData.appId, apiClient); - logger.trace("Will disable webhooks"); + logger.trace("Will disable webhooks"); - await webhooksToggler.disableOwnWebhooks(context.payload.recipient?.webhooks?.map((w) => w.id)); + await webhooksToggler.disableOwnWebhooks( + context.payload.recipient?.webhooks?.map((w) => w.id), + ); - logger.trace("Webhooks disabling operation finished"); + logger.trace("Webhooks disabling operation finished"); - return res.status(500).send("Operation failed, webhooks are disabled"); + return res.status(500).send("Operation failed, webhooks are disabled"); + } + } catch (e) { + return res.status(400).json({ + message: (e as Error).message, + }); } - res.status(200).end(); - return; }; export default webhookProductDeleted.createHandler(handler); diff --git a/apps/search/src/pages/api/webhooks/saleor/product_updated.ts b/apps/search/src/pages/api/webhooks/saleor/product_updated.ts index e0ae0412f..c4f723b6d 100644 --- a/apps/search/src/pages/api/webhooks/saleor/product_updated.ts +++ b/apps/search/src/pages/api/webhooks/saleor/product_updated.ts @@ -1,11 +1,9 @@ import { NextWebhookApiHandler } from "@saleor/app-sdk/handlers/next"; -import { ChannelsDocument, ProductUpdated } from "../../../../../generated/graphql"; -import { AlgoliaSearchProvider } from "../../../../lib/algolia/algoliaSearchProvider"; -import { getAlgoliaConfiguration } from "../../../../lib/algolia/getAlgoliaConfiguration"; +import { ProductUpdated } from "../../../../../generated/graphql"; import { WebhookActivityTogglerService } from "../../../../domain/WebhookActivityToggler.service"; import { createLogger } from "../../../../lib/logger"; -import { createGraphQLClient } from "@saleor/apps-shared"; import { webhookProductUpdated } from "../../../../webhooks/definitions/product-updated"; +import { createWebhookContext } from "../../../../webhooks/webhook-context"; export const config = { api: { @@ -31,53 +29,34 @@ export const handler: NextWebhookApiHandler = async (req, res, c return res.status(200).end(); } - const { settings, errors } = await getAlgoliaConfiguration({ authData }); - const client = createGraphQLClient({ - saleorApiUrl: authData.saleorApiUrl, - token: authData.token, - }); - const { data: channelsData } = await client.query(ChannelsDocument, {}).toPromise(); - const channels = channelsData?.channels || []; + try { + const { algoliaClient, apiClient } = await createWebhookContext({ authData }); - if (errors?.length || !settings) { - logger.warn("Aborting due to lack of settings"); - logger.debug(errors); + try { + await algoliaClient.updateProduct(product); - const errorMessage = (errors && errors.length && errors[0].message) ?? "Unknown error"; + res.status(200).end(); + return; + } catch (e) { + logger.info(e, "Algolia updateProduct failed. Webhooks will be disabled"); - return res.status(400).json({ - message: errorMessage, - }); - } + const webhooksToggler = new WebhookActivityTogglerService(authData.appId, apiClient); - const searchProvider = new AlgoliaSearchProvider({ - appId: settings.appId, - apiKey: settings.secretKey, - indexNamePrefix: settings.indexNamePrefix, - channels, - }); - - try { - await searchProvider.updateProduct(product); - } catch (e) { - logger.info(e, "Algolia updateProduct failed. Webhooks will be disabled"); + logger.trace("Will disable webhooks"); - const webhooksToggler = new WebhookActivityTogglerService( - authData.appId, - createGraphQLClient({ saleorApiUrl: authData.saleorApiUrl, token: authData.token }), - ); + await webhooksToggler.disableOwnWebhooks( + context.payload.recipient?.webhooks?.map((w) => w.id), + ); - logger.trace("Will disable webhooks"); + logger.trace("Webhooks disabling operation finished"); - await webhooksToggler.disableOwnWebhooks(context.payload.recipient?.webhooks?.map((w) => w.id)); - - logger.trace("Webhooks disabling operation finished"); - - return res.status(500).send("Operation failed, webhooks are disabled"); + return res.status(500).send("Operation failed, webhooks are disabled"); + } + } catch (e) { + return res.status(400).json({ + message: (e as Error).message, + }); } - - res.status(200).end(); - return; }; export default webhookProductUpdated.createHandler(handler); diff --git a/apps/search/src/pages/api/webhooks/saleor/product_variant_back_in_stock.ts b/apps/search/src/pages/api/webhooks/saleor/product_variant_back_in_stock.ts index ac697d186..2c0491e35 100644 --- a/apps/search/src/pages/api/webhooks/saleor/product_variant_back_in_stock.ts +++ b/apps/search/src/pages/api/webhooks/saleor/product_variant_back_in_stock.ts @@ -1,11 +1,9 @@ import { NextWebhookApiHandler } from "@saleor/app-sdk/handlers/next"; -import { createGraphQLClient } from "@saleor/apps-shared"; -import { ChannelsDocument, ProductVariantBackInStock } from "../../../../../generated/graphql"; +import { ProductVariantBackInStock } from "../../../../../generated/graphql"; import { WebhookActivityTogglerService } from "../../../../domain/WebhookActivityToggler.service"; -import { AlgoliaSearchProvider } from "../../../../lib/algolia/algoliaSearchProvider"; -import { getAlgoliaConfiguration } from "../../../../lib/algolia/getAlgoliaConfiguration"; import { createLogger } from "../../../../lib/logger"; import { webhookProductVariantBackInStock } from "../../../../webhooks/definitions/product-variant-back-in-stock"; +import { createWebhookContext } from "../../../../webhooks/webhook-context"; export const config = { api: { @@ -35,53 +33,34 @@ export const handler: NextWebhookApiHandler = async ( return res.status(200).end(); } - const { settings, errors } = await getAlgoliaConfiguration({ authData }); - const client = createGraphQLClient({ - saleorApiUrl: authData.saleorApiUrl, - token: authData.token, - }); - const { data: channelsData } = await client.query(ChannelsDocument, {}).toPromise(); - const channels = channelsData?.channels || []; + try { + const { algoliaClient, apiClient } = await createWebhookContext({ authData }); - if (errors?.length || !settings) { - logger.warn("Aborting due to lack of settings"); - logger.debug(errors); + try { + await algoliaClient.updateProductVariant(productVariant); - const errorMessage = (errors && errors.length && errors[0].message) ?? "Unknown error"; + res.status(200).end(); + return; + } catch (e) { + logger.info(e, "Algolia updateProductVariant failed. Webhooks will be disabled"); - return res.status(400).json({ - message: errorMessage, - }); - } + const webhooksToggler = new WebhookActivityTogglerService(authData.appId, apiClient); - const searchProvider = new AlgoliaSearchProvider({ - appId: settings.appId, - apiKey: settings.secretKey, - indexNamePrefix: settings.indexNamePrefix, - channels, - }); - - try { - await searchProvider.updateProductVariant(productVariant); - } catch (e) { - logger.info(e, "Algolia updateProductVariant failed. Webhooks will be disabled"); + logger.trace("Will disable webhooks"); - const webhooksToggler = new WebhookActivityTogglerService( - authData.appId, - createGraphQLClient({ saleorApiUrl: authData.saleorApiUrl, token: authData.token }), - ); + await webhooksToggler.disableOwnWebhooks( + context.payload.recipient?.webhooks?.map((w) => w.id), + ); - logger.trace("Will disable webhooks"); + logger.trace("Webhooks disabling operation finished"); - await webhooksToggler.disableOwnWebhooks(context.payload.recipient?.webhooks?.map((w) => w.id)); - - logger.trace("Webhooks disabling operation finished"); - - return res.status(500).send("Operation failed, webhooks are disabled"); + return res.status(500).send("Operation failed, webhooks are disabled"); + } + } catch (e) { + return res.status(400).json({ + message: (e as Error).message, + }); } - - res.status(200).end(); - return; }; export default webhookProductVariantBackInStock.createHandler(handler); diff --git a/apps/search/src/pages/api/webhooks/saleor/product_variant_created.ts b/apps/search/src/pages/api/webhooks/saleor/product_variant_created.ts index 588f3bc65..ec2414dab 100644 --- a/apps/search/src/pages/api/webhooks/saleor/product_variant_created.ts +++ b/apps/search/src/pages/api/webhooks/saleor/product_variant_created.ts @@ -1,11 +1,9 @@ import { NextWebhookApiHandler } from "@saleor/app-sdk/handlers/next"; -import { ChannelsDocument, ProductVariantCreated } from "../../../../../generated/graphql"; -import { AlgoliaSearchProvider } from "../../../../lib/algolia/algoliaSearchProvider"; -import { getAlgoliaConfiguration } from "../../../../lib/algolia/getAlgoliaConfiguration"; -import { createLogger } from "../../../../lib/logger"; +import { ProductVariantCreated } from "../../../../../generated/graphql"; import { WebhookActivityTogglerService } from "../../../../domain/WebhookActivityToggler.service"; -import { createGraphQLClient } from "@saleor/apps-shared"; +import { createLogger } from "../../../../lib/logger"; import { webhookProductVariantCreated } from "../../../../webhooks/definitions/product-variant-created"; +import { createWebhookContext } from "../../../../webhooks/webhook-context"; export const config = { api: { @@ -31,53 +29,34 @@ export const handler: NextWebhookApiHandler = async (req, return res.status(200).end(); } - const { settings, errors } = await getAlgoliaConfiguration({ authData }); - const client = createGraphQLClient({ - saleorApiUrl: authData.saleorApiUrl, - token: authData.token, - }); - const { data: channelsData } = await client.query(ChannelsDocument, {}).toPromise(); - const channels = channelsData?.channels || []; - - if (errors?.length || !settings) { - logger.warn("Aborting due to lack of settings"); - logger.debug(errors); - - const errorMessage = (errors && errors.length && errors[0].message) ?? "Unknown error"; - - return res.status(400).json({ - message: errorMessage, - }); - } + try { + const { algoliaClient, apiClient } = await createWebhookContext({ authData }); - const searchProvider = new AlgoliaSearchProvider({ - appId: settings.appId, - apiKey: settings.secretKey, - indexNamePrefix: settings.indexNamePrefix, - channels, - }); + try { + await algoliaClient.createProductVariant(productVariant); - try { - await searchProvider.createProductVariant(productVariant); - } catch (e) { - logger.info(e, "Algolia createProductVariant failed. Webhooks will be disabled"); + res.status(200).end(); + return; + } catch (e) { + logger.info(e, "Algolia createProductVariant failed. Webhooks will be disabled"); - const webhooksToggler = new WebhookActivityTogglerService( - authData.appId, - createGraphQLClient({ saleorApiUrl: authData.saleorApiUrl, token: authData.token }), - ); + const webhooksToggler = new WebhookActivityTogglerService(authData.appId, apiClient); - logger.trace("Will disable webhooks"); + logger.trace("Will disable webhooks"); - await webhooksToggler.disableOwnWebhooks(context.payload.recipient?.webhooks?.map((w) => w.id)); + await webhooksToggler.disableOwnWebhooks( + context.payload.recipient?.webhooks?.map((w) => w.id), + ); - logger.trace("Webhooks disabling operation finished"); + logger.trace("Webhooks disabling operation finished"); - return res.status(500).send("Operation failed, webhooks are disabled"); + return res.status(500).send("Operation failed, webhooks are disabled"); + } + } catch (e) { + return res.status(400).json({ + message: (e as Error).message, + }); } - - res.status(200).end(); - return; }; export default webhookProductVariantCreated.createHandler(handler); diff --git a/apps/search/src/pages/api/webhooks/saleor/product_variant_deleted.ts b/apps/search/src/pages/api/webhooks/saleor/product_variant_deleted.ts index 92c90dd3d..3c09f6f9e 100644 --- a/apps/search/src/pages/api/webhooks/saleor/product_variant_deleted.ts +++ b/apps/search/src/pages/api/webhooks/saleor/product_variant_deleted.ts @@ -1,11 +1,9 @@ import { NextWebhookApiHandler } from "@saleor/app-sdk/handlers/next"; -import { ChannelsDocument, ProductVariantDeleted } from "../../../../../generated/graphql"; -import { AlgoliaSearchProvider } from "../../../../lib/algolia/algoliaSearchProvider"; -import { getAlgoliaConfiguration } from "../../../../lib/algolia/getAlgoliaConfiguration"; -import { createLogger } from "../../../../lib/logger"; +import { ProductVariantDeleted } from "../../../../../generated/graphql"; import { WebhookActivityTogglerService } from "../../../../domain/WebhookActivityToggler.service"; -import { createGraphQLClient } from "@saleor/apps-shared"; +import { createLogger } from "../../../../lib/logger"; import { webhookProductVariantDeleted } from "../../../../webhooks/definitions/product-variant-deleted"; +import { createWebhookContext } from "../../../../webhooks/webhook-context"; export const config = { api: { @@ -31,53 +29,34 @@ export const handler: NextWebhookApiHandler = async (req, return res.status(200).end(); } - const { settings, errors } = await getAlgoliaConfiguration({ authData }); - const client = createGraphQLClient({ - saleorApiUrl: authData.saleorApiUrl, - token: authData.token, - }); - const { data: channelsData } = await client.query(ChannelsDocument, {}).toPromise(); - const channels = channelsData?.channels || []; - - if (errors?.length || !settings) { - logger.warn("Aborting due to lack of settings"); - logger.debug(errors); - - const errorMessage = (errors && errors.length && errors[0].message) ?? "Unknown error"; - - return res.status(400).json({ - message: errorMessage, - }); - } + try { + const { algoliaClient, apiClient } = await createWebhookContext({ authData }); - const searchProvider = new AlgoliaSearchProvider({ - appId: settings.appId, - apiKey: settings.secretKey, - indexNamePrefix: settings.indexNamePrefix, - channels, - }); + try { + await algoliaClient.deleteProductVariant(productVariant); - try { - await searchProvider.deleteProductVariant(productVariant); - } catch (e) { - logger.info(e, "Algolia deleteProductVariant failed. Webhooks will be disabled"); + res.status(200).end(); + return; + } catch (e) { + logger.info(e, "Algolia deleteProductVariant failed. Webhooks will be disabled"); - const webhooksToggler = new WebhookActivityTogglerService( - authData.appId, - createGraphQLClient({ saleorApiUrl: authData.saleorApiUrl, token: authData.token }), - ); + const webhooksToggler = new WebhookActivityTogglerService(authData.appId, apiClient); - logger.trace("Will disable webhooks"); + logger.trace("Will disable webhooks"); - await webhooksToggler.disableOwnWebhooks(context.payload.recipient?.webhooks?.map((w) => w.id)); + await webhooksToggler.disableOwnWebhooks( + context.payload.recipient?.webhooks?.map((w) => w.id), + ); - logger.trace("Webhooks disabling operation finished"); + logger.trace("Webhooks disabling operation finished"); - return res.status(500).send("Operation failed, webhooks are disabled"); + return res.status(500).send("Operation failed, webhooks are disabled"); + } + } catch (e) { + return res.status(400).json({ + message: (e as Error).message, + }); } - - res.status(200).end(); - return; }; export default webhookProductVariantDeleted.createHandler(handler); diff --git a/apps/search/src/pages/api/webhooks/saleor/product_variant_out_of_stock.ts b/apps/search/src/pages/api/webhooks/saleor/product_variant_out_of_stock.ts index 747891163..8115200eb 100644 --- a/apps/search/src/pages/api/webhooks/saleor/product_variant_out_of_stock.ts +++ b/apps/search/src/pages/api/webhooks/saleor/product_variant_out_of_stock.ts @@ -1,11 +1,9 @@ import { NextWebhookApiHandler } from "@saleor/app-sdk/handlers/next"; -import { createGraphQLClient } from "@saleor/apps-shared"; -import { ChannelsDocument, ProductVariantOutOfStock } from "../../../../../generated/graphql"; +import { ProductVariantOutOfStock } from "../../../../../generated/graphql"; import { WebhookActivityTogglerService } from "../../../../domain/WebhookActivityToggler.service"; -import { AlgoliaSearchProvider } from "../../../../lib/algolia/algoliaSearchProvider"; -import { getAlgoliaConfiguration } from "../../../../lib/algolia/getAlgoliaConfiguration"; import { createLogger } from "../../../../lib/logger"; import { webhookProductVariantOutOfStock } from "../../../../webhooks/definitions/product-variant-out-of-stock"; +import { createWebhookContext } from "../../../../webhooks/webhook-context"; export const config = { api: { @@ -35,53 +33,34 @@ export const handler: NextWebhookApiHandler = async ( return res.status(200).end(); } - const { settings, errors } = await getAlgoliaConfiguration({ authData }); - const client = createGraphQLClient({ - saleorApiUrl: authData.saleorApiUrl, - token: authData.token, - }); - const { data: channelsData } = await client.query(ChannelsDocument, {}).toPromise(); - const channels = channelsData?.channels || []; + try { + const { algoliaClient, apiClient } = await createWebhookContext({ authData }); - if (errors?.length || !settings) { - logger.warn("Aborting due to lack of settings"); - logger.debug(errors); + try { + await algoliaClient.updateProductVariant(productVariant); - const errorMessage = (errors && errors.length && errors[0].message) ?? "Unknown error"; + res.status(200).end(); + return; + } catch (e) { + logger.info(e, "Algolia updateProductVariant failed. Webhooks will be disabled"); - return res.status(400).json({ - message: errorMessage, - }); - } + const webhooksToggler = new WebhookActivityTogglerService(authData.appId, apiClient); - const searchProvider = new AlgoliaSearchProvider({ - appId: settings.appId, - apiKey: settings.secretKey, - indexNamePrefix: settings.indexNamePrefix, - channels, - }); - - try { - await searchProvider.updateProductVariant(productVariant); - } catch (e) { - logger.info(e, "Algolia updateProductVariant failed. Webhooks will be disabled"); + logger.trace("Will disable webhooks"); - const webhooksToggler = new WebhookActivityTogglerService( - authData.appId, - createGraphQLClient({ saleorApiUrl: authData.saleorApiUrl, token: authData.token }), - ); + await webhooksToggler.disableOwnWebhooks( + context.payload.recipient?.webhooks?.map((w) => w.id), + ); - logger.trace("Will disable webhooks"); + logger.trace("Webhooks disabling operation finished"); - await webhooksToggler.disableOwnWebhooks(context.payload.recipient?.webhooks?.map((w) => w.id)); - - logger.trace("Webhooks disabling operation finished"); - - return res.status(500).send("Operation failed, webhooks are disabled"); + return res.status(500).send("Operation failed, webhooks are disabled"); + } + } catch (e) { + return res.status(400).json({ + message: (e as Error).message, + }); } - - res.status(200).end(); - return; }; export default webhookProductVariantOutOfStock.createHandler(handler); diff --git a/apps/search/src/pages/api/webhooks/saleor/product_variant_updated.ts b/apps/search/src/pages/api/webhooks/saleor/product_variant_updated.ts index 6e488ab09..be6d781c4 100644 --- a/apps/search/src/pages/api/webhooks/saleor/product_variant_updated.ts +++ b/apps/search/src/pages/api/webhooks/saleor/product_variant_updated.ts @@ -1,11 +1,9 @@ import { NextWebhookApiHandler } from "@saleor/app-sdk/handlers/next"; -import { createGraphQLClient } from "@saleor/apps-shared"; -import { ChannelsDocument, ProductVariantUpdated } from "../../../../../generated/graphql"; +import { ProductVariantUpdated } from "../../../../../generated/graphql"; import { WebhookActivityTogglerService } from "../../../../domain/WebhookActivityToggler.service"; -import { AlgoliaSearchProvider } from "../../../../lib/algolia/algoliaSearchProvider"; -import { getAlgoliaConfiguration } from "../../../../lib/algolia/getAlgoliaConfiguration"; import { createLogger } from "../../../../lib/logger"; import { webhookProductVariantUpdated } from "../../../../webhooks/definitions/product-variant-updated"; +import { createWebhookContext } from "../../../../webhooks/webhook-context"; export const config = { api: { @@ -31,54 +29,34 @@ export const handler: NextWebhookApiHandler = async (req, return res.status(200).end(); } - const { settings, errors } = await getAlgoliaConfiguration({ authData }); - const client = createGraphQLClient({ - saleorApiUrl: authData.saleorApiUrl, - token: authData.token, - }); - const { data: channelsData } = await client.query(ChannelsDocument, {}).toPromise(); - const channels = channelsData?.channels || []; + try { + const { algoliaClient, apiClient } = await createWebhookContext({ authData }); - if (errors?.length || !settings) { - logger.warn("Aborting due to lack of settings"); - logger.debug(errors); + try { + await algoliaClient.updateProductVariant(productVariant); - const errorMessage = (errors && errors.length && errors[0].message) ?? "Unknown error"; + res.status(200).end(); + return; + } catch (e) { + logger.info(e, "Algolia updateProductVariant failed. Webhooks will be disabled"); - return res.status(400).json({ - message: errorMessage, - }); - } + const webhooksToggler = new WebhookActivityTogglerService(authData.appId, apiClient); - const searchProvider = new AlgoliaSearchProvider({ - appId: settings.appId, - apiKey: settings.secretKey, - indexNamePrefix: settings.indexNamePrefix, - channels, - }); - - try { - logger.debug("Updating variant"); - await searchProvider.updateProductVariant(productVariant); - } catch (e) { - logger.info(e, "Algolia updateProductVariant failed. Webhooks will be disabled"); + logger.trace("Will disable webhooks"); - const webhooksToggler = new WebhookActivityTogglerService( - authData.appId, - createGraphQLClient({ saleorApiUrl: authData.saleorApiUrl, token: authData.token }), - ); + await webhooksToggler.disableOwnWebhooks( + context.payload.recipient?.webhooks?.map((w) => w.id), + ); - logger.trace("Will disable webhooks"); + logger.trace("Webhooks disabling operation finished"); - await webhooksToggler.disableOwnWebhooks(context.payload.recipient?.webhooks?.map((w) => w.id)); - - logger.trace("Webhooks disabling operation finished"); - - return res.status(500).send("Operation failed, webhooks are disabled"); + return res.status(500).send("Operation failed, webhooks are disabled"); + } + } catch (e) { + return res.status(400).json({ + message: (e as Error).message, + }); } - - res.status(200).end(); - return; }; export default webhookProductVariantUpdated.createHandler(handler);