diff --git a/packages/medusa-plugin-brightpearl/src/services/__tests__/brightpearl.js b/packages/medusa-plugin-brightpearl/src/services/__tests__/brightpearl.js index 8f28621da8..47851931b0 100644 --- a/packages/medusa-plugin-brightpearl/src/services/__tests__/brightpearl.js +++ b/packages/medusa-plugin-brightpearl/src/services/__tests__/brightpearl.js @@ -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", + }, + ], + }) + }) + }) }) diff --git a/packages/medusa-plugin-brightpearl/src/services/brightpearl.js b/packages/medusa-plugin-brightpearl/src/services/brightpearl.js index 2fd27359e8..88a50fe54b 100644 --- a/packages/medusa-plugin-brightpearl/src/services/brightpearl.js +++ b/packages/medusa-plugin-brightpearl/src/services/brightpearl.js @@ -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)) } } diff --git a/packages/medusa/src/services/__tests__/customer.js b/packages/medusa/src/services/__tests__/customer.js index 975d6bdb77..d23c10dc52 100644 --- a/packages/medusa/src/services/__tests__/customer.js +++ b/packages/medusa/src/services/__tests__/customer.js @@ -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", } )