Skip to content

Commit

Permalink
refactor: ♻️ extract AvataxAddressResolver from order-confirmed
Browse files Browse the repository at this point in the history
  • Loading branch information
peelar committed Aug 30, 2023
1 parent 5f82d27 commit 2f8051b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AvataxCalculateTaxesPayloadLinesTransformer } from "./avatax-calculate-
import { AvataxEntityTypeMatcher } from "../avatax-entity-type-matcher";
import { taxProviderUtils } from "../../taxes/tax-provider-utils";
import { CalculateTaxesPayload } from "../../../pages/api/webhooks/checkout-calculate-taxes";
import { AvataxAddressResolver } from "../order-confirmed/avatax-address-resolver";

export class AvataxCalculateTaxesPayloadTransformer {
private matchDocumentType(config: AvataxConfig): DocumentType {
Expand Down Expand Up @@ -47,6 +48,11 @@ export class AvataxCalculateTaxesPayloadTransformer {
);

const customerCode = this.resolveCustomerCode(payload);
const addressResolver = new AvataxAddressResolver();
const addresses = addressResolver.resolve({
from: avataxConfig.address,
to: payload.taxBase.address!,
});

return {
model: {
Expand All @@ -56,10 +62,7 @@ export class AvataxCalculateTaxesPayloadTransformer {
companyCode: avataxConfig.companyCode ?? defaultAvataxConfig.companyCode,
// * commit: If true, the transaction will be committed immediately after it is created. See: https://developer.avalara.com/communications/dev-guide_rest_v2/commit-uncommit
commit: avataxConfig.isAutocommit,
addresses: {
shipFrom: avataxAddressFactory.fromChannelAddress(avataxConfig.address),
shipTo: avataxAddressFactory.fromSaleorAddress(payload.taxBase.address!),
},
addresses,
currencyCode: payload.taxBase.currency,
lines: payloadLinesTransformer.transform(payload.taxBase, avataxConfig, matches),
date: new Date(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { AddressFragment } from "../../../../generated/graphql";
import { avataxAddressFactory } from "../address-factory";
import { AvataxConfig } from "../avatax-connection-schema";
import { CreateTransactionModel } from "avatax/lib/models/CreateTransactionModel";

export class AvataxAddressResolver {
resolve({
from,
to,
}: {
from: AvataxConfig["address"];
to: AddressFragment;
}): CreateTransactionModel["addresses"] {
return {
shipFrom: avataxAddressFactory.fromChannelAddress(from),
shipTo: avataxAddressFactory.fromSaleorAddress(to),
};
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { DocumentType } from "avatax/lib/enums/DocumentType";
import { OrderConfirmedSubscriptionFragment } from "../../../../generated/graphql";
import { discountUtils } from "../../taxes/discount-utils";
import { avataxAddressFactory } from "../address-factory";
import { AvataxClient, CreateTransactionArgs } from "../avatax-client";
import { AvataxConfig, defaultAvataxConfig } from "../avatax-connection-schema";
import { AvataxTaxCodeMatches } from "../tax-code/avatax-tax-code-match-repository";
Expand All @@ -10,6 +9,7 @@ import { AvataxEntityTypeMatcher } from "../avatax-entity-type-matcher";
import { AvataxDocumentCodeResolver } from "../avatax-document-code-resolver";
import { AvataxCalculationDateResolver } from "../avatax-calculation-date-resolver";
import { taxProviderUtils } from "../../taxes/tax-provider-utils";
import { AvataxAddressResolver } from "./avatax-address-resolver";

export const SHIPPING_ITEM_CODE = "Shipping";

Expand Down Expand Up @@ -41,6 +41,12 @@ export class AvataxOrderConfirmedPayloadTransformer {
orderId: order.id,
});

const addressResolver = new AvataxAddressResolver();
const addresses = addressResolver.resolve({
from: avataxConfig.address,
to: order.shippingAddress!,
});

return {
model: {
code,
Expand All @@ -50,11 +56,7 @@ export class AvataxOrderConfirmedPayloadTransformer {
companyCode: avataxConfig.companyCode ?? defaultAvataxConfig.companyCode,
// * commit: If true, the transaction will be committed immediately after it is created. See: https://developer.avalara.com/communications/dev-guide_rest_v2/commit-uncommit
commit: avataxConfig.isAutocommit,
addresses: {
shipFrom: avataxAddressFactory.fromChannelAddress(avataxConfig.address),
// billing or shipping address?
shipTo: avataxAddressFactory.fromSaleorAddress(order.billingAddress!),
},
addresses,
currencyCode: order.total.currency,
email: taxProviderUtils.resolveStringOrThrow(order.user?.email),
lines: linesTransformer.transform(order, avataxConfig, matches),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AvataxTaxCodeMatches } from "../tax-code/avatax-tax-code-match-reposito
import { AvataxOrderConfirmedTaxCodeMatcher } from "./avatax-order-confirmed-tax-code-matcher";

const mockedLine: OrderLineFragment = {
id: "T3JkZXJMaW5lOjE=",
productSku: "sku",
productName: "Test product",
quantity: 1,
Expand Down

0 comments on commit 2f8051b

Please sign in to comment.