feat: auto generate placeholder nationalId for users and professors, auto calculate class end date

This commit is contained in:
2026-08-21 15:03:18 +03:30
parent cbcf3f55db
commit 30db6582b7
6 changed files with 157 additions and 21 deletions
+27 -1
View File
@@ -109,6 +109,31 @@ const buildClassScheduleContext = (classDoc, sessions = [], currentSession = nul
};
};
const calculateClassEndDate = (startDate, days = [], numberOfSessions = 0) => {
const date = toDate(startDate);
const countNeeded = parseInt(numberOfSessions, 10);
if (!date || isNaN(countNeeded) || countNeeded < 1) return null;
const validDays = normalizeWeekdays(days);
if (!validDays.length) return null;
let current = new Date(date);
let count = 0;
let lastMatchingDate = null;
let safetyLimit = 365 * 5;
while (count < countNeeded && safetyLimit > 0) {
safetyLimit--;
if (validDays.includes(current.getDay())) {
count++;
lastMatchingDate = new Date(current);
}
if (count >= countNeeded) break;
current.setDate(current.getDate() + 1);
}
return lastMatchingDate;
};
module.exports = {
IRAN_WEEK_ORDER,
PERSIAN_WEEKDAYS,
@@ -118,5 +143,6 @@ module.exports = {
formatClassStartDate,
normalizeWeekdays,
normalizeClockTime,
buildClassScheduleContext
buildClassScheduleContext,
calculateClassEndDate
};
+25
View File
@@ -0,0 +1,25 @@
'use strict';
const User = require('../components/users/userModel');
const Professor = require('../components/professors/professorModel');
/**
* Generates a unique 10-character placeholder national ID (starting with TMP).
* @param {string} [seed] - Optional seed like phone number or timestamp
* @returns {Promise<string>}
*/
const allocatePlaceholderNationalId = async (seed) => {
const cleanDigits = String(seed || '').replace(/\D/g, '');
const baseSuffix = (cleanDigits.slice(-7) || Date.now().toString().slice(-7)).padStart(7, '0').slice(0, 7);
let candidate = `TMP${baseSuffix}`.slice(0, 10);
let i = 0;
while ((await User.exists({ nationalIdCode: candidate })) || (await Professor.exists({ nationalIdCode: candidate }))) {
i += 1;
candidate = `TMP${String(i).padStart(7, '0')}`.slice(0, 10);
}
return candidate;
};
module.exports = {
allocatePlaceholderNationalId
};