|
3 | 3 |
|
4 | 4 | const TAG = `[ HACKER.MIDDLEWARE.js ]`; |
5 | 5 | const mongoose = require("mongoose"); |
| 6 | +const ObjectId = mongoose.Types.ObjectId; |
6 | 7 | const { HACKER_REVIEWER_STATUS_NONE } = require("../constants/general.constant"); |
7 | 8 | const { HACKER_REVIEWER_STATUSES } = require("../constants/general.constant"); |
8 | 9 | const { HACKER_REVIEWER_NAMES } = require("../constants/general.constant"); |
@@ -740,6 +741,97 @@ async function updateBatchHacker(req, res, next) { |
740 | 741 | next(); |
741 | 742 | } |
742 | 743 |
|
| 744 | + |
| 745 | +/** |
| 746 | + * Updates a hacker that is specified by req.params.id, and then sets req.email |
| 747 | + * to the email of the hacker, found in Account. |
| 748 | + * Assigns reviewers to hackers in a batch process. |
| 749 | + * @param {*} req |
| 750 | + * @param {*} res |
| 751 | + * @param {*} next |
| 752 | + */ |
| 753 | +async function assignReviewers(req, res, next) { |
| 754 | + try { |
| 755 | + console.log('Starting assignReviewers'); |
| 756 | + |
| 757 | + // const REVIEWER_NAMES = HACKER_REVIEWER_NAMES.filter(name => name !== ''); // get all non-empty reviewer names |
| 758 | + const REVIEWER_NAMES = req.body.names; |
| 759 | + console.log('Reviewer names:', REVIEWER_NAMES); |
| 760 | + |
| 761 | + const cutoff = new Date('2025-11-27T17:23:59.000Z'); // EDIT: set your desired cutoff date here |
| 762 | + const cutoffObjectId = new ObjectId(Math.floor(cutoff.getTime() / 1000).toString(16) + "0000000000000000"); |
| 763 | + |
| 764 | + const hackerModel = require('../models/hacker.model'); |
| 765 | + |
| 766 | + // find all hackers created before the cutoff date |
| 767 | + const hackers = await hackerModel.find({ |
| 768 | + _id: { $lte: cutoffObjectId } |
| 769 | + }).select('_id'); |
| 770 | + |
| 771 | + console.log('Found hackers:', hackers.length); |
| 772 | + |
| 773 | + // get counts |
| 774 | + const hackerCount = hackers.length; |
| 775 | + const revwiewerCount = REVIEWER_NAMES.length; |
| 776 | + |
| 777 | + if (hackerCount === 0) { |
| 778 | + console.log('No hackers found for reviewer assignment.'); |
| 779 | + |
| 780 | + req.body = { |
| 781 | + success: true, |
| 782 | + assigned: 0, |
| 783 | + reviewers: revwiewerCount, |
| 784 | + assignments: [] |
| 785 | + } |
| 786 | + |
| 787 | + return next(); |
| 788 | + } |
| 789 | + |
| 790 | + console.log(`Found ${hackerCount} assignable reviewers.`); |
| 791 | + |
| 792 | + let assignments = []; |
| 793 | + let hackerIndex = 0; |
| 794 | + let updatePromises = []; |
| 795 | + |
| 796 | + // assign reviewers to hackers |
| 797 | + for (const hacker of hackers) { |
| 798 | + const assignedReviewer1 = REVIEWER_NAMES[hackerIndex % revwiewerCount]; |
| 799 | + const assignedReviewer2 = REVIEWER_NAMES[(hackerIndex + 1) % revwiewerCount]; |
| 800 | + |
| 801 | + assignments.push({ hackerId: hacker._id, reviewer: assignedReviewer1, reviewer2: assignedReviewer2 }); |
| 802 | + |
| 803 | + updatePromises.push( |
| 804 | + Services.Hacker.updateOne(hacker._id, { reviewerName: assignedReviewer1, reviewerName2: assignedReviewer2 }) |
| 805 | + ); |
| 806 | + |
| 807 | + hackerIndex++; |
| 808 | + } |
| 809 | + |
| 810 | + // exec all updates |
| 811 | + await Promise.all(updatePromises); |
| 812 | + |
| 813 | + console.log(`Completed reviewer assignment at ${new Date().toISOString()}`); |
| 814 | + console.log(`Assignments:`, assignments); |
| 815 | + |
| 816 | + req.body = { |
| 817 | + success: true, |
| 818 | + assigned: hackerCount, |
| 819 | + reviewers: revwiewerCount, |
| 820 | + assignments: assignments |
| 821 | + } |
| 822 | + |
| 823 | + return next(); |
| 824 | + |
| 825 | + } catch (error) { |
| 826 | + console.error('Error during reviewer assignment:', error); |
| 827 | + return next({ |
| 828 | + status: 500, |
| 829 | + message: Constants.Error.GENERIC_500_MESSAGE, |
| 830 | + data: { error: error } |
| 831 | + }); |
| 832 | + } |
| 833 | +} |
| 834 | + |
743 | 835 | /** |
744 | 836 | * Sets req.body.status to Accepted for next middleware, and store req.params.id as req.hackerId |
745 | 837 | * @param {{params:{id: string}, body: *}} req |
@@ -911,6 +1003,7 @@ module.exports = { |
911 | 1003 | ), |
912 | 1004 | updateHacker: Middleware.Util.asyncMiddleware(updateHacker), |
913 | 1005 | updateBatchHacker: Middleware.Util.asyncMiddleware(updateBatchHacker), |
| 1006 | + assignReviewers: Middleware.Util.asyncMiddleware(assignReviewers), |
914 | 1007 | parseAccept: parseAccept, |
915 | 1008 | parseAcceptBatch: parseAcceptBatch, |
916 | 1009 | parseAcceptEmail: parseAcceptEmail, |
|
0 commit comments