2016-12-06 09:27:32 +01:00
|
|
|
/* eslint camelcase:off */
|
2020-04-14 22:29:11 +02:00
|
|
|
|
2021-01-06 13:25:25 +01:00
|
|
|
const NotFoundError = require('../error/notfound-error');
|
2016-12-06 09:19:15 +01:00
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
const COLUMNS = [
|
|
|
|
'app_name',
|
|
|
|
'created_at',
|
2021-03-05 14:01:15 +01:00
|
|
|
'created_by',
|
2017-06-28 10:17:14 +02:00
|
|
|
'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,
|
2021-03-05 14:01:15 +01:00
|
|
|
createdBy: row.created_by,
|
2016-12-06 09:19:15 +01:00
|
|
|
url: row.url,
|
|
|
|
color: row.color,
|
|
|
|
icon: row.icon,
|
|
|
|
});
|
|
|
|
|
2021-03-23 12:43:33 +01:00
|
|
|
const remapRow = input => {
|
|
|
|
const temp = {
|
|
|
|
app_name: input.appName,
|
|
|
|
updated_at: input.updatedAt || new Date(),
|
|
|
|
seen_at: input.lastSeen || new Date(),
|
|
|
|
description: input.description,
|
|
|
|
created_by: input.createdBy,
|
|
|
|
announced: input.announced,
|
|
|
|
url: input.url,
|
|
|
|
color: input.color,
|
|
|
|
icon: input.icon,
|
|
|
|
strategies: JSON.stringify(input.strategies),
|
|
|
|
};
|
|
|
|
Object.keys(temp).forEach(k => {
|
|
|
|
if (temp[k] === undefined) {
|
|
|
|
// not using !temp[k] to allow false and null values to get through
|
|
|
|
delete temp[k];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return temp;
|
|
|
|
};
|
2016-12-06 09:19:15 +01:00
|
|
|
|
|
|
|
class ClientApplicationsDb {
|
2019-08-04 11:10:51 +02:00
|
|
|
constructor(db, eventBus) {
|
2016-12-06 09:19:15 +01:00
|
|
|
this.db = db;
|
2019-08-04 11:10:51 +02:00
|
|
|
this.eventBus = eventBus;
|
2016-12-06 09:19:15 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 12:54:13 +01:00
|
|
|
async upsert(details) {
|
2021-03-23 12:43:33 +01:00
|
|
|
const row = remapRow(details);
|
2016-12-06 09:19:15 +01:00
|
|
|
return this.db(TABLE)
|
2021-03-23 12:43:33 +01:00
|
|
|
.insert(row)
|
2021-03-04 12:54:13 +01:00
|
|
|
.onConflict('app_name')
|
|
|
|
.merge();
|
2016-12-06 09:19:15 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 12:54:13 +01:00
|
|
|
async bulkUpsert(apps) {
|
|
|
|
const rows = apps.map(remapRow);
|
|
|
|
return this.db(TABLE)
|
|
|
|
.insert(rows)
|
|
|
|
.onConflict('app_name')
|
|
|
|
.merge();
|
2016-12-06 09:19:15 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 12:54:13 +01:00
|
|
|
async exists({ appName }) {
|
|
|
|
const result = await this.db.raw(
|
|
|
|
`SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE app_name = ?) AS present`,
|
|
|
|
[appName],
|
|
|
|
);
|
|
|
|
const { present } = result.rows[0];
|
|
|
|
return present;
|
2016-12-06 09:19:15 +01:00
|
|
|
}
|
|
|
|
|
2020-09-18 09:05:09 +02:00
|
|
|
async getAll() {
|
|
|
|
const rows = await this.db
|
2017-11-02 09:23:38 +01:00
|
|
|
.select(COLUMNS)
|
|
|
|
.from(TABLE)
|
2020-09-18 09:05:09 +02:00
|
|
|
.orderBy('app_name', 'asc');
|
|
|
|
|
|
|
|
return rows.map(mapRow);
|
2016-12-09 17:30:12 +01:00
|
|
|
}
|
|
|
|
|
2020-09-18 09:05:09 +02:00
|
|
|
async getApplication(appName) {
|
|
|
|
const row = await this.db
|
2017-06-28 10:17:14 +02:00
|
|
|
.select(COLUMNS)
|
|
|
|
.where('app_name', appName)
|
|
|
|
.from(TABLE)
|
2020-09-18 09:05:09 +02:00
|
|
|
.first();
|
|
|
|
|
2021-01-06 13:25:25 +01:00
|
|
|
if (!row) {
|
|
|
|
throw new NotFoundError(`Could not find appName=${appName}`);
|
|
|
|
}
|
|
|
|
|
2020-09-18 09:05:09 +02:00
|
|
|
return mapRow(row);
|
2016-12-09 17:30:12 +01:00
|
|
|
}
|
|
|
|
|
2020-09-25 09:39:12 +02:00
|
|
|
async deleteApplication(appName) {
|
|
|
|
return this.db(TABLE)
|
|
|
|
.where('app_name', appName)
|
|
|
|
.del();
|
|
|
|
}
|
|
|
|
|
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"';
|
|
|
|
*/
|
2020-09-18 09:05:09 +02:00
|
|
|
async getAppsForStrategy(strategyName) {
|
|
|
|
const rows = await this.db.select(COLUMNS).from(TABLE);
|
|
|
|
|
|
|
|
return rows
|
2016-12-09 17:30:12 +01:00
|
|
|
.map(mapRow)
|
2020-09-18 09:05:09 +02:00
|
|
|
.filter(apps =>
|
2020-04-14 22:29:11 +02:00
|
|
|
apps.filter(app => app.strategies.includes(strategyName)),
|
2017-06-28 10:17:14 +02:00
|
|
|
);
|
2016-12-09 17:30:12 +01:00
|
|
|
}
|
|
|
|
|
2020-09-18 09:05:09 +02:00
|
|
|
async getApplications(filter) {
|
2017-06-28 10:17:14 +02:00
|
|
|
return filter && filter.strategyName
|
|
|
|
? this.getAppsForStrategy(filter.strategyName)
|
|
|
|
: this.getAll();
|
2016-12-06 09:19:15 +01:00
|
|
|
}
|
2021-03-05 14:01:15 +01:00
|
|
|
|
|
|
|
async getUnannounced() {
|
|
|
|
const rows = await this.db(TABLE)
|
|
|
|
.select(COLUMNS)
|
|
|
|
.where('announced', false);
|
|
|
|
return rows.map(mapRow);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** *
|
|
|
|
* Updates all rows that have announced = false to announced =true and returns the rows altered
|
|
|
|
* @return {[app]} - Apps that hadn't been announced
|
|
|
|
*/
|
|
|
|
async setUnannouncedToAnnounced() {
|
|
|
|
const rows = await this.db(TABLE)
|
|
|
|
.update({ announced: true })
|
|
|
|
.where('announced', false)
|
|
|
|
.whereNotNull('announced')
|
|
|
|
.returning(COLUMNS);
|
|
|
|
return rows.map(mapRow);
|
|
|
|
}
|
2017-06-28 10:17:14 +02:00
|
|
|
}
|
2016-12-06 09:19:15 +01:00
|
|
|
|
|
|
|
module.exports = ClientApplicationsDb;
|