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
+59 -54
View File
@@ -3,6 +3,7 @@ module.exports = function registerAdminScreenCommandRoutes(app, deps) {
const forwardPlayerCommand = deps.forwardPlayerCommand;
const getScreenConnections = deps.getScreenConnections;
const isClientNameAvailable = deps.isClientNameAvailable;
const withClientNameReservation = deps.withClientNameReservation;
app.post('/admin/screens/:slug/commands', async function (req, res, next) {
try {
@@ -56,40 +57,66 @@ module.exports = function registerAdminScreenCommandRoutes(app, deps) {
unchanged: true
});
}
let liveConnections = [];
try {
const [screenSlugs] = await pool.query('SELECT slug FROM screens ORDER BY slug ASC');
const liveResults = await Promise.all((screenSlugs || []).map(async function (row) {
const screenSlug = String(row && row.slug ? row.slug : '').trim();
if (!screenSlug || typeof getScreenConnections !== 'function') {
return [];
}
try {
const liveResponse = await getScreenConnections(screenSlug);
return Array.isArray(liveResponse && liveResponse.connections) ? liveResponse.connections : [];
} catch (_error) {
return [];
}
}));
liveConnections = liveResults.flat();
} catch (_error) {
liveConnections = [];
if (typeof withClientNameReservation !== 'function') {
return res.status(500).json({ error: 'Client name reservation is unavailable.' });
}
const available = typeof isClientNameAvailable === 'function'
? await isClientNameAvailable(pool, clientName, deviceId, liveConnections)
: true;
if (!available) {
return res.status(409).json({ error: 'Client name already exists.' });
}
return withClientNameReservation(pool, clientName, async function () {
let liveConnections = [];
try {
const [screenSlugs] = await pool.query('SELECT slug FROM screens ORDER BY slug ASC');
const liveResults = await Promise.all((screenSlugs || []).map(async function (row) {
const screenSlug = String(row && row.slug ? row.slug : '').trim();
if (!screenSlug || typeof getScreenConnections !== 'function') {
return [];
}
try {
const liveResponse = await getScreenConnections(screenSlug);
return Array.isArray(liveResponse && liveResponse.connections) ? liveResponse.connections : [];
} catch (_error) {
return [];
}
}));
liveConnections = liveResults.flat();
} catch (_error) {
liveConnections = [];
}
if (!onboardingRow) {
await forwardPlayerCommand(slug, {
command: command,
clientName: clientName,
clientId: connectionId || deviceId || null,
deviceId: deviceId || null
}, connectionId || deviceId || undefined);
const available = await isClientNameAvailable(pool, clientName, deviceId, liveConnections);
if (!available) {
return res.status(409).json({ error: 'Client name already exists.' });
}
if (!onboardingRow) {
await forwardPlayerCommand(slug, {
command: command,
clientName: clientName,
clientId: connectionId || deviceId || null,
deviceId: deviceId || null
}, connectionId || deviceId || undefined);
return res.json({
screen: screenRows[0],
screenSlug: slug,
command: command,
connectionId: connectionId || null,
deviceId: deviceId,
clientName: clientName,
ok: true,
liveOnly: true
});
}
const [updateResult] = await pool.query(
`UPDATE player_onboarding_devices pod
JOIN screens s ON s.id = pod.screen_id
SET pod.client_name = ?, pod.modified_at = CURRENT_TIMESTAMP
WHERE s.slug = ? AND pod.device_id = ?`,
[clientName, slug, deviceId]
);
if (!updateResult.affectedRows) {
return res.status(404).json({ error: 'Client not found' });
}
return res.json({
screen: screenRows[0],
@@ -98,30 +125,8 @@ module.exports = function registerAdminScreenCommandRoutes(app, deps) {
connectionId: connectionId || null,
deviceId: deviceId,
clientName: clientName,
ok: true,
liveOnly: true
ok: true
});
}
const [updateResult] = await pool.query(
`UPDATE player_onboarding_devices pod
JOIN screens s ON s.id = pod.screen_id
SET pod.client_name = ?, pod.modified_at = CURRENT_TIMESTAMP
WHERE s.slug = ? AND pod.device_id = ?`,
[clientName, slug, deviceId]
);
if (!updateResult.affectedRows) {
return res.status(404).json({ error: 'Client not found' });
}
return res.json({
screen: screenRows[0],
screenSlug: slug,
command: command,
connectionId: connectionId || null,
deviceId: deviceId,
clientName: clientName,
ok: true
});
}