Skip to content

Commit

Permalink
feat: completely implemented robust split equal function
Browse files Browse the repository at this point in the history
  • Loading branch information
abhiraj-ku committed Nov 11, 2024
1 parent 18f328b commit 4799dcb
Showing 1 changed file with 71 additions and 33 deletions.
104 changes: 71 additions & 33 deletions src/controllers/splitLogicController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ const validateBillSplit = require('../helpers/validateBillsplitInputs');

module.exports.splitBill = async (req, res) => {
// Step 1. Get the bare split equal done
const { description, amount, paidBy, splitType, selectedMemeber } = req.body;
const {
description,
amount,
paidBy,
splitType,
selectedMembers,
customAmounts,
isPaidByIncluded,
} = req.body;
if (!description || !amount || !paidBy || !splitType || !selectedMemeber) {
return res.status(400).json({ message: 'All fields are required' });
}
Expand All @@ -17,37 +25,67 @@ module.exports.splitBill = async (req, res) => {

//Bill splitting logics
try {
const paidUsers = [];

// Type 1: splitType === 'EQUAL'
if (splitType === 'EQUAL') {
let membersInex = [...selectedMembers];

// If paidBy should be included and isn't already in the array
if (isPaidByIncluded && !membersInex.includes(paidBy)) {
membersInex.push(paidBy);
}

// Calculate split amount
const toEachMem = Number(amount / membersInex.length);
const eachShare = Math.round(toEachMem * 10) / 10;

// Show who owes what
membersInex.forEach((mem) => {
if (mem === paidBy) {
console.log(`${mem} paid Rs. ${amount} and owes Rs. ${eachShare}`);
} else {
console.log(`${mem} has to pay Rs. ${eachShare} to ${paidBy}`);
}
});

return {
paymentType: 'EQUAL',
totalAmount: amount,
perPersonShare: eachShare,
paidBy,
participants: membersInex,
};
let response;

// Bill spliting logic based on splitType
if (splitType === 'equal') {
response = splitEqual(amount, paidBy, selectedMembers, isPaidByIncluded);
} else if (splitType === 'custom') {
response = splitCustom(amount, paidBy, selectedMembers, customAmounts, isPaidByIncluded);
}

// currently supports splitType=== 'equal' & 'custom'
if (response) {
return res.status(200).json(response);
} else {
return res.status(400).json({ message: 'Unsupported split type' });
}
} catch (error) {}
} catch (error) {
console.error('Failed to split bill:', error);
return res.status(500).json({ message: 'An error occurred while processing the request' });
}
};

// Helper function fot splitType ='equal'
const splitEqual = (amount, paidBy, selectedMembers, isPaidByIncluded) => {
let membersIndex = [...selectedMembers];

// If paidBy should be included and isn't already in the array
if (isPaidByIncluded && !membersIndex.includes(paidBy)) {
membersIndex.push(paidBy);
}

// share per person is equal
const sharePerPerson = Number(amount / membersIndex.length).toFixed(2);

// Payments description about who owes to whom

const payments = membersIndex.map((member) => ({
member,
owes: member === paidBy ? 0 : sharePerPerson,
description:
member === paidBy ? `paid Rs. ${amount}` : `Owes Rs. ${sharePerPerson} to ${paidBy} `,
}));

return {
splitType: 'equal',
totalAmount: amount,
perPersonShare: sharePerPerson,
paidBy,
selectedMembers: membersIndex,
payments,
};
};

// Helper function fot splitType ='custom' where
// participating members can have different payments
// amount to be paid.

const custom = (amount, paidBy, selectedMembers, customAmounts, isPaidByIncluded) => {
let membersIndex = [...selectedMembers];

// If paidBy should be included and isn't already in the array
if (isPaidByIncluded && !membersIndex.includes(paidBy)) {
membersIndex.push(paidBy);
}
};

0 comments on commit 4799dcb

Please sign in to comment.