-
Notifications
You must be signed in to change notification settings - Fork 0
Mav #31
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
base: base-sha/520c1674316532bd5a4206996f87f78b85e10bfb
Are you sure you want to change the base?
Mav #31
Changes from all commits
05bd432
1978dd7
7b2b001
55be42a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -615,5 +615,86 @@ router.get('/notifications/:userId', async (req, res) => { | |
| } | ||
| }); | ||
|
|
||
| router.get('/time-due/:userId/:orderId', async (req, res) => { | ||
| const userId = req.params.userId; | ||
| const orderId = req.params.orderId; | ||
|
|
||
| try { | ||
| // Find the user by ID | ||
| const user = await User.findById(userId); | ||
|
|
||
| if (!user) { | ||
| return res.status(404).json({ message: "User not found" }); | ||
| } | ||
|
|
||
| // Find the order by ID in the user's rented array | ||
| const order = user.Rented.find(order => order._id.toString() === orderId); | ||
|
|
||
| if (!order) { | ||
| return res.status(404).json({ message: "Order not found" }); | ||
| } | ||
|
|
||
| // Check if live status is true | ||
| if (order.Live) { | ||
| // Calculate time due in months | ||
| const rentDuration = order.RentDuration; // Rent duration in months | ||
| const startDate = new Date(order.Date); // Date the product was rented | ||
| const currentDate = new Date(); // Current date | ||
| const monthsDiff = (currentDate.getFullYear() - startDate.getFullYear()) * 12 + (currentDate.getMonth() - startDate.getMonth()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (edge_case_not_handled): Potential issue with month difference calculation. This calculation might not account for partial months or correct boundary conditions around year changes. Consider edge cases or a library to handle date differences. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this comment correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this comment helpful? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the comment type correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the comment area correct? |
||
| const timeDue = rentDuration - monthsDiff; | ||
|
|
||
| res.json({ timeDue: timeDue }); | ||
| } else { | ||
| res.json({ message: "Order is not active" }); | ||
| } | ||
| } catch (error) { | ||
| console.error(error); | ||
| res.status(500).json({ message: "Server Error" }); | ||
| } | ||
| }); | ||
|
|
||
| router.get('/total-loan/:userId/:orderId', async (req, res) => { | ||
| const userId = req.params.userId; | ||
| const orderId = req.params.orderId; | ||
|
|
||
| try { | ||
| // Find the user by ID | ||
| const user = await User.findById(userId); | ||
|
|
||
| if (!user) { | ||
| return res.status(404).json({ message: "User not found" }); | ||
| } | ||
|
|
||
| // Find the order by ID in the user's rented array | ||
| const order = user.Rented.find(order => order._id.toString() === orderId); | ||
|
|
||
| if (!order) { | ||
| return res.status(404).json({ message: "Order not found" }); | ||
| } | ||
|
|
||
| // Calculate total loan | ||
| let totalLoan = 0; | ||
|
|
||
| if (order.Live) { | ||
| const rentDuration = order.RentDuration; // Rent duration in months | ||
| const startDate = new Date(order.Date); // Date the product was rented | ||
| const currentDate = new Date(); // Current date | ||
| const monthsDiff = (currentDate.getFullYear() - startDate.getFullYear()) * 12 + (currentDate.getMonth() - startDate.getMonth()); | ||
| const timeDue = rentDuration - monthsDiff; | ||
|
|
||
| // If time due is negative, calculate total loan | ||
| if (timeDue < 0) { | ||
| totalLoan = Math.abs(timeDue) * order.Quantity * order.Price; | ||
| } | ||
| } | ||
|
|
||
| res.json({ totalLoan: totalLoan }); | ||
| } catch (error) { | ||
| console.error(error); | ||
| res.status(500).json({ message: "Server Error" }); | ||
| } | ||
| }); | ||
|
|
||
|
|
||
| module.exports = router; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -106,19 +106,46 @@ const UserSchema = new mongoose.Schema({ | |
|
|
||
| UserSchema.methods.calculateDueTime = function() { | ||
| this.Rented.forEach(item => { | ||
| const rentDuration = item.RentDuration; // Rent duration in months | ||
| const startDate = item.Date; // Date the product was rented | ||
| const currentDate = new Date(); // Current date | ||
|
|
||
| // Calculate due time in months | ||
| const dueTime = rentDuration - Math.floor((currentDate - startDate) / (30 * 24 * 60 * 60 * 1000)); | ||
|
|
||
| // Update TimeDue field with the calculated due time | ||
| item.TimeDue = dueTime; | ||
| if (item.Live) { | ||
| const rentDuration = item.RentDuration; // Rent duration in months | ||
| const startDate = item.Date; // Date the product was rented | ||
| const currentDate = new Date(); // Current date | ||
|
|
||
| // Calculate due time in months | ||
| const dueTime = rentDuration - Math.floor((currentDate - startDate) / (30 * 24 * 60 * 60 * 1000)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): Inconsistent date handling might lead to errors. Using a fixed 30-day month might introduce inaccuracies in calculations, especially over longer periods or different month lengths. Consider using a date library for more accurate calculations. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this comment correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this comment helpful? |
||
|
|
||
| // Update TimeDue field with the calculated due time | ||
| item.TimeDue = dueTime; | ||
| } | ||
| }); | ||
| // Save the changes to the user document | ||
| return this.save(); | ||
| }; | ||
|
|
||
| UserSchema.methods.calculateLoan = function() { | ||
| this.Rented.forEach(item => { | ||
| if (item.Live) { | ||
| const rentDuration = item.RentDuration; // Rent duration in months | ||
| const startDate = item.Date; // Date the product was rented | ||
| const currentDate = new Date(); // Current date | ||
|
|
||
| // Calculate due time in months | ||
| const dueTime = rentDuration - Math.floor((currentDate - startDate) / (30 * 24 * 60 * 60 * 1000)); | ||
|
|
||
| // If due time is negative, calculate loan | ||
| if (dueTime < 0) { | ||
| item.Loan = Math.abs(dueTime) * item.Quantity * item.Price; | ||
| } else { | ||
| item.Loan = 0; // If due time is not negative, set loan to 0 | ||
| } | ||
| } else { | ||
| item.Loan = 0; // If status is not live, set loan to 0 | ||
| } | ||
| }); | ||
| // Save the changes to the user document | ||
| return this.save(); | ||
| }; | ||
|
|
||
|
|
||
| const User = mongoose.model("User", UserSchema); | ||
| module.exports = User; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (performance): Consider using a more efficient method to find orders.
Using
findon an array can be inefficient for large datasets. Consider indexing or a more direct query method if performance is a concern.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this comment correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this comment helpful?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the comment type correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the comment area correct?