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
+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
};