From 7a6ced3a98e499596bde8a5e870b6d5bc3e1e140 Mon Sep 17 00:00:00 2001 From: rob Date: Wed, 1 May 2024 01:10:38 -0400 Subject: [PATCH] added data patches directory and client-stats migration --- data/patches/client-stats.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 data/patches/client-stats.js 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