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
Show file tree
Hide file tree
Changes from all commits
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
87 changes: 1 addition & 86 deletions back-end/routes/addEventMemberRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,7 @@ const { User } = require("../models/User.js");
const { Event } = require("../models/Event.js");
const { body, validationResult } = require("express-validator");

// Hardcoded user data to simulate a database

/*
let friendData = [{
"id":"507f1f77bcf86cd799439011",
"name":"Gaby Coupar",
"avatar":"https://robohash.org/temporererumomnis.png?size=50x50\u0026set=set1",
"phone":"435-715-2899",
"email":"[email protected]"
}, {
"id": "507f1f77bcf86cd799439012",
"name": "Andy Gaber",
"avatar": "https://robohash.org/quaeetcorrupti.png?size=50x50&set=set1",
"phone":"425-712-2309",
"email":"[email protected]"
}]

// Endpoint to get user data after search
router.get('/friend', (req, res) => {
res.json(friendData);
});
*/

router.get("/friendsList/:userId", async (req, res) => {
router.get('/friendsList/:userId', async (req, res) => {
try {
//fetch all data
const userId = req.params.userId;
Expand All @@ -47,66 +24,4 @@ router.get("/friendsList/:userId", async (req, res) => {
}
});

/*
router.get('/friends/:userId', async (req, res) => {
try {
const userId = req.params.userId;
console.log('Fetching user with ID:', userId);

const user = await User.findById(userId).populate('friends');
console.log('User:', user);

if (!user) {
console.log('User not found');
return res.status(404).json({ message: 'User not found' });
}

res.json(user.friends);
} catch (error) {
console.error('Error:', error);
res.status(500).json({ message: 'Server error', error: error.message });
}
});


router.post('/events/:eventId/participants', async (req, res) => {
try {
const { eventId } = req.params;
const { memberId } = req.body;

// Find the event and add the new member
const event = await Event.findById(eventId);
const user = await User.findById(userId)

if (!event) {
return res.status(404).json({ message: 'Event not found' });
}
if(!user){
return res.status(404).json({ message: 'User not found'})
}

// Check if member is already in the participants list
if (event.participants.includes(memberId)) {
return res.status(400).json({ message: 'Member already in the event' });
}

// Validate if the memberId is in the user's friend list
const isFriend = user.friends.some(friend => friend._id.toString() === memberId);

if (!isFriend) {
return res.status(400).json({ message: 'Member is not in the user\'s friend list' });
}

// Add the member to the participants list
event.participants.push(memberId);
await event.save();

res.json({message: 'Member added successfully', event});
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Server error' });
}
});
*/

module.exports = router;
Empty file.
42 changes: 24 additions & 18 deletions front-end/src/components/Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -180,12 +188,10 @@ const Event = (props) => {
<div>{item.expense.name}</div>
</Link>
</div>
<div className="amount">
{item.settlement.toFixed(2) === "0.00" ? (
<span className="settled"> Settled </span>
) : (
`$${item.settlement.toFixed(2)}`
)}
<div className={`amount ${item.settlement < 0 ? 'negative' : 'positive'}`}>
{item.settlement.toFixed(2) === "0.00" ?
<span className="settled">Settled</span> :
`$${item.settlement.toFixed(2)}`}
</div>
</div>
))}
Expand Down
Loading
Loading