fix: store operator id for each service

This commit is contained in:
José Oliveira 2021-06-16 11:40:14 +01:00
parent 9b28c6a3f1
commit 8884c75ffd
4 changed files with 14 additions and 9 deletions

View file

@ -132,7 +132,7 @@ function sendRadar (data) {
function mapRecord (rates, settings) { function mapRecord (rates, settings) {
const timestamp = new Date().toISOString() const timestamp = new Date().toISOString()
return Promise.all([getMachines(rates, settings), getOperatorId()]) return Promise.all([getMachines(rates, settings), getOperatorId('coinatmradar')])
.then(([machines, operatorId]) => { .then(([machines, operatorId]) => {
return { return {
operatorId: operatorId, operatorId: operatorId,

View file

@ -1,7 +1,7 @@
const { getOperatorId } = require('../operator') const { getOperatorId } = require('../operator')
function findOperatorId (req, res, next) { function findOperatorId (req, res, next) {
return getOperatorId() return getOperatorId('middleware')
.then(({ id }) => { .then(({ id }) => {
res.locals.operatorId = id res.locals.operatorId = id
return next() return next()

View file

@ -1,7 +1,7 @@
const db = require('./db') const db = require('./db')
function getOperatorId () { function getOperatorId (service) {
const sql = `SELECT id FROM operator_ids WHERE description = 'mnemonic'` const sql = `SELECT operator_id FROM operator_ids WHERE service = '${service}'`
return db.oneOrNone(sql) return db.oneOrNone(sql)
} }

View file

@ -34,14 +34,19 @@ exports.up = function (next) {
const sql = const sql =
[ [
`CREATE TABLE operator_ids ( `CREATE TABLE operator_ids (
id TEXT PRIMARY KEY, id serial PRIMARY KEY,
description TEXT NOT NULL)` operator_id TEXT NOT NULL,
service TEXT NOT NULL
)`
] ]
generateOperatorId() generateOperatorId()
.then(operatorId => { .then(operatorId => {
const sql2 = `INSERT INTO operator_ids (id, description) VALUES ('${operatorId}','mnemonic' )` const sql2 = [
sql.push(sql2) `INSERT INTO operator_ids (operator_id, service) VALUES ('${operatorId}','middleware')`,
db.multi(sql, next) `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()) .then(() => next())
}) })
.catch(e => { .catch(e => {