diff --git a/data/patches/client-stats.js b/data/patches/client-stats.js new file mode 100644 index 0000000..614eaa5 --- /dev/null +++ b/data/patches/client-stats.js @@ -0,0 +1,35 @@ +'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, + }, + }, + }, + ); +} \ No newline at end of file