2023-03-07 10:31:44 +00:00
|
|
|
import { SaleorSyncWebhook } from "@saleor/app-sdk/handlers/next";
|
2023-04-17 11:58:21 +00:00
|
|
|
import {
|
|
|
|
CalculateTaxesEventFragment,
|
|
|
|
UntypedCalculateTaxesDocument,
|
|
|
|
} from "../../../../generated/graphql";
|
2023-03-02 11:01:17 +00:00
|
|
|
import { saleorApp } from "../../../../saleor-app";
|
|
|
|
import { createLogger } from "../../../lib/logger";
|
2023-04-17 11:58:21 +00:00
|
|
|
import { WebhookResponse } from "../../../modules/app/webhook-response";
|
|
|
|
import { getActiveTaxProvider } from "../../../modules/taxes/active-tax-provider";
|
2023-03-02 11:01:17 +00:00
|
|
|
|
2023-03-07 10:31:44 +00:00
|
|
|
export const config = {
|
|
|
|
api: {
|
|
|
|
bodyParser: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-04-17 11:58:21 +00:00
|
|
|
type CalculateTaxesPayload = Extract<CalculateTaxesEventFragment, { __typename: "CalculateTaxes" }>;
|
|
|
|
|
|
|
|
function verifyCalculateTaxesPayload(payload: CalculateTaxesPayload) {
|
|
|
|
if (!payload.taxBase.lines) {
|
|
|
|
throw new Error("No lines found in taxBase");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!payload.taxBase.address) {
|
|
|
|
throw new Error("No address found in taxBase");
|
|
|
|
}
|
|
|
|
|
|
|
|
return payload;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const checkoutCalculateTaxesSyncWebhook = new SaleorSyncWebhook<CalculateTaxesPayload>({
|
2023-03-02 11:01:17 +00:00
|
|
|
name: "CheckoutCalculateTaxes",
|
|
|
|
apl: saleorApp.apl,
|
2023-03-07 10:31:44 +00:00
|
|
|
event: "CHECKOUT_CALCULATE_TAXES",
|
|
|
|
query: UntypedCalculateTaxesDocument,
|
2023-03-02 11:01:17 +00:00
|
|
|
webhookPath: "/api/webhooks/checkout-calculate-taxes",
|
|
|
|
});
|
|
|
|
|
2023-03-07 10:31:44 +00:00
|
|
|
export default checkoutCalculateTaxesSyncWebhook.createHandler(async (req, res, ctx) => {
|
|
|
|
const logger = createLogger({ event: ctx.event });
|
2023-03-30 11:12:52 +00:00
|
|
|
const { payload } = ctx;
|
2023-04-17 11:58:21 +00:00
|
|
|
const webhookResponse = new WebhookResponse(res);
|
2023-03-02 11:01:17 +00:00
|
|
|
|
2023-04-17 11:58:21 +00:00
|
|
|
logger.info({ payload }, "Handler called with payload");
|
2023-03-07 10:31:44 +00:00
|
|
|
|
2023-04-17 11:58:21 +00:00
|
|
|
try {
|
|
|
|
verifyCalculateTaxesPayload(payload);
|
|
|
|
logger.info("Payload validated succesfully");
|
|
|
|
} catch (error) {
|
2023-03-07 10:31:44 +00:00
|
|
|
logger.info("Returning no data");
|
2023-04-17 11:58:21 +00:00
|
|
|
return webhookResponse.failureNoRetry("Payload is invalid");
|
2023-03-07 10:31:44 +00:00
|
|
|
}
|
|
|
|
|
2023-03-30 11:12:52 +00:00
|
|
|
try {
|
2023-04-17 11:58:21 +00:00
|
|
|
const appMetadata = payload.recipient?.privateMetadata ?? [];
|
|
|
|
const channelSlug = payload.taxBase.channel.slug;
|
|
|
|
const activeTaxProvider = getActiveTaxProvider(channelSlug, appMetadata);
|
2023-03-07 10:31:44 +00:00
|
|
|
|
2023-04-17 11:58:21 +00:00
|
|
|
if (!activeTaxProvider.ok) {
|
2023-03-07 10:31:44 +00:00
|
|
|
logger.info("Returning no data");
|
2023-04-17 11:58:21 +00:00
|
|
|
return webhookResponse.failureNoRetry(activeTaxProvider.error);
|
2023-03-07 10:31:44 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 11:58:21 +00:00
|
|
|
logger.info({ activeTaxProvider }, "Fetched activeTaxProvider");
|
|
|
|
const taxProvider = activeTaxProvider.data;
|
|
|
|
const calculatedTaxes = await taxProvider.calculateTaxes(payload.taxBase);
|
2023-03-07 10:31:44 +00:00
|
|
|
|
|
|
|
logger.info({ calculatedTaxes }, "Taxes calculated");
|
2023-04-17 11:58:21 +00:00
|
|
|
return webhookResponse.success(ctx.buildResponse(calculatedTaxes));
|
2023-03-07 10:31:44 +00:00
|
|
|
} catch (error) {
|
2023-04-17 11:58:21 +00:00
|
|
|
return webhookResponse.failureRetry("Error while calculating taxes");
|
2023-03-07 10:31:44 +00:00
|
|
|
}
|
|
|
|
});
|