const CLIENT_ID_RE = /^[A-Za-z0-9_-]{16,64}$/; function normalizeEntryClientId(raw) { const value = String(raw == null ? '' : raw).trim(); return CLIENT_ID_RE.test(value) ? value : null; } async function findEntryByClientId(db, clientId) { if (!clientId) return null; const { rows } = await db.query( 'SELECT * FROM entries WHERE client_id = $1 ORDER BY id LIMIT 1', [clientId] ); return rows[0] || null; } async function entryExistsByClientId(db, clientId) { if (!clientId) return false; const { rows } = await db.query('SELECT 1 FROM entries WHERE client_id = $1 LIMIT 1', [clientId]); return rows.length > 0; } module.exports = { normalizeEntryClientId, findEntryByClientId, entryExistsByClientId };