mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-22 07:40:32 +00:00
fix: delete and bulk delete accounts.
fix: activate and inactivate accounts.
This commit is contained in:
@@ -63,6 +63,7 @@ export default function DataTable({
|
|||||||
expandColumnSpace = 1.5,
|
expandColumnSpace = 1.5,
|
||||||
|
|
||||||
updateDebounceTime = 200,
|
updateDebounceTime = 200,
|
||||||
|
selectionColumnWidth = 42,
|
||||||
|
|
||||||
// Read this document to know why! https://bit.ly/2Uw9SEc
|
// Read this document to know why! https://bit.ly/2Uw9SEc
|
||||||
autoResetPage = true,
|
autoResetPage = true,
|
||||||
@@ -134,9 +135,9 @@ export default function DataTable({
|
|||||||
{
|
{
|
||||||
id: 'selection',
|
id: 'selection',
|
||||||
disableResizing: true,
|
disableResizing: true,
|
||||||
minWidth: 42,
|
minWidth: selectionColumnWidth,
|
||||||
width: 42,
|
width: selectionColumnWidth,
|
||||||
maxWidth: 42,
|
maxWidth: selectionColumnWidth,
|
||||||
// The header can use the table's getToggleAllRowsSelectedProps method
|
// The header can use the table's getToggleAllRowsSelectedProps method
|
||||||
// to render a checkbox
|
// to render a checkbox
|
||||||
Header: ({ getToggleAllRowsSelectedProps }) => (
|
Header: ({ getToggleAllRowsSelectedProps }) => (
|
||||||
|
|||||||
@@ -109,14 +109,6 @@ function AccountsChart({
|
|||||||
intent: Intent.DANGER,
|
intent: Intent.DANGER,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (errors.find(e => e.type === 'ACCOUNT.HAS.CHILD.ACCOUNTS')) {
|
|
||||||
AppToaster.show({
|
|
||||||
message: formatMessage({
|
|
||||||
id: 'you_could_not_delete_account_has_child_accounts',
|
|
||||||
}),
|
|
||||||
intent: Intent.DANGER,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle confirm account delete
|
// Handle confirm account delete
|
||||||
@@ -130,6 +122,7 @@ function AccountsChart({
|
|||||||
}),
|
}),
|
||||||
intent: Intent.SUCCESS,
|
intent: Intent.SUCCESS,
|
||||||
});
|
});
|
||||||
|
queryCache.invalidateQueries('accounts-table');
|
||||||
})
|
})
|
||||||
.catch((errors) => {
|
.catch((errors) => {
|
||||||
setDeleteAccount(false);
|
setDeleteAccount(false);
|
||||||
@@ -215,6 +208,7 @@ function AccountsChart({
|
|||||||
}),
|
}),
|
||||||
intent: Intent.SUCCESS,
|
intent: Intent.SUCCESS,
|
||||||
});
|
});
|
||||||
|
queryCache.invalidateQueries('accounts-table');
|
||||||
})
|
})
|
||||||
.catch((errors) => {
|
.catch((errors) => {
|
||||||
setBulkDelete(false);
|
setBulkDelete(false);
|
||||||
@@ -288,6 +282,7 @@ function AccountsChart({
|
|||||||
}),
|
}),
|
||||||
intent: Intent.SUCCESS,
|
intent: Intent.SUCCESS,
|
||||||
});
|
});
|
||||||
|
queryCache.invalidateQueries('accounts-table');
|
||||||
})
|
})
|
||||||
.catch((errors) => {
|
.catch((errors) => {
|
||||||
setBulkActivate(false);
|
setBulkActivate(false);
|
||||||
@@ -318,6 +313,7 @@ function AccountsChart({
|
|||||||
}),
|
}),
|
||||||
intent: Intent.SUCCESS,
|
intent: Intent.SUCCESS,
|
||||||
});
|
});
|
||||||
|
queryCache.invalidateQueries('accounts-table');
|
||||||
})
|
})
|
||||||
.catch((errors) => {
|
.catch((errors) => {
|
||||||
setBulkInactiveAccounts(false);
|
setBulkInactiveAccounts(false);
|
||||||
|
|||||||
@@ -192,10 +192,7 @@ function AccountsDataTable({
|
|||||||
[actionMenuList, formatMessage],
|
[actionMenuList, formatMessage],
|
||||||
);
|
);
|
||||||
|
|
||||||
const selectionColumn = useMemo(
|
|
||||||
() => ({ minWidth: 40, width: 40, maxWidth: 40 }),
|
|
||||||
[],
|
|
||||||
);
|
|
||||||
|
|
||||||
const handleDatatableFetchData = useCallback((...params) => {
|
const handleDatatableFetchData = useCallback((...params) => {
|
||||||
onFetchData && onFetchData(...params);
|
onFetchData && onFetchData(...params);
|
||||||
@@ -216,14 +213,15 @@ function AccountsDataTable({
|
|||||||
columns={columns}
|
columns={columns}
|
||||||
data={accountsTable}
|
data={accountsTable}
|
||||||
onFetchData={handleDatatableFetchData}
|
onFetchData={handleDatatableFetchData}
|
||||||
manualSortBy={true}
|
selectionColumn={true}
|
||||||
selectionColumn={selectionColumn}
|
|
||||||
expandable={true}
|
expandable={true}
|
||||||
sticky={true}
|
sticky={true}
|
||||||
onSelectedRowsChange={handleSelectedRowsChange}
|
onSelectedRowsChange={handleSelectedRowsChange}
|
||||||
loading={accountsLoading && !isMounted}
|
loading={accountsLoading && !isMounted}
|
||||||
rowContextMenu={rowContextMenu}
|
rowContextMenu={rowContextMenu}
|
||||||
expandColumnSpace={1}
|
expandColumnSpace={1}
|
||||||
|
autoResetExpanded={false}
|
||||||
|
selectionColumnWidth={50}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -16,11 +16,6 @@
|
|||||||
padding-bottom: 0.3rem;
|
padding-bottom: 0.3rem;
|
||||||
}
|
}
|
||||||
.account_name{
|
.account_name{
|
||||||
> div{
|
|
||||||
width: 100%;
|
|
||||||
font-weight: 500;
|
|
||||||
}
|
|
||||||
|
|
||||||
.bp3-popover-wrapper--inactive-semafro{
|
.bp3-popover-wrapper--inactive-semafro{
|
||||||
margin-left: 8px;
|
margin-left: 8px;
|
||||||
margin-right: 6px;
|
margin-right: 6px;
|
||||||
|
|||||||
@@ -256,7 +256,7 @@ export const flatToNestedArray = (
|
|||||||
if (!item[config.parentId]) {
|
if (!item[config.parentId]) {
|
||||||
nestedArray.push(item);
|
nestedArray.push(item);
|
||||||
}
|
}
|
||||||
if (parentItemId) {
|
if (parentItemId && map[parentItemId]) {
|
||||||
map[parentItemId].children.push(item);
|
map[parentItemId].children.push(item);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -155,7 +155,7 @@ export default class AccountsController extends BaseController{
|
|||||||
|
|
||||||
get bulkSelectIdsQuerySchema() {
|
get bulkSelectIdsQuerySchema() {
|
||||||
return [
|
return [
|
||||||
query('ids').isArray({ min: 2 }),
|
query('ids').isArray({ min: 1 }),
|
||||||
query('ids.*').isNumeric().toInt(),
|
query('ids.*').isNumeric().toInt(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -240,7 +240,11 @@ export default class AccountsController extends BaseController{
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
await this.accountsService.deleteAccount(tenantId, accountId);
|
await this.accountsService.deleteAccount(tenantId, accountId);
|
||||||
return res.status(200).send({ id: accountId });
|
|
||||||
|
return res.status(200).send({
|
||||||
|
id: accountId,
|
||||||
|
message: 'The deleted account has been deleted successfully.',
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
@@ -298,7 +302,12 @@ export default class AccountsController extends BaseController{
|
|||||||
const isActive = (type === 'activate' ? true : false);
|
const isActive = (type === 'activate' ? true : false);
|
||||||
await this.accountsService.activateAccounts(tenantId, accountsIds, isActive);
|
await this.accountsService.activateAccounts(tenantId, accountsIds, isActive);
|
||||||
|
|
||||||
return res.status(200).send({ ids: accountsIds });
|
const activatedText = isActive ? 'activated' : 'inactivated';
|
||||||
|
|
||||||
|
return res.status(200).send({
|
||||||
|
ids: accountsIds,
|
||||||
|
message: `The given accounts have been ${activatedText} successfully`,
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
@@ -316,8 +325,11 @@ export default class AccountsController extends BaseController{
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
await this.accountsService.deleteAccounts(tenantId, accountsIds);
|
await this.accountsService.deleteAccounts(tenantId, accountsIds);
|
||||||
return res.status(200).send({ ids: accountsIds });
|
|
||||||
|
|
||||||
|
return res.status(200).send({
|
||||||
|
ids: accountsIds,
|
||||||
|
message: 'The given accounts have been deleted successfully.',
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
@@ -426,12 +438,6 @@ export default class AccountsController extends BaseController{
|
|||||||
{ errors: [{ type: 'NOT_UNIQUE_CODE', code: 600 }] }
|
{ errors: [{ type: 'NOT_UNIQUE_CODE', code: 600 }] }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (error.errorType === 'account_has_children') {
|
|
||||||
return res.boom.badRequest(
|
|
||||||
'You could not delete account has children.',
|
|
||||||
{ errors: [{ type: 'ACCOUNT.HAS.CHILD.ACCOUNTS', code: 700 }] }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (error.errorType === 'account_has_associated_transactions') {
|
if (error.errorType === 'account_has_associated_transactions') {
|
||||||
return res.boom.badRequest(
|
return res.boom.badRequest(
|
||||||
'You could not delete account has associated transactions.',
|
'You could not delete account has associated transactions.',
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
import { Inject, Service } from 'typedi';
|
||||||
import { difference } from 'lodash';
|
import { difference, chain, uniq } from 'lodash';
|
||||||
import { kebabCase } from 'lodash'
|
import { kebabCase } from 'lodash'
|
||||||
import TenancyService from 'services/Tenancy/TenancyService';
|
import TenancyService from 'services/Tenancy/TenancyService';
|
||||||
import { ServiceError } from 'exceptions';
|
import { ServiceError } from 'exceptions';
|
||||||
@@ -273,23 +273,20 @@ export default class AccountsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Throws error if account has children accounts.
|
* Unlink the given parent account with children accounts.
|
||||||
* @param {number} tenantId
|
* @param {number} tenantId -
|
||||||
* @param {number} accountId
|
* @param {number|number[]} parentAccountId -
|
||||||
*/
|
*/
|
||||||
private async throwErrorIfAccountHasChildren(tenantId: number, accountId: number) {
|
private async unassociateChildrenAccountsFromParent(
|
||||||
|
tenantId: number,
|
||||||
|
parentAccountId: number | number[],
|
||||||
|
) {
|
||||||
const { Account } = this.tenancy.models(tenantId);
|
const { Account } = this.tenancy.models(tenantId);
|
||||||
|
const accountsIds = Array.isArray(parentAccountId) ? parentAccountId : [parentAccountId];
|
||||||
|
|
||||||
this.logger.info('[account] validating if the account has children.', {
|
await Account.query()
|
||||||
tenantId, accountId,
|
.whereIn('parent_account_id', accountsIds)
|
||||||
});
|
.patch({ parent_account_id: null });
|
||||||
const childAccounts = await Account.query().where(
|
|
||||||
'parent_account_id',
|
|
||||||
accountId,
|
|
||||||
);
|
|
||||||
if (childAccounts.length > 0) {
|
|
||||||
throw new ServiceError('account_has_children');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -316,11 +313,15 @@ export default class AccountsService {
|
|||||||
const { accountRepository } = this.tenancy.repositories(tenantId);
|
const { accountRepository } = this.tenancy.repositories(tenantId);
|
||||||
const account = await this.getAccountOrThrowError(tenantId, accountId);
|
const account = await this.getAccountOrThrowError(tenantId, accountId);
|
||||||
|
|
||||||
|
// Throw error if the account was predefined.
|
||||||
this.throwErrorIfAccountPredefined(account);
|
this.throwErrorIfAccountPredefined(account);
|
||||||
|
|
||||||
await this.throwErrorIfAccountHasChildren(tenantId, accountId);
|
// Throw error if the account has transactions.
|
||||||
await this.throwErrorIfAccountHasTransactions(tenantId, accountId);
|
await this.throwErrorIfAccountHasTransactions(tenantId, accountId);
|
||||||
|
|
||||||
|
// Unlink the parent account from children accounts.
|
||||||
|
await this.unassociateChildrenAccountsFromParent(tenantId, accountId);
|
||||||
|
|
||||||
await accountRepository.deleteById(account.id);
|
await accountRepository.deleteById(account.id);
|
||||||
this.logger.info('[account] account has been deleted successfully.', {
|
this.logger.info('[account] account has been deleted successfully.', {
|
||||||
tenantId, accountId,
|
tenantId, accountId,
|
||||||
@@ -336,7 +337,10 @@ export default class AccountsService {
|
|||||||
* @param {number[]} accountsIds
|
* @param {number[]} accountsIds
|
||||||
* @return {IAccount[]}
|
* @return {IAccount[]}
|
||||||
*/
|
*/
|
||||||
private async getAccountsOrThrowError(tenantId: number, accountsIds: number[]): IAccount[] {
|
private async getAccountsOrThrowError(
|
||||||
|
tenantId: number,
|
||||||
|
accountsIds: number[]
|
||||||
|
): Promise<IAccount[]> {
|
||||||
const { Account } = this.tenancy.models(tenantId);
|
const { Account } = this.tenancy.models(tenantId);
|
||||||
|
|
||||||
this.logger.info('[account] trying to validate accounts not exist.', { tenantId, accountsIds });
|
this.logger.info('[account] trying to validate accounts not exist.', { tenantId, accountsIds });
|
||||||
@@ -400,14 +404,21 @@ export default class AccountsService {
|
|||||||
const { Account } = this.tenancy.models(tenantId);
|
const { Account } = this.tenancy.models(tenantId);
|
||||||
const accounts = await this.getAccountsOrThrowError(tenantId, accountsIds);
|
const accounts = await this.getAccountsOrThrowError(tenantId, accountsIds);
|
||||||
|
|
||||||
|
// Validate the accounts are not predefined.
|
||||||
this.validatePrefinedAccounts(accounts);
|
this.validatePrefinedAccounts(accounts);
|
||||||
|
|
||||||
|
// Valdiate the accounts have transactions.
|
||||||
await this.validateAccountsHaveTransactions(tenantId, accountsIds);
|
await this.validateAccountsHaveTransactions(tenantId, accountsIds);
|
||||||
|
|
||||||
|
// Unlink the parent account from children accounts.
|
||||||
|
await this.unassociateChildrenAccountsFromParent(tenantId, accountsIds);
|
||||||
|
|
||||||
|
// Delete the accounts in one query.
|
||||||
await Account.query().whereIn('id', accountsIds).delete();
|
await Account.query().whereIn('id', accountsIds).delete();
|
||||||
|
|
||||||
this.logger.info('[account] given accounts deleted in bulk successfully.', {
|
this.logger.info('[account] given accounts deleted in bulk successfully.', {
|
||||||
tenantId, accountsIds
|
tenantId, accountsIds
|
||||||
});
|
});
|
||||||
|
|
||||||
// Triggers `onBulkDeleted` event.
|
// Triggers `onBulkDeleted` event.
|
||||||
this.eventDispatcher.dispatch(events.accounts.onBulkDeleted);
|
this.eventDispatcher.dispatch(events.accounts.onBulkDeleted);
|
||||||
}
|
}
|
||||||
@@ -420,10 +431,23 @@ export default class AccountsService {
|
|||||||
*/
|
*/
|
||||||
public async activateAccounts(tenantId: number, accountsIds: number[], activate: boolean = true) {
|
public async activateAccounts(tenantId: number, accountsIds: number[], activate: boolean = true) {
|
||||||
const { Account } = this.tenancy.models(tenantId);
|
const { Account } = this.tenancy.models(tenantId);
|
||||||
|
const { accountRepository } = this.tenancy.repositories(tenantId);
|
||||||
|
|
||||||
|
// Retrieve the given account or throw not found.
|
||||||
await this.getAccountsOrThrowError(tenantId, accountsIds);
|
await this.getAccountsOrThrowError(tenantId, accountsIds);
|
||||||
|
|
||||||
|
// Get all children accounts.
|
||||||
|
const accountsGraph = await accountRepository.getDependencyGraph();
|
||||||
|
const dependenciesAccounts = chain(accountsIds)
|
||||||
|
.map(accountId => accountsGraph.dependenciesOf(accountId))
|
||||||
|
.flatten()
|
||||||
|
.value();
|
||||||
|
|
||||||
|
// The children and parent accounts.
|
||||||
|
const patchAccountsIds = uniq([...dependenciesAccounts, accountsIds]);
|
||||||
|
|
||||||
this.logger.info('[account] trying activate/inactive the given accounts ids.', { accountsIds });
|
this.logger.info('[account] trying activate/inactive the given accounts ids.', { accountsIds });
|
||||||
await Account.query().whereIn('id', accountsIds)
|
await Account.query().whereIn('id', patchAccountsIds)
|
||||||
.patch({
|
.patch({
|
||||||
active: activate ? 1 : 0,
|
active: activate ? 1 : 0,
|
||||||
});
|
});
|
||||||
@@ -441,14 +465,25 @@ export default class AccountsService {
|
|||||||
*/
|
*/
|
||||||
public async activateAccount(tenantId: number, accountId: number, activate?: boolean) {
|
public async activateAccount(tenantId: number, accountId: number, activate?: boolean) {
|
||||||
const { Account } = this.tenancy.models(tenantId);
|
const { Account } = this.tenancy.models(tenantId);
|
||||||
|
const { accountRepository } = this.tenancy.repositories(tenantId);
|
||||||
|
|
||||||
|
// Retrieve the given account or throw not found error.
|
||||||
const account = await this.getAccountOrThrowError(tenantId, accountId);
|
const account = await this.getAccountOrThrowError(tenantId, accountId);
|
||||||
|
|
||||||
|
// Get all children accounts.
|
||||||
|
const accountsGraph = await accountRepository.getDependencyGraph();
|
||||||
|
const dependenciesAccounts = accountsGraph.dependenciesOf(accountId);
|
||||||
|
|
||||||
this.logger.info('[account] trying to activate/inactivate the given account id.');
|
this.logger.info('[account] trying to activate/inactivate the given account id.');
|
||||||
await Account.query().where('id', accountId)
|
await Account.query()
|
||||||
|
.whereIn('id', [...dependenciesAccounts, accountId])
|
||||||
.patch({
|
.patch({
|
||||||
active: activate ? 1 : 0,
|
active: activate ? 1 : 0,
|
||||||
})
|
})
|
||||||
this.logger.info('[account] account have been activated successfully.', { tenantId, accountId });
|
this.logger.info('[account] account have been activated successfully.', {
|
||||||
|
tenantId,
|
||||||
|
accountId
|
||||||
|
});
|
||||||
|
|
||||||
// Triggers `onAccountActivated` event.
|
// Triggers `onAccountActivated` event.
|
||||||
this.eventDispatcher.dispatch(events.accounts.onActivated);
|
this.eventDispatcher.dispatch(events.accounts.onActivated);
|
||||||
|
|||||||
Reference in New Issue
Block a user