refactor(update-config): move logic to config model
This commit is contained in:
@@ -4,35 +4,8 @@ import Config from '../../../../../models/config.js';
|
|||||||
|
|
||||||
export default async (request, response) => {
|
export default async (request, response) => {
|
||||||
const config = configParams(request);
|
const config = configParams(request);
|
||||||
const configKeys = Object.keys(config);
|
|
||||||
const updates = [];
|
|
||||||
|
|
||||||
for (const key of configKeys) {
|
await Config.batchUpdate(config);
|
||||||
const newValue = config[key];
|
|
||||||
|
|
||||||
if (newValue) {
|
|
||||||
const entryUpdate = Config.query()
|
|
||||||
.insert({
|
|
||||||
key,
|
|
||||||
value: {
|
|
||||||
data: newValue,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.onConflict('key')
|
|
||||||
.merge({
|
|
||||||
value: {
|
|
||||||
data: newValue,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
updates.push(entryUpdate);
|
|
||||||
} else {
|
|
||||||
const entryUpdate = Config.query().findOne({ key }).delete();
|
|
||||||
updates.push(entryUpdate);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await Promise.all(updates);
|
|
||||||
|
|
||||||
renderObject(response, config);
|
renderObject(response, config);
|
||||||
};
|
};
|
||||||
|
@@ -15,14 +15,14 @@ class Config extends Base {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static async isInstallationCompleted() {
|
static async isInstallationCompleted() {
|
||||||
const installationCompletedEntry = await this
|
const installationCompletedEntry = await this.query()
|
||||||
.query()
|
|
||||||
.where({
|
.where({
|
||||||
key: 'installation.completed'
|
key: 'installation.completed',
|
||||||
})
|
})
|
||||||
.first();
|
.first();
|
||||||
|
|
||||||
const installationCompleted = installationCompletedEntry?.value?.data === true;
|
const installationCompleted =
|
||||||
|
installationCompletedEntry?.value?.data === true;
|
||||||
|
|
||||||
return installationCompleted;
|
return installationCompleted;
|
||||||
}
|
}
|
||||||
@@ -35,6 +35,38 @@ class Config extends Base {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static async batchUpdate(config) {
|
||||||
|
const configKeys = Object.keys(config);
|
||||||
|
const updates = [];
|
||||||
|
|
||||||
|
for (const key of configKeys) {
|
||||||
|
const newValue = config[key];
|
||||||
|
|
||||||
|
if (newValue) {
|
||||||
|
const entryUpdate = Config.query()
|
||||||
|
.insert({
|
||||||
|
key,
|
||||||
|
value: {
|
||||||
|
data: newValue,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.onConflict('key')
|
||||||
|
.merge({
|
||||||
|
value: {
|
||||||
|
data: newValue,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
updates.push(entryUpdate);
|
||||||
|
} else {
|
||||||
|
const entryUpdate = Config.query().findOne({ key }).delete();
|
||||||
|
updates.push(entryUpdate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return await Promise.all(updates);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Config;
|
export default Config;
|
||||||
|
Reference in New Issue
Block a user