From e8d8996ae0208542af738a5eb523fa4cdabf3f59 Mon Sep 17 00:00:00 2001 From: Chrystian Huot Date: Mon, 27 Apr 2020 09:55:35 -0400 Subject: [PATCH] Fix missing aliases feature since last code refactoring --- README.md | 2 +- .../rdio-scanner/rdio-scanner.component.ts | 2 +- package.json | 2 +- .../20191220093214-new-v3-tables.js | 42 +++++++++++ .../rdio-scanner/models/rdio-scanner-call.js | 8 ++ .../models/rdio-scanner-system.js | 5 ++ server/lib/rdio-scanner/routes/index.js | 3 + .../routes/trunk-recorder-alias-upload.js | 74 +++++++++++++++++++ .../routes/trunk-recorder-call-upload.js | 2 + server/package-lock.json | 2 +- server/package.json | 2 +- 11 files changed, 139 insertions(+), 5 deletions(-) create mode 100644 server/lib/rdio-scanner/migrations/20191220093214-new-v3-tables.js create mode 100644 server/lib/rdio-scanner/routes/trunk-recorder-alias-upload.js diff --git a/README.md b/README.md index 31a0d18..62712be 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Rdio Scanner v3.1 +# Rdio Scanner v3.1.1 *Rdio Scanner* is a progressive web interface designed to act like an old school police radio scanner. It integrates all frontend / backend components to manage audio files from different sources. diff --git a/client/src/app/components/rdio-scanner/rdio-scanner.component.ts b/client/src/app/components/rdio-scanner/rdio-scanner.component.ts index ea49f71..91485b3 100644 --- a/client/src/app/components/rdio-scanner/rdio-scanner.component.ts +++ b/client/src/app/components/rdio-scanner/rdio-scanner.component.ts @@ -645,7 +645,7 @@ export class AppRdioScannerComponent implements OnDestroy, OnInit { private async loadCall(call: RdioScannerCall = {}): Promise { const query = await this.appRdioScannerCallQuery.fetch({ id: call.id }).toPromise(); - Object.assign(call, query.data.rdioScannerCall); + Object.assign(call, this.transformCall(query.data.rdioScannerCall)); return call; } diff --git a/package.json b/package.json index 221a4e6..27fad51 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rdio-scanner", - "version": "3.1.0", + "version": "3.1.1", "private": true, "main": "run.js", "scripts": { diff --git a/server/lib/rdio-scanner/migrations/20191220093214-new-v3-tables.js b/server/lib/rdio-scanner/migrations/20191220093214-new-v3-tables.js new file mode 100644 index 0000000..afda986 --- /dev/null +++ b/server/lib/rdio-scanner/migrations/20191220093214-new-v3-tables.js @@ -0,0 +1,42 @@ +'use strict'; + +module.exports = { + + up: async (queryInterface, Sequelize) => { + const transaction = await queryInterface.sequelize.transaction(); + + try { + await queryInterface.addColumn('rdioScannerCalls', 'audioName', Sequelize.STRING, { transaction }); + + await queryInterface.addColumn('rdioScannerCalls', 'audioType', Sequelize.STRING, { transaction }); + + await queryInterface.addColumn('rdioScannerSystems', 'aliases', { allowNull: false, defaultValue: [], type: Sequelize.JSON }, { transaction }); + + await transaction.commit(); + + } catch (err) { + await transaction.rollback(); + + throw err; + } + }, + + down: async (queryInterface, Sequelize) => { + const transaction = queryInterface.sequelize.transaction(); + + try { + await transaction.commit(); + + await queryInterface.removeColumn('rdioScannerCalls', 'audioName', Sequelize.STRING, { transaction }); + + await queryInterface.removeColumn('rdioScannerCalls', 'audioType', Sequelize.STRING, { transaction }); + + await queryInterface.removeColumn('rdioScannerSystems', 'aliases', Sequelize.JSON, { transaction }); + + } catch (err) { + await transaction.rollback(); + + throw err; + } + }, +}; diff --git a/server/lib/rdio-scanner/models/rdio-scanner-call.js b/server/lib/rdio-scanner/models/rdio-scanner-call.js index 856fecf..3e27923 100644 --- a/server/lib/rdio-scanner/models/rdio-scanner-call.js +++ b/server/lib/rdio-scanner/models/rdio-scanner-call.js @@ -10,6 +10,14 @@ function rdioScannerCallFactory(sequelize) { type: DataTypes.BLOB('long'), allowNull: false, }, + audioName: { + type: DataTypes.STRING, + allowNull: false, + }, + audioType: { + type: DataTypes.STRING, + allowNull: true, + }, emergency: { type: DataTypes.BOOLEAN, defaultValue: false, diff --git a/server/lib/rdio-scanner/models/rdio-scanner-system.js b/server/lib/rdio-scanner/models/rdio-scanner-system.js index 8b2a8f8..5785d5c 100644 --- a/server/lib/rdio-scanner/models/rdio-scanner-system.js +++ b/server/lib/rdio-scanner/models/rdio-scanner-system.js @@ -6,6 +6,11 @@ class RdioScannerSystem extends Model { } function rdioScannerSystemFactory(sequelize) { RdioScannerSystem.init({ + aliases: { + type: DataTypes.JSON, + defaultValue: [], + allowNull: false, + }, name: { type: DataTypes.STRING, allowNull: false, diff --git a/server/lib/rdio-scanner/routes/index.js b/server/lib/rdio-scanner/routes/index.js index 157b4e3..ae81cb1 100644 --- a/server/lib/rdio-scanner/routes/index.js +++ b/server/lib/rdio-scanner/routes/index.js @@ -1,13 +1,16 @@ 'use strict'; +const TrunkRecorderAliasUpload = require('./trunk-recorder-alias-upload'); const TrunkRecorderCallUpload = require('./trunk-recorder-call-upload'); const TrunkRecorderSystemUpload = require('./trunk-recorder-system-upload'); class Routes { constructor(models, pubsub, router) { + this.trunkRecorderAliasUpload = new TrunkRecorderAliasUpload(models, pubsub); this.trunkRecorderCallUpload = new TrunkRecorderCallUpload(models, pubsub); this.trunkRecorderSystemUpload = new TrunkRecorderSystemUpload(models, pubsub); + router.use(this.trunkRecorderAliasUpload.path, this.trunkRecorderAliasUpload.middleware); router.use(this.trunkRecorderCallUpload.path, this.trunkRecorderCallUpload.middleware); router.use(this.trunkRecorderSystemUpload.path, this.trunkRecorderSystemUpload.middleware); } diff --git a/server/lib/rdio-scanner/routes/trunk-recorder-alias-upload.js b/server/lib/rdio-scanner/routes/trunk-recorder-alias-upload.js new file mode 100644 index 0000000..0441a0d --- /dev/null +++ b/server/lib/rdio-scanner/routes/trunk-recorder-alias-upload.js @@ -0,0 +1,74 @@ +'use strict'; + +const multer = require('multer'); + +const upload = multer({ storage: multer.memoryStorage() }); + +const validateApiKey = require('../helpers/validate-api-keys'); + +class TrunkRecorderAliasUpload { + constructor(models, pubsub) { + return { + path: '/api/trunk-recorder-alias-upload', + + middleware: (req, res) => upload.fields([ + { name: 'csv', maxCount: 1 }, + { name: 'key', maxCount: 1 }, + { name: 'system', maxCount: 1 }, + ])(req, res, async (err) => { + if (err instanceof multer.MulterError) { + res.send(`${err.message}: ${err.field}\n`); + + } else if (err) { + res.send(`${err.message}\n`); + + } else { + const key = req.body.key; + + const system = parseInt(req.body.system, 10); + + const aliases = []; + + if (!validateApiKey(key, system)) { + return res.send(`Api key is invalid for system ${system}.\n`); + } + + const csv = req.files.csv[0].buffer.toString() + .split(/\n/) + .filter((l) => l.length && !/^\s*#/.test(l)); + + for (const line of csv) { + const vals = line.split(','); + + aliases.push({ + name: vals[1], + uid: parseInt(vals[0], 10), + }) + } + + const rec = await models.rdioScannerSystem.findOne({ where: { system } }); + + if (rec) { + try { + await rec.update({ aliases }); + + const systems = await models.rdioScannerSystem.findAll({ order: [['system', 'ASC']] }); + + res.send(`System ${rec.name} updated successfully.\n`); + + pubsub.publish('rdioScannerSystems', { rdioScannerSystems: systems }); + + } catch (err) { + res.send(err.message); + } + + } else { + res.send(`Cannot update aliases for system #${system}.\n`); + } + } + }), + }; + } +} + +module.exports = TrunkRecorderAliasUpload; diff --git a/server/lib/rdio-scanner/routes/trunk-recorder-call-upload.js b/server/lib/rdio-scanner/routes/trunk-recorder-call-upload.js index af682c0..184c1d8 100644 --- a/server/lib/rdio-scanner/routes/trunk-recorder-call-upload.js +++ b/server/lib/rdio-scanner/routes/trunk-recorder-call-upload.js @@ -61,6 +61,8 @@ class TrunkRecorderCallUpload { const data = { audio: req.files.audio[0].buffer, + audioName: req.files.audio[0].originalname, + audioType: req.files.audio[0].mimetype, emergency: !!meta.emergency, freq: parseInt(meta.freq, 10), freqList: Array.isArray(meta.freqList) ? meta.freqList : [], diff --git a/server/package-lock.json b/server/package-lock.json index 5b6bc5b..76594a3 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -1,6 +1,6 @@ { "name": "rdio-scanner-server", - "version": "3.1.0", + "version": "3.1.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/server/package.json b/server/package.json index 270ae99..453a96e 100644 --- a/server/package.json +++ b/server/package.json @@ -1,6 +1,6 @@ { "name": "rdio-scanner-server", - "version": "3.1.0", + "version": "3.1.1", "private": true, "main": "index.js", "scripts": {