From 8884c75ffd41aa2366b9bc54b73197d8aff6ef19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira?= Date: Wed, 16 Jun 2021 11:40:14 +0100 Subject: [PATCH] fix: store operator id for each service --- lib/coinatmradar/coinatmradar.js | 2 +- lib/middlewares/operatorId.js | 2 +- lib/operator.js | 4 ++-- migrations/1623413776161-create-operator-ids.js | 15 ++++++++++----- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/coinatmradar/coinatmradar.js b/lib/coinatmradar/coinatmradar.js index 55fbf5c6..64660f4e 100644 --- a/lib/coinatmradar/coinatmradar.js +++ b/lib/coinatmradar/coinatmradar.js @@ -132,7 +132,7 @@ function sendRadar (data) { function mapRecord (rates, settings) { const timestamp = new Date().toISOString() - return Promise.all([getMachines(rates, settings), getOperatorId()]) + return Promise.all([getMachines(rates, settings), getOperatorId('coinatmradar')]) .then(([machines, operatorId]) => { return { operatorId: operatorId, diff --git a/lib/middlewares/operatorId.js b/lib/middlewares/operatorId.js index 04548aaf..663b7527 100644 --- a/lib/middlewares/operatorId.js +++ b/lib/middlewares/operatorId.js @@ -1,7 +1,7 @@ const { getOperatorId } = require('../operator') function findOperatorId (req, res, next) { - return getOperatorId() + return getOperatorId('middleware') .then(({ id }) => { res.locals.operatorId = id return next() diff --git a/lib/operator.js b/lib/operator.js index a39eaeed..52e64217 100644 --- a/lib/operator.js +++ b/lib/operator.js @@ -1,7 +1,7 @@ const db = require('./db') -function getOperatorId () { - const sql = `SELECT id FROM operator_ids WHERE description = 'mnemonic'` +function getOperatorId (service) { + const sql = `SELECT operator_id FROM operator_ids WHERE service = '${service}'` return db.oneOrNone(sql) } diff --git a/migrations/1623413776161-create-operator-ids.js b/migrations/1623413776161-create-operator-ids.js index d48fcb1e..b7f27830 100644 --- a/migrations/1623413776161-create-operator-ids.js +++ b/migrations/1623413776161-create-operator-ids.js @@ -34,14 +34,19 @@ exports.up = function (next) { const sql = [ `CREATE TABLE operator_ids ( - id TEXT PRIMARY KEY, - description TEXT NOT NULL)` + id serial PRIMARY KEY, + operator_id TEXT NOT NULL, + service TEXT NOT NULL + )` ] generateOperatorId() .then(operatorId => { - const sql2 = `INSERT INTO operator_ids (id, description) VALUES ('${operatorId}','mnemonic' )` - sql.push(sql2) - db.multi(sql, next) + const sql2 = [ + `INSERT INTO operator_ids (operator_id, service) VALUES ('${operatorId}','middleware')`, + `INSERT INTO operator_ids (operator_id, service) VALUES ('${operatorId}','coinatmradar')`, + `INSERT INTO operator_ids (operator_id, service) VALUES ('${operatorId}','authentication')` + ] + db.multi(sql.concat(sql2), next) .then(() => next()) }) .catch(e => {