test: ✅ add tests for AvataxOrderCancelledPayloadTransformer
This commit is contained in:
parent
d458452573
commit
ac9acdf0c6
4 changed files with 38 additions and 8 deletions
|
@ -0,0 +1,30 @@
|
|||
import { OrderCancelledPayload } from "../../../pages/api/webhooks/order-cancelled";
|
||||
import { AvataxConfigMockGenerator } from "../avatax-config-mock-generator";
|
||||
import { AvataxOrderCancelledPayloadTransformer } from "./avatax-order-cancelled-payload-transformer";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
const configMockGenerator = new AvataxConfigMockGenerator();
|
||||
const avataxMockConfig = configMockGenerator.generateAvataxConfig();
|
||||
|
||||
describe("AvataxOrderCancelledPayloadTransformer", () => {
|
||||
it("throws an error when order = null", () => {
|
||||
const payload = { order: null } as any as OrderCancelledPayload;
|
||||
const transformer = new AvataxOrderCancelledPayloadTransformer(avataxMockConfig);
|
||||
|
||||
expect(() => transformer.transform(payload)).toThrow("Order is required");
|
||||
});
|
||||
it("throws an error when no externalId is present", () => {
|
||||
const payload = { order: {} } as any as OrderCancelledPayload;
|
||||
const transformer = new AvataxOrderCancelledPayloadTransformer(avataxMockConfig);
|
||||
|
||||
expect(() => transformer.transform(payload)).toThrow();
|
||||
});
|
||||
it("returns a valid AvataxOrderCancelledTarget", () => {
|
||||
const payload = { order: { externalId: "123" } } as any as OrderCancelledPayload;
|
||||
const transformer = new AvataxOrderCancelledPayloadTransformer(avataxMockConfig);
|
||||
|
||||
const target = transformer.transform(payload);
|
||||
|
||||
expect(target).toEqual({ transactionCode: "123", companyCode: "DEFAULT" });
|
||||
});
|
||||
});
|
|
@ -15,7 +15,7 @@ export class AvataxOrderCancelledPayloadTransformer {
|
|||
|
||||
return {
|
||||
transactionCode,
|
||||
companyCode: this.config.companyCode ?? "",
|
||||
companyCode: this.config.companyCode ?? "", // If companyCode is not defined, we set the value to empty string so that the default company is used.
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,7 +48,6 @@ export class TaxJarWebhookService implements ProviderWebhookService {
|
|||
}
|
||||
|
||||
async cancelOrder(payload: OrderCancelledEventSubscriptionFragment) {
|
||||
// todo: implement
|
||||
this.logger.debug("cancelOrder", payload);
|
||||
// TaxJar isn't implemented yet
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,22 +33,23 @@ export default orderCancelledAsyncWebhook.createHandler(async (req, res, ctx) =>
|
|||
|
||||
logger.info("Handler called with payload");
|
||||
|
||||
if (!payload.order) {
|
||||
return webhookResponse.error(new Error("Insufficient order data"));
|
||||
}
|
||||
|
||||
try {
|
||||
const appMetadata = payload.recipient?.privateMetadata ?? [];
|
||||
const channelSlug = payload.order?.channel.slug;
|
||||
const channelSlug = payload.order.channel.slug;
|
||||
const taxProvider = getActiveConnectionService(channelSlug, appMetadata, ctx.authData);
|
||||
|
||||
logger.info("Fetched taxProvider");
|
||||
|
||||
if (!payload.order) {
|
||||
return webhookResponse.error(new Error("Insufficient order data"));
|
||||
}
|
||||
await taxProvider.cancelOrder(payload);
|
||||
|
||||
logger.info("Order cancelled");
|
||||
|
||||
return webhookResponse.success();
|
||||
} catch (error) {
|
||||
return webhookResponse.error(new Error("Error while fulfilling tax provider order"));
|
||||
return webhookResponse.error(new Error("Error while cancelling tax provider order"));
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue