From 46c8d83dc178d51d4546ef3459aec60f38c4a3da Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Fri, 9 Dec 2016 16:25:18 +0100 Subject: [PATCH] A client-register should upsert client_applications table --- lib/db/client-applications-store.js | 5 ++++- lib/routes/metrics.js | 17 +++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/db/client-applications-store.js b/lib/db/client-applications-store.js index bfbb08373c..6f4c4eec8f 100644 --- a/lib/db/client-applications-store.js +++ b/lib/db/client-applications-store.js @@ -1,7 +1,8 @@ /* eslint camelcase:off */ 'use strict'; -const COLUMNS = ['app_name', 'created_at', 'updated_at', 'description', 'url', 'color', 'icon']; +const COLUMNS = [ + 'app_name', 'created_at', 'updated_at', 'description', 'strategies', 'url', 'color', 'icon']; const TABLE = 'client_applications'; const mapRow = (row) => ({ @@ -9,6 +10,7 @@ const mapRow = (row) => ({ createdAt: row.created_at, updatedAt: row.updated_at, description: row.description, + strategies: row.strategies, url: row.url, color: row.color, icon: row.icon, @@ -21,6 +23,7 @@ const remapRow = (input, old = {}) => ({ url: input.url || old.url, color: input.color || old.color, icon: input.icon || old.icon, + strategies: JSON.stringify(input.strategies || old.strategies), }); diff --git a/lib/routes/metrics.js b/lib/routes/metrics.js index 2c5bd471fb..52b921c39c 100644 --- a/lib/routes/metrics.js +++ b/lib/routes/metrics.js @@ -77,22 +77,19 @@ module.exports = function (app, config) { app.post('/client/register', (req, res) => { const data = req.body; - const clientIp = req.ip; - joi.validate(data, clientRegisterSchema, (err, cleaned) => { + joi.validate(data, clientRegisterSchema, (err, clientRegistration) => { if (err) { return res.status(400).json(err); } - clientStrategyStore - .insert(cleaned.appName, cleaned.strategies) - .then(() => clientInstanceStore.insert({ - appName: cleaned.appName, - instanceId: cleaned.instanceId, - clientIp, - })) + clientRegistration.clientIp = req.ip; + + clientApplicationsStore + .upsert(clientRegistration) + .then(() => clientInstanceStore.insert(clientRegistration)) .then(() => logger.info(`New client registered with - appName=${cleaned.appName} and instanceId=${cleaned.instanceId}`)) + appName=${clientRegistration.appName} and instanceId=${clientRegistration.instanceId}`)) .catch(err => logger.error('failed to register client', err)); res.status(202).end();