mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 14:20:31 +00:00
fix api global options.
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import express from 'express';
|
||||
import { body, query, validationResult } from 'express-validator';
|
||||
import { pick } from 'lodash';
|
||||
import asyncMiddleware from '@/http/middleware/asyncMiddleware';
|
||||
import Option from '@/models/Option';
|
||||
import jwtAuth from '@/http/middleware/jwtAuth';
|
||||
|
||||
export default {
|
||||
/**
|
||||
@@ -10,13 +12,15 @@ export default {
|
||||
router() {
|
||||
const router = express.Router();
|
||||
|
||||
router.use(jwtAuth);
|
||||
|
||||
router.post('/',
|
||||
this.saveOptions.validation,
|
||||
asyncMiddleware(this.saveOptions.handler));
|
||||
|
||||
router.get('/',
|
||||
this.getOptions.validation,
|
||||
asyncMiddleware(this.getSettings));
|
||||
asyncMiddleware(this.getOptions.handler));
|
||||
|
||||
return router;
|
||||
},
|
||||
@@ -26,7 +30,7 @@ export default {
|
||||
*/
|
||||
saveOptions: {
|
||||
validation: [
|
||||
body('options').isArray(),
|
||||
body('options').isArray({ min: 1 }),
|
||||
body('options.*.key').exists(),
|
||||
body('options.*.value').exists(),
|
||||
body('options.*.group').exists(),
|
||||
@@ -42,12 +46,25 @@ export default {
|
||||
const form = { ...req.body };
|
||||
const optionsCollections = await Option.query();
|
||||
|
||||
const errorReasons = [];
|
||||
const notDefinedOptions = Option.validateDefined(form.options);
|
||||
|
||||
if (notDefinedOptions.length) {
|
||||
errorReasons.push({
|
||||
type: 'OPTIONS.KEY.NOT.DEFINED',
|
||||
code: 200,
|
||||
keys: notDefinedOptions.map(o => ({ ...pick(o, ['key', 'group']) })),
|
||||
});
|
||||
}
|
||||
if (errorReasons.length) {
|
||||
return res.status(400).send({ errors: errorReasons });
|
||||
}
|
||||
form.options.forEach((option) => {
|
||||
optionsCollections.setMeta(option.key, option.value, option.group);
|
||||
optionsCollections.setMeta({ ...option });
|
||||
});
|
||||
await optionsCollections.saveMeta();
|
||||
|
||||
return res.status(200).send();
|
||||
return res.status(200).send({ options: form });
|
||||
},
|
||||
},
|
||||
|
||||
@@ -57,6 +74,7 @@ export default {
|
||||
getOptions: {
|
||||
validation: [
|
||||
query('key').optional(),
|
||||
query('group').optional(),
|
||||
],
|
||||
async handler(req, res) {
|
||||
const validationErrors = validationResult(req);
|
||||
@@ -66,9 +84,17 @@ export default {
|
||||
code: 'VALIDATION_ERROR', ...validationErrors,
|
||||
});
|
||||
}
|
||||
const options = await Option.query();
|
||||
|
||||
return res.status(200).sends({ options });
|
||||
const filter = { ...req.query };
|
||||
const options = await Option.query().onBuild((builder) => {
|
||||
if (filter.key) {
|
||||
builder.where('key', filter.key);
|
||||
}
|
||||
if (filter.group) {
|
||||
builder.where('group', filter.group);
|
||||
}
|
||||
});
|
||||
|
||||
return res.status(200).send({ options: options.metadata });
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user