-
Notifications
You must be signed in to change notification settings - Fork 0
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
Fill level change api #8
base: main
Are you sure you want to change the base?
Changes from 3 commits
0e25ba9
edf8fe1
789eab8
dbea605
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 |
---|---|---|
|
@@ -164,8 +164,9 @@ export const updateProject = async (req: any, res: any) => { | |
if (updatedProject.users?.length > 0) { | ||
updatedProject.users.forEach(async (userId: any) => { | ||
const user: any = await User.findById(userId); | ||
if (!user.projects.includes(updatedProject._id)) { | ||
user.projects.push(updatedProject._id); | ||
if (!user?.projects.includes(updatedProject._id ) && user) { | ||
console.log("users",user) | ||
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. remove log |
||
user?.projects.push(updatedProject._id); | ||
await user.save(); | ||
} | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import { generateUniqueTrashbinIdentifier } from '../service/trashbin'; | ||
import { Project } from '../models/project'; | ||
import { Trashbin } from '../models/trashbin'; | ||
import { History } from '../models/history'; | ||
import { Types } from 'mongoose'; | ||
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. if you check for example line 151 you can see, that they were already referencing mongoose.Types. So I would propose to either remove this import and do line 411 with "mongoose.Types" or change the other references to just "Types" and removing the import in line 7. |
||
|
||
import mongoose from 'mongoose'; | ||
|
||
export const createTrashItem = async (req: any, res: any, next: any) => { | ||
try { | ||
console.log('Inside Trash Can'); | ||
const projectId = req.body.project; | ||
const trashcanName = req.body.name; | ||
const longitude = req.body.longitude; | ||
|
@@ -39,7 +41,7 @@ export const createTrashItem = async (req: any, res: any, next: any) => { | |
|
||
// Fetch location string from the longitude and latitude | ||
|
||
console.log('Coming inside line # 41'); | ||
|
||
|
||
const trashbin = new Trashbin({ | ||
name: trashcanName, | ||
|
@@ -171,20 +173,32 @@ export const getAllTrashItems = async (req: any, res: any, next: any) => { | |
const projectQuery = req.query.project; | ||
let trashbins; | ||
let count; | ||
console.log("Received project query:", projectQuery); | ||
if(!projectQuery){ | ||
return res.status(400).json({ message: "Project query parameter is required" }); | ||
} | ||
|
||
if (projectQuery) { | ||
// Check if the projectQuery is a valid ObjectId | ||
if (mongoose.Types.ObjectId.isValid(projectQuery)) { | ||
// console.log("Querying trashbins by ObjectId:", projectQuery); | ||
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. remove commented out log |
||
trashbins = await Trashbin.find({ project: projectQuery }) | ||
.populate('assignee') | ||
.populate('project'); | ||
} else { | ||
//console.log("Querying trashbins by project identifier:", projectQuery); | ||
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. remove commented out log |
||
// If not a valid ObjectId, assume it's an identifier | ||
const project = await Project.findOne({ identifier: projectQuery }); | ||
if (!project) { | ||
console.error("Project not found for identifier:", projectQuery); | ||
return res.status(404).json({ message: "Project not found" }); | ||
} | ||
if (project) { | ||
trashbins = await Trashbin.find({ project: project._id }) | ||
.populate('assignee') | ||
.populate('project'); | ||
// console.log("Fetched trashbins:", trashbins); | ||
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. remove commented out log |
||
|
||
} else { | ||
return res.status(404).json({ message: 'Project not found' }); | ||
} | ||
|
@@ -360,3 +374,80 @@ export const addMultipleTrashItems = async (req: any, res: any, next: any) => { | |
res.status(500).json({ message: error.message }); | ||
} | ||
}; | ||
|
||
export const updateFillLevelChangesCore = async (req: any, res: any): Promise<void> => { | ||
try { | ||
const { hours } = req.body; | ||
// Validate the `hours` input | ||
if (hours !== undefined && (typeof hours !== 'number' || hours < 0)) { | ||
return res.status(400).json({ error: '`hours` must be a positive number or null.' }); | ||
} | ||
|
||
const now: Date = new Date(); | ||
const cutoffDate: Date = hours | ||
? new Date(now.getTime() - hours * 60 * 60 * 1000) | ||
: new Date(0); // Default to epoch if hours is null | ||
// Fetch all trashbins | ||
const trashbins = await Trashbin.find(); | ||
if (!trashbins.length) { | ||
console.warn('No trashbins found.'); | ||
return res.status(404).json({ message: 'No trashbins found.' }); | ||
} | ||
|
||
// Iterate over each trashbin | ||
for (const trashbin of trashbins) { | ||
let totalFillLevelChange = 0; | ||
|
||
if (!trashbin.sensors || trashbin.sensors.length === 0) { | ||
continue; | ||
} | ||
|
||
// Process each sensor for the current trashbin | ||
for (const sensorId of trashbin.sensors) { | ||
// Query to find all history, converting string dates dynamically | ||
const histories = await History.aggregate([ | ||
{ | ||
$match: { | ||
sensor: new Types.ObjectId(sensorId), | ||
measureType: 'fill_level', | ||
$expr: { | ||
$and: [ | ||
{ $gte: [{ $toDate: '$createdAt' }, cutoffDate] }, | ||
{ $lte: [{ $toDate: '$createdAt' }, now] }, | ||
], | ||
}, | ||
}, | ||
}, | ||
{ $sort: { createdAt: 1 } }, // Sort by createdAt ascending | ||
]); | ||
|
||
if (histories.length >= 1) { | ||
const oldestMeasurement = histories[0].measurement; // First entry | ||
const newestMeasurement = histories[histories.length - 1].measurement; // Last entry | ||
const fillLevelChange = newestMeasurement - oldestMeasurement; | ||
|
||
totalFillLevelChange += fillLevelChange; | ||
} else { | ||
console.warn(`No valid history data found for Sensor: ${sensorId}`); | ||
} | ||
} | ||
|
||
// Update trashbin's total fill-level change | ||
if (totalFillLevelChange !== 0) { | ||
await Trashbin.findByIdAndUpdate(trashbin._id, { fillLevelChange: totalFillLevelChange }); | ||
} else { | ||
console.log(`No changes to update for Trashbin: ${trashbin._id}`); | ||
} | ||
} | ||
|
||
res.status(200).json({ message: 'Fill-level changes updated successfully for all trashbins.' }); | ||
} catch (error) { | ||
console.error('Error updating fill-level changes:', error); | ||
res.status(500).json({ error: 'An error occurred while updating fill-level changes.' }); | ||
} | ||
}; | ||
|
||
|
||
|
||
|
||
|
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.
with the new middleware, that you added, we dont need this line anymore, right? How about moving line 25 and 26 to here and removing the bodyparser.json?