Skip to content

Commit

Permalink
Merge pull request #1093 from OneCommunityGlobal/huijie-recalculation…
Browse files Browse the repository at this point in the history
…-tangible-hours-quick-fix

Huijie - fix recalculation for tangible hours
  • Loading branch information
one-community authored Sep 12, 2024
2 parents bdd1940 + 50e6f19 commit b5d5536
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/controllers/timeEntryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -1373,8 +1373,15 @@ const timeEntrycontroller = function (TimeEntry) {
const recalculateHoursByCategoryAllUsers = async function (req, res) {
const session = await mongoose.startSession();
session.startTransaction();
let keepAliveInterval;

try {
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Transfer-Encoding', 'chunked');
keepAliveInterval = setInterval(() => {
res.write('Processing... keep connection alive\n');
}, 150 * 1000); // interval of 150 seconds

const userprofiles = await UserProfile.find({}, '_id').lean();

const recalculationPromises = userprofiles.map(async (userprofile) => {
Expand All @@ -1385,13 +1392,17 @@ const timeEntrycontroller = function (TimeEntry) {
await Promise.all(recalculationPromises);

await session.commitTransaction();
return res.status(200).send({
message: 'finished the recalculation for hoursByCategory for all users',
});
clearInterval(keepAliveInterval);
res.write('finished the recalculation for hoursByCategory for all users\n');
return res.end();
} catch (err) {
await session.abortTransaction();
if (keepAliveInterval) {
clearInterval(keepAliveInterval);
}
logger.logException(err);
return res.status(500).send({ error: err.toString() });
res.write(`error: ${err.toString()}\n`);
return res.end();
} finally {
session.endSession();
}
Expand Down

0 comments on commit b5d5536

Please sign in to comment.