Add sessionHolding template, unique 8-digit codes, reason slot for sessionCancelled, and disable unused notifications

This commit is contained in:
2026-08-21 10:44:51 +03:30
parent 5328f36f06
commit 091e519280
28 changed files with 2180 additions and 280 deletions
+30
View File
@@ -0,0 +1,30 @@
'use strict';
const crypto = require('crypto');
const MIN_CODE = 10000000;
const MAX_CODE = 100000000;
/** Generate a random 8-digit numeric string (1000000099999999). */
const generateCode = () => String(crypto.randomInt(MIN_CODE, MAX_CODE));
/**
* Allocate a unique code for a Mongoose model field.
* Retries on collision up to maxAttempts times.
*/
const generateUniqueCode = async (Model, field = 'uniqueCode', maxAttempts = 24) => {
if (!Model) throw new Error('Model is required to generate a unique code');
for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
const code = generateCode();
const exists = await Model.exists({ [field]: code });
if (!exists) return code;
}
throw new Error(`Could not generate a unique ${field} after ${maxAttempts} attempts`);
};
module.exports = {
generateCode,
generateUniqueCode
};