2023-03-02 11:01:17 +00:00
|
|
|
import Avatax from "avatax";
|
|
|
|
import { CreateTransactionModel } from "avatax/lib/models/CreateTransactionModel";
|
2023-03-10 12:04:25 +00:00
|
|
|
import packageJson from "../../../package.json";
|
2023-05-05 06:15:47 +00:00
|
|
|
import { createLogger, Logger } from "../../lib/logger";
|
2023-06-15 07:01:50 +00:00
|
|
|
import { AvataxConfig } from "./avatax-connection-schema";
|
2023-04-17 11:58:21 +00:00
|
|
|
import { CommitTransactionModel } from "avatax/lib/models/CommitTransactionModel";
|
|
|
|
import { DocumentType } from "avatax/lib/enums/DocumentType";
|
2023-04-21 05:55:43 +00:00
|
|
|
import { AddressLocationInfo as AvataxAddress } from "avatax/lib/models/AddressLocationInfo";
|
2023-06-20 15:53:27 +00:00
|
|
|
import { AvataxClientTaxCodeService } from "./avatax-client-tax-code.service";
|
2023-03-02 11:01:17 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2023-04-17 11:58:21 +00:00
|
|
|
export type CommitTransactionArgs = {
|
|
|
|
companyCode: string;
|
|
|
|
transactionCode: string;
|
|
|
|
model: CommitTransactionModel;
|
|
|
|
documentType: DocumentType;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type CreateTransactionArgs = {
|
|
|
|
model: CreateTransactionModel;
|
|
|
|
};
|
|
|
|
|
2023-04-21 05:55:43 +00:00
|
|
|
export type ValidateAddressArgs = {
|
|
|
|
address: AvataxAddress;
|
|
|
|
};
|
|
|
|
|
2023-03-02 11:01:17 +00:00
|
|
|
export class AvataxClient {
|
|
|
|
private client: Avatax;
|
2023-05-05 06:15:47 +00:00
|
|
|
private logger: Logger;
|
2023-03-02 11:01:17 +00:00
|
|
|
|
|
|
|
constructor(config: AvataxConfig) {
|
2023-06-20 15:53:27 +00:00
|
|
|
this.logger = createLogger({ name: "AvataxClient" });
|
2023-03-02 11:01:17 +00:00
|
|
|
const settings = createAvataxSettings(config);
|
2023-06-15 07:01:50 +00:00
|
|
|
const avataxClient = new Avatax(settings).withSecurity(config.credentials);
|
2023-04-17 11:58:21 +00:00
|
|
|
|
2023-03-02 11:01:17 +00:00
|
|
|
this.client = avataxClient;
|
|
|
|
}
|
|
|
|
|
2023-04-17 11:58:21 +00:00
|
|
|
async createTransaction({ model }: CreateTransactionArgs) {
|
2023-03-02 11:01:17 +00:00
|
|
|
return this.client.createTransaction({ model });
|
|
|
|
}
|
|
|
|
|
2023-04-17 11:58:21 +00:00
|
|
|
async commitTransaction(args: CommitTransactionArgs) {
|
|
|
|
return this.client.commitTransaction(args);
|
|
|
|
}
|
|
|
|
|
2023-04-21 05:55:43 +00:00
|
|
|
async validateAddress({ address }: ValidateAddressArgs) {
|
|
|
|
return this.client.resolveAddress(address);
|
|
|
|
}
|
2023-06-20 15:53:27 +00:00
|
|
|
|
|
|
|
async getTaxCodes() {
|
|
|
|
const taxCodeService = new AvataxClientTaxCodeService(this.client);
|
|
|
|
|
|
|
|
return taxCodeService.getTaxCodes();
|
|
|
|
}
|
2023-03-02 11:01:17 +00:00
|
|
|
}
|