fix: 🐛 use "DEFAULT" value of companyCode for commit to work

This commit is contained in:
Adrian Pilarczyk 2023-08-02 11:47:24 +02:00
parent 79a1411a55
commit 293407e9f0
7 changed files with 12 additions and 21 deletions

View file

@ -3,8 +3,8 @@ import { DocumentType } from "avatax/lib/enums/DocumentType";
import { AddressLocationInfo as AvataxAddress } from "avatax/lib/models/AddressLocationInfo"; import { AddressLocationInfo as AvataxAddress } from "avatax/lib/models/AddressLocationInfo";
import { CommitTransactionModel } from "avatax/lib/models/CommitTransactionModel"; import { CommitTransactionModel } from "avatax/lib/models/CommitTransactionModel";
import { CreateTransactionModel } from "avatax/lib/models/CreateTransactionModel"; import { CreateTransactionModel } from "avatax/lib/models/CreateTransactionModel";
import { LogOptions } from "avatax/lib/utils/logger";
import packageJson from "../../../package.json"; import packageJson from "../../../package.json";
import { createLogger, Logger } from "../../lib/logger";
import { AvataxClientTaxCodeService } from "./avatax-client-tax-code.service"; import { AvataxClientTaxCodeService } from "./avatax-client-tax-code.service";
import { BaseAvataxConfig } from "./avatax-connection-schema"; import { BaseAvataxConfig } from "./avatax-connection-schema";
@ -14,11 +14,7 @@ type AvataxSettings = {
environment: "sandbox" | "production"; environment: "sandbox" | "production";
machineName: string; machineName: string;
timeout: number; timeout: number;
logOptions?: { logOptions?: LogOptions;
logEnabled: boolean;
logLevel: number;
logRequestAndResponseInfo: boolean;
};
}; };
const defaultAvataxSettings: AvataxSettings = { const defaultAvataxSettings: AvataxSettings = {
@ -60,10 +56,8 @@ export type VoidTransactionArgs = {
export class AvataxClient { export class AvataxClient {
private client: Avatax; private client: Avatax;
private logger: Logger;
constructor(baseConfig: BaseAvataxConfig) { constructor(baseConfig: BaseAvataxConfig) {
this.logger = createLogger({ name: "AvataxClient" });
const settings = createAvataxSettings({ isSandbox: baseConfig.isSandbox }); const settings = createAvataxSettings({ isSandbox: baseConfig.isSandbox });
const avataxClient = new Avatax(settings).withSecurity(baseConfig.credentials); const avataxClient = new Avatax(settings).withSecurity(baseConfig.credentials);

View file

@ -24,7 +24,7 @@ export type BaseAvataxConfig = z.infer<typeof baseAvataxConfigSchema>;
export const avataxConfigSchema = z export const avataxConfigSchema = z
.object({ .object({
name: z.string().min(1, { message: "Name requires at least one character." }), name: z.string().min(1, { message: "Name requires at least one character." }),
companyCode: z.string().optional(), companyCode: z.string(),
isAutocommit: z.boolean(), isAutocommit: z.boolean(),
shippingTaxCode: z.string().optional(), shippingTaxCode: z.string().optional(),
isDocumentRecordingEnabled: z.boolean().default(true), isDocumentRecordingEnabled: z.boolean().default(true),
@ -36,7 +36,7 @@ export type AvataxConfig = z.infer<typeof avataxConfigSchema>;
export const defaultAvataxConfig: AvataxConfig = { export const defaultAvataxConfig: AvataxConfig = {
name: "", name: "",
companyCode: "", companyCode: "DEFAULT",
isSandbox: false, isSandbox: false,
isAutocommit: false, isAutocommit: false,
isDocumentRecordingEnabled: true, isDocumentRecordingEnabled: true,

View file

@ -15,10 +15,7 @@ export class AvataxOrderCancelledAdapter implements WebhookAdapter<OrderCancelle
} }
async send(payload: OrderCancelledPayload) { async send(payload: OrderCancelledPayload) {
this.logger.debug( this.logger.debug("Transforming the Saleor payload for cancelling transaction with Avatax...");
{ payload },
"Transforming the Saleor payload for cancelling transaction with Avatax..."
);
const payloadTransformer = new AvataxOrderCancelledPayloadTransformer(this.config); const payloadTransformer = new AvataxOrderCancelledPayloadTransformer(this.config);
const target = payloadTransformer.transform({ ...payload }); const target = payloadTransformer.transform({ ...payload });

View file

@ -20,7 +20,7 @@ export class AvataxOrderFulfilledPayloadTransformer {
return { return {
transactionCode, transactionCode,
companyCode: this.config.companyCode ?? "", companyCode: this.config.companyCode,
documentType: this.matchDocumentType(this.config), documentType: this.matchDocumentType(this.config),
model: { model: {
commit: true, commit: true,

View file

@ -42,7 +42,7 @@ export default orderCancelledAsyncWebhook.createHandler(async (req, res, ctx) =>
const channelSlug = payload.order.channel.slug; const channelSlug = payload.order.channel.slug;
const taxProvider = getActiveConnectionService(channelSlug, appMetadata, ctx.authData); const taxProvider = getActiveConnectionService(channelSlug, appMetadata, ctx.authData);
logger.info("Fetched taxProvider"); logger.info("Cancelling order...");
await taxProvider.cancelOrder(payload); await taxProvider.cancelOrder(payload);

View file

@ -43,8 +43,6 @@ export default orderCreatedAsyncWebhook.createHandler(async (req, res, ctx) => {
const channelSlug = payload.order?.channel.slug; const channelSlug = payload.order?.channel.slug;
const taxProvider = getActiveConnectionService(channelSlug, appMetadata, ctx.authData); const taxProvider = getActiveConnectionService(channelSlug, appMetadata, ctx.authData);
logger.info("Fetched taxProvider");
// todo: figure out what fields are needed and add validation // todo: figure out what fields are needed and add validation
if (!payload.order) { if (!payload.order) {
return webhookResponse.error(new Error("Insufficient order data")); return webhookResponse.error(new Error("Insufficient order data"));
@ -54,6 +52,8 @@ export default orderCreatedAsyncWebhook.createHandler(async (req, res, ctx) => {
return webhookResponse.error(new Error("Skipping fulfilled order to prevent duplication")); return webhookResponse.error(new Error("Skipping fulfilled order to prevent duplication"));
} }
logger.info("Creating order...");
const createdOrder = await taxProvider.createOrder(payload.order); const createdOrder = await taxProvider.createOrder(payload.order);
logger.info({ createdOrder }, "Order created"); logger.info({ createdOrder }, "Order created");

View file

@ -38,18 +38,18 @@ export default orderFulfilledAsyncWebhook.createHandler(async (req, res, ctx) =>
const channelSlug = payload.order?.channel.slug; const channelSlug = payload.order?.channel.slug;
const taxProvider = getActiveConnectionService(channelSlug, appMetadata, ctx.authData); const taxProvider = getActiveConnectionService(channelSlug, appMetadata, ctx.authData);
logger.info("Fetched taxProvider");
// todo: figure out what fields are needed and add validation // todo: figure out what fields are needed and add validation
if (!payload.order) { if (!payload.order) {
return webhookResponse.error(new Error("Insufficient order data")); return webhookResponse.error(new Error("Insufficient order data"));
} }
logger.info("Fulfilling order...");
await taxProvider.fulfillOrder(payload.order); await taxProvider.fulfillOrder(payload.order);
logger.info("Order fulfilled"); logger.info("Order fulfilled");
return webhookResponse.success(); return webhookResponse.success();
} catch (error) { } catch (error) {
return webhookResponse.error(new Error("Error while fulfilling tax provider order")); return webhookResponse.error(error);
} }
}); });