refactor: move pairing.js:isPaired() to machine-loader.js:getPairedMachineName()

This commit is contained in:
siiky 2025-06-03 11:32:52 +01:00
parent 22b6b64fe4
commit 49080b0412
3 changed files with 11 additions and 13 deletions

View file

@ -173,6 +173,13 @@ function getMachineName(machineId) {
return db.oneOrNone(sql, [machineId]).then(it => it?.name) return db.oneOrNone(sql, [machineId]).then(it => it?.name)
} }
const getPairedMachineName = deviceId =>
db.oneOrNone(
'SELECT name FROM devices WHERE device_id = $1 AND paired = TRUE',
[deviceId],
machine => machine?.name,
)
function getMachine(machineId, config) { function getMachine(machineId, config) {
const sql = `${MACHINE_WITH_CALCULATED_FIELD_SQL} WHERE d.device_id = $1` const sql = `${MACHINE_WITH_CALCULATED_FIELD_SQL} WHERE d.device_id = $1`
@ -750,6 +757,7 @@ const batchRecordPendingPings = () => {
module.exports = { module.exports = {
getMachineName, getMachineName,
getPairedMachineName,
getMachines, getMachines,
getUnpairedMachines, getUnpairedMachines,
getMachine, getMachine,

View file

@ -1,9 +1,8 @@
const pairing = require('../pairing') const { getPairedMachineName } = require('../machine-loader')
const logger = require('../logger') const logger = require('../logger')
const authorize = function (req, res, next) { const authorize = function (req, res, next) {
return pairing return getPairedMachineName(req.deviceId)
.isPaired(req.deviceId)
.then(deviceName => { .then(deviceName => {
if (deviceName) { if (deviceName) {
req.deviceName = deviceName req.deviceName = deviceName

View file

@ -81,13 +81,4 @@ function authorizeCaDownload(caToken) {
}) })
} }
function isPaired(deviceId) { module.exports = { pair, unpair, authorizeCaDownload }
const sql =
'select device_id, name from devices where device_id=$1 and paired=TRUE'
return db
.oneOrNone(sql, [deviceId])
.then(row => (row && row.device_id === deviceId ? row.name : false))
}
module.exports = { pair, unpair, authorizeCaDownload, isPaired }