26 lines
904 B
JavaScript
26 lines
904 B
JavaScript
'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
|
|
};
|