saleor-apps-redis_apl/apps/taxes/src/modules/avatax/avatax-client.ts
Adrian Pilarczyk 09e07995b5
feat: tax code matcher (#564)
* feat:  add tax-classes-fetcher

* refactor: ♻️ add "byId" suffix to crud-settings & remove upsert

* feat:  add updateMany method to CrudSettingsManager

* feat:  add avatax & taxjar tax-code-map-repository

* refactor: 🚚 move to tax-code directory

* feat:  add getTaxCodes methods to provider clients

* refactor: ♻️ extract taxClass and taxCode schema

* refactor: 🚚 tax-code-map -> tax-code-match

* feat:  add taxjar-tax-code.service

* feat:  add avatax-tax-code.service

* feat:  add taxClass to graphql fragment

* feat:  use tax codes in calculate taxes

* fix: 🐛 undefined tax code bug & add tests

* build: 👷 add changeset

* Update avatax-tax-code-mapper.ts

* feat:  add routers & get rid of adapters & mappers

* refactor: ♻️ logger location -> name

* refactor: ♻️ clean up & rephrase logs

* refactor: ♻️ remove __typename from query

* docs: 💡 make comments about tax codes more informative

* refactor: ♻️ use resolveOptionalOrThrow on tax code description

* refactor: ♻️ rename tax-codes -> tax-classes, move and rename tax-code-schema

* refactor: 🚚 ctx -> authData

* refactor: 🚚 createUrqlClientFromCtx -> createUrqlClientFromAuthdata

* refactor: ♻️ dont spread ctx

* docs: 💡 add comment about fallback tax code

* refactor: ♻️ remove ..ctx

* fix: 🐛 use createGraphQLClient

* feat: tax code matcher ui (#658)

* feat:  use tax codes in calculate taxes

* feat:  add getTaxCodes methods to provider clients

* feat:  add matcher tables

* feat:  add log errors middleware

* fix: 🔊 fix misleading logs

* fix: 🐛 ctx appToken bug

* feat:  add Select override with wrapping label

* feat:  pre-select match

* docs: 💡 add comments about first connection

* docs: 💡 add comment about redirect

* refactor: 🔥 duplicate file

* feat:  add AppCard to tables

* feat:  add _error to breadcrumbs exceptions

* fix: 🐛 value not set on data

* feat: 🥅 add error for no channels configured

* fix: 🐛 replace update with upsert

* refactor: 🚚 channel-configuration-settings to repository

* fix: 🐛 updating a channel configuration

* fix: 🧪 fix wrong mock

* fix: 🐛 duplicating configs

* Update cool-turtles-reflect.md
2023-06-20 17:53:27 +02:00

85 lines
2.3 KiB
TypeScript

import Avatax from "avatax";
import { CreateTransactionModel } from "avatax/lib/models/CreateTransactionModel";
import packageJson from "../../../package.json";
import { createLogger, Logger } from "../../lib/logger";
import { AvataxConfig } from "./avatax-connection-schema";
import { CommitTransactionModel } from "avatax/lib/models/CommitTransactionModel";
import { DocumentType } from "avatax/lib/enums/DocumentType";
import { AddressLocationInfo as AvataxAddress } from "avatax/lib/models/AddressLocationInfo";
import { AvataxClientTaxCodeService } from "./avatax-client-tax-code.service";
type AvataxSettings = {
appName: string;
appVersion: string;
environment: "sandbox" | "production";
machineName: string;
timeout: number;
logOptions?: {
logEnabled: boolean;
logLevel: number;
logRequestAndResponseInfo: boolean;
};
};
const defaultAvataxSettings: AvataxSettings = {
appName: packageJson.name,
appVersion: packageJson.version,
environment: "sandbox",
machineName: "tax-app",
timeout: 5000,
};
const createAvataxSettings = (config: AvataxConfig): AvataxSettings => {
const settings: AvataxSettings = {
...defaultAvataxSettings,
environment: config.isSandbox ? "sandbox" : "production",
};
return settings;
};
export type CommitTransactionArgs = {
companyCode: string;
transactionCode: string;
model: CommitTransactionModel;
documentType: DocumentType;
};
export type CreateTransactionArgs = {
model: CreateTransactionModel;
};
export type ValidateAddressArgs = {
address: AvataxAddress;
};
export class AvataxClient {
private client: Avatax;
private logger: Logger;
constructor(config: AvataxConfig) {
this.logger = createLogger({ name: "AvataxClient" });
const settings = createAvataxSettings(config);
const avataxClient = new Avatax(settings).withSecurity(config.credentials);
this.client = avataxClient;
}
async createTransaction({ model }: CreateTransactionArgs) {
return this.client.createTransaction({ model });
}
async commitTransaction(args: CommitTransactionArgs) {
return this.client.commitTransaction(args);
}
async validateAddress({ address }: ValidateAddressArgs) {
return this.client.resolveAddress(address);
}
async getTaxCodes() {
const taxCodeService = new AvataxClientTaxCodeService(this.client);
return taxCodeService.getTaxCodes();
}
}