Skip to content

Commit

Permalink
Apply webhook context to all webhook handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
lkostrowski committed Aug 30, 2023
1 parent cbc34d1 commit 6813ccc
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 304 deletions.
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
64 changes: 22 additions & 42 deletions apps/search/src/pages/api/webhooks/saleor/product_deleted.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -31,52 +29,34 @@ export const handler: NextWebhookApiHandler<ProductDeleted> = 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);
65 changes: 22 additions & 43 deletions apps/search/src/pages/api/webhooks/saleor/product_updated.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -31,53 +29,34 @@ export const handler: NextWebhookApiHandler<ProductUpdated> = 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);
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -35,53 +33,34 @@ export const handler: NextWebhookApiHandler<ProductVariantBackInStock> = 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);
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -31,53 +29,34 @@ export const handler: NextWebhookApiHandler<ProductVariantCreated> = 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);
Loading

0 comments on commit 6813ccc

Please sign in to comment.