Fix duplicate onboarding client names

This commit is contained in:
2026-07-21 02:03:09 +01:00
parent 6416dbfd99
commit 7973ee0ea4
5 changed files with 125 additions and 77 deletions
+19 -19
View File
@@ -1,4 +1,4 @@
const { isClientNameAvailable } = require('../client-name-check');
const { isClientNameAvailable, withClientNameReservation } = require('../client-name-check');
const { isTransientDbError } = require('./onboarding-store');
function normalizeDeviceId(value) {
return String(value || '').trim().replace(/[^a-zA-Z0-9_-]/g, '').slice(0, 128);
@@ -48,27 +48,27 @@ async function commitDeviceBinding(pool, deviceId, clientName, screenSlug, isNam
throw new Error('Screen is required.');
}
const [screenRows] = await pool.query('SELECT id, name, slug FROM screens WHERE slug = ?', [normalizedScreenSlug]);
if (!screenRows.length) {
throw new Error('Screen not found.');
}
const screen = screenRows[0];
return withClientNameReservation(pool, normalizedClientName, async function () {
const [screenRows] = await pool.query('SELECT id, name, slug FROM screens WHERE slug = ?', [normalizedScreenSlug]);
if (!screenRows.length) {
throw new Error('Screen not found.');
}
const screen = screenRows[0];
const available = typeof isClientNameAvailableOnScreen === 'function'
? await isClientNameAvailable(pool, normalizedClientName, normalizedDeviceId, liveConnections)
: true;
if (!available) {
const error = new Error('Client name already exists.');
error.statusCode = 400;
throw error;
}
const available = await isClientNameAvailable(pool, normalizedClientName, normalizedDeviceId, liveConnections);
if (!available) {
const error = new Error('Client name already exists.');
error.statusCode = 400;
throw error;
}
await pool.query(
'INSERT INTO player_onboarding_devices (device_id, client_name, screen_id) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE client_name = VALUES(client_name), screen_id = VALUES(screen_id), modified_at = CURRENT_TIMESTAMP',
[normalizedDeviceId, normalizedClientName, screen.id]
);
await pool.query(
'INSERT INTO player_onboarding_devices (device_id, client_name, screen_id) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE client_name = VALUES(client_name), screen_id = VALUES(screen_id), modified_at = CURRENT_TIMESTAMP',
[normalizedDeviceId, normalizedClientName, screen.id]
);
return getOnboardingStatus(pool, normalizedDeviceId);
return getOnboardingStatus(pool, normalizedDeviceId);
});
}
async function bindDeviceToScreen(pool, deviceId, clientName, screenSlug, isNameAvailableOnScreen, playerRuntime, onboardingStore) {