Skip to content

Commit

Permalink
extra logs for lines + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lkostrowski committed Jan 23, 2025
1 parent 989cb68 commit 67aaa35
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-shirts-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"app-avatax": patch
---

Added test for suspicious line+tax calculation checker and additional debugging logs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { describe, expect, it } from "vitest";

import { suspiciousLineCalculationCheck } from "@/modules/avatax/calculate-taxes/avatax-calculate-taxes-adapter";

describe("suspiciousLineCalculationCheck", () => {
it("Returns false if line is zero", () => {
expect(
suspiciousLineCalculationCheck({
total_gross_amount: 0,
total_net_amount: 0,
tax_rate: 0.2, // If its zero-line, it doesn matter
}),
).toBe(false);
});

it("Returns true if net & gross is the same, but rate is not: 1.00 + 1.00 + rate 0.08", () => {
expect(
suspiciousLineCalculationCheck({
total_gross_amount: 1,
total_net_amount: 1,
tax_rate: 0.08, // If its zero-line, it doesn matter
}),
).toBe(true);
});

it("Returns true for small numbers: 0.06 + 0.06 + rate 0.07", () => {
expect(
suspiciousLineCalculationCheck({
total_gross_amount: 0.06,
total_net_amount: 0.06,
tax_rate: 0.07, // If its zero-line, it doesn matter
}),
).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ export type AvataxCalculateTaxesResponse = CalculateTaxesResponse;

const errorParser = new AvataxErrorsParser();

export function suspiciousLineCalculationCheck(line: {
total_gross_amount: number;
total_net_amount: number;
tax_rate: number;
}) {
const tax = line.total_gross_amount - line.total_net_amount;
const rate = line.tax_rate;
const lineIsZero = line.total_net_amount === 0 ?? line.total_gross_amount === 0;

if (tax === 0 && rate !== 0 && !lineIsZero) {
return true;
}

return false;
}

export class AvataxCalculateTaxesAdapter {
private logger = createLogger("AvataxCalculateTaxesAdapter");

Expand Down Expand Up @@ -38,11 +54,9 @@ export class AvataxCalculateTaxesAdapter {
const transformedResponse = this.avataxCalculateTaxesResponseTransformer.transform(response);

transformedResponse.lines.forEach((l) => {
const tax = l.total_gross_amount - l.total_net_amount;
const rate = l.tax_rate;
const lineIsZero = l.total_net_amount === 0 ?? l.total_gross_amount === 0;
const isSuspiciousLine = suspiciousLineCalculationCheck(l);

if (tax === 0 && rate !== 0 && !lineIsZero) {
if (isSuspiciousLine) {
this.logger.warn("Non-zero line has zero tax, but rate is not zero", {
taxCalculationSummary: response.summary,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export class AvataxCalculateTaxesResponseLinesTransformer {
total_net_amount: lineTotalNetAmount,
tax_code: line.taxCode,
tax_rate: rate,
line_taxable_amount: line.taxableAmount,
line_tax_calculated: line.taxCalculated,
},
);

Expand Down

0 comments on commit 67aaa35

Please sign in to comment.