Skip to content

Commit

Permalink
fix: rakuten amount value rounded up to nearest integer (#3784)
Browse files Browse the repository at this point in the history
* fix: rakuten amount value rounded up to nearest integer

* fix: review comments addressed
  • Loading branch information
shrouti1507 authored Oct 8, 2024
1 parent f8cde8c commit f3046f0
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 9 deletions.
22 changes: 14 additions & 8 deletions src/cdk/v2/destinations/rakuten/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 };
62 changes: 61 additions & 1 deletion src/cdk/v2/destinations/rakuten/utils.test.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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);
});
});

0 comments on commit f3046f0

Please sign in to comment.