hotfix(brightpearl): rounding errors + failing customer test

This commit is contained in:
Sebastian Rindom committed 2021-03-11 08:10:19 +01:00
1 parent 1e5a1398d8
commit d02b804ed3
3 files changed
+223 -17

No files matched your search

@@ -47,8 +47,63 @@ const order = {
email: "test@example.com", 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 = { const OrderService = {
retrieve: () => { retrieve: (id) => {
if (id === "rounding") {
return Promise.resolve(roundingOrder)
}
return Promise.resolve(order) return Promise.resolve(order)
}, },
update: () => { update: () => {
@@ -60,17 +115,29 @@ const TotalsService = {
getTotal: () => { getTotal: () => {
return Promise.resolve(123) return Promise.resolve(123)
}, },
getLineDiscounts: () => { getLineDiscounts: (o) => {
return Promise.resolve([]) if (o.id === "rounding") {
return [
{
item: { id: "rounding-item", quantity: 1 },
amount: 15800,
}, },
getShippingTotal: () => { ]
}
return []
},
getShippingTotal: (o) => {
if (o.id === "rounding") {
return 0
}
return 12399 return 12399
}, },
rounded: (value) => { rounded: (value) => {
const decimalPlaces = 4 return Math.round(value)
return Number( // const decimalPlaces = 4
Math.round(parseFloat(value + "e" + decimalPlaces)) + "e-" + decimalPlaces // return Number(
) // Math.round(parseFloat(value + "e" + decimalPlaces)) + "e-" + decimalPlaces
// )
}, },
} }
@@ -146,6 +213,8 @@ describe("BrightpearlService", () => {
) )
it("successfully builds sales order", async () => { it("successfully builds sales order", async () => {
jest.clearAllMocks()
await bpService.createSalesOrder(order) await bpService.createSalesOrder(order)
expect(mockCreateOrder).toHaveBeenCalledWith({ 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",
},
],
})
})
})
}) })
@@ -355,10 +355,10 @@ class BrightpearlService extends BaseService {
return row.externalRef === i.item_id return row.externalRef === i.item_id
}) })
return { return {
net: this.totalsService_.rounded( net: this.bpround_(
(parentRow.net / parentRow.quantity) * i.quantity (parentRow.net / parentRow.quantity) * i.quantity
), ),
tax: this.totalsService_.rounded( tax: this.bpround_(
(parentRow.tax / parentRow.quantity) * i.quantity (parentRow.tax / parentRow.quantity) * i.quantity
), ),
productId: parentRow.productId, productId: parentRow.productId,
@@ -697,10 +697,10 @@ class BrightpearlService extends BaseService {
return row.externalRef === i.item_id return row.externalRef === i.item_id
}) })
return { return {
net: this.totalsService_.rounded( net: this.bpround_(
(parentRow.net / parentRow.quantity) * i.quantity (parentRow.net / parentRow.quantity) * i.quantity
), ),
tax: this.totalsService_.rounded( tax: this.bpround_(
(parentRow.tax / parentRow.quantity) * i.quantity (parentRow.tax / parentRow.quantity) * i.quantity
), ),
productId: parentRow.productId, productId: parentRow.productId,
@@ -1096,9 +1096,16 @@ class BrightpearlService extends BaseService {
return { contactId: customer } return { contactId: customer }
} }
bpround_(n) {
const decimalPlaces = 4
return Number(
Math.round(parseFloat(n + "e" + decimalPlaces)) + "e-" + decimalPlaces
)
}
bpnum_(number, taxRate = 100) { bpnum_(number, taxRate = 100) {
const bpNumber = number / 100 const bpNumber = number / 100
return this.totalsService_.rounded(bpNumber * (taxRate / 100)) return this.bpround_(bpNumber * (taxRate / 100))
} }
} }
@@ -140,15 +140,15 @@ describe("CustomerService", () => {
}) })
it("fails if email is in incorrect format", async () => { it("fails if email is in incorrect format", async () => {
expect( await expect(
customerService.create({ customerService.create({
email: "olivermedusa.com", email: "olivermedusa.com",
}) })
).rejects.toThrow("The email is not valid") ).rejects.toThrow("The email is not valid")
}) })
it("fails if billing address is in incorrect format", () => { it("fails if billing address is in incorrect format", async () => {
expect( await expect(
customerService.create({ customerService.create({
email: "oliver@medusa.com", email: "oliver@medusa.com",
first_name: "Oliver", first_name: "Oliver",
@@ -289,13 +289,15 @@ describe("CustomerService", () => {
}) })
it("throws on invalid address", async () => { it("throws on invalid address", async () => {
expect( await expect(
customerService.updateAddress( customerService.updateAddress(
IdMap.getId("ironman"), IdMap.getId("ironman"),
IdMap.getId("hollywood-boulevard"), IdMap.getId("hollywood-boulevard"),
{ {
first_name: "Tony", first_name: "Tony",
last_name: "Stark", last_name: "Stark",
country_code: "us",
unknown: "key",
address_1: "Hollywood", address_1: "Hollywood",
} }
) )