refactor(update-config): introduce updateFirstOrInsert via query builder
This commit is contained in:
@@ -1,21 +1,28 @@
|
||||
import pick from 'lodash/pick.js';
|
||||
import { renderObject } from '../../../../../helpers/renderer.js';
|
||||
import Config from '../../../../../models/config.js';
|
||||
|
||||
export default async (request, response) => {
|
||||
const updatedConfig = await Config.update(configParams(request));
|
||||
const config = await Config.query().updateFirstOrInsert(
|
||||
configParams(request)
|
||||
);
|
||||
|
||||
renderObject(response, updatedConfig);
|
||||
renderObject(response, config);
|
||||
};
|
||||
|
||||
const configParams = (request) => {
|
||||
const updatableConfigurationKeys = [
|
||||
'logoSvgData',
|
||||
'palettePrimaryDark',
|
||||
'palettePrimaryLight',
|
||||
'palettePrimaryMain',
|
||||
'title',
|
||||
];
|
||||
const {
|
||||
logoSvgData,
|
||||
palettePrimaryDark,
|
||||
palettePrimaryLight,
|
||||
palettePrimaryMain,
|
||||
title,
|
||||
} = request.body;
|
||||
|
||||
return pick(request.body, updatableConfigurationKeys);
|
||||
return {
|
||||
logoSvgData,
|
||||
palettePrimaryDark,
|
||||
palettePrimaryLight,
|
||||
palettePrimaryMain,
|
||||
title,
|
||||
};
|
||||
};
|
||||
|
@@ -53,6 +53,18 @@ class ExtendedQueryBuilder extends Model.QueryBuilder {
|
||||
[DELETED_COLUMN_NAME]: null,
|
||||
});
|
||||
}
|
||||
|
||||
async updateFirstOrInsert(data = {}) {
|
||||
let firstRow = await this.first();
|
||||
|
||||
if (firstRow) {
|
||||
return firstRow.$query().patchAndFetch(data);
|
||||
}
|
||||
|
||||
const newInstance = this.insertAndFetch(data);
|
||||
|
||||
return newInstance;
|
||||
}
|
||||
}
|
||||
|
||||
export default ExtendedQueryBuilder;
|
||||
|
Reference in New Issue
Block a user