From f3046f0ae37c113c1239d988d056fc204f2776a0 Mon Sep 17 00:00:00 2001 From: shrouti1507 <60211312+shrouti1507@users.noreply.github.com> Date: Tue, 8 Oct 2024 17:41:12 +0530 Subject: [PATCH] fix: rakuten amount value rounded up to nearest integer (#3784) * fix: rakuten amount value rounded up to nearest integer * fix: review comments addressed --- src/cdk/v2/destinations/rakuten/utils.js | 22 ++++--- src/cdk/v2/destinations/rakuten/utils.test.js | 62 ++++++++++++++++++- 2 files changed, 75 insertions(+), 9 deletions(-) diff --git a/src/cdk/v2/destinations/rakuten/utils.js b/src/cdk/v2/destinations/rakuten/utils.js index 2dd628a250..b4897c46ed 100644 --- a/src/cdk/v2/destinations/rakuten/utils.js +++ b/src/cdk/v2/destinations/rakuten/utils.js @@ -17,6 +17,18 @@ const constructProperties = (message) => { return payload; }; +/** + * Calculates the amount for a single product + * @param {Object} product + * @returns {number} + */ +const calculateProductAmount = (product) => { + if (!product?.amount && !product?.price) { + throw new InstrumentationError('Either amount or price is required for every product'); + } + return Math.round(product.amount * 100 || (product.quantity || 1) * 100 * product.price); +}; + /** * This fucntion build the item level list * @param {*} properties @@ -52,14 +64,8 @@ const constructLineItems = (properties) => { }); // Map 'amountList' by evaluating 'amount' or deriving it from 'price' and 'quantity' - const amountList = products.map((product) => { - if (!product?.amount && !product?.price) { - throw new InstrumentationError('Either amount or price is required for every product'); - } - return product.amount * 100 || (product.quantity || 1) * 100 * product.price; - }); - productList.amtlist = amountList.join('|'); + productList.amtlist = products.map(calculateProductAmount).join('|'); return productList; }; -module.exports = { constructProperties, constructLineItems }; +module.exports = { constructProperties, constructLineItems, calculateProductAmount }; diff --git a/src/cdk/v2/destinations/rakuten/utils.test.js b/src/cdk/v2/destinations/rakuten/utils.test.js index 9cc7f5fd4c..2d82037b1c 100644 --- a/src/cdk/v2/destinations/rakuten/utils.test.js +++ b/src/cdk/v2/destinations/rakuten/utils.test.js @@ -1,4 +1,5 @@ -const { constructLineItems } = require('./utils'); +const { InstrumentationError } = require('@rudderstack/integrations-lib'); +const { constructLineItems, calculateProductAmount } = require('./utils'); describe('constructLineItems', () => { it('should return a non-empty object when given a valid properties object with at least one product', () => { const properties = { @@ -115,3 +116,62 @@ describe('constructLineItems', () => { ); }); }); + +describe('calculateProductAmount', () => { + // Calculates product amount correctly when amount is defined + it('should return the correct product amount when amount is defined', () => { + const product = { amount: 5 }; + const result = calculateProductAmount(product); + expect(result).toBe(500); + }); + + // Throws error when both amount and price are undefined or null + it('should throw an error when both amount and price are undefined or null', () => { + const product = {}; + expect(() => calculateProductAmount(product)).toThrow(InstrumentationError); + }); + + // Calculates product amount correctly when price and quantity are defined + it('should calculate product amount correctly when price and quantity are defined', () => { + const product = { amount: 10, price: 5, quantity: 2 }; + const result = calculateProductAmount(product); + expect(result).toEqual(1000); + }); + + // Returns correct value when only price is defined and quantity defaults to 1 + it('should return correct value when only price is defined and quantity defaults to 1', () => { + const product = { price: 20 }; + const result = calculateProductAmount(product); + expect(result).toEqual(2000); + }); + + // Handles cases where amount is a floating-point number + it('should handle cases where amount is a floating-point number', () => { + const product = { amount: 5.5, price: 10, quantity: 2 }; + const result = calculateProductAmount(product); + expect(result).toEqual(550); + }); + + it('should handle cases where amount is a floating-point number', () => { + const product = { amount: 5.1, price: 10, quantity: 2 }; + const result = calculateProductAmount(product); + expect(result).toEqual(510); + }); + + it('should handle cases where amount is a floating-point number', () => { + const product = { amount: 5.19, price: 10, quantity: 2 }; + const result = calculateProductAmount(product); + expect(result).toEqual(519); + }); + + it('should handle cases where amount is a floating-point number', () => { + const product = { amount: 5.199, price: 10, quantity: 2 }; + const result = calculateProductAmount(product); + expect(result).toEqual(520); + }); + it('should handle cases where amount is a floating-point number', () => { + const product = { amount: 5.479, price: 10, quantity: 2 }; + const result = calculateProductAmount(product); + expect(result).toEqual(548); + }); +});