'use strict'; /* globals db */ const clients = db.clients.find(); while (clients.hasNext()) { let timeWorked = 0, billable = 0; const client = clients.next(); const sessions = db.tasksessions.find({ client: client._id }); while (sessions.hasNext()) { const session = sessions.next(); timeWorked += session.duration; billable += session.hourlyRate * (session.duration / 3600); } /* * Fix some JavaScript goofiness with numbers (round correctly to 2nd decimal * digit). */ billable = Math.round((billable + Number.EPSILON) * 100) / 100; print(`client: ${client._id}:${client.name} time:${timeWorked} bill:${billable}`); db.clients.updateOne( { _id: client._id }, { $set: { weeklyTotals: { timeWorked, billable, }, }, }, ); }