Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event main #208

Merged
merged 8 commits into from
Dec 7, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
consider the situation when the currentuser is the paidby but not the…
… participant
elaineZhang67 committed Dec 6, 2023
commit 36ade4845ad0fe33ca1bf54436ee6362dadcc924
32 changes: 20 additions & 12 deletions front-end/src/components/Event.js
Original file line number Diff line number Diff line change
@@ -52,27 +52,35 @@ const Event = (props) => {
const isParticipant = expense.splitDetails.find(
(detail) => detail.user === userId
);
//let settlement;
let userBalance = 0;

if (isParticipant) {
if (expense.paidBy === userId) {

if (expense.paidBy === userId) {
// Check if the currentUser is involved in the split
if (isParticipant) {
expense.splitDetails.forEach((split) => {
if (!split.settlement.status) {
userBalance += split.settlement.amount;
}
});
} else {
//user is not the one who paid, find what the user owe to the person who paid
const user = expense.splitDetails.find(
(split) => split.user === userId
);
if (user) {
userBalance = user.settlement.status ? 0 : -user.settlement.amount;
}
let totalSettledAmount = 0;
expense.splitDetails.forEach((split) => {
if (split && split.settlement && split.settlement.status) {
totalSettledAmount += split.settlement.amount;
}
});
userBalance = (expense.totalAmount ? expense.totalAmount : 0) - totalSettledAmount;
}
} else if (isParticipant) {
// If currentUser is not the one who paid, but is involved in the split
const user = expense.splitDetails.find(
(split) => split.user === userId
);
if (user) {
userBalance = user.settlement.status ? 0 : -user.settlement.amount;
}
} else {
// User is not a participant, create a settlement object with amount 0
// User is neither the payer nor a participant in the split
userBalance = 0;
}

32 changes: 24 additions & 8 deletions front-end/src/components/Expense.jsx
Original file line number Diff line number Diff line change
@@ -79,17 +79,33 @@ function Expense({ isDarkMode }) {
(split) => split.user._id === userId
);

if (!isParticipant) {
// If not a participant, return an empty array or another appropriate value
return [];
}

let filteredExpenses = [];

if (expensesData.paidBy._id === userId) {
filteredExpenses = expensesData.splitDetails.filter(split => split.user._id !== userId).map(split => ({ ...split, displayName: split.user.username, amount: split.settlement.amount}));
} else {
filteredExpenses = expensesData.splitDetails.filter(split => split.user._id === userId && expensesData.paidBy !== userId).map(split => ({ ...split, displayName: expensesData.paidBy.username, amount: -split.settlement.amount}));;
if (!isParticipant) {
// If currentUser is not a participant, then the amount will be negative (owed by others)
filteredExpenses = expensesData.splitDetails.map(split => ({
...split,
displayName: split.user.username,
amount: -split.settlement.amount
}));
} else {
// If currentUser is also a participant
filteredExpenses = expensesData.splitDetails.filter(split => split.user._id !== userId).map(split => ({
...split,
displayName: split.user.username,
amount: split.settlement.amount
}));
}
} else if (isParticipant) {
// When currentUser is not the one who paid but is a participant
filteredExpenses = expensesData.splitDetails.filter(split => split.user._id === userId && expensesData.paidBy !== userId).map(split => ({
...split,
displayName: expensesData.paidBy.username,
amount: -split.settlement.amount
}));
}else{
filteredExpenses = [];
}
return filteredExpenses;
};