Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/controllers/userProfileController.js
Original file line number Diff line number Diff line change
Expand Up @@ -1078,16 +1078,42 @@ const userProfileController = function (UserProfile, Project) {
const current = Array.isArray(user.infringements) ? user.infringements : [];
const old = Array.isArray(user.oldInfringements) ? user.oldInfringements : [];

const infringements = [...old, ...current].sort((a, b) =>
const combined = [...current, ...old];

// build date -> best record
const byDate = new Map();

for (const inf of combined) {
if (!inf?.date) continue;

const existing = byDate.get(inf.date);
if (!existing) {
byDate.set(inf.date, inf);
continue;
}

const a = inf.createdDate ? new Date(inf.createdDate).getTime() : 0;
const b = existing.createdDate ? new Date(existing.createdDate).getTime() : 0;

if (a > b) {
byDate.set(inf.date, inf);
continue;
}

const ida = String(inf._id || '');
const idb = String(existing._id || '');
if (ida > idb) {
byDate.set(inf.date, inf);
}
}

const infringements = Array.from(byDate.values()).sort((a, b) =>
a.date < b.date ? 1 : a.date > b.date ? -1 : 0,
);

user.set('infringements', infringements, { strict: false });
user.set('oldInfringements', undefined, { strict: false });

const hours = await userHelper.getTangibleHoursReportedThisWeekByUserId(userid);
user.set('tangibleHoursReportedThisWeek', hours, { strict: false });

cache.setCache(`user-${userid}`, JSON.stringify(user));
return res.status(200).send(user);
})
Expand Down
Loading