mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 22:30:31 +00:00
feat: application configuration completed flag.
This commit is contained in:
@@ -1,9 +1,13 @@
|
|||||||
import { Router, Request, Response } from 'express';
|
import { Router, Request, Response } from 'express';
|
||||||
import { body, query, validationResult } from 'express-validator';
|
import { body, query } from 'express-validator';
|
||||||
import { pick } from 'lodash';
|
import { pick } from 'lodash';
|
||||||
import { IOptionDTO, IOptionsDTO } from 'interfaces';
|
import { IOptionDTO, IOptionsDTO } from 'interfaces';
|
||||||
import BaseController from 'api/controllers/BaseController';
|
import BaseController from 'api/controllers/BaseController';
|
||||||
import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
||||||
|
import {
|
||||||
|
getDefinedOptions,
|
||||||
|
isDefinedOptionConfigurable,
|
||||||
|
} from 'utils';
|
||||||
|
|
||||||
export default class SettingsController extends BaseController{
|
export default class SettingsController extends BaseController{
|
||||||
/**
|
/**
|
||||||
@@ -14,12 +18,14 @@ export default class SettingsController extends BaseController{
|
|||||||
|
|
||||||
router.post('/',
|
router.post('/',
|
||||||
this.saveSettingsValidationSchema,
|
this.saveSettingsValidationSchema,
|
||||||
asyncMiddleware(this.saveSettings.bind(this)));
|
this.validationResult,
|
||||||
|
asyncMiddleware(this.saveSettings.bind(this)),
|
||||||
|
);
|
||||||
router.get('/',
|
router.get('/',
|
||||||
this.getSettingsSchema,
|
this.getSettingsSchema,
|
||||||
asyncMiddleware(this.getSettings.bind(this)));
|
this.validationResult,
|
||||||
|
asyncMiddleware(this.getSettings.bind(this)),
|
||||||
|
);
|
||||||
return router;
|
return router;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,9 +35,9 @@ export default class SettingsController extends BaseController{
|
|||||||
get saveSettingsValidationSchema() {
|
get saveSettingsValidationSchema() {
|
||||||
return [
|
return [
|
||||||
body('options').isArray({ min: 1 }),
|
body('options').isArray({ min: 1 }),
|
||||||
body('options.*.key').exists(),
|
body('options.*.key').exists().trim().escape().isLength({ min: 1 }),
|
||||||
body('options.*.value').exists(),
|
body('options.*.value').exists().trim().escape().isLength({ min: 1 }),
|
||||||
body('options.*.group').exists(),
|
body('options.*.group').exists().trim().escape().isLength({ min: 1 }),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,11 +46,32 @@ export default class SettingsController extends BaseController{
|
|||||||
*/
|
*/
|
||||||
get getSettingsSchema() {
|
get getSettingsSchema() {
|
||||||
return [
|
return [
|
||||||
query('key').optional(),
|
query('key').optional().trim().escape(),
|
||||||
query('group').optional(),
|
query('group').optional().trim().escape(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Observes application configuration option, whether all options configured
|
||||||
|
* sets `app_configured` option.
|
||||||
|
* @param {Setting} settings
|
||||||
|
*/
|
||||||
|
observeAppConfigsComplete(settings) {
|
||||||
|
if (!settings.get('app_configured', false)) {
|
||||||
|
const definedOptions = getDefinedOptions();
|
||||||
|
|
||||||
|
const isNotConfigured = definedOptions.some((option) => {
|
||||||
|
const isDefined = isDefinedOptionConfigurable(option.key, option.group);
|
||||||
|
const hasStoredOption = settings.get({ key: option.key, group: option.group });
|
||||||
|
|
||||||
|
return (isDefined && !hasStoredOption);
|
||||||
|
});
|
||||||
|
if (!isNotConfigured) {
|
||||||
|
settings.set('app_configured', true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves the given options to the storage.
|
* Saves the given options to the storage.
|
||||||
* @param {Request} req -
|
* @param {Request} req -
|
||||||
@@ -71,6 +98,7 @@ export default class SettingsController extends BaseController{
|
|||||||
optionsDTO.options.forEach((option: IOptionDTO) => {
|
optionsDTO.options.forEach((option: IOptionDTO) => {
|
||||||
settings.set({ ...option });
|
settings.set({ ...option });
|
||||||
});
|
});
|
||||||
|
this.observeAppConfigsComplete(settings);
|
||||||
|
|
||||||
return res.status(200).send({
|
return res.status(200).send({
|
||||||
type: 'success',
|
type: 'success',
|
||||||
|
|||||||
@@ -41,6 +41,8 @@ import Licenses from 'api/controllers/Subscription/Licenses';
|
|||||||
export default () => {
|
export default () => {
|
||||||
const app = Router();
|
const app = Router();
|
||||||
|
|
||||||
|
// - Global routes.
|
||||||
|
// ---------------------------
|
||||||
app.use(I18nMiddleware);
|
app.use(I18nMiddleware);
|
||||||
|
|
||||||
app.use('/auth', Container.get(Authentication).router());
|
app.use('/auth', Container.get(Authentication).router());
|
||||||
@@ -50,6 +52,23 @@ export default () => {
|
|||||||
app.use('/ping', Container.get(Ping).router());
|
app.use('/ping', Container.get(Ping).router());
|
||||||
app.use('/organization', Container.get(Organization).router());
|
app.use('/organization', Container.get(Organization).router());
|
||||||
|
|
||||||
|
// - Settings routes.
|
||||||
|
// ---------------------------
|
||||||
|
const settings = Router();
|
||||||
|
|
||||||
|
settings.use(JWTAuth);
|
||||||
|
settings.use(AttachCurrentTenantUser);
|
||||||
|
settings.use(TenancyMiddleware);
|
||||||
|
settings.use(SubscriptionMiddleware('main'));
|
||||||
|
settings.use(EnsureTenantIsInitialized);
|
||||||
|
settings.use(SettingsMiddleware);
|
||||||
|
|
||||||
|
settings.use('/settings', Container.get(Settings).router());
|
||||||
|
|
||||||
|
app.use('/', settings);
|
||||||
|
|
||||||
|
// - Dashboard routes.
|
||||||
|
// ---------------------------
|
||||||
const dashboard = Router();
|
const dashboard = Router();
|
||||||
|
|
||||||
dashboard.use(JWTAuth);
|
dashboard.use(JWTAuth);
|
||||||
@@ -73,7 +92,6 @@ export default () => {
|
|||||||
dashboard.use('/item_categories', Container.get(ItemCategories).router());
|
dashboard.use('/item_categories', Container.get(ItemCategories).router());
|
||||||
dashboard.use('/expenses', Container.get(Expenses).router());
|
dashboard.use('/expenses', Container.get(Expenses).router());
|
||||||
dashboard.use('/financial_statements', FinancialStatements.router());
|
dashboard.use('/financial_statements', FinancialStatements.router());
|
||||||
dashboard.use('/settings', Container.get(Settings).router());
|
|
||||||
dashboard.use('/sales', Sales.router());
|
dashboard.use('/sales', Sales.router());
|
||||||
dashboard.use('/customers', Container.get(Customers).router());
|
dashboard.use('/customers', Container.get(Customers).router());
|
||||||
dashboard.use('/vendors', Container.get(Vendors).router());
|
dashboard.use('/vendors', Container.get(Vendors).router());
|
||||||
|
|||||||
@@ -5,12 +5,12 @@ export default {
|
|||||||
{
|
{
|
||||||
key: 'name',
|
key: 'name',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
configure: true,
|
config: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'base_currency',
|
key: 'base_currency',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
configure: true,
|
config: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'industry',
|
key: 'industry',
|
||||||
@@ -23,22 +23,22 @@ export default {
|
|||||||
{
|
{
|
||||||
key: 'fiscal_year',
|
key: 'fiscal_year',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
configure: true,
|
// config: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'language',
|
key: 'language',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
configure: true,
|
config: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'time_zone',
|
key: 'time_zone',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
configure: true,
|
// config: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'date_format',
|
key: 'date_format',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
configure: true,
|
// config: true,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import bcrypt from 'bcryptjs';
|
import bcrypt from 'bcryptjs';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
import definedOptions from 'data/options';
|
||||||
|
|
||||||
const hashPassword = (password) =>
|
const hashPassword = (password) =>
|
||||||
new Promise((resolve) => {
|
new Promise((resolve) => {
|
||||||
@@ -155,6 +156,29 @@ const formatDateFields = (inputDTO, fields, format = 'YYYY-MM-DD') => {
|
|||||||
return _inputDTO;
|
return _inputDTO;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getDefinedOptions = () => {
|
||||||
|
const options = [];
|
||||||
|
|
||||||
|
Object.keys(definedOptions).forEach((groupKey) => {
|
||||||
|
const groupOptions = definedOptions[groupKey];
|
||||||
|
groupOptions.forEach((option) => {
|
||||||
|
options.push({ ...option, group: groupKey });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return options;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getDefinedOption = (key, group) => {
|
||||||
|
return definedOptions?.[group]?.find(option => option.key == key);
|
||||||
|
};
|
||||||
|
|
||||||
|
const isDefinedOptionConfigurable = (key, group) => {
|
||||||
|
const definedOption = getDefinedOption(key, group);
|
||||||
|
console.log(definedOption, 'definedOption');
|
||||||
|
|
||||||
|
return definedOption?.config || false;
|
||||||
|
};
|
||||||
|
|
||||||
export {
|
export {
|
||||||
hashPassword,
|
hashPassword,
|
||||||
origin,
|
origin,
|
||||||
@@ -168,4 +192,8 @@ export {
|
|||||||
getTotalDeep,
|
getTotalDeep,
|
||||||
applyMixins,
|
applyMixins,
|
||||||
formatDateFields,
|
formatDateFields,
|
||||||
|
|
||||||
|
isDefinedOptionConfigurable,
|
||||||
|
getDefinedOption,
|
||||||
|
getDefinedOptions,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user