hotfix(brightpearl): rounding errors + failing customer test (#199)

This commit is contained in:
Sebastian Rindom
2021-03-11 08:23:53 +01:00
committed by GitHub
parent 1e5a1398d8
commit 833da81cc2
3 changed files with 223 additions and 17 deletions

View File

@@ -47,8 +47,63 @@ const order = {
email: "test@example.com",
}
const roundingOrder = {
region: {
tax_code: "1234",
},
items: [
{
id: "rounding-item",
title: "Test",
allow_discounts: true,
variant: {
sku: "TEST",
},
unit_price: 31600,
quantity: 1,
},
],
shipping_total: 0,
shipping_methods: [
{
name: "standard",
price: 0,
},
],
discounts: [
{
code: "testdiscount",
rule: {
type: "percentage",
allocation: "total",
value: 50,
},
},
],
payment_method: {
id: "123",
},
tax_rate: 25,
currency_code: "DKK",
display_id: "1234",
id: "rounding",
shipping_address: {
first_name: "Test",
last_name: "Testson",
address_1: "Test",
address_2: "TEst",
postal_code: "1234",
country_code: "DK",
phone: "12345678",
},
email: "test@example.com",
}
const OrderService = {
retrieve: () => {
retrieve: (id) => {
if (id === "rounding") {
return Promise.resolve(roundingOrder)
}
return Promise.resolve(order)
},
update: () => {
@@ -60,17 +115,29 @@ const TotalsService = {
getTotal: () => {
return Promise.resolve(123)
},
getLineDiscounts: () => {
return Promise.resolve([])
getLineDiscounts: (o) => {
if (o.id === "rounding") {
return [
{
item: { id: "rounding-item", quantity: 1 },
amount: 15800,
},
]
}
return []
},
getShippingTotal: () => {
getShippingTotal: (o) => {
if (o.id === "rounding") {
return 0
}
return 12399
},
rounded: (value) => {
const decimalPlaces = 4
return Number(
Math.round(parseFloat(value + "e" + decimalPlaces)) + "e-" + decimalPlaces
)
return Math.round(value)
// const decimalPlaces = 4
// return Number(
// Math.round(parseFloat(value + "e" + decimalPlaces)) + "e-" + decimalPlaces
// )
},
}
@@ -146,6 +213,8 @@ describe("BrightpearlService", () => {
)
it("successfully builds sales order", async () => {
jest.clearAllMocks()
await bpService.createSalesOrder(order)
expect(mockCreateOrder).toHaveBeenCalledWith({
@@ -201,4 +270,132 @@ describe("BrightpearlService", () => {
})
})
})
describe("rounding", () => {
const bpService = new BrightpearlService(
{
orderService: OrderService,
totalsService: TotalsService,
oauthService: OAuthService,
regionService: RegionService,
},
{ account: "test" }
)
it("rounds correctly", async () => {
jest.clearAllMocks()
await bpService.createSalesOrder("rounding")
expect(mockCreateOrder).toHaveBeenCalledTimes(1)
expect(mockCreateOrder).toHaveBeenCalledWith({
currency: { code: "DKK" },
ref: "1234",
externalRef: "rounding",
channelId: "1",
installedIntegrationInstanceId: undefined,
statusId: "3",
customer: {
id: "12345",
address: {
addressFullName: "Test Testson",
addressLine1: "Test",
addressLine2: "TEst",
postalCode: "1234",
countryIsoCode: "DK",
telephone: "12345678",
email: "test@example.com",
},
},
delivery: {
shippingMethodId: 0,
address: {
addressFullName: "Test Testson",
addressLine1: "Test",
addressLine2: "TEst",
postalCode: "1234",
countryIsoCode: "DK",
telephone: "12345678",
email: "test@example.com",
},
},
rows: [
{
name: "Test",
net: 158,
tax: 39.5,
quantity: 1,
taxCode: "1234",
externalRef: "rounding-item",
nominalCode: "4000",
},
{
name: "Shipping: standard",
quantity: 1,
net: 0,
tax: 0,
taxCode: "1234",
nominalCode: "4040",
},
],
})
})
it("rounds correctly", async () => {
jest.clearAllMocks()
await bpService.createSalesOrder("rounding")
expect(mockCreateOrder).toHaveBeenCalledTimes(1)
expect(mockCreateOrder).toHaveBeenCalledWith({
currency: { code: "DKK" },
ref: "1234",
externalRef: "rounding",
channelId: "1",
installedIntegrationInstanceId: undefined,
statusId: "3",
customer: {
id: "12345",
address: {
addressFullName: "Test Testson",
addressLine1: "Test",
addressLine2: "TEst",
postalCode: "1234",
countryIsoCode: "DK",
telephone: "12345678",
email: "test@example.com",
},
},
delivery: {
shippingMethodId: 0,
address: {
addressFullName: "Test Testson",
addressLine1: "Test",
addressLine2: "TEst",
postalCode: "1234",
countryIsoCode: "DK",
telephone: "12345678",
email: "test@example.com",
},
},
rows: [
{
name: "Test",
net: 158,
tax: 39.5,
quantity: 1,
taxCode: "1234",
externalRef: "rounding-item",
nominalCode: "4000",
},
{
name: "Shipping: standard",
quantity: 1,
net: 0,
tax: 0,
taxCode: "1234",
nominalCode: "4040",
},
],
})
})
})
})

View File

@@ -355,10 +355,10 @@ class BrightpearlService extends BaseService {
return row.externalRef === i.item_id
})
return {
net: this.totalsService_.rounded(
net: this.bpround_(
(parentRow.net / parentRow.quantity) * i.quantity
),
tax: this.totalsService_.rounded(
tax: this.bpround_(
(parentRow.tax / parentRow.quantity) * i.quantity
),
productId: parentRow.productId,
@@ -697,10 +697,10 @@ class BrightpearlService extends BaseService {
return row.externalRef === i.item_id
})
return {
net: this.totalsService_.rounded(
net: this.bpround_(
(parentRow.net / parentRow.quantity) * i.quantity
),
tax: this.totalsService_.rounded(
tax: this.bpround_(
(parentRow.tax / parentRow.quantity) * i.quantity
),
productId: parentRow.productId,
@@ -1096,9 +1096,16 @@ class BrightpearlService extends BaseService {
return { contactId: customer }
}
bpround_(n) {
const decimalPlaces = 4
return Number(
Math.round(parseFloat(n + "e" + decimalPlaces)) + "e-" + decimalPlaces
)
}
bpnum_(number, taxRate = 100) {
const bpNumber = number / 100
return this.totalsService_.rounded(bpNumber * (taxRate / 100))
return this.bpround_(bpNumber * (taxRate / 100))
}
}

View File

@@ -140,15 +140,15 @@ describe("CustomerService", () => {
})
it("fails if email is in incorrect format", async () => {
expect(
await expect(
customerService.create({
email: "olivermedusa.com",
})
).rejects.toThrow("The email is not valid")
})
it("fails if billing address is in incorrect format", () => {
expect(
it("fails if billing address is in incorrect format", async () => {
await expect(
customerService.create({
email: "oliver@medusa.com",
first_name: "Oliver",
@@ -289,13 +289,15 @@ describe("CustomerService", () => {
})
it("throws on invalid address", async () => {
expect(
await expect(
customerService.updateAddress(
IdMap.getId("ironman"),
IdMap.getId("hollywood-boulevard"),
{
first_name: "Tony",
last_name: "Stark",
country_code: "us",
unknown: "key",
address_1: "Hollywood",
}
)