2016-12-06 09:27:32 +01:00
|
|
|
/* eslint camelcase:off */
|
2016-12-06 09:19:15 +01:00
|
|
|
'use strict';
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
const COLUMNS = [
|
|
|
|
'app_name',
|
|
|
|
'created_at',
|
|
|
|
'updated_at',
|
|
|
|
'description',
|
|
|
|
'strategies',
|
|
|
|
'url',
|
|
|
|
'color',
|
|
|
|
'icon',
|
|
|
|
];
|
2016-12-06 09:19:15 +01:00
|
|
|
const TABLE = 'client_applications';
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
const mapRow = row => ({
|
2016-12-06 09:19:15 +01:00
|
|
|
appName: row.app_name,
|
|
|
|
createdAt: row.created_at,
|
|
|
|
updatedAt: row.updated_at,
|
|
|
|
description: row.description,
|
2016-12-09 16:25:18 +01:00
|
|
|
strategies: row.strategies,
|
2016-12-06 09:19:15 +01:00
|
|
|
url: row.url,
|
|
|
|
color: row.color,
|
|
|
|
icon: row.icon,
|
|
|
|
});
|
|
|
|
|
|
|
|
const remapRow = (input, old = {}) => ({
|
|
|
|
app_name: input.appName,
|
|
|
|
updated_at: input.updatedAt,
|
|
|
|
description: input.description || old.description,
|
|
|
|
url: input.url || old.url,
|
|
|
|
color: input.color || old.color,
|
|
|
|
icon: input.icon || old.icon,
|
2016-12-09 16:25:18 +01:00
|
|
|
strategies: JSON.stringify(input.strategies || old.strategies),
|
2016-12-06 09:19:15 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
class ClientApplicationsDb {
|
2017-06-28 10:17:14 +02:00
|
|
|
constructor(db) {
|
2016-12-06 09:19:15 +01:00
|
|
|
this.db = db;
|
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
updateRow(details, prev) {
|
2016-12-06 09:19:15 +01:00
|
|
|
details.updatedAt = 'now()';
|
|
|
|
return this.db(TABLE)
|
|
|
|
.where('app_name', details.appName)
|
|
|
|
.update(remapRow(details, prev));
|
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
insertNewRow(details) {
|
2016-12-06 09:19:15 +01:00
|
|
|
return this.db(TABLE).insert(remapRow(details));
|
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
upsert(data) {
|
2016-12-06 09:19:15 +01:00
|
|
|
if (!data) {
|
|
|
|
throw new Error('Missing data to add / update');
|
|
|
|
}
|
|
|
|
return this.db(TABLE)
|
|
|
|
.select(COLUMNS)
|
|
|
|
.where('app_name', data.appName)
|
|
|
|
.then(result => {
|
|
|
|
if (result && result[0]) {
|
|
|
|
return this.updateRow(data, result[0]);
|
|
|
|
} else {
|
|
|
|
return this.insertNewRow(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
getAll() {
|
2017-11-02 09:23:38 +01:00
|
|
|
return this.db
|
|
|
|
.select(COLUMNS)
|
|
|
|
.from(TABLE)
|
|
|
|
.map(mapRow);
|
2016-12-09 17:30:12 +01:00
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
getApplication(appName) {
|
2016-12-09 17:30:12 +01:00
|
|
|
return this.db
|
2017-06-28 10:17:14 +02:00
|
|
|
.select(COLUMNS)
|
|
|
|
.where('app_name', appName)
|
|
|
|
.from(TABLE)
|
|
|
|
.map(mapRow)
|
|
|
|
.then(list => list[0]);
|
2016-12-09 17:30:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Could also be done in SQL:
|
|
|
|
* (not sure if it is faster though)
|
|
|
|
*
|
|
|
|
* SELECT app_name from (
|
|
|
|
* SELECT app_name, json_array_elements(strategies)::text as strategyName from client_strategies
|
|
|
|
* ) as foo
|
|
|
|
* WHERE foo.strategyName = '"other"';
|
|
|
|
*/
|
2017-06-28 10:17:14 +02:00
|
|
|
getAppsForStrategy(strategyName) {
|
2016-12-06 09:19:15 +01:00
|
|
|
return this.db
|
2016-12-09 22:01:43 +01:00
|
|
|
.select(COLUMNS)
|
2016-12-06 09:19:15 +01:00
|
|
|
.from(TABLE)
|
2016-12-09 17:30:12 +01:00
|
|
|
.map(mapRow)
|
2017-06-28 10:17:14 +02:00
|
|
|
.then(apps =>
|
|
|
|
apps.filter(app => app.strategies.includes(strategyName))
|
|
|
|
);
|
2016-12-09 17:30:12 +01:00
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
getApplications(filter) {
|
|
|
|
return filter && filter.strategyName
|
|
|
|
? this.getAppsForStrategy(filter.strategyName)
|
|
|
|
: this.getAll();
|
2016-12-06 09:19:15 +01:00
|
|
|
}
|
2017-06-28 10:17:14 +02:00
|
|
|
}
|
2016-12-06 09:19:15 +01:00
|
|
|
|
|
|
|
module.exports = ClientApplicationsDb;
|