Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | 1x 1x 1x 1x 1x 1x 1x 6x 6x 5x 5x 4x 4x 1x 1x 3x 3x 2x 2x 2x 1x 2x 1x 4x 4x 4x 4x 1x 3x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* Admin temporary unique ID routes.
*
* GET /admin/users/check-id/:id → Check if a unique ID is available
* POST /admin/users/:uniqueId/temp-id → Set a temporary unique ID
* DELETE /admin/users/:uniqueId/temp-id → Clear a temporary unique ID
*/
const router = require('express').Router();
const { db, FieldValue } = require('../utils/firebase');
const { requireAdmin } = require('../middleware/auth');
const { generateId, now } = require('../utils/helpers');
const { sendSystemPm } = require('../utils/system-pm');
const log = require('../utils/log');
// ── Check ID availability ──
router.get('/admin/users/check-id/:id', async (req, res) => {
try {
if (requireAdmin(req, res)) return;
const id = Number.parseInt(req.params.id, 10);
if (!id || id < 10000000) return res.status(400).json({ error: 'Invalid ID' });
// Check real uniqueIds
const realSnap = await db.collection('users').where('uniqueId', '==', id).limit(1).get();
if (!realSnap.empty) {
const user = realSnap.docs[0].data();
return res.json({ available: false, conflictType: 'real', conflictUser: user.uniqueId });
}
// Check active temp IDs
const tempSnap = await db.collection('users').where('tempUniqueId', '==', id).limit(1).get();
if (!tempSnap.empty) {
const user = tempSnap.docs[0].data();
const expiry = user.tempUniqueIdExpiry;
if (expiry && expiry > Date.now()) {
return res.json({ available: false, conflictType: 'temp', conflictUser: user.uniqueId });
}
}
res.json({ available: true });
} catch (err) {
log.error('admin-temp-id', 'Check ID failed', { id: req.params.id, error: err.message });
res.status(500).json({ error: 'Internal server error' });
}
});
// ── Set temporary unique ID ──
router.post('/admin/users/:uniqueId/temp-id', async (req, res) => {
try {
Iif (requireAdmin(req, res)) return;
const { tempUniqueId, expiryDate } = req.body || {};
if (!tempUniqueId || tempUniqueId < 10000000) {
return res.status(400).json({ error: 'Invalid ID' });
}
if (!expiryDate) {
return res.status(400).json({ error: 'Expiry date is required' });
}
// Availability check
const realSnap = await db
.collection('users')
.where('uniqueId', '==', tempUniqueId)
.limit(1)
.get();
if (!realSnap.empty) {
return res.status(409).json({ error: 'ID is in use as a real unique ID' });
}
const tempSnap = await db
.collection('users')
.where('tempUniqueId', '==', tempUniqueId)
.limit(1)
.get();
Iif (!tempSnap.empty) {
const user = tempSnap.docs[0].data();
if (user.tempUniqueIdExpiry && user.tempUniqueIdExpiry > Date.now()) {
return res.status(409).json({ error: 'ID is in use as an active temp ID' });
}
}
const targetUniqueId = req.params.uniqueId;
const timestamp = now();
await db.doc(`users/${targetUniqueId}`).update({
tempUniqueId,
tempUniqueIdExpiry: expiryDate,
});
// Audit log
await db.doc(`adminAuditLog/${generateId()}`).set({
adminId: req.auth.uid,
action: 'SET_TEMP_ID',
targetUserId: targetUniqueId,
details: `Set temp ID to ${tempUniqueId}, expires ${new Date(expiryDate).toISOString()}`,
createdAt: timestamp,
});
// System PM (fire-and-forget)
const expiryStr = new Date(expiryDate).toLocaleDateString('en-GB', {
day: 'numeric',
month: 'long',
year: 'numeric',
});
sendSystemPm(
targetUniqueId,
`Your display ID has been temporarily changed to ${tempUniqueId}. It will expire on ${expiryStr} and return to your original ID.`,
).catch((err) =>
log.warn('system-pm', 'Failed to send', { uniqueId: targetUniqueId, error: err.message }),
);
log.info('admin-temp-id', 'Temp ID set', {
adminId: req.auth.uid,
targetUniqueId,
tempUniqueId,
expiryDate,
});
res.json({ success: true });
} catch (err) {
log.error('admin-temp-id', 'Set temp ID failed', {
uniqueId: req.params.uniqueId,
error: err.message,
});
res.status(500).json({ error: 'Internal server error' });
}
});
// ── Clear temporary unique ID ──
router.delete('/admin/users/:uniqueId/temp-id', async (req, res) => {
try {
if (requireAdmin(req, res)) return;
const targetUniqueId = req.params.uniqueId;
const timestamp = now();
await db.doc(`users/${targetUniqueId}`).update({
tempUniqueId: FieldValue.delete(),
tempUniqueIdExpiry: FieldValue.delete(),
});
// Audit log
await db.doc(`adminAuditLog/${generateId()}`).set({
adminId: req.auth.uid,
action: 'CLEAR_TEMP_ID',
targetUserId: targetUniqueId,
details: 'Cleared temporary unique ID',
createdAt: timestamp,
});
// System PM (fire-and-forget)
sendSystemPm(targetUniqueId, 'Your display ID has been restored to your original ID.').catch(
(err) =>
log.warn('system-pm', 'Failed to send', { uniqueId: targetUniqueId, error: err.message }),
);
log.info('admin-temp-id', 'Temp ID cleared', { adminId: req.auth.uid, targetUniqueId });
res.json({ success: true });
} catch (err) {
log.error('admin-temp-id', 'Clear temp ID failed', {
uniqueId: req.params.uniqueId,
error: err.message,
});
res.status(500).json({ error: 'Internal server error' });
}
});
module.exports = router;
|