mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-21 07:10:33 +00:00
feat(ManualJournals): Auto-increment.
fix(BillPayment): Validate the opened payment bills. fix(redux): presist redux state. fix(useRequestQuery): hook.
This commit is contained in:
@@ -35,10 +35,10 @@ function ItemsEntriesTable({
|
|||||||
const [cellsLoading, setCellsLoading] = React.useState(null);
|
const [cellsLoading, setCellsLoading] = React.useState(null);
|
||||||
|
|
||||||
// Fetches the item details.
|
// Fetches the item details.
|
||||||
const { data: item, isFetching: isItemFetching } = useItem(
|
const { data: item, isFetching: isItemFetching, isSuccess: isItemSuccess } = useItem(
|
||||||
rowItem && rowItem.itemId,
|
rowItem && rowItem.itemId,
|
||||||
{
|
{
|
||||||
enabled: !!rowItem,
|
enabled: !!(rowItem && rowItem.itemId),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ function ItemsEntriesTable({
|
|||||||
|
|
||||||
// Once the item selected and fetched set the initial details to the table.
|
// Once the item selected and fetched set the initial details to the table.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (item && rowItem) {
|
if (isItemSuccess && item && rowItem) {
|
||||||
const { rowIndex } = rowItem;
|
const { rowIndex } = rowItem;
|
||||||
const price =
|
const price =
|
||||||
itemType === ITEM_TYPE.PURCHASABLE
|
itemType === ITEM_TYPE.PURCHASABLE
|
||||||
@@ -82,7 +82,7 @@ function ItemsEntriesTable({
|
|||||||
setRowItem(null);
|
setRowItem(null);
|
||||||
saveInvoke(onUpdateData, newRows);
|
saveInvoke(onUpdateData, newRows);
|
||||||
}
|
}
|
||||||
}, [item, rowItem, rows, itemType, onUpdateData]);
|
}, [item, rowItem, rows, itemType, onUpdateData, isItemSuccess]);
|
||||||
|
|
||||||
// Allows to observes `entries` to make table rows outside controlled.
|
// Allows to observes `entries` to make table rows outside controlled.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
@@ -20,17 +20,18 @@ function ItemFormProvider({ itemId, ...props }) {
|
|||||||
const { state } = useLocation();
|
const { state } = useLocation();
|
||||||
|
|
||||||
const duplicateId = state?.action;
|
const duplicateId = state?.action;
|
||||||
|
|
||||||
// Fetches the accounts list.
|
// Fetches the accounts list.
|
||||||
const { isFetching: isAccountsLoading, data: accounts } = useAccounts();
|
const { isLoading: isAccountsLoading, data: accounts } = useAccounts();
|
||||||
|
|
||||||
// Fetches the items categories list.
|
// Fetches the items categories list.
|
||||||
const {
|
const {
|
||||||
isFetching: isItemsCategoriesLoading,
|
isLoading: isItemsCategoriesLoading,
|
||||||
data: { itemsCategories },
|
data: { itemsCategories },
|
||||||
} = useItemsCategories();
|
} = useItemsCategories();
|
||||||
|
|
||||||
// Fetches the given item details.
|
// Fetches the given item details.
|
||||||
const { isFetching: isItemLoading, data: item } = useItem(
|
const { isLoading: isItemLoading, data: item } = useItem(
|
||||||
itemId || duplicateId,
|
itemId || duplicateId,
|
||||||
{
|
{
|
||||||
enabled: !!itemId || !!duplicateId,
|
enabled: !!itemId || !!duplicateId,
|
||||||
|
|||||||
@@ -16,13 +16,13 @@ function PaymentMadesListProvider({ query, ...props }) {
|
|||||||
// Fetch accounts resource views and fields.
|
// Fetch accounts resource views and fields.
|
||||||
const {
|
const {
|
||||||
data: paymentMadesViews,
|
data: paymentMadesViews,
|
||||||
isFetching: isViewsLoading,
|
isLoading: isViewsLoading,
|
||||||
} = useResourceViews('bill_payments');
|
} = useResourceViews('bill_payments');
|
||||||
|
|
||||||
// Fetch the accounts resource fields.
|
// Fetch the accounts resource fields.
|
||||||
const {
|
const {
|
||||||
data: paymentMadesFields,
|
data: paymentMadesFields,
|
||||||
isFetching: isFieldsLoading,
|
isLoading: isFieldsLoading,
|
||||||
} = useResourceFields('bill_payments');
|
} = useResourceFields('bill_payments');
|
||||||
|
|
||||||
// Fetch accounts list according to the given custom view id.
|
// Fetch accounts list according to the given custom view id.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { useMutation, useQueryClient } from 'react-query';
|
import { useMutation, useQueryClient } from 'react-query';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
import { useQueryTenant, useRequestQuery } from '../useQueryRequest';
|
||||||
import useApiRequest from '../useRequest';
|
import useApiRequest from '../useRequest';
|
||||||
import t from './types';
|
import t from './types';
|
||||||
|
|
||||||
@@ -14,27 +14,20 @@ const commonInvalidateQueries = (query) => {
|
|||||||
|
|
||||||
// Invalidate financial reports.
|
// Invalidate financial reports.
|
||||||
query.invalidateQueries(t.FINANCIAL_REPORT);
|
query.invalidateQueries(t.FINANCIAL_REPORT);
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve accounts list.
|
* Retrieve accounts list.
|
||||||
*/
|
*/
|
||||||
export function useAccounts(query, props) {
|
export function useAccounts(query, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.ACCOUNTS, query],
|
[t.ACCOUNTS, query],
|
||||||
() => apiRequest.get('accounts', { params: query }),
|
{ method: 'get', url: 'accounts', params: query },
|
||||||
{
|
{
|
||||||
select: (response) => {
|
select: (response) => {
|
||||||
return response.data.accounts;
|
return response.data.accounts;
|
||||||
},
|
},
|
||||||
initialDataUpdatedAt: 0,
|
defaultData: [],
|
||||||
initialData: {
|
|
||||||
data: {
|
|
||||||
accounts: []
|
|
||||||
},
|
|
||||||
},
|
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -45,17 +38,13 @@ export function useAccounts(query, props) {
|
|||||||
* @param {number} id - Account id.
|
* @param {number} id - Account id.
|
||||||
*/
|
*/
|
||||||
export function useAccount(id, props) {
|
export function useAccount(id, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.ACCOUNT, id],
|
[t.ACCOUNT, id],
|
||||||
() => apiRequest.get(`accounts/${id}`).then(transformAccount),
|
{ method: 'get', url: `accounts/${id}` },
|
||||||
{
|
{
|
||||||
initialDataUpdatedAt: 0,
|
select: transformAccount,
|
||||||
initialData: {
|
defaultData: {},
|
||||||
data: { account: {} }
|
...props,
|
||||||
},
|
|
||||||
...props
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -64,19 +53,12 @@ export function useAccount(id, props) {
|
|||||||
* Retrieve accounts types list.
|
* Retrieve accounts types list.
|
||||||
*/
|
*/
|
||||||
export function useAccountsTypes(props) {
|
export function useAccountsTypes(props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.ACCOUNTS_TYPES],
|
[t.ACCOUNTS_TYPES],
|
||||||
() => apiRequest.get('account_types'),
|
{ method: 'get', url: 'account_types' },
|
||||||
{
|
{
|
||||||
select: (res) => res.data.account_types,
|
select: (res) => res.data.account_types,
|
||||||
initialData: {
|
defaultData: [],
|
||||||
data: {
|
|
||||||
account_types: [],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
initialDataUpdatedAt: 0,
|
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { useQueryClient, useMutation } from 'react-query';
|
import { useQueryClient, useMutation } from 'react-query';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
import { useRequestQuery } from '../useQueryRequest';
|
||||||
import { defaultTo } from 'lodash';
|
|
||||||
import { transformPagination } from 'utils';
|
import { transformPagination } from 'utils';
|
||||||
import useApiRequest from '../useRequest';
|
import useApiRequest from '../useRequest';
|
||||||
import t from './types';
|
import t from './types';
|
||||||
@@ -67,16 +66,13 @@ export function useEditBill(props) {
|
|||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const apiRequest = useApiRequest();
|
const apiRequest = useApiRequest();
|
||||||
|
|
||||||
return useMutation(
|
return useMutation((id) => apiRequest.post(`purchases/bills/${id}/open`), {
|
||||||
(id) => apiRequest.post(`purchases/bills/${id}/open`),
|
|
||||||
{
|
|
||||||
onSuccess: (res, id) => {
|
onSuccess: (res, id) => {
|
||||||
// Common invalidate queries.
|
// Common invalidate queries.
|
||||||
commonInvalidateQueries(queryClient);
|
commonInvalidateQueries(queryClient);
|
||||||
},
|
},
|
||||||
...props,
|
...props,
|
||||||
},
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -95,29 +91,26 @@ export function useDeleteBill(props) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const transformBillsResponse = (response) => ({
|
||||||
|
bills: response.data.bills,
|
||||||
|
pagination: transformPagination(response.data.pagination),
|
||||||
|
filterMeta: response.data.filter_meta,
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve sale invoices list with pagination meta.
|
* Retrieve sale invoices list with pagination meta.
|
||||||
*/
|
*/
|
||||||
export function useBills(query, props) {
|
export function useBills(query, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
const states = useQueryTenant(
|
|
||||||
[t.BILLS, query],
|
[t.BILLS, query],
|
||||||
() =>
|
|
||||||
apiRequest.get('purchases/bills', { params: query }),
|
|
||||||
{
|
{
|
||||||
select: (response) => ({
|
method: 'get',
|
||||||
bills: response.data.bills,
|
url: 'purchases/bills',
|
||||||
pagination: transformPagination(response.data.pagination),
|
params: query,
|
||||||
filterMeta: response.data.filter_meta,
|
|
||||||
}),
|
|
||||||
...props,
|
|
||||||
},
|
},
|
||||||
);
|
{
|
||||||
|
select: transformBillsResponse,
|
||||||
return {
|
defaultData: {
|
||||||
...states,
|
|
||||||
data: defaultTo(states.data, {
|
|
||||||
bills: [],
|
bills: [],
|
||||||
pagination: {
|
pagination: {
|
||||||
page: 1,
|
page: 1,
|
||||||
@@ -125,8 +118,10 @@ export function useBills(query, props) {
|
|||||||
total: 0,
|
total: 0,
|
||||||
},
|
},
|
||||||
filterMeta: {},
|
filterMeta: {},
|
||||||
})
|
},
|
||||||
}
|
...props,
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -134,21 +129,15 @@ export function useBills(query, props) {
|
|||||||
* @param {number} id - Bill id.
|
* @param {number} id - Bill id.
|
||||||
*/
|
*/
|
||||||
export function useBill(id, props) {
|
export function useBill(id, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
const states = useQueryTenant(
|
|
||||||
[t.BILL, id],
|
[t.BILL, id],
|
||||||
() => apiRequest.get(`/purchases/bills/${id}`),
|
{ method: 'get', url: `/purchases/bills/${id}`, },
|
||||||
{
|
{
|
||||||
select: (res) => res.data.bill,
|
select: (res) => res.data.bill,
|
||||||
|
defaultData: {},
|
||||||
...props,
|
...props,
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
|
||||||
...states,
|
|
||||||
data: defaultTo(states.data, {}),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -156,22 +145,17 @@ export function useBill(id, props) {
|
|||||||
* @param {number} vendorId -
|
* @param {number} vendorId -
|
||||||
*/
|
*/
|
||||||
export function useDueBills(vendorId, props) {
|
export function useDueBills(vendorId, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
const states = useQueryTenant(
|
|
||||||
[t.BILLS, t.BILLS_DUE, vendorId],
|
[t.BILLS, t.BILLS_DUE, vendorId],
|
||||||
() =>
|
{
|
||||||
apiRequest.get(`purchases/bills/due`, {
|
method: 'get',
|
||||||
|
url: 'purchases/bills/due',
|
||||||
params: { vendor_id: vendorId },
|
params: { vendor_id: vendorId },
|
||||||
}),
|
},
|
||||||
{
|
{
|
||||||
select: (res) => res.data.bills,
|
select: (res) => res.data.bills,
|
||||||
|
defaultData: [],
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
|
||||||
...states,
|
|
||||||
data: defaultTo(states.data, []),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import useApiRequest from '../useRequest';
|
import useApiRequest from '../useRequest';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
import { useQueryTenant } from '../useQueryRequest';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the contact duplicate.
|
* Retrieve the contact duplicate.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { useMutation, useQueryClient } from 'react-query';
|
import { useMutation, useQueryClient } from 'react-query';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
import { useRequestQuery } from '../useQueryRequest';
|
||||||
import useApiRequest from '../useRequest';
|
import useApiRequest from '../useRequest';
|
||||||
import t from './types';
|
import t from './types';
|
||||||
|
|
||||||
@@ -62,14 +62,12 @@ export function useDeleteCurrency(props) {
|
|||||||
* Retrieve the currencies list.
|
* Retrieve the currencies list.
|
||||||
*/
|
*/
|
||||||
export function useCurrencies(props) {
|
export function useCurrencies(props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.CURRENCIES],
|
[t.CURRENCIES],
|
||||||
() => apiRequest.get('currencies').then((res) => res.data.currencies),
|
{ method: 'get', url: 'currencies' },
|
||||||
{
|
{
|
||||||
initialDataUpdatedAt: 0,
|
select: (res) => res.data.currencies,
|
||||||
initialData: [],
|
defaultData: [],
|
||||||
...props
|
...props
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { useMutation, useQueryClient } from 'react-query';
|
import { useMutation, useQueryClient } from 'react-query';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
import { useRequestQuery } from '../useQueryRequest';
|
||||||
import { transformPagination } from 'utils';
|
import { transformPagination } from 'utils';
|
||||||
import useApiRequest from '../useRequest';
|
import useApiRequest from '../useRequest';
|
||||||
import t from './types';
|
import t from './types';
|
||||||
@@ -22,28 +22,26 @@ const commonInvalidateQueries = (queryClient) => {
|
|||||||
queryClient.invalidateQueries(t.FINANCIAL_REPORT);
|
queryClient.invalidateQueries(t.FINANCIAL_REPORT);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Customers response selector.
|
||||||
|
const customersSelector = (response) => ({
|
||||||
|
customers: response.data.customers,
|
||||||
|
pagination: transformPagination(response.data.pagination),
|
||||||
|
filterMeta: response.data.filter_meta,
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve customers list with pagination meta.
|
* Retrieve customers list with pagination meta.
|
||||||
*/
|
*/
|
||||||
export function useCustomers(query, props) {
|
export function useCustomers(query, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.CUSTOMERS, query],
|
[t.CUSTOMERS, query],
|
||||||
() => apiRequest.get(`customers`, { params: query }),
|
{ method: 'get', url: `customers`, params: query },
|
||||||
{
|
{
|
||||||
select: (response) => ({
|
select: customersSelector,
|
||||||
customers: response.data.customers,
|
defaultData: {
|
||||||
pagination: transformPagination(response.data.pagination),
|
|
||||||
filterMeta: response.data.filter_meta,
|
|
||||||
}),
|
|
||||||
initialDataUpdatedAt: 0,
|
|
||||||
initialData: {
|
|
||||||
data: {
|
|
||||||
customers: [],
|
customers: [],
|
||||||
pagination: defaultPagination,
|
pagination: defaultPagination,
|
||||||
filter_meta: {},
|
filterMeta: {},
|
||||||
}
|
|
||||||
},
|
},
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
@@ -117,19 +115,12 @@ export function useCreateCustomer(props) {
|
|||||||
* Retrieve the customer details.
|
* Retrieve the customer details.
|
||||||
*/
|
*/
|
||||||
export function useCustomer(id, props) {
|
export function useCustomer(id, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.CUSTOMER, id],
|
[t.CUSTOMER, id],
|
||||||
() => apiRequest.get(`customers/${id}`),
|
{ method: 'get', url: `customers/${id}` },
|
||||||
{
|
{
|
||||||
select: (res) => res.data.customer,
|
select: (res) => res.data.customer,
|
||||||
initialDataUpdatedAt: 0,
|
defaultData: {},
|
||||||
initialData: {
|
|
||||||
data: {
|
|
||||||
customer: {}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
...props
|
...props
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { useQueryClient, useMutation } from 'react-query';
|
import { useQueryClient, useMutation } from 'react-query';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
import { useRequestQuery } from '../useQueryRequest';
|
||||||
import useApiRequest from '../useRequest';
|
import useApiRequest from '../useRequest';
|
||||||
import { transformPagination } from 'utils';
|
import { transformPagination } from 'utils';
|
||||||
import t from './types';
|
import t from './types';
|
||||||
@@ -55,48 +55,40 @@ export function useEditEstimate(props) {
|
|||||||
* Retrieve sale estimate details.
|
* Retrieve sale estimate details.
|
||||||
*/
|
*/
|
||||||
export function useEstimate(id, props) {
|
export function useEstimate(id, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.SALE_ESTIMATE, id],
|
[t.SALE_ESTIMATE, id],
|
||||||
() => apiRequest.get(`sales/estimates/${id}`),
|
{ method: 'get', url: `sales/estimates/${id}` },
|
||||||
{
|
{
|
||||||
select: (res) => res.data.estimate,
|
select: (res) => res.data.estimate,
|
||||||
initialDataUpdatedAt: 0,
|
defaultData: {},
|
||||||
initialData: {
|
|
||||||
data: { estimate: {} },
|
|
||||||
},
|
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const transformEstimates = (res) => ({
|
||||||
|
estimates: res.data.sales_estimates,
|
||||||
|
pagination: transformPagination(res.data.pagination),
|
||||||
|
filterMeta: res.data.filter_meta,
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve sale invoices list with pagination meta.
|
* Retrieve sale invoices list with pagination meta.
|
||||||
*/
|
*/
|
||||||
export function useEstimates(query, props) {
|
export function useEstimates(query, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.SALE_ESTIMATES, query],
|
[t.SALE_ESTIMATES, query],
|
||||||
() => apiRequest.get('sales/estimates', { params: query }),
|
{ method: 'get', url: 'sales/estimates', params: query },
|
||||||
{
|
{
|
||||||
select: (res) => ({
|
select: transformEstimates,
|
||||||
estimates: res.data.sales_estimates,
|
defaultData: {
|
||||||
pagination: transformPagination(res.data.pagination),
|
estimates: [],
|
||||||
filterMeta: res.data.filter_meta,
|
|
||||||
}),
|
|
||||||
initialDataUpdatedAt: 0,
|
|
||||||
initialData: {
|
|
||||||
data:{
|
|
||||||
sales_estimates: [],
|
|
||||||
pagination: {
|
pagination: {
|
||||||
page: 1,
|
page: 1,
|
||||||
pageSize: 12,
|
pageSize: 12,
|
||||||
total: 0,
|
total: 0,
|
||||||
},
|
},
|
||||||
filter_meta: {},
|
filterMeta: {},
|
||||||
}
|
|
||||||
},
|
},
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { useMutation, useQueryClient } from 'react-query';
|
import { useMutation, useQueryClient } from 'react-query';
|
||||||
import { defaultTo } from 'lodash';
|
import { defaultTo } from 'lodash';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
import { useQueryTenant } from '../useQueryRequest';
|
||||||
import { transformPagination } from 'utils';
|
import { transformPagination } from 'utils';
|
||||||
import useApiRequest from '../useRequest';
|
import useApiRequest from '../useRequest';
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { useMutation, useQueryClient } from 'react-query';
|
import { useMutation, useQueryClient } from 'react-query';
|
||||||
import useApiRequest from '../useRequest';
|
import useApiRequest from '../useRequest';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
import { useRequestQuery } from '../useQueryRequest';
|
||||||
import { transformPagination } from 'utils';
|
import { transformPagination } from 'utils';
|
||||||
import t from './types';
|
import t from './types';
|
||||||
|
|
||||||
@@ -23,28 +23,29 @@ const commonInvalidateQueries = (queryClient) => {
|
|||||||
queryClient.invalidateQueries(t.FINANCIAL_REPORT);
|
queryClient.invalidateQueries(t.FINANCIAL_REPORT);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const transformExpenses = (response) => ({
|
||||||
|
expenses: response.data.expenses,
|
||||||
|
pagination: transformPagination(response.data.pagination),
|
||||||
|
filterMeta: response.data.filter_meta,
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the expenses list.
|
* Retrieve the expenses list.
|
||||||
*/
|
*/
|
||||||
export function useExpenses(query, props) {
|
export function useExpenses(query, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.EXPENSES, query],
|
[t.EXPENSES, query],
|
||||||
() => apiRequest.get(`expenses`, { params: { ...query } }),
|
|
||||||
{
|
{
|
||||||
select: (response) => ({
|
method: 'get',
|
||||||
expenses: response.data.expenses,
|
url: `expenses`,
|
||||||
pagination: transformPagination(response.data.pagination),
|
params: { ...query }
|
||||||
filterMeta: response.data.filter_meta,
|
},
|
||||||
}),
|
{
|
||||||
initialDataUpdatedAt: 0,
|
select: transformExpenses,
|
||||||
initialData: {
|
defaultData: {
|
||||||
data: {
|
|
||||||
expenses: [],
|
expenses: [],
|
||||||
pagination: defaultPagination,
|
pagination: defaultPagination,
|
||||||
filter_meta: {},
|
filterMeta: {},
|
||||||
},
|
|
||||||
},
|
},
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
@@ -56,19 +57,15 @@ export function useExpenses(query, props) {
|
|||||||
* @param {number} id - Expense id.
|
* @param {number} id - Expense id.
|
||||||
*/
|
*/
|
||||||
export function useExpense(id, props) {
|
export function useExpense(id, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.EXPENSE, id],
|
[t.EXPENSE, id],
|
||||||
() => apiRequest.get(`expenses/${id}`),
|
{
|
||||||
|
method: 'get',
|
||||||
|
url: `expenses/${id}`
|
||||||
|
},
|
||||||
{
|
{
|
||||||
select: (res) => res.data.expense,
|
select: (res) => res.data.expense,
|
||||||
initialDataUpdatedAt: 0,
|
defaultData: {},
|
||||||
initialData: {
|
|
||||||
data: {
|
|
||||||
expense: {},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { defaultTo } from 'lodash';
|
import { useRequestQuery } from '../useQueryRequest';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
|
||||||
import {
|
import {
|
||||||
trialBalanceSheetReducer,
|
trialBalanceSheetReducer,
|
||||||
balanceSheetRowsReducer,
|
balanceSheetRowsReducer,
|
||||||
@@ -7,176 +6,150 @@ import {
|
|||||||
generalLedgerTableRowsReducer,
|
generalLedgerTableRowsReducer,
|
||||||
journalTableRowsReducer,
|
journalTableRowsReducer,
|
||||||
ARAgingSummaryTableRowsMapper,
|
ARAgingSummaryTableRowsMapper,
|
||||||
APAgingSummaryTableRowsMapper
|
APAgingSummaryTableRowsMapper,
|
||||||
} from 'containers/FinancialStatements/reducers';
|
} from 'containers/FinancialStatements/reducers';
|
||||||
import useApiRequest from '../useRequest';
|
|
||||||
import t from './types';
|
import t from './types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve balance sheet.
|
* Retrieve balance sheet.
|
||||||
*/
|
*/
|
||||||
export function useBalanceSheet(query, props) {
|
export function useBalanceSheet(query, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
const states = useQueryTenant(
|
|
||||||
[t.FINANCIAL_REPORT, t.BALANCE_SHEET, query],
|
[t.FINANCIAL_REPORT, t.BALANCE_SHEET, query],
|
||||||
() =>
|
{
|
||||||
apiRequest.get('/financial_statements/balance_sheet', {
|
method: 'get',
|
||||||
|
url: '/financial_statements/balance_sheet',
|
||||||
params: query,
|
params: query,
|
||||||
}),
|
},
|
||||||
{
|
{
|
||||||
select: (res) => ({
|
select: (res) => ({
|
||||||
tableRows: balanceSheetRowsReducer(res.data.data),
|
tableRows: balanceSheetRowsReducer(res.data.data),
|
||||||
...res.data,
|
...res.data,
|
||||||
}),
|
}),
|
||||||
...props,
|
defaultData: {
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...states,
|
|
||||||
data: defaultTo(states.data, {
|
|
||||||
data: [],
|
data: [],
|
||||||
columns: [],
|
columns: [],
|
||||||
query: {},
|
query: {},
|
||||||
tableRows: [],
|
tableRows: [],
|
||||||
}),
|
},
|
||||||
};
|
...props,
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve trial balance sheet.
|
* Retrieve trial balance sheet.
|
||||||
*/
|
*/
|
||||||
export function useTrialBalanceSheet(query, props) {
|
export function useTrialBalanceSheet(query, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
const states = useQueryTenant(
|
|
||||||
[t.FINANCIAL_REPORT, t.TRIAL_BALANCE_SHEET, query],
|
[t.FINANCIAL_REPORT, t.TRIAL_BALANCE_SHEET, query],
|
||||||
() =>
|
{
|
||||||
apiRequest.get('/financial_statements/trial_balance_sheet', {
|
method: 'get',
|
||||||
|
url: '/financial_statements/trial_balance_sheet',
|
||||||
params: query,
|
params: query,
|
||||||
}),
|
},
|
||||||
{
|
{
|
||||||
select: (res) => ({
|
select: (res) => ({
|
||||||
tableRows: trialBalanceSheetReducer(res.data.data),
|
tableRows: trialBalanceSheetReducer(res.data.data),
|
||||||
...res.data,
|
...res.data,
|
||||||
}),
|
}),
|
||||||
...props,
|
defaultData: {
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...states,
|
|
||||||
data: defaultTo(states.data, {
|
|
||||||
tableRows: [],
|
tableRows: [],
|
||||||
data: [],
|
data: [],
|
||||||
query: {},
|
query: {},
|
||||||
}),
|
},
|
||||||
};
|
...props,
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve profit/loss (P&L) sheet.
|
* Retrieve profit/loss (P&L) sheet.
|
||||||
*/
|
*/
|
||||||
export function useProfitLossSheet(query, props) {
|
export function useProfitLossSheet(query, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
const states = useQueryTenant(
|
|
||||||
[t.FINANCIAL_REPORT, t.PROFIT_LOSS_SHEET, query],
|
[t.FINANCIAL_REPORT, t.PROFIT_LOSS_SHEET, query],
|
||||||
() =>
|
{
|
||||||
apiRequest.get('/financial_statements/profit_loss_sheet', {
|
method: 'get',
|
||||||
|
url: '/financial_statements/profit_loss_sheet',
|
||||||
params: query,
|
params: query,
|
||||||
}),
|
},
|
||||||
{
|
{
|
||||||
select: (res) => ({
|
select: (res) => ({
|
||||||
tableRows: profitLossSheetReducer(res.data.data),
|
tableRows: profitLossSheetReducer(res.data.data),
|
||||||
...res.data,
|
...res.data,
|
||||||
}),
|
}),
|
||||||
...props,
|
defaultData: {
|
||||||
},
|
|
||||||
);
|
|
||||||
return {
|
|
||||||
...states,
|
|
||||||
data: defaultTo(states.data, {
|
|
||||||
data: {},
|
data: {},
|
||||||
tableRows: [],
|
tableRows: [],
|
||||||
columns: [],
|
columns: [],
|
||||||
query: {},
|
query: {},
|
||||||
}),
|
},
|
||||||
};
|
...props,
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve general ledger (GL) sheet.
|
* Retrieve general ledger (GL) sheet.
|
||||||
*/
|
*/
|
||||||
export function useGeneralLedgerSheet(query, props) {
|
export function useGeneralLedgerSheet(query, props) {
|
||||||
const apiRequest = useApiRequest();
|
|
||||||
|
|
||||||
const states = useQueryTenant(
|
return useRequestQuery(
|
||||||
[t.FINANCIAL_REPORT, t.GENERAL_LEDGER, query],
|
[t.FINANCIAL_REPORT, t.GENERAL_LEDGER, query],
|
||||||
() =>
|
{
|
||||||
apiRequest.get('/financial_statements/general_ledger', {
|
method: 'get',
|
||||||
|
url: '/financial_statements/general_ledger',
|
||||||
params: query,
|
params: query,
|
||||||
}),
|
},
|
||||||
{
|
{
|
||||||
select: (res) => ({
|
select: (res) => ({
|
||||||
tableRows: generalLedgerTableRowsReducer(res.data.data),
|
tableRows: generalLedgerTableRowsReducer(res.data.data),
|
||||||
...res.data,
|
...res.data,
|
||||||
}),
|
}),
|
||||||
...props,
|
defaultData: {
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...states,
|
|
||||||
data: defaultTo(states.data, {
|
|
||||||
tableRows: [],
|
tableRows: [],
|
||||||
data: {},
|
data: {},
|
||||||
query: {},
|
query: {},
|
||||||
}),
|
},
|
||||||
};
|
...props,
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve journal sheet.
|
* Retrieve journal sheet.
|
||||||
*/
|
*/
|
||||||
export function useJournalSheet(query, props) {
|
export function useJournalSheet(query, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
const states = useQueryTenant(
|
|
||||||
[t.FINANCIAL_REPORT, t.JOURNAL, query],
|
[t.FINANCIAL_REPORT, t.JOURNAL, query],
|
||||||
() => apiRequest.get('/financial_statements/journal', { params: query }),
|
{ method: 'get', url: '/financial_statements/journal', params: query },
|
||||||
{
|
{
|
||||||
select: (res) => ({
|
select: (res) => ({
|
||||||
tableRows: journalTableRowsReducer(res.data.data),
|
tableRows: journalTableRowsReducer(res.data.data),
|
||||||
...res.data,
|
...res.data,
|
||||||
}),
|
}),
|
||||||
...props,
|
defaultData: {
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...states,
|
|
||||||
data: defaultTo(states.data, {
|
|
||||||
data: {},
|
data: {},
|
||||||
tableRows: [],
|
tableRows: [],
|
||||||
query: {},
|
query: {},
|
||||||
}),
|
},
|
||||||
};
|
...props,
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve A/R aging summary report.
|
* Retrieve A/R aging summary report.
|
||||||
*/
|
*/
|
||||||
export function useARAgingSummaryReport(query, props) {
|
export function useARAgingSummaryReport(query, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.FINANCIAL_REPORT, t.AR_AGING_SUMMARY, query],
|
[t.FINANCIAL_REPORT, t.AR_AGING_SUMMARY, query],
|
||||||
() =>
|
{
|
||||||
apiRequest.get('/financial_statements/receivable_aging_summary', {
|
method: 'get',
|
||||||
|
url: '/financial_statements/receivable_aging_summary',
|
||||||
params: query,
|
params: query,
|
||||||
}),
|
},
|
||||||
{
|
{
|
||||||
select: (res) => ({
|
select: (res) => ({
|
||||||
columns: res.data.columns,
|
columns: res.data.columns,
|
||||||
@@ -188,8 +161,7 @@ export function useARAgingSummaryReport(query, props) {
|
|||||||
columns: res.data.columns,
|
columns: res.data.columns,
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
initialData: {
|
defaultData: {
|
||||||
data: {
|
|
||||||
data: {
|
data: {
|
||||||
customers: [],
|
customers: [],
|
||||||
total: {},
|
total: {},
|
||||||
@@ -197,8 +169,6 @@ export function useARAgingSummaryReport(query, props) {
|
|||||||
columns: [],
|
columns: [],
|
||||||
tableRows: [],
|
tableRows: [],
|
||||||
},
|
},
|
||||||
},
|
|
||||||
initialDataUpdatedAt: 0,
|
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -208,14 +178,13 @@ export function useARAgingSummaryReport(query, props) {
|
|||||||
* Retrieve A/P aging summary report.
|
* Retrieve A/P aging summary report.
|
||||||
*/
|
*/
|
||||||
export function useAPAgingSummaryReport(query, props) {
|
export function useAPAgingSummaryReport(query, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.FINANCIAL_REPORT, t.AP_AGING_SUMMARY, query],
|
[t.FINANCIAL_REPORT, t.AP_AGING_SUMMARY, query],
|
||||||
() =>
|
{
|
||||||
apiRequest.get('/financial_statements/payable_aging_summary', {
|
method: 'get',
|
||||||
|
url: '/financial_statements/payable_aging_summary',
|
||||||
params: query,
|
params: query,
|
||||||
}),
|
},
|
||||||
{
|
{
|
||||||
select: (res) => ({
|
select: (res) => ({
|
||||||
columns: res.data.columns,
|
columns: res.data.columns,
|
||||||
@@ -227,8 +196,7 @@ export function useAPAgingSummaryReport(query, props) {
|
|||||||
columns: res.data.columns,
|
columns: res.data.columns,
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
initialData: {
|
defaultData: {
|
||||||
data: {
|
|
||||||
data: {
|
data: {
|
||||||
vendors: [],
|
vendors: [],
|
||||||
total: {},
|
total: {},
|
||||||
@@ -236,8 +204,6 @@ export function useAPAgingSummaryReport(query, props) {
|
|||||||
columns: [],
|
columns: [],
|
||||||
tableRows: [],
|
tableRows: [],
|
||||||
},
|
},
|
||||||
},
|
|
||||||
initialDataUpdatedAt: 0,
|
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
import { useMutation, useQueryClient } from 'react-query';
|
import { useMutation, useQueryClient } from 'react-query';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
import { useRequestQuery } from '../useQueryRequest';
|
||||||
import { transformPagination } from 'utils';
|
import { transformPagination } from 'utils';
|
||||||
import useApiRequest from '../useRequest';
|
import useApiRequest from '../useRequest';
|
||||||
import t from './types';
|
import t from './types';
|
||||||
|
|
||||||
|
|
||||||
const commonInvalidateQueries = (queryClient) => {
|
const commonInvalidateQueries = (queryClient) => {
|
||||||
// Invalidate inventory adjustments.
|
// Invalidate inventory adjustments.
|
||||||
queryClient.invalidateQueries(t.INVENTORY_ADJUSTMENTS);
|
queryClient.invalidateQueries(t.INVENTORY_ADJUSTMENTS);
|
||||||
@@ -47,16 +46,13 @@ export function useDeleteInventoryAdjustment(props) {
|
|||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const apiRequest = useApiRequest();
|
const apiRequest = useApiRequest();
|
||||||
|
|
||||||
return useMutation(
|
return useMutation((id) => apiRequest.delete(`inventory_adjustments/${id}`), {
|
||||||
(id) => apiRequest.delete(`inventory_adjustments/${id}`),
|
|
||||||
{
|
|
||||||
onSuccess: (res, id) => {
|
onSuccess: (res, id) => {
|
||||||
// Common invalidate queries.
|
// Common invalidate queries.
|
||||||
commonInvalidateQueries(queryClient);
|
commonInvalidateQueries(queryClient);
|
||||||
},
|
},
|
||||||
...props
|
...props,
|
||||||
},
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const inventoryAdjustmentsTransformer = (response) => {
|
const inventoryAdjustmentsTransformer = (response) => {
|
||||||
@@ -64,22 +60,18 @@ const inventoryAdjustmentsTransformer = (response) => {
|
|||||||
transactions: response.data.inventoy_adjustments,
|
transactions: response.data.inventoy_adjustments,
|
||||||
pagination: transformPagination(response.data.pagination),
|
pagination: transformPagination(response.data.pagination),
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve inventory adjustment list with pagination meta.
|
* Retrieve inventory adjustment list with pagination meta.
|
||||||
*/
|
*/
|
||||||
export function useInventoryAdjustments(query, props) {
|
export function useInventoryAdjustments(query, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
['INVENTORY_ADJUSTMENTS', query],
|
['INVENTORY_ADJUSTMENTS', query],
|
||||||
() => apiRequest.get('inventory_adjustments', { params: query })
|
{ url: 'inventory_adjustments', params: query },
|
||||||
.then(inventoryAdjustmentsTransformer),
|
|
||||||
{
|
{
|
||||||
initialDataUpdatedAt: 0,
|
select: inventoryAdjustmentsTransformer,
|
||||||
initialData: {
|
defaultData: {
|
||||||
data: {
|
|
||||||
transactions: [],
|
transactions: [],
|
||||||
pagination: {
|
pagination: {
|
||||||
page: 1,
|
page: 1,
|
||||||
@@ -87,9 +79,8 @@ export function useInventoryAdjustments(query, props) {
|
|||||||
total: 0,
|
total: 0,
|
||||||
pagesCount: 0,
|
pagesCount: 0,
|
||||||
},
|
},
|
||||||
}
|
|
||||||
},
|
},
|
||||||
...props
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { useMutation } from 'react-query';
|
import { useMutation } from 'react-query';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
import { useRequestQuery } from '../useQueryRequest';
|
||||||
import useApiRequest from '../useRequest';
|
import useApiRequest from '../useRequest';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -19,11 +19,9 @@ export const useAuthInviteAccept = (props) => {
|
|||||||
* @param {string} token - Token.
|
* @param {string} token - Token.
|
||||||
*/
|
*/
|
||||||
export const useInviteMetaByToken = (token, props) => {
|
export const useInviteMetaByToken = (token, props) => {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
['INVITE_META', token],
|
['INVITE_META', token],
|
||||||
() => apiRequest.get(`invite/invited/${token}`),
|
{ method: 'get', url: `invite/invited/${token}` },
|
||||||
{
|
{
|
||||||
select: (res) => res.data,
|
select: (res) => res.data,
|
||||||
...props
|
...props
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { useQueryClient, useMutation } from 'react-query';
|
import { useQueryClient, useMutation } from 'react-query';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
import { useRequestQuery } from '../useQueryRequest';
|
||||||
import { transformPagination } from 'utils';
|
import { transformPagination } from 'utils';
|
||||||
import useApiRequest from '../useRequest';
|
import useApiRequest from '../useRequest';
|
||||||
import t from './types';
|
import t from './types';
|
||||||
@@ -90,32 +90,29 @@ export function useDeleteInvoice(props) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const transformInvoices = (res) => ({
|
||||||
|
invoices: res.data.sales_invoices,
|
||||||
|
pagination: transformPagination(res.data.pagination),
|
||||||
|
filterMeta: res.data.filter_meta,
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve sale invoices list with pagination meta.
|
* Retrieve sale invoices list with pagination meta.
|
||||||
*/
|
*/
|
||||||
export function useInvoices(query, props) {
|
export function useInvoices(query, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.SALE_INVOICES, query],
|
[t.SALE_INVOICES, query],
|
||||||
() => apiRequest.get('sales/invoices', { params: query }),
|
{ method: 'get', url: 'sales/invoices', params: query },
|
||||||
{
|
{
|
||||||
select: (res) => ({
|
select: transformInvoices,
|
||||||
invoices: res.data.sales_invoices,
|
defaultData: {
|
||||||
pagination: transformPagination(res.data.pagination),
|
invoices: [],
|
||||||
filterMeta: res.data.filter_meta,
|
|
||||||
}),
|
|
||||||
initialDataUpdatedAt: 0,
|
|
||||||
initialData: {
|
|
||||||
data: {
|
|
||||||
sales_invoices: [],
|
|
||||||
pagination: {
|
pagination: {
|
||||||
page: 1,
|
page: 1,
|
||||||
pageSize: 12,
|
pageSize: 12,
|
||||||
total: 0,
|
total: 0,
|
||||||
},
|
},
|
||||||
filter_meta: {},
|
filterMeta: {},
|
||||||
},
|
|
||||||
},
|
},
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
@@ -149,19 +146,12 @@ export function useDeliverInvoice(props) {
|
|||||||
* @param {number} invoiceId - Invoice id.
|
* @param {number} invoiceId - Invoice id.
|
||||||
*/
|
*/
|
||||||
export function useInvoice(invoiceId, props) {
|
export function useInvoice(invoiceId, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.SALE_INVOICE, invoiceId],
|
[t.SALE_INVOICE, invoiceId],
|
||||||
() => apiRequest.get(`sales/invoices/${invoiceId}`),
|
{ method: 'get', url: `sales/invoices/${invoiceId}` },
|
||||||
{
|
{
|
||||||
select: (res) => res.data.sale_invoice,
|
select: (res) => res.data.sale_invoice,
|
||||||
initialDataUpdatedAt: 0,
|
defaultData: {},
|
||||||
initialData: {
|
|
||||||
data: {
|
|
||||||
sale_invoice: {}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -172,22 +162,16 @@ export function useInvoice(invoiceId, props) {
|
|||||||
* @param {number} customerId - Customer id.
|
* @param {number} customerId - Customer id.
|
||||||
*/
|
*/
|
||||||
export function useDueInvoices(customerId, props) {
|
export function useDueInvoices(customerId, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.SALE_INVOICES, t.SALE_INVOICES_DUE, customerId],
|
[t.SALE_INVOICES, t.SALE_INVOICES_DUE, customerId],
|
||||||
() =>
|
{
|
||||||
apiRequest.get(`sales/invoices/payable`, {
|
method: 'get',
|
||||||
|
url: `sales/invoices/payable`,
|
||||||
params: { customer_id: customerId },
|
params: { customer_id: customerId },
|
||||||
}),
|
},
|
||||||
{
|
{
|
||||||
select: (res) => res.data.sales_invoices,
|
select: (res) => res.data.sales_invoices,
|
||||||
initialDataUpdatedAt: 0,
|
defaultData: [],
|
||||||
initialData: {
|
|
||||||
data: {
|
|
||||||
sales_invoices: [],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { useMutation, useQueryClient } from 'react-query';
|
import { useMutation, useQueryClient } from 'react-query';
|
||||||
import { transformPagination, transformResponse } from 'utils';
|
import { transformPagination, transformResponse } from 'utils';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
import { useRequestQuery } from '../useQueryRequest';
|
||||||
import useApiRequest from '../useRequest';
|
import useApiRequest from '../useRequest';
|
||||||
import t from './types';
|
import t from './types';
|
||||||
|
|
||||||
@@ -114,9 +114,7 @@ export function useInactivateItem(props) {
|
|||||||
const transformItemsResponse = (response) => {
|
const transformItemsResponse = (response) => {
|
||||||
return {
|
return {
|
||||||
items: response.data.items,
|
items: response.data.items,
|
||||||
pagination: transformPagination(
|
pagination: transformPagination(transformResponse(response.data.pagination)),
|
||||||
transformResponse(response.data.pagination)
|
|
||||||
),
|
|
||||||
filterMeta: transformResponse(response.data.filter_meta),
|
filterMeta: transformResponse(response.data.filter_meta),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -125,19 +123,21 @@ const transformItemsResponse = (response) => {
|
|||||||
* Retrieves items list.
|
* Retrieves items list.
|
||||||
*/
|
*/
|
||||||
export function useItems(query, props) {
|
export function useItems(query, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.ITEMS, query],
|
[t.ITEMS, query],
|
||||||
() => apiRequest.get(`items`, { params: query }).then(transformItemsResponse),
|
|
||||||
{
|
{
|
||||||
initialDataUpdatedAt: 0,
|
method: 'get',
|
||||||
initialData: {
|
url: 'items',
|
||||||
|
params: { ...query },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
select: transformItemsResponse,
|
||||||
|
defaultData: {
|
||||||
items: [],
|
items: [],
|
||||||
pagination: DEFAULT_PAGINATION,
|
pagination: DEFAULT_PAGINATION,
|
||||||
filterMeta: {},
|
filterMeta: {},
|
||||||
},
|
},
|
||||||
...props,
|
...props
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -147,14 +147,15 @@ export function useItems(query, props) {
|
|||||||
* @param {number} id - Item id.
|
* @param {number} id - Item id.
|
||||||
*/
|
*/
|
||||||
export function useItem(id, props) {
|
export function useItem(id, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.ITEM, id],
|
[t.ITEM, id],
|
||||||
() => apiRequest.get(`items/${id}`).then((response) => response.data.item),
|
|
||||||
{
|
{
|
||||||
initialDataUpdatedAt: 0,
|
method: 'get',
|
||||||
initialData: {},
|
url: `items/${id}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
select: (response) => response.data.item,
|
||||||
|
defaultData: {},
|
||||||
...props
|
...props
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { useMutation, useQueryClient } from 'react-query';
|
import { useMutation, useQueryClient } from 'react-query';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
import { useRequestQuery } from '../useQueryRequest';
|
||||||
import useApiRequest from '../useRequest';
|
import useApiRequest from '../useRequest';
|
||||||
import t from './types';
|
import t from './types';
|
||||||
|
|
||||||
@@ -68,27 +68,25 @@ export function useDeleteItemCategory(props) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const transformCategories = (res) => ({
|
||||||
|
itemsCategories: res.data.item_categories,
|
||||||
|
pagination: res.data.pagination,
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the items categories.
|
* Retrieve the items categories.
|
||||||
*/
|
*/
|
||||||
export function useItemsCategories(query, props) {
|
export function useItemsCategories(query, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.ITEMS_CATEGORIES, query],
|
[t.ITEMS_CATEGORIES, query],
|
||||||
() => apiRequest.get(`item_categories`, { params: query }),
|
{ method: 'get', url: `item_categories`, params: query },
|
||||||
{
|
{
|
||||||
select: (response) => ({
|
select: transformCategories,
|
||||||
itemsCategories: response.data.item_categories,
|
defaultData: {
|
||||||
pagination: response.data.pagination,
|
itemsCategories: [],
|
||||||
}),
|
|
||||||
initialDataUpdatedAt: 0,
|
|
||||||
initialData: {
|
|
||||||
data: {
|
|
||||||
item_categories: [],
|
|
||||||
pagination: {}
|
pagination: {}
|
||||||
},
|
},
|
||||||
},
|
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -99,15 +97,12 @@ export function useItemsCategories(query, props) {
|
|||||||
* @param {number} id - Item category.
|
* @param {number} id - Item category.
|
||||||
*/
|
*/
|
||||||
export function useItemCategory(id, props) {
|
export function useItemCategory(id, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.ITEM_CATEGORY, id],
|
[t.ITEM_CATEGORY, id],
|
||||||
() =>
|
{ method: 'get', url: `item_categories/${id}` },
|
||||||
apiRequest.get(`item_categories/${id}`).then((res) => res.data.category),
|
|
||||||
{
|
{
|
||||||
initialDataUpdatedAt: 0,
|
select: (res) => res.data.category,
|
||||||
initialData: {},
|
defaultData: {},
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { defaultTo } from 'lodash';
|
|
||||||
import { useMutation, useQueryClient } from 'react-query';
|
import { useMutation, useQueryClient } from 'react-query';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
import { useRequestQuery } from '../useQueryRequest';
|
||||||
import { transformPagination } from 'utils';
|
import { transformPagination } from 'utils';
|
||||||
import useApiRequest from '../useRequest';
|
import useApiRequest from '../useRequest';
|
||||||
import t from './types';
|
import t from './types';
|
||||||
@@ -108,46 +107,41 @@ export function usePublishJournal(props) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const transformJournals = (response) => ({
|
||||||
|
manualJournals: response.data.manual_journals,
|
||||||
|
pagination: transformPagination(response.data.pagination),
|
||||||
|
filterMeta: response.data.filter_meta
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the manual journals with pagination meta.
|
* Retrieve the manual journals with pagination meta.
|
||||||
*/
|
*/
|
||||||
export function useJournals(query, props) {
|
export function useJournals(query, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
const states = useQueryTenant(
|
|
||||||
[t.MANUAL_JOURNALS, query],
|
[t.MANUAL_JOURNALS, query],
|
||||||
() => apiRequest.get('manual-journals', { params: query }),
|
{ method: 'get', url: 'manual-journals', params: query },
|
||||||
{
|
{
|
||||||
select: (response) => ({
|
select: transformJournals,
|
||||||
manualJournals: response.data.manual_journals,
|
defaultData: {
|
||||||
pagination: transformPagination(response.data.pagination),
|
|
||||||
filterMeta: response.data.filter_meta
|
|
||||||
}),
|
|
||||||
...props,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...states,
|
|
||||||
data: defaultTo(states.data, {
|
|
||||||
manualJournals: [],
|
manualJournals: [],
|
||||||
pagination: {},
|
pagination: {},
|
||||||
filterMeta: {},
|
filterMeta: {},
|
||||||
}),
|
},
|
||||||
};
|
...props,
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the manual journal details.
|
* Retrieve the manual journal details.
|
||||||
*/
|
*/
|
||||||
export function useJournal(id, props) {
|
export function useJournal(id, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.MANUAL_JOURNAL, id],
|
[t.MANUAL_JOURNAL, id],
|
||||||
() => apiRequest.get(`manual-journals/${id}`),
|
{ method: 'get', url: `manual-journals/${id}` },
|
||||||
{
|
{
|
||||||
select: (res) => res.data.manual_journal,
|
select: (res) => res.data.manual_journal,
|
||||||
|
defaultData: {},
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { useMutation } from 'react-query';
|
import { useMutation } from 'react-query';
|
||||||
import t from './types';
|
import t from './types';
|
||||||
import useApiRequest from '../useRequest';
|
import useApiRequest from '../useRequest';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
import { useRequestQuery } from '../useQueryRequest';
|
||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
import { useSetOrganizations, useSetSubscriptions } from '../state';
|
import { useSetOrganizations, useSetSubscriptions } from '../state';
|
||||||
import { omit } from 'lodash';
|
import { omit } from 'lodash';
|
||||||
@@ -10,11 +10,9 @@ import { omit } from 'lodash';
|
|||||||
* Retrieve organizations of the authenticated user.
|
* Retrieve organizations of the authenticated user.
|
||||||
*/
|
*/
|
||||||
export function useOrganizations(props) {
|
export function useOrganizations(props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.ORGANIZATIONS],
|
[t.ORGANIZATIONS],
|
||||||
() => apiRequest.get(`organization/all`),
|
{ method: 'get', url: `organization/all` },
|
||||||
{
|
{
|
||||||
select: (res) => res.data.organizations,
|
select: (res) => res.data.organizations,
|
||||||
initialDataUpdatedAt: 0,
|
initialDataUpdatedAt: 0,
|
||||||
@@ -32,21 +30,15 @@ export function useOrganizations(props) {
|
|||||||
* Retrieve the current organization metadata.
|
* Retrieve the current organization metadata.
|
||||||
*/
|
*/
|
||||||
export function useCurrentOrganization(props) {
|
export function useCurrentOrganization(props) {
|
||||||
const apiRequest = useApiRequest();
|
|
||||||
const setOrganizations = useSetOrganizations();
|
const setOrganizations = useSetOrganizations();
|
||||||
const setSubscriptions = useSetSubscriptions();
|
const setSubscriptions = useSetSubscriptions();
|
||||||
|
|
||||||
const query = useQueryTenant(
|
const query = useRequestQuery(
|
||||||
[t.ORGANIZATION_CURRENT],
|
[t.ORGANIZATION_CURRENT],
|
||||||
() => apiRequest.get(`organization/current`),
|
{ method: 'get', url: `organization/current` },
|
||||||
{
|
{
|
||||||
select: (res) => res.data.organization,
|
select: (res) => res.data.organization,
|
||||||
initialDataUpdatedAt: 0,
|
defaultData: {},
|
||||||
initialData: {
|
|
||||||
data: {
|
|
||||||
organization: {},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { defaultTo } from 'lodash';
|
|
||||||
import { useMutation, useQueryClient } from 'react-query';
|
import { useMutation, useQueryClient } from 'react-query';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
import { useRequestQuery } from '../useQueryRequest';
|
||||||
import { transformPagination } from 'utils';
|
import { transformPagination } from 'utils';
|
||||||
import useApiRequest from '../useRequest';
|
import useApiRequest from '../useRequest';
|
||||||
import t from './types';
|
import t from './types';
|
||||||
@@ -34,29 +33,23 @@ const commonInvalidateQueries = (client) => {
|
|||||||
* Retrieve payment mades list.
|
* Retrieve payment mades list.
|
||||||
*/
|
*/
|
||||||
export function usePaymentMades(query, props) {
|
export function usePaymentMades(query, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
const states = useQueryTenant(
|
|
||||||
[t.PAYMENT_MADES, query],
|
[t.PAYMENT_MADES, query],
|
||||||
() => apiRequest.get('purchases/bill_payments', { params: query }),
|
{ url: 'purchases/bill_payments', params: query },
|
||||||
{
|
{
|
||||||
select: (res) => ({
|
select: (res) => ({
|
||||||
paymentMades: res.data.bill_payments,
|
paymentMades: res.data.bill_payments,
|
||||||
pagination: transformPagination(res.data.pagination),
|
pagination: transformPagination(res.data.pagination),
|
||||||
filterMeta: res.data.filter_meta,
|
filterMeta: res.data.filter_meta,
|
||||||
}),
|
}),
|
||||||
...props,
|
defaultData: {
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...states,
|
|
||||||
data: defaultTo(states.data, {
|
|
||||||
paymentMades: [],
|
paymentMades: [],
|
||||||
pagination: {},
|
pagination: {},
|
||||||
filterMeta: {},
|
filterMeta: {},
|
||||||
}),
|
},
|
||||||
};
|
...props,
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -126,24 +119,24 @@ export function useDeletePaymentMade(props) {
|
|||||||
* Retrieve specific payment made.
|
* Retrieve specific payment made.
|
||||||
*/
|
*/
|
||||||
export function usePaymentMadeEditPage(id, props) {
|
export function usePaymentMadeEditPage(id, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
const states = useQueryTenant(
|
|
||||||
[t.PAYMENT_MADE_EDIT_PAGE, id],
|
[t.PAYMENT_MADE_EDIT_PAGE, id],
|
||||||
() => apiRequest.get(`purchases/bill_payments/${id}/edit-page`),
|
{
|
||||||
|
method: 'get',
|
||||||
|
url: `purchases/bill_payments/${id}/edit-page`,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
select: (res) => ({
|
select: (res) => ({
|
||||||
paymentMade: res.data.bill_payment,
|
paymentMade: res.data.bill_payment,
|
||||||
entries: res.data.entries,
|
entries: res.data.entries,
|
||||||
}),
|
}),
|
||||||
|
defaultData: {
|
||||||
|
paymentMade: {},
|
||||||
|
entries: [],
|
||||||
|
},
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
|
||||||
...states,
|
|
||||||
data: defaultTo(states.data, {}),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -151,22 +144,16 @@ export function usePaymentMadeEditPage(id, props) {
|
|||||||
* @param {number} vendorId -
|
* @param {number} vendorId -
|
||||||
*/
|
*/
|
||||||
export function usePaymentMadeNewPageEntries(vendorId, props) {
|
export function usePaymentMadeNewPageEntries(vendorId, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.PAYMENT_MADE_NEW_ENTRIES, vendorId],
|
[t.PAYMENT_MADE_NEW_ENTRIES, vendorId],
|
||||||
() =>
|
{
|
||||||
apiRequest.get(`purchases/bill_payments/new-page/entries`, {
|
method: 'get',
|
||||||
|
url: `purchases/bill_payments/new-page/entries`,
|
||||||
params: { vendor_id: vendorId },
|
params: { vendor_id: vendorId },
|
||||||
}),
|
},
|
||||||
{
|
{
|
||||||
select: (res) => res.data.entries,
|
select: (res) => res.data.entries,
|
||||||
initialDataUpdatedAt: 0,
|
defaultData: [],
|
||||||
initialData: {
|
|
||||||
data: {
|
|
||||||
entries: [],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { useMutation, useQueryClient } from 'react-query';
|
import { useMutation, useQueryClient } from 'react-query';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
import { useRequestQuery } from '../useQueryRequest';
|
||||||
import { defaultTo } from 'lodash';
|
|
||||||
import useApiRequest from '../useRequest';
|
import useApiRequest from '../useRequest';
|
||||||
import { transformPagination, saveInvoke } from 'utils';
|
import { transformPagination, saveInvoke } from 'utils';
|
||||||
import t from './types';
|
import t from './types';
|
||||||
@@ -27,37 +26,30 @@ const commonInvalidateQueries = (client) => {
|
|||||||
client.invalidateQueries(t.CUSTOMER);
|
client.invalidateQueries(t.CUSTOMER);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Transform payment receives.
|
||||||
|
const transformPaymentReceives = (res) => ({
|
||||||
|
paymentReceives: res.data.payment_receives,
|
||||||
|
pagination: transformPagination(res.data.pagination),
|
||||||
|
filterMeta: res.data.filter_meta,
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve accounts list.
|
* Retrieve accounts list.
|
||||||
*/
|
*/
|
||||||
export function usePaymentReceives(query, props) {
|
export function usePaymentReceives(query, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
const states = useQueryTenant(
|
|
||||||
[t.PAYMENT_RECEIVES, query],
|
[t.PAYMENT_RECEIVES, query],
|
||||||
() => apiRequest.get('sales/payment_receives', { params: query }),
|
{ method: 'get', url: 'sales/payment_receives', params: query },
|
||||||
{
|
{
|
||||||
select: (res) => ({
|
select: transformPaymentReceives,
|
||||||
paymentReceives: res.data.payment_receives,
|
defaultData: {
|
||||||
pagination: transformPagination(res.data.pagination),
|
paymentReceives: [],
|
||||||
filterMeta: res.data.filter_meta,
|
pagination: { page: 1, pageSize: 12, total: 0 },
|
||||||
}),
|
filterMeta: {},
|
||||||
|
},
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
|
||||||
...states,
|
|
||||||
data: defaultTo(states.data, {
|
|
||||||
paymentReceives: [],
|
|
||||||
pagination: {
|
|
||||||
page: 1,
|
|
||||||
pageSize: 12,
|
|
||||||
total: 0,
|
|
||||||
},
|
|
||||||
filterMeta: {},
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -136,23 +128,15 @@ export function useDeletePaymentReceive(props) {
|
|||||||
* @param {number} id - Payment receive.
|
* @param {number} id - Payment receive.
|
||||||
*/
|
*/
|
||||||
export function usePaymentReceive(id, props) {
|
export function usePaymentReceive(id, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
const states = useQueryTenant(
|
|
||||||
[t.PAYMENT_RECEIVE, id],
|
[t.PAYMENT_RECEIVE, id],
|
||||||
() => apiRequest.get(`sales/payment_receives/${id}`),
|
{ method: 'get', url: `sales/payment_receives/${id}` },
|
||||||
{
|
{
|
||||||
select: (res) => ({
|
select: (res) => res.data.payment_receive,
|
||||||
paymentReceive: res.data.payment_receive,
|
defaultData: {},
|
||||||
}),
|
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
|
||||||
...states,
|
|
||||||
data: defaultTo(states.data, {}),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -160,25 +144,19 @@ export function usePaymentReceive(id, props) {
|
|||||||
* @param {number} id - Payment receive id.
|
* @param {number} id - Payment receive id.
|
||||||
*/
|
*/
|
||||||
export function usePaymentReceiveEditPage(id, props) {
|
export function usePaymentReceiveEditPage(id, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
const states = useQueryTenant(
|
|
||||||
[t.PAYMENT_RECEIVE_EDIT_PAGE, id],
|
[t.PAYMENT_RECEIVE_EDIT_PAGE, id],
|
||||||
() => apiRequest.get(`sales/payment_receives/${id}/edit-page`),
|
{ method: 'get', url: `sales/payment_receives/${id}/edit-page` },
|
||||||
{
|
{
|
||||||
select: (res) => ({
|
select: (res) => ({
|
||||||
paymentReceive: res.data.payment_receive,
|
paymentReceive: res.data.payment_receive,
|
||||||
entries: res.data.entries,
|
entries: res.data.entries,
|
||||||
}),
|
}),
|
||||||
|
defaultData: {
|
||||||
|
paymentReceive: {},
|
||||||
|
entries: [],
|
||||||
|
},
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
|
||||||
...states,
|
|
||||||
data: defaultTo(states.data, {
|
|
||||||
paymentReceive: {},
|
|
||||||
entries: [],
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { useQueryClient, useMutation } from 'react-query';
|
import { useQueryClient, useMutation } from 'react-query';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
import { useRequestQuery } from '../useQueryRequest';
|
||||||
import { defaultTo } from 'lodash';
|
|
||||||
import useApiRequest from '../useRequest';
|
import useApiRequest from '../useRequest';
|
||||||
import { transformPagination } from 'utils';
|
import { transformPagination } from 'utils';
|
||||||
import t from './types';
|
import t from './types';
|
||||||
@@ -99,28 +98,22 @@ export function useCloseReceipt(props) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const transformReceipts = (res) => ({
|
||||||
|
receipts: res.data.sale_receipts,
|
||||||
|
pagination: transformPagination(res.data.pagination),
|
||||||
|
filterMeta: res.data.filter_meta,
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve sale invoices list with pagination meta.
|
* Retrieve sale invoices list with pagination meta.
|
||||||
*/
|
*/
|
||||||
export function useReceipts(query, props) {
|
export function useReceipts(query, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
const states = useQueryTenant(
|
|
||||||
['SALE_RECEIPTS', query],
|
['SALE_RECEIPTS', query],
|
||||||
() => apiRequest.get('sales/receipts', { params: query }),
|
{ method: 'get', url: 'sales/receipts', params: query },
|
||||||
{
|
{
|
||||||
select: (response) => ({
|
select: transformReceipts,
|
||||||
receipts: response.data.sale_receipts,
|
defaultData: {
|
||||||
pagination: transformPagination(response.data.pagination),
|
|
||||||
filterMeta: response.data.filter_meta,
|
|
||||||
}),
|
|
||||||
...props,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...states,
|
|
||||||
data: defaultTo(states.data, {
|
|
||||||
receipts: [],
|
receipts: [],
|
||||||
pagination: {
|
pagination: {
|
||||||
page: 1,
|
page: 1,
|
||||||
@@ -128,27 +121,23 @@ export function useReceipts(query, props) {
|
|||||||
total: 0,
|
total: 0,
|
||||||
},
|
},
|
||||||
filterMeta: {},
|
filterMeta: {},
|
||||||
}),
|
},
|
||||||
};
|
...props,
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve sale invoices list with pagination meta.
|
* Retrieve sale invoices list with pagination meta.
|
||||||
*/
|
*/
|
||||||
export function useReceipt(id, props) {
|
export function useReceipt(id, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
const states = useQueryTenant(
|
|
||||||
['SALE_RECEIPT', id],
|
['SALE_RECEIPT', id],
|
||||||
() => apiRequest.get(`sales/receipts/${id}`),
|
{ method: 'get', url: `sales/receipts/${id}` },
|
||||||
{
|
{
|
||||||
select: (res) => res.data.sale_receipt,
|
select: (res) => res.data.sale_receipt,
|
||||||
|
defaultData: {},
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
|
||||||
...states,
|
|
||||||
data: defaultTo(states.data, {}),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
import { useMutation, useQueryClient } from 'react-query';
|
import { useMutation, useQueryClient } from 'react-query';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
import { useRequestQuery } from '../useQueryRequest';
|
||||||
import useApiRequest from '../useRequest';
|
import useApiRequest from '../useRequest';
|
||||||
import { useSetSettings } from 'hooks/state';
|
import { useSetSettings } from 'hooks/state';
|
||||||
import t from './types';
|
import t from './types';
|
||||||
@@ -21,20 +21,14 @@ export function useSaveSettings(props) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function useSettingsQuery(key, query, props) {
|
function useSettingsQuery(key, query, props) {
|
||||||
const apiRequest = useApiRequest();
|
|
||||||
const setSettings = useSetSettings();
|
const setSettings = useSetSettings();
|
||||||
|
|
||||||
const state = useQueryTenant(
|
const state = useRequestQuery(
|
||||||
key,
|
key,
|
||||||
() => apiRequest.get('settings', { params: query }),
|
{ method: 'get', url: 'settings', params: query },
|
||||||
{
|
{
|
||||||
select: (res) => res.data.settings,
|
select: (res) => res.data.settings,
|
||||||
initialDataUpdatedAt: 0,
|
defaultData: [],
|
||||||
initialData: {
|
|
||||||
data: {
|
|
||||||
settings: [],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { useMutation, useQueryClient } from 'react-query';
|
import { useMutation, useQueryClient } from 'react-query';
|
||||||
import { defaultTo } from 'lodash';
|
import { defaultTo } from 'lodash';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
import { useQueryTenant } from '../useQueryRequest';
|
||||||
import useApiRequest from '../useRequest';
|
import useApiRequest from '../useRequest';
|
||||||
import t from './types';
|
import t from './types';
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { useMutation, useQueryClient } from 'react-query';
|
|||||||
import t from './types';
|
import t from './types';
|
||||||
import { transformPagination } from 'utils';
|
import { transformPagination } from 'utils';
|
||||||
import useApiRequest from '../useRequest';
|
import useApiRequest from '../useRequest';
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
import { useRequestQuery } from '../useQueryRequest';
|
||||||
|
|
||||||
// Common invalidate queries.
|
// Common invalidate queries.
|
||||||
const commonInvalidateQueries = (queryClient) => {
|
const commonInvalidateQueries = (queryClient) => {
|
||||||
@@ -17,28 +17,26 @@ const commonInvalidateQueries = (queryClient) => {
|
|||||||
queryClient.invalidateQueries(t.FINANCIAL_REPORT);
|
queryClient.invalidateQueries(t.FINANCIAL_REPORT);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Transformes vendors response.
|
||||||
|
const transformVendorsResponse = (res) => ({
|
||||||
|
vendors: res.data.vendors,
|
||||||
|
pagination: transformPagination(res.data.pagination),
|
||||||
|
filterMeta: res.data.filter_meta,
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve vendors list.
|
* Retrieve vendors list.
|
||||||
*/
|
*/
|
||||||
export function useVendors(query, props) {
|
export function useVendors(query, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
[t.VENDORS, query],
|
[t.VENDORS, query],
|
||||||
() => apiRequest.get(`vendors`, { params: query }),
|
{ method: 'get', url: `vendors`, params: query },
|
||||||
{
|
{
|
||||||
select: (res) => ({
|
select: transformVendorsResponse,
|
||||||
vendors: res.data.vendors,
|
defaultData: {
|
||||||
pagination: transformPagination(res.data.pagination),
|
|
||||||
filterMeta: res.data.filter_meta,
|
|
||||||
}),
|
|
||||||
initialDataUpdatedAt: 0,
|
|
||||||
initialData: {
|
|
||||||
data: {
|
|
||||||
vendors: [],
|
vendors: [],
|
||||||
pagination: {},
|
pagination: {},
|
||||||
filter_meta: {},
|
filterMeta: {},
|
||||||
},
|
|
||||||
},
|
},
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
@@ -106,14 +104,13 @@ export function useCreateVendor(props) {
|
|||||||
* Retrieve vendor details.
|
* Retrieve vendor details.
|
||||||
*/
|
*/
|
||||||
export function useVendor(id, props) {
|
export function useVendor(id, props) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
[t.VENDOR, id],
|
||||||
return useQueryTenant([t.VENDOR, id], () => apiRequest.get(`vendors/${id}`), {
|
{ method: 'get', url: `vendors/${id}` },
|
||||||
|
{
|
||||||
select: (res) => res.data.vendor,
|
select: (res) => res.data.vendor,
|
||||||
initialDataUpdatedAt: 0,
|
defaultData: {},
|
||||||
initialData: {
|
|
||||||
data: { vendor: {} },
|
|
||||||
},
|
|
||||||
...props,
|
...props,
|
||||||
});
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,46 +1,46 @@
|
|||||||
import { defaultTo } from 'lodash';
|
import { useRequestQuery } from '../useQueryRequest';
|
||||||
import useApiRequest from '../useRequest';
|
|
||||||
import { useQueryTenant } from '../useQueryTenant';
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the resource views.
|
||||||
|
* @param {string} resourceSlug - Resource slug.
|
||||||
|
*/
|
||||||
export function useResourceViews(resourceSlug) {
|
export function useResourceViews(resourceSlug) {
|
||||||
const apiRequest = useApiRequest();
|
return useRequestQuery(
|
||||||
|
|
||||||
const states = useQueryTenant(
|
|
||||||
['RESOURCE_VIEW', resourceSlug],
|
['RESOURCE_VIEW', resourceSlug],
|
||||||
() => apiRequest.get(`views/resource/${resourceSlug}`)
|
{ method: 'get', url: `views/resource/${resourceSlug}` },
|
||||||
.then((response) => response.data.views),
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...states,
|
|
||||||
data: defaultTo(states.data, []),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function useResourceColumns(resourceSlug) {
|
|
||||||
const apiRequest = useApiRequest();
|
|
||||||
|
|
||||||
return useQueryTenant(
|
|
||||||
['RESOURCE_COLUMNS', resourceSlug],
|
|
||||||
() => apiRequest.get(`resources/${resourceSlug}/columns`),
|
|
||||||
{
|
{
|
||||||
initialData: [],
|
select: (response) => response.data.views,
|
||||||
|
defaultData: [],
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useResourceFields(resourceSlug, props) {
|
/**
|
||||||
const apiRequest = useApiRequest();
|
* Retrieve the resource columns.
|
||||||
|
* @param {string} resourceSlug - Resource slug.
|
||||||
|
*/
|
||||||
|
export function useResourceColumns(resourceSlug) {
|
||||||
|
return useRequestQuery(
|
||||||
|
['RESOURCE_COLUMNS', resourceSlug],
|
||||||
|
{ method: 'get', url: `resources/${resourceSlug}/columns` },
|
||||||
|
{
|
||||||
|
defaultData: [],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const states = useQueryTenant(
|
/**
|
||||||
|
* Retrieve the resource fields.
|
||||||
|
* @param {string} resourceSlug - Resource slug.
|
||||||
|
*/
|
||||||
|
export function useResourceFields(resourceSlug, props) {
|
||||||
|
return useRequestQuery(
|
||||||
['RESOURCE_FIELDS', resourceSlug],
|
['RESOURCE_FIELDS', resourceSlug],
|
||||||
() => apiRequest.get(`resources/${resourceSlug}/fields`)
|
{ method: 'get', url: `resources/${resourceSlug}/fields` },
|
||||||
.then((res) => res.data.resource_fields),
|
{
|
||||||
|
select: (res) => res.data.resource_fields,
|
||||||
|
defaultData: [],
|
||||||
|
},
|
||||||
props
|
props
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
|
||||||
...states,
|
|
||||||
data: defaultTo(states.data, []),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
import { useDispatch, useSelector } from 'react-redux';
|
import { useDispatch, useSelector } from 'react-redux';
|
||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
import { isAuthenticated } from 'store/authentication/authentication.reducer';
|
import { isAuthenticated } from 'store/authentication/authentication.reducer';
|
||||||
import { setLogin, setLogout } from 'store/authentication/authentication.actions';
|
import {
|
||||||
|
setLogin,
|
||||||
|
setLogout,
|
||||||
|
setStoreReset,
|
||||||
|
} from 'store/authentication/authentication.actions';
|
||||||
|
import { purgePersistedState } from 'store/createStore';
|
||||||
import { useQueryClient } from 'react-query';
|
import { useQueryClient } from 'react-query';
|
||||||
|
|
||||||
export const useAuthActions = () => {
|
export const useAuthActions = () => {
|
||||||
@@ -11,9 +16,8 @@ export const useAuthActions = () => {
|
|||||||
return {
|
return {
|
||||||
setLogin: useCallback((login) => dispatch(setLogin(login)), [dispatch]),
|
setLogin: useCallback((login) => dispatch(setLogin(login)), [dispatch]),
|
||||||
setLogout: useCallback(() => {
|
setLogout: useCallback(() => {
|
||||||
|
// Resets store state.
|
||||||
// Logout action.
|
dispatch(setStoreReset());
|
||||||
dispatch(setLogout());
|
|
||||||
|
|
||||||
// Remove all cached queries.
|
// Remove all cached queries.
|
||||||
queryClient.removeQueries();
|
queryClient.removeQueries();
|
||||||
|
|||||||
31
client/src/hooks/useQueryRequest.js
Normal file
31
client/src/hooks/useQueryRequest.js
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import { useQuery } from 'react-query';
|
||||||
|
import { castArray, defaultTo } from 'lodash';
|
||||||
|
import { useAuthOrganizationId } from './state';
|
||||||
|
import useApiRequest from './useRequest';
|
||||||
|
import { useRef } from 'react';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Query for tenant requests.
|
||||||
|
*/
|
||||||
|
export function useQueryTenant(query, callback, props) {
|
||||||
|
const organizationId = useAuthOrganizationId();
|
||||||
|
|
||||||
|
return useQuery([...castArray(query), organizationId], callback, props);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useRequestQuery(query, axios, props) {
|
||||||
|
const apiRequest = useApiRequest();
|
||||||
|
|
||||||
|
const states = useQuery(
|
||||||
|
query,
|
||||||
|
() => apiRequest.http({ ...axios, url: `/api/${axios.url}` }),
|
||||||
|
props,
|
||||||
|
);
|
||||||
|
// Momerize the default data.
|
||||||
|
const defaultData = useRef(props.defaultData || undefined);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...states,
|
||||||
|
data: defaultTo(states.data, defaultData.current),
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
import { useQuery } from 'react-query';
|
|
||||||
import { castArray } from 'lodash';
|
|
||||||
import { useAuthOrganizationId } from './state';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Query for tenant requests.
|
|
||||||
*/
|
|
||||||
export function useQueryTenant(query, callback, props) {
|
|
||||||
const organizationId = useAuthOrganizationId();
|
|
||||||
|
|
||||||
return useQuery([...castArray(query), organizationId], callback, props);
|
|
||||||
}
|
|
||||||
@@ -61,6 +61,8 @@ export default function useApiRequest() {
|
|||||||
}, [token, organizationId, setGlobalErrors, setLogout]);
|
}, [token, organizationId, setGlobalErrors, setLogout]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
http,
|
||||||
|
|
||||||
get(resource, params) {
|
get(resource, params) {
|
||||||
return http.get(`/api/${resource}`, params);
|
return http.get(`/api/${resource}`, params);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { createReducer } from '@reduxjs/toolkit';
|
import { createReducer } from '@reduxjs/toolkit';
|
||||||
import {
|
import { persistReducer } from 'redux-persist';
|
||||||
createTableStateReducers,
|
import storage from 'redux-persist/lib/storage';
|
||||||
} from 'store/tableState.reducer';
|
import { createTableStateReducers } from 'store/tableState.reducer';
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
tableState: {
|
tableState: {
|
||||||
@@ -10,6 +10,17 @@ const initialState = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default createReducer(initialState, {
|
const STORAGE_KEY = 'bigcapital:bills';
|
||||||
|
|
||||||
|
const reducerInstance = createReducer(initialState, {
|
||||||
...createTableStateReducers('BILLS'),
|
...createTableStateReducers('BILLS'),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export default persistReducer(
|
||||||
|
{
|
||||||
|
key: STORAGE_KEY,
|
||||||
|
whitelist: ['tableState'],
|
||||||
|
storage,
|
||||||
|
},
|
||||||
|
reducerInstance,
|
||||||
|
);
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
import { createReducer } from '@reduxjs/toolkit';
|
import { createReducer } from '@reduxjs/toolkit';
|
||||||
|
import { persistReducer } from 'redux-persist';
|
||||||
|
import storage from 'redux-persist/lib/storage';
|
||||||
import {
|
import {
|
||||||
createTableStateReducers,
|
createTableStateReducers,
|
||||||
} from 'store/tableState.reducer';
|
} from 'store/tableState.reducer';
|
||||||
@@ -10,6 +12,17 @@ const initialState = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default createReducer(initialState, {
|
const reducerInstance = createReducer(initialState, {
|
||||||
...createTableStateReducers('ESTIMATES'),
|
...createTableStateReducers('ESTIMATES'),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const STORAGE_KEY = 'bigcapital:estimates';
|
||||||
|
|
||||||
|
export default persistReducer(
|
||||||
|
{
|
||||||
|
key: STORAGE_KEY,
|
||||||
|
whitelist: ['tableState'],
|
||||||
|
storage,
|
||||||
|
},
|
||||||
|
reducerInstance,
|
||||||
|
);
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import { createReducer } from '@reduxjs/toolkit';
|
import { createReducer } from '@reduxjs/toolkit';
|
||||||
|
import { persistReducer } from 'redux-persist';
|
||||||
|
import storage from 'redux-persist/lib/storage';
|
||||||
import {
|
import {
|
||||||
createTableStateReducers,
|
createTableStateReducers,
|
||||||
} from 'store/tableState.reducer';
|
} from 'store/tableState.reducer';
|
||||||
@@ -10,6 +12,17 @@ const initialState = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default createReducer(initialState, {
|
const reducerInstance = createReducer(initialState, {
|
||||||
...createTableStateReducers('INVOICES'),
|
...createTableStateReducers('INVOICES'),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const STORAGE_KEY = 'bigcapital:invoices';
|
||||||
|
|
||||||
|
export default persistReducer(
|
||||||
|
{
|
||||||
|
key: STORAGE_KEY,
|
||||||
|
whitelist: ['tableState'],
|
||||||
|
storage,
|
||||||
|
},
|
||||||
|
reducerInstance,
|
||||||
|
);
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import { createReducer } from '@reduxjs/toolkit';
|
import { createReducer } from '@reduxjs/toolkit';
|
||||||
|
import { persistReducer } from 'redux-persist';
|
||||||
|
import storage from 'redux-persist/lib/storage';
|
||||||
import {
|
import {
|
||||||
createTableStateReducers,
|
createTableStateReducers,
|
||||||
} from 'store/tableState.reducer';
|
} from 'store/tableState.reducer';
|
||||||
@@ -11,6 +13,17 @@ const initialState = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default createReducer(initialState, {
|
const reducerInstance = createReducer(initialState, {
|
||||||
...createTableStateReducers('PAYMENT_MADES'),
|
...createTableStateReducers('PAYMENT_MADES'),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const STORAGE_KEY = 'bigcapital:paymentMades';
|
||||||
|
|
||||||
|
export default persistReducer(
|
||||||
|
{
|
||||||
|
key: STORAGE_KEY,
|
||||||
|
whitelist: ['tableState'],
|
||||||
|
storage,
|
||||||
|
},
|
||||||
|
reducerInstance,
|
||||||
|
);
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import { createReducer } from '@reduxjs/toolkit';
|
import { createReducer } from '@reduxjs/toolkit';
|
||||||
|
import { persistReducer } from 'redux-persist';
|
||||||
|
import storage from 'redux-persist/lib/storage';
|
||||||
import {
|
import {
|
||||||
createTableStateReducers,
|
createTableStateReducers,
|
||||||
} from 'store/tableState.reducer';
|
} from 'store/tableState.reducer';
|
||||||
@@ -10,6 +12,17 @@ const initialState = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default createReducer(initialState, {
|
const reducerInstance = createReducer(initialState, {
|
||||||
...createTableStateReducers('PAYMENT_RECEIVES'),
|
...createTableStateReducers('PAYMENT_RECEIVES'),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const STORAGE_KEY = 'bigcapital:paymentReceives';
|
||||||
|
|
||||||
|
export default persistReducer(
|
||||||
|
{
|
||||||
|
key: STORAGE_KEY,
|
||||||
|
whitelist: ['tableState'],
|
||||||
|
storage,
|
||||||
|
},
|
||||||
|
reducerInstance,
|
||||||
|
);
|
||||||
|
|||||||
@@ -1,14 +1,25 @@
|
|||||||
import { createReducer} from '@reduxjs/toolkit';
|
import { createReducer} from '@reduxjs/toolkit';
|
||||||
|
import { persistReducer } from 'redux-persist';
|
||||||
|
import storage from 'redux-persist/lib/storage';
|
||||||
import {
|
import {
|
||||||
createTableStateReducers,
|
createTableStateReducers,
|
||||||
} from 'store/tableState.reducer';
|
} from 'store/tableState.reducer';
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
tableState: {
|
tableState: {},
|
||||||
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default createReducer(initialState, {
|
const reducerInstance = createReducer(initialState, {
|
||||||
...createTableStateReducers('ACCOUNTS'),
|
...createTableStateReducers('ACCOUNTS'),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const STORAGE_KEY = 'bigcapital:accounts';
|
||||||
|
|
||||||
|
export default persistReducer(
|
||||||
|
{
|
||||||
|
key: STORAGE_KEY,
|
||||||
|
whitelist: ['tableState'],
|
||||||
|
storage,
|
||||||
|
},
|
||||||
|
reducerInstance,
|
||||||
|
);
|
||||||
|
|||||||
@@ -2,13 +2,8 @@ import t from 'store/types';
|
|||||||
|
|
||||||
export const setLogin = ({ user, token, tenant }) => ({
|
export const setLogin = ({ user, token, tenant }) => ({
|
||||||
type: t.LOGIN_SUCCESS,
|
type: t.LOGIN_SUCCESS,
|
||||||
payload: {
|
payload: { user, token, tenant, },
|
||||||
user,
|
|
||||||
token,
|
|
||||||
tenant,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export const setLogout = () => ({
|
export const setLogout = () => ({ type: t.LOGOUT });
|
||||||
type: t.LOGOUT,
|
export const setStoreReset = () => ({ type: t.RESET });
|
||||||
});
|
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
import { createReducer } from '@reduxjs/toolkit';
|
import { createReducer } from '@reduxjs/toolkit';
|
||||||
|
import { persistReducer } from 'redux-persist';
|
||||||
|
import storage from 'redux-persist/lib/storage';
|
||||||
import t from 'store/types';
|
import t from 'store/types';
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
@@ -11,7 +13,9 @@ const initialState = {
|
|||||||
errors: [],
|
errors: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
export default createReducer(initialState, {
|
const STORAGE_KEY = 'bigcapital:authentication';
|
||||||
|
|
||||||
|
const reducerInstance = createReducer(initialState, {
|
||||||
[t.LOGIN_SUCCESS]: (state, action) => {
|
[t.LOGIN_SUCCESS]: (state, action) => {
|
||||||
const { token, user, tenant } = action.payload;
|
const { token, user, tenant } = action.payload;
|
||||||
state.token = token;
|
state.token = token;
|
||||||
@@ -25,19 +29,20 @@ export default createReducer(initialState, {
|
|||||||
state.errors = action.errors;
|
state.errors = action.errors;
|
||||||
},
|
},
|
||||||
|
|
||||||
[t.LOGOUT]: (state) => {
|
|
||||||
state.token = '';
|
|
||||||
state.user = {};
|
|
||||||
state.organization = '';
|
|
||||||
state.organizationId = null;
|
|
||||||
state.tenant = {};
|
|
||||||
},
|
|
||||||
|
|
||||||
[t.LOGIN_CLEAR_ERRORS]: (state) => {
|
[t.LOGIN_CLEAR_ERRORS]: (state) => {
|
||||||
state.errors = [];
|
state.errors = [];
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export default persistReducer(
|
||||||
|
{
|
||||||
|
key: STORAGE_KEY,
|
||||||
|
blacklist: ['errors'],
|
||||||
|
storage,
|
||||||
|
},
|
||||||
|
reducerInstance,
|
||||||
|
);
|
||||||
|
|
||||||
export const isAuthenticated = (state) => !!state.authentication.token;
|
export const isAuthenticated = (state) => !!state.authentication.token;
|
||||||
export const hasErrorType = (state, errorType) => {
|
export const hasErrorType = (state, errorType) => {
|
||||||
return state.authentication.errors.find((e) => e.type === errorType);
|
return state.authentication.errors.find((e) => e.type === errorType);
|
||||||
|
|||||||
@@ -5,4 +5,5 @@ export default {
|
|||||||
LOGIN_FAILURE: 'LOGIN_FAILURE',
|
LOGIN_FAILURE: 'LOGIN_FAILURE',
|
||||||
LOGOUT: 'LOGOUT',
|
LOGOUT: 'LOGOUT',
|
||||||
LOGIN_CLEAR_ERRORS: 'LOGIN_CLEAR_ERRORS',
|
LOGIN_CLEAR_ERRORS: 'LOGIN_CLEAR_ERRORS',
|
||||||
|
RESET: 'RESET',
|
||||||
};
|
};
|
||||||
@@ -4,7 +4,7 @@ import {
|
|||||||
compose,
|
compose,
|
||||||
} from 'redux';
|
} from 'redux';
|
||||||
import thunkMiddleware from 'redux-thunk';
|
import thunkMiddleware from 'redux-thunk';
|
||||||
import { persistStore, persistReducer } from 'redux-persist';
|
import { persistStore, persistReducer, purgeStoredState } from 'redux-persist';
|
||||||
import storage from 'redux-persist/lib/storage';
|
import storage from 'redux-persist/lib/storage';
|
||||||
import monitorReducerEnhancer from 'store/enhancers/monitorReducer';
|
import monitorReducerEnhancer from 'store/enhancers/monitorReducer';
|
||||||
import loggerMiddleware from 'middleware/logger';
|
import loggerMiddleware from 'middleware/logger';
|
||||||
@@ -44,7 +44,7 @@ const createStoreFactory = (initialState = {}) => {
|
|||||||
|--------------------------------------------------
|
|--------------------------------------------------
|
||||||
*/
|
*/
|
||||||
const store = createReduxStore(
|
const store = createReduxStore(
|
||||||
persistReducer(persistConfig, rootReducer),
|
rootReducer,
|
||||||
initialState,
|
initialState,
|
||||||
composeEnhancers(applyMiddleware(...middleware), ...enhancers),
|
composeEnhancers(applyMiddleware(...middleware), ...enhancers),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import { createReducer } from '@reduxjs/toolkit';
|
import { createReducer } from '@reduxjs/toolkit';
|
||||||
|
import { persistReducer } from 'redux-persist';
|
||||||
|
import storage from 'redux-persist/lib/storage';
|
||||||
import { createTableStateReducers } from 'store/tableState.reducer';
|
import { createTableStateReducers } from 'store/tableState.reducer';
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
@@ -8,6 +10,17 @@ const initialState = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default createReducer(initialState, {
|
const reducerInstance = createReducer(initialState, {
|
||||||
...createTableStateReducers('CUSTOMERS'),
|
...createTableStateReducers('CUSTOMERS'),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const STORAGE_KEY = 'bigcapital:estimates';
|
||||||
|
|
||||||
|
export default persistReducer(
|
||||||
|
{
|
||||||
|
key: STORAGE_KEY,
|
||||||
|
whitelist: ['tableState'],
|
||||||
|
storage,
|
||||||
|
},
|
||||||
|
reducerInstance,
|
||||||
|
);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import t from 'store/types';
|
import t from 'store/types';
|
||||||
import { createReducer } from '@reduxjs/toolkit';
|
import { createReducer } from '@reduxjs/toolkit';
|
||||||
import { persistReducer } from 'redux-persist';
|
import { persistReducer } from 'redux-persist';
|
||||||
import storage from 'redux-persist/lib/storage'; // defaults to localStorage for web
|
import storage from 'redux-persist/lib/storage';
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
pageTitle: '',
|
pageTitle: '',
|
||||||
@@ -18,6 +18,8 @@ const initialState = {
|
|||||||
backLink: false,
|
backLink: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const STORAGE_KEY = 'bigcapital:dashboard';
|
||||||
|
|
||||||
const reducerInstance = createReducer(initialState, {
|
const reducerInstance = createReducer(initialState, {
|
||||||
[t.CHANGE_DASHBOARD_PAGE_TITLE]: (state, action) => {
|
[t.CHANGE_DASHBOARD_PAGE_TITLE]: (state, action) => {
|
||||||
state.pageTitle = action.pageTitle;
|
state.pageTitle = action.pageTitle;
|
||||||
@@ -117,14 +119,10 @@ const reducerInstance = createReducer(initialState, {
|
|||||||
|
|
||||||
export default persistReducer(
|
export default persistReducer(
|
||||||
{
|
{
|
||||||
key: 'bigcapital:dashboard',
|
key: STORAGE_KEY,
|
||||||
blacklist: [
|
whitelist: [
|
||||||
'pageTitle',
|
'sidebarExpended',
|
||||||
'pageSubtitle',
|
'previousSidebarExpended',
|
||||||
'pageHint',
|
|
||||||
'preferencesPageTitle',
|
|
||||||
'topbarEditViewId',
|
|
||||||
'backLink',
|
|
||||||
],
|
],
|
||||||
storage,
|
storage,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { createReducer } from '@reduxjs/toolkit';
|
import { createReducer } from '@reduxjs/toolkit';
|
||||||
import {
|
import { persistReducer } from 'redux-persist';
|
||||||
createTableStateReducers,
|
import storage from 'redux-persist/lib/storage';
|
||||||
} from 'store/tableState.reducer';
|
import { createTableStateReducers } from 'store/tableState.reducer';
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
tableState: {
|
tableState: {
|
||||||
@@ -10,6 +10,17 @@ const initialState = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default createReducer(initialState, {
|
const reducerInstance = createReducer(initialState, {
|
||||||
...createTableStateReducers('EXPENSES'),
|
...createTableStateReducers('EXPENSES'),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const STORAGE_KEY = 'bigcapital:expenses';
|
||||||
|
|
||||||
|
export default persistReducer(
|
||||||
|
{
|
||||||
|
key: STORAGE_KEY,
|
||||||
|
whitelist: ['tableState'],
|
||||||
|
storage,
|
||||||
|
},
|
||||||
|
reducerInstance,
|
||||||
|
);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { createReducer } from '@reduxjs/toolkit';
|
import { createReducer } from '@reduxjs/toolkit';
|
||||||
import {
|
import { persistReducer } from 'redux-persist';
|
||||||
createTableStateReducers,
|
import storage from 'redux-persist/lib/storage';
|
||||||
} from 'store/tableState.reducer';
|
import { createTableStateReducers } from 'store/tableState.reducer';
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
tableState: {
|
tableState: {
|
||||||
@@ -12,6 +12,17 @@ const initialState = {
|
|||||||
selectedRows: [],
|
selectedRows: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
export default createReducer(initialState, {
|
const reducerInstance = createReducer(initialState, {
|
||||||
...createTableStateReducers('INVENTORY_ADJUSTMENTS'),
|
...createTableStateReducers('INVENTORY_ADJUSTMENTS'),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const STORAGE_KEY = 'bigcapital:inventoryAdjustments';
|
||||||
|
|
||||||
|
export default persistReducer(
|
||||||
|
{
|
||||||
|
key: STORAGE_KEY,
|
||||||
|
whitelist: ['tableState'],
|
||||||
|
storage,
|
||||||
|
},
|
||||||
|
reducerInstance,
|
||||||
|
);
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import { createReducer } from '@reduxjs/toolkit';
|
import { createReducer } from '@reduxjs/toolkit';
|
||||||
|
import { persistReducer } from 'redux-persist';
|
||||||
|
import storage from 'redux-persist/lib/storage';
|
||||||
import {
|
import {
|
||||||
createTableStateReducers,
|
createTableStateReducers,
|
||||||
} from 'store/tableState.reducer';
|
} from 'store/tableState.reducer';
|
||||||
@@ -8,6 +10,18 @@ const initialState = {
|
|||||||
tableState: {},
|
tableState: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default createReducer(initialState, {
|
const reducerInstance = createReducer(initialState, {
|
||||||
...createTableStateReducers('ITEMS_CATEGORIES'),
|
...createTableStateReducers('ITEMS_CATEGORIES'),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
const STORAGE_KEY = 'bigcapital:itemCategories';
|
||||||
|
|
||||||
|
export default persistReducer(
|
||||||
|
{
|
||||||
|
key: STORAGE_KEY,
|
||||||
|
whitelist: ['tableState'],
|
||||||
|
storage,
|
||||||
|
},
|
||||||
|
reducerInstance,
|
||||||
|
);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { createReducer } from '@reduxjs/toolkit';
|
import { createReducer } from '@reduxjs/toolkit';
|
||||||
import {
|
import { persistReducer } from 'redux-persist';
|
||||||
createTableStateReducers,
|
import storage from 'redux-persist/lib/storage';
|
||||||
} from 'store/tableState.reducer';
|
import { createTableStateReducers } from 'store/tableState.reducer';
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
tableState: {
|
tableState: {
|
||||||
@@ -12,6 +12,17 @@ const initialState = {
|
|||||||
selectedRows: [],
|
selectedRows: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
export default createReducer(initialState, {
|
const reducerInstance = createReducer(initialState, {
|
||||||
...createTableStateReducers('ITEMS'),
|
...createTableStateReducers('ITEMS'),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const STORAGE_KEY = 'bigcapital:items';
|
||||||
|
|
||||||
|
export default persistReducer(
|
||||||
|
{
|
||||||
|
key: STORAGE_KEY,
|
||||||
|
whitelist: ['tableState'],
|
||||||
|
storage,
|
||||||
|
},
|
||||||
|
reducerInstance,
|
||||||
|
);
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { createReducer } from '@reduxjs/toolkit';
|
import { createReducer } from '@reduxjs/toolkit';
|
||||||
import {
|
import { persistReducer } from 'redux-persist';
|
||||||
createTableStateReducers,
|
import storage from 'redux-persist/lib/storage';
|
||||||
} from 'store/tableState.reducer';
|
import { createTableStateReducers } from 'store/tableState.reducer';
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
tableState: {
|
tableState: {
|
||||||
@@ -10,6 +10,17 @@ const initialState = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default createReducer(initialState, {
|
const reducerInstance = createReducer(initialState, {
|
||||||
...createTableStateReducers('MANUAL_JOURNALS'),
|
...createTableStateReducers('MANUAL_JOURNALS'),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const STORAGE_KEY = 'bigcapital:manualJournals';
|
||||||
|
|
||||||
|
export default persistReducer(
|
||||||
|
{
|
||||||
|
key: STORAGE_KEY,
|
||||||
|
whitelist: ['tableState'],
|
||||||
|
storage,
|
||||||
|
},
|
||||||
|
reducerInstance,
|
||||||
|
);
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import { createReducer } from '@reduxjs/toolkit';
|
import { createReducer } from '@reduxjs/toolkit';
|
||||||
|
import { persistReducer } from 'redux-persist';
|
||||||
|
import storage from 'redux-persist/lib/storage';
|
||||||
import {
|
import {
|
||||||
createTableStateReducers,
|
createTableStateReducers,
|
||||||
} from 'store/tableState.reducer';
|
} from 'store/tableState.reducer';
|
||||||
@@ -10,6 +12,17 @@ const initialState = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default createReducer(initialState, {
|
const reducerInstance = createReducer(initialState, {
|
||||||
...createTableStateReducers('RECEIPTS'),
|
...createTableStateReducers('RECEIPTS'),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const STORAGE_KEY = 'bigcapital:receipts';
|
||||||
|
|
||||||
|
export default persistReducer(
|
||||||
|
{
|
||||||
|
key: STORAGE_KEY,
|
||||||
|
whitelist: ['tableState'],
|
||||||
|
storage,
|
||||||
|
},
|
||||||
|
reducerInstance,
|
||||||
|
);
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import { combineReducers } from 'redux';
|
import { combineReducers } from 'redux';
|
||||||
|
|
||||||
|
import types from './types';
|
||||||
|
|
||||||
import authentication from './authentication/authentication.reducer';
|
import authentication from './authentication/authentication.reducer';
|
||||||
import dashboard from './dashboard/dashboard.reducer';
|
import dashboard from './dashboard/dashboard.reducer';
|
||||||
import users from './users/users.reducer';
|
import users from './users/users.reducer';
|
||||||
@@ -30,7 +32,7 @@ import subscriptions from './subscription/subscription.reducer';
|
|||||||
import inventoryAdjustments from './inventoryAdjustments/inventoryAdjustment.reducer';
|
import inventoryAdjustments from './inventoryAdjustments/inventoryAdjustment.reducer';
|
||||||
import plans from './plans/plans.reducer';
|
import plans from './plans/plans.reducer';
|
||||||
|
|
||||||
export default combineReducers({
|
const appReducer = combineReducers({
|
||||||
authentication,
|
authentication,
|
||||||
organizations,
|
organizations,
|
||||||
subscriptions,
|
subscriptions,
|
||||||
@@ -61,3 +63,13 @@ export default combineReducers({
|
|||||||
inventoryAdjustments,
|
inventoryAdjustments,
|
||||||
plans
|
plans
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Reset the state of a redux store
|
||||||
|
const rootReducer = (state, action) => {
|
||||||
|
if (action.type === types.RESET) {
|
||||||
|
state = undefined;
|
||||||
|
}
|
||||||
|
return appReducer(state, action)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default rootReducer;
|
||||||
15
client/src/store/vendors/vendors.reducer.js
vendored
15
client/src/store/vendors/vendors.reducer.js
vendored
@@ -1,4 +1,6 @@
|
|||||||
import { createReducer } from '@reduxjs/toolkit';
|
import { createReducer } from '@reduxjs/toolkit';
|
||||||
|
import { persistReducer } from 'redux-persist';
|
||||||
|
import storage from 'redux-persist/lib/storage';
|
||||||
import {
|
import {
|
||||||
createTableStateReducers,
|
createTableStateReducers,
|
||||||
} from 'store/tableState.reducer';
|
} from 'store/tableState.reducer';
|
||||||
@@ -9,6 +11,17 @@ const initialState = {
|
|||||||
pageIndex: 0,
|
pageIndex: 0,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
export default createReducer(initialState, {
|
const reducerInstance = createReducer(initialState, {
|
||||||
...createTableStateReducers('VENDORS'),
|
...createTableStateReducers('VENDORS'),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const STORAGE_KEY = 'bigcapital:vendors';
|
||||||
|
|
||||||
|
export default persistReducer(
|
||||||
|
{
|
||||||
|
key: STORAGE_KEY,
|
||||||
|
whitelist: ['tableState'],
|
||||||
|
storage,
|
||||||
|
},
|
||||||
|
reducerInstance,
|
||||||
|
);
|
||||||
|
|||||||
@@ -42,6 +42,7 @@
|
|||||||
"helmet": "^3.21.0",
|
"helmet": "^3.21.0",
|
||||||
"i18n": "^0.8.5",
|
"i18n": "^0.8.5",
|
||||||
"is-my-json-valid": "^2.20.5",
|
"is-my-json-valid": "^2.20.5",
|
||||||
|
"js-money": "^0.6.3",
|
||||||
"jsonwebtoken": "^8.5.1",
|
"jsonwebtoken": "^8.5.1",
|
||||||
"knex": "^0.20.3",
|
"knex": "^0.20.3",
|
||||||
"knex-cleaner": "^1.3.0",
|
"knex-cleaner": "^1.3.0",
|
||||||
|
|||||||
@@ -547,6 +547,14 @@ export default class ItemsController extends BaseController {
|
|||||||
}],
|
}],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (error.errorType === 'TYPE_CANNOT_CHANGE_WITH_ITEM_HAS_TRANSACTIONS') {
|
||||||
|
return res.status(400).send({
|
||||||
|
errors: [{
|
||||||
|
type: 'TYPE_CANNOT_CHANGE_WITH_ITEM_HAS_TRANSACTIONS',
|
||||||
|
message: 'Cannot change item type to inventory with item has associated transactions.',
|
||||||
|
}],
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ export default class ManualJournalsController extends BaseController {
|
|||||||
return [
|
return [
|
||||||
check('date').exists().isISO8601(),
|
check('date').exists().isISO8601(),
|
||||||
check('journal_number')
|
check('journal_number')
|
||||||
.exists()
|
.optional()
|
||||||
.isString()
|
.isString()
|
||||||
.trim()
|
.trim()
|
||||||
.escape()
|
.escape()
|
||||||
@@ -470,6 +470,15 @@ export default class ManualJournalsController extends BaseController {
|
|||||||
errors: [{ type: 'MANUAL_JOURNAL_ALREADY_PUBLISHED', code: 900 }],
|
errors: [{ type: 'MANUAL_JOURNAL_ALREADY_PUBLISHED', code: 900 }],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (error.errorType === 'MANUAL_JOURNAL_NO_REQUIRED') {
|
||||||
|
return res.boom.badRequest('', {
|
||||||
|
errors: [{
|
||||||
|
type: 'MANUAL_JOURNAL_NO_REQUIRED',
|
||||||
|
message: 'The manual journal number required.',
|
||||||
|
code: 1000
|
||||||
|
}],
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ export default class OrganizationController extends BaseController {
|
|||||||
if (error instanceof ServiceError) {
|
if (error instanceof ServiceError) {
|
||||||
if (error.errorType === 'tenant_not_found') {
|
if (error.errorType === 'tenant_not_found') {
|
||||||
return res.status(400).send({
|
return res.status(400).send({
|
||||||
errors: [{ type: 'TENANT.NOT.FOUND', code: 100 }],
|
// errors: [{ type: 'TENANT.NOT.FOUND', code: 100 }],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (error.errorType === 'tenant_already_initialized') {
|
if (error.errorType === 'tenant_already_initialized') {
|
||||||
|
|||||||
@@ -31,62 +31,54 @@ export default class BillsController extends BaseController {
|
|||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
router.post(
|
router.post(
|
||||||
'/', [
|
'/',
|
||||||
...this.billValidationSchema,
|
[...this.billValidationSchema],
|
||||||
],
|
|
||||||
this.validationResult,
|
this.validationResult,
|
||||||
asyncMiddleware(this.newBill.bind(this)),
|
asyncMiddleware(this.newBill.bind(this)),
|
||||||
this.handleServiceError,
|
this.handleServiceError
|
||||||
);
|
);
|
||||||
router.post(
|
router.post(
|
||||||
'/:id/open', [
|
'/:id/open',
|
||||||
...this.specificBillValidationSchema,
|
[...this.specificBillValidationSchema],
|
||||||
],
|
|
||||||
this.validationResult,
|
this.validationResult,
|
||||||
asyncMiddleware(this.openBill.bind(this)),
|
asyncMiddleware(this.openBill.bind(this)),
|
||||||
this.handleServiceError,
|
this.handleServiceError
|
||||||
);
|
);
|
||||||
router.post(
|
router.post(
|
||||||
'/:id', [
|
'/:id',
|
||||||
...this.billEditValidationSchema,
|
[...this.billEditValidationSchema, ...this.specificBillValidationSchema],
|
||||||
...this.specificBillValidationSchema,
|
|
||||||
],
|
|
||||||
this.validationResult,
|
this.validationResult,
|
||||||
asyncMiddleware(this.editBill.bind(this)),
|
asyncMiddleware(this.editBill.bind(this)),
|
||||||
this.handleServiceError,
|
this.handleServiceError
|
||||||
);
|
);
|
||||||
router.get(
|
router.get(
|
||||||
'/due', [
|
'/due',
|
||||||
...this.dueBillsListingValidationSchema
|
[...this.dueBillsListingValidationSchema],
|
||||||
],
|
|
||||||
this.validationResult,
|
this.validationResult,
|
||||||
asyncMiddleware(this.getDueBills.bind(this)),
|
asyncMiddleware(this.getDueBills.bind(this)),
|
||||||
this.handleServiceError,
|
this.handleServiceError
|
||||||
)
|
|
||||||
router.get(
|
|
||||||
'/:id', [
|
|
||||||
...this.specificBillValidationSchema,
|
|
||||||
],
|
|
||||||
this.validationResult,
|
|
||||||
asyncMiddleware(this.getBill.bind(this)),
|
|
||||||
this.handleServiceError,
|
|
||||||
);
|
);
|
||||||
router.get(
|
router.get(
|
||||||
'/', [
|
'/:id',
|
||||||
...this.billsListingValidationSchema,
|
[...this.specificBillValidationSchema],
|
||||||
],
|
this.validationResult,
|
||||||
|
asyncMiddleware(this.getBill.bind(this)),
|
||||||
|
this.handleServiceError
|
||||||
|
);
|
||||||
|
router.get(
|
||||||
|
'/',
|
||||||
|
[...this.billsListingValidationSchema],
|
||||||
this.validationResult,
|
this.validationResult,
|
||||||
asyncMiddleware(this.billsList.bind(this)),
|
asyncMiddleware(this.billsList.bind(this)),
|
||||||
this.handleServiceError,
|
this.handleServiceError,
|
||||||
this.dynamicListService.handlerErrorsToResponse,
|
this.dynamicListService.handlerErrorsToResponse
|
||||||
);
|
);
|
||||||
router.delete(
|
router.delete(
|
||||||
'/:id', [
|
'/:id',
|
||||||
...this.specificBillValidationSchema
|
[...this.specificBillValidationSchema],
|
||||||
],
|
|
||||||
this.validationResult,
|
this.validationResult,
|
||||||
asyncMiddleware(this.deleteBill.bind(this)),
|
asyncMiddleware(this.deleteBill.bind(this)),
|
||||||
this.handleServiceError,
|
this.handleServiceError
|
||||||
);
|
);
|
||||||
return router;
|
return router;
|
||||||
}
|
}
|
||||||
@@ -110,8 +102,14 @@ export default class BillsController extends BaseController {
|
|||||||
check('entries.*.item_id').exists().isNumeric().toInt(),
|
check('entries.*.item_id').exists().isNumeric().toInt(),
|
||||||
check('entries.*.rate').exists().isNumeric().toFloat(),
|
check('entries.*.rate').exists().isNumeric().toFloat(),
|
||||||
check('entries.*.quantity').exists().isNumeric().toFloat(),
|
check('entries.*.quantity').exists().isNumeric().toFloat(),
|
||||||
check('entries.*.discount').optional({ nullable: true }).isNumeric().toFloat(),
|
check('entries.*.discount')
|
||||||
check('entries.*.description').optional({ nullable: true }).trim().escape(),
|
.optional({ nullable: true })
|
||||||
|
.isNumeric()
|
||||||
|
.toFloat(),
|
||||||
|
check('entries.*.description')
|
||||||
|
.optional({ nullable: true })
|
||||||
|
.trim()
|
||||||
|
.escape(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,8 +133,14 @@ export default class BillsController extends BaseController {
|
|||||||
check('entries.*.item_id').exists().isNumeric().toInt(),
|
check('entries.*.item_id').exists().isNumeric().toInt(),
|
||||||
check('entries.*.rate').exists().isNumeric().toFloat(),
|
check('entries.*.rate').exists().isNumeric().toFloat(),
|
||||||
check('entries.*.quantity').exists().isNumeric().toFloat(),
|
check('entries.*.quantity').exists().isNumeric().toFloat(),
|
||||||
check('entries.*.discount').optional({ nullable: true }).isNumeric().toFloat(),
|
check('entries.*.discount')
|
||||||
check('entries.*.description').optional({ nullable: true }).trim().escape(),
|
.optional({ nullable: true })
|
||||||
|
.isNumeric()
|
||||||
|
.toFloat(),
|
||||||
|
check('entries.*.description')
|
||||||
|
.optional({ nullable: true })
|
||||||
|
.trim()
|
||||||
|
.escape(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,7 +183,11 @@ export default class BillsController extends BaseController {
|
|||||||
const billDTO: IBillDTO = this.matchedBodyData(req);
|
const billDTO: IBillDTO = this.matchedBodyData(req);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const storedBill = await this.billsService.createBill(tenantId, billDTO, user);
|
const storedBill = await this.billsService.createBill(
|
||||||
|
tenantId,
|
||||||
|
billDTO,
|
||||||
|
user
|
||||||
|
);
|
||||||
|
|
||||||
return res.status(200).send({
|
return res.status(200).send({
|
||||||
id: storedBill.id,
|
id: storedBill.id,
|
||||||
@@ -293,7 +301,11 @@ export default class BillsController extends BaseController {
|
|||||||
filter.filterRoles = JSON.parse(filter.stringifiedFilterRoles);
|
filter.filterRoles = JSON.parse(filter.stringifiedFilterRoles);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const { bills, pagination, filterMeta } = await this.billsService.getBills(tenantId, filter);
|
const {
|
||||||
|
bills,
|
||||||
|
pagination,
|
||||||
|
filterMeta,
|
||||||
|
} = await this.billsService.getBills(tenantId, filter);
|
||||||
|
|
||||||
return res.status(200).send({
|
return res.status(200).send({
|
||||||
bills,
|
bills,
|
||||||
@@ -330,7 +342,12 @@ export default class BillsController extends BaseController {
|
|||||||
* @param {Response} res
|
* @param {Response} res
|
||||||
* @param {NextFunction} next
|
* @param {NextFunction} next
|
||||||
*/
|
*/
|
||||||
handleServiceError(error: Error, req: Request, res: Response, next: NextFunction) {
|
handleServiceError(
|
||||||
|
error: Error,
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
) {
|
||||||
if (error instanceof ServiceError) {
|
if (error instanceof ServiceError) {
|
||||||
if (error.errorType === 'BILL_NOT_FOUND') {
|
if (error.errorType === 'BILL_NOT_FOUND') {
|
||||||
return res.status(400).send({
|
return res.status(400).send({
|
||||||
@@ -349,8 +366,8 @@ export default class BillsController extends BaseController {
|
|||||||
}
|
}
|
||||||
if (error.errorType === 'BILL_ITEMS_NOT_PURCHASABLE') {
|
if (error.errorType === 'BILL_ITEMS_NOT_PURCHASABLE') {
|
||||||
return res.status(400).send({
|
return res.status(400).send({
|
||||||
errors: [{ type: 'BILL_ITEMS_NOT_PURCHASABLE', code: 700 }]
|
errors: [{ type: 'BILL_ITEMS_NOT_PURCHASABLE', code: 700 }],
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
if (error.errorType === 'NOT_PURCHASE_ABLE_ITEMS') {
|
if (error.errorType === 'NOT_PURCHASE_ABLE_ITEMS') {
|
||||||
return res.status(400).send({
|
return res.status(400).send({
|
||||||
@@ -377,6 +394,22 @@ export default class BillsController extends BaseController {
|
|||||||
errors: [{ type: 'BILL_ALREADY_OPEN', code: 1100 }],
|
errors: [{ type: 'BILL_ALREADY_OPEN', code: 1100 }],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (error.errorType === 'contact_not_found') {
|
||||||
|
return res.boom.badRequest(null, {
|
||||||
|
errors: [
|
||||||
|
{ type: 'VENDOR_NOT_FOUND', message: 'Vendor not found.', code: 1200 },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (error.errorType === 'BILL_HAS_ASSOCIATED_PAYMENT_ENTRIES') {
|
||||||
|
return res.status(400).send({
|
||||||
|
errors: [{
|
||||||
|
type: 'BILL_HAS_ASSOCIATED_PAYMENT_ENTRIES',
|
||||||
|
message: 'Cannot delete bill that has associated payment transactions.',
|
||||||
|
code: 1200
|
||||||
|
}],
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -415,6 +415,15 @@ export default class BillsPayments extends BaseController {
|
|||||||
errors: [{ type: 'PAYMENT_NUMBER_SHOULD_NOT_MODIFY', code: 1100 }],
|
errors: [{ type: 'PAYMENT_NUMBER_SHOULD_NOT_MODIFY', code: 1100 }],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (error.errorType === 'BILLS_NOT_OPENED_YET') {
|
||||||
|
return res.status(400).send({
|
||||||
|
errors: [{
|
||||||
|
type: 'BILLS_NOT_OPENED_YET',
|
||||||
|
message: 'The given bills are not opened yet.',
|
||||||
|
code: 1200,
|
||||||
|
}],
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -170,7 +170,6 @@ export default class PaymentReceivesController extends BaseController {
|
|||||||
message: 'The payment receive has been created successfully.',
|
message: 'The payment receive has been created successfully.',
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,52 +34,31 @@ export default class SettingsController extends BaseController{
|
|||||||
/**
|
/**
|
||||||
* Save settings validation schema.
|
* Save settings validation schema.
|
||||||
*/
|
*/
|
||||||
get saveSettingsValidationSchema() {
|
private get saveSettingsValidationSchema() {
|
||||||
return [
|
return [
|
||||||
body('options').isArray({ min: 1 }),
|
body('options').isArray({ min: 1 }),
|
||||||
body('options.*.key').exists().trim().escape().isLength({ min: 1 }),
|
body('options.*.key').exists().trim().isLength({ min: 1 }),
|
||||||
body('options.*.value').exists().trim().escape().isLength({ min: 1 }),
|
body('options.*.value').exists().trim().isLength({ min: 1 }),
|
||||||
body('options.*.group').exists().trim().escape().isLength({ min: 1 }),
|
body('options.*.group').exists().trim().isLength({ min: 1 }),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the application options from the storage.
|
* Retrieve the application options from the storage.
|
||||||
*/
|
*/
|
||||||
get getSettingsSchema() {
|
private get getSettingsSchema() {
|
||||||
return [
|
return [
|
||||||
query('key').optional().trim().escape(),
|
query('key').optional().trim().escape(),
|
||||||
query('group').optional().trim().escape(),
|
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 -
|
||||||
* @param {Response} res -
|
* @param {Response} res -
|
||||||
*/
|
*/
|
||||||
async saveSettings(req: Request, res: Response) {
|
public async saveSettings(req: Request, res: Response, next) {
|
||||||
const { Option } = req.models;
|
const { Option } = req.models;
|
||||||
const optionsDTO: IOptionsDTO = this.matchedBodyData(req);
|
const optionsDTO: IOptionsDTO = this.matchedBodyData(req);
|
||||||
const { settings } = req;
|
const { settings } = req;
|
||||||
@@ -100,8 +79,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);
|
try {
|
||||||
|
|
||||||
await settings.save();
|
await settings.save();
|
||||||
|
|
||||||
return res.status(200).send({
|
return res.status(200).send({
|
||||||
@@ -109,6 +87,9 @@ export default class SettingsController extends BaseController{
|
|||||||
code: 'OPTIONS.SAVED.SUCCESSFULLY',
|
code: 'OPTIONS.SAVED.SUCCESSFULLY',
|
||||||
message: 'Options have been saved successfully.',
|
message: 'Options have been saved successfully.',
|
||||||
});
|
});
|
||||||
|
} catch (error) {
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -116,7 +97,7 @@ export default class SettingsController extends BaseController{
|
|||||||
* @param {Request} req
|
* @param {Request} req
|
||||||
* @param {Response} res
|
* @param {Response} res
|
||||||
*/
|
*/
|
||||||
getSettings(req: Request, res: Response) {
|
public getSettings(req: Request, res: Response) {
|
||||||
const { settings } = req;
|
const { settings } = req;
|
||||||
const allSettings = settings.all();
|
const allSettings = settings.all();
|
||||||
|
|
||||||
|
|||||||
102
server/src/api/controllers/Setup.ts
Normal file
102
server/src/api/controllers/Setup.ts
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
import { Router, Request, Response, NextFunction } from 'express';
|
||||||
|
import { check, ValidationChain } from 'express-validator';
|
||||||
|
import BaseController from './BaseController';
|
||||||
|
import SetupService from 'services/Setup/SetupService';
|
||||||
|
import { Inject, Service } from 'typedi';
|
||||||
|
import { IOrganizationSetupDTO } from 'interfaces';
|
||||||
|
import { ServiceError } from 'exceptions';
|
||||||
|
// Middlewares
|
||||||
|
import JWTAuth from 'api/middleware/jwtAuth';
|
||||||
|
import AttachCurrentTenantUser from 'api/middleware/AttachCurrentTenantUser';
|
||||||
|
import SubscriptionMiddleware from 'api/middleware/SubscriptionMiddleware';
|
||||||
|
import TenancyMiddleware from 'api/middleware/TenancyMiddleware';
|
||||||
|
import EnsureTenantIsInitialized from 'api/middleware/EnsureTenantIsInitialized';
|
||||||
|
import SettingsMiddleware from 'api/middleware/SettingsMiddleware';
|
||||||
|
|
||||||
|
@Service()
|
||||||
|
export default class SetupController extends BaseController {
|
||||||
|
@Inject()
|
||||||
|
setupService: SetupService;
|
||||||
|
|
||||||
|
router() {
|
||||||
|
const router = Router('/setup');
|
||||||
|
|
||||||
|
router.use(JWTAuth);
|
||||||
|
router.use(AttachCurrentTenantUser);
|
||||||
|
router.use(TenancyMiddleware);
|
||||||
|
router.use(SubscriptionMiddleware('main'));
|
||||||
|
router.use(EnsureTenantIsInitialized);
|
||||||
|
router.use(SettingsMiddleware);
|
||||||
|
router.post(
|
||||||
|
'/organization',
|
||||||
|
this.organizationSetupSchema,
|
||||||
|
this.validationResult,
|
||||||
|
this.asyncMiddleware(this.organizationSetup.bind(this)),
|
||||||
|
this.handleServiceErrors
|
||||||
|
);
|
||||||
|
return router;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Organization setup schema.
|
||||||
|
*/
|
||||||
|
private get organizationSetupSchema(): ValidationChain[] {
|
||||||
|
return [
|
||||||
|
check('organization_name').exists().trim(),
|
||||||
|
check('base_currency').exists(),
|
||||||
|
check('time_zone').exists(),
|
||||||
|
check('fiscal_year').exists(),
|
||||||
|
check('industry').optional(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Organization setup.
|
||||||
|
* @param {Request} req
|
||||||
|
* @param {Response} res
|
||||||
|
* @param {NextFunction} next
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
async organizationSetup(req: Request, res: Response, next: NextFunction) {
|
||||||
|
const { tenantId } = req;
|
||||||
|
const setupDTO: IOrganizationSetupDTO = this.matchedBodyData(req);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.setupService.organizationSetup(tenantId, setupDTO);
|
||||||
|
|
||||||
|
return res.status(200).send({
|
||||||
|
message: 'The setup settings set successfully.',
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles service errors.
|
||||||
|
* @param {Error} error
|
||||||
|
* @param {Request} req
|
||||||
|
* @param {Response} res
|
||||||
|
* @param {NextFunction} next
|
||||||
|
*/
|
||||||
|
handleServiceErrors(
|
||||||
|
error: Error,
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
) {
|
||||||
|
if (error instanceof ServiceError) {
|
||||||
|
if (error.errorType === 'TENANT_IS_ALREADY_SETUPED') {
|
||||||
|
return res.status(400).send({
|
||||||
|
errors: [{ type: 'TENANT_IS_ALREADY_SETUPED', code: 1000 }],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (error.errorType === 'BASE_CURRENCY_INVALID') {
|
||||||
|
return res.status(400).send({
|
||||||
|
errors: [{ type: 'BASE_CURRENCY_INVALID', code: 110 }],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -40,6 +40,8 @@ import Subscription from 'api/controllers/Subscription';
|
|||||||
import Licenses from 'api/controllers/Subscription/Licenses';
|
import Licenses from 'api/controllers/Subscription/Licenses';
|
||||||
import InventoryAdjustments from 'api/controllers/Inventory/InventoryAdjustments';
|
import InventoryAdjustments from 'api/controllers/Inventory/InventoryAdjustments';
|
||||||
|
|
||||||
|
import Setup from 'api/controllers/Setup';
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
const app = Router();
|
const app = Router();
|
||||||
|
|
||||||
@@ -53,22 +55,7 @@ export default () => {
|
|||||||
app.use('/subscription', Container.get(Subscription).router());
|
app.use('/subscription', Container.get(Subscription).router());
|
||||||
app.use('/organization', Container.get(Organization).router());
|
app.use('/organization', Container.get(Organization).router());
|
||||||
app.use('/ping', Container.get(Ping).router());
|
app.use('/ping', Container.get(Ping).router());
|
||||||
|
app.use('/setup', Container.get(Setup).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('/', Container.get(Settings).router());
|
|
||||||
|
|
||||||
app.use('/settings', settings);
|
|
||||||
|
|
||||||
// - Dashboard routes.
|
// - Dashboard routes.
|
||||||
// ---------------------------
|
// ---------------------------
|
||||||
const dashboard = Router();
|
const dashboard = Router();
|
||||||
@@ -86,6 +73,7 @@ export default () => {
|
|||||||
dashboard.use('/users', Container.get(Users).router());
|
dashboard.use('/users', Container.get(Users).router());
|
||||||
dashboard.use('/invite', Container.get(InviteUsers).authRouter());
|
dashboard.use('/invite', Container.get(InviteUsers).authRouter());
|
||||||
dashboard.use('/currencies', Container.get(Currencies).router());
|
dashboard.use('/currencies', Container.get(Currencies).router());
|
||||||
|
dashboard.use('/settings', Container.get(Settings).router());
|
||||||
dashboard.use('/accounts', Container.get(Accounts).router());
|
dashboard.use('/accounts', Container.get(Accounts).router());
|
||||||
dashboard.use('/account_types', Container.get(AccountTypes).router());
|
dashboard.use('/account_types', Container.get(AccountTypes).router());
|
||||||
dashboard.use('/manual-journals', Container.get(ManualJournals).router());
|
dashboard.use('/manual-journals', Container.get(ManualJournals).router());
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ export default {
|
|||||||
manual_journals: [
|
manual_journals: [
|
||||||
{
|
{
|
||||||
key: "next_number",
|
key: "next_number",
|
||||||
type: "number",
|
type: "string",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: "number_prefix",
|
key: "number_prefix",
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ exports.up = function(knex) {
|
|||||||
table.increments();
|
table.increments();
|
||||||
|
|
||||||
table.integer('bill_payment_id').unsigned().index().references('id').inTable('bills_payments');
|
table.integer('bill_payment_id').unsigned().index().references('id').inTable('bills_payments');
|
||||||
table.integer('bill_id').unsigned().index();
|
table.integer('bill_id').unsigned().index().references('id').inTable('bills');
|
||||||
table.decimal('payment_amount', 13, 3).unsigned();
|
table.decimal('payment_amount', 13, 3).unsigned();
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,14 +5,29 @@ exports.up = (knex) => {
|
|||||||
const tenancyService = Container.get(TenancyService);
|
const tenancyService = Container.get(TenancyService);
|
||||||
const settings = tenancyService.settings(knex.userParams.tenantId);
|
const settings = tenancyService.settings(knex.userParams.tenantId);
|
||||||
|
|
||||||
settings.set({ group: 'manual_journals', key: 'next_number', value: 1 });
|
// Manual journals settings.
|
||||||
settings.set({ group: 'sales_invoices', key: 'next_number', value: 1 });
|
settings.set({ group: 'manual_journals', key: 'next_number', value: '00001' });
|
||||||
settings.set({ group: 'sales_invoices', key: 'number_prefix', value: 'INV' });
|
settings.set({ group: 'manual_journals', key: 'auto_increment', value: true });
|
||||||
settings.set({ group: 'sales_receipts', key: 'next_number', value: 1 });
|
|
||||||
settings.set({ group: 'sales_receipts', key: 'number_prefix', value: 'REC' });
|
// Sale invoices settings.
|
||||||
settings.set({ group: 'sales_estimates', key: 'next_number', value: 1 });
|
settings.set({ group: 'sales_invoices', key: 'next_number', value: '00001' });
|
||||||
settings.set({ group: 'sales_estimates', key: 'number_prefix', value: 'EST' });
|
settings.set({ group: 'sales_invoices', key: 'number_prefix', value: 'INV-' });
|
||||||
settings.set({ group: 'payment_receives', key: 'next_number', value: 1 });
|
settings.set({ group: 'sales_invoices', key: 'auto_increment', value: true });
|
||||||
|
|
||||||
|
// Sale receipts settings.
|
||||||
|
settings.set({ group: 'sales_receipts', key: 'next_number', value: '00001' });
|
||||||
|
settings.set({ group: 'sales_receipts', key: 'number_prefix', value: 'REC-' });
|
||||||
|
settings.set({ group: 'sales_receipts', key: 'auto_increment', value: true });
|
||||||
|
|
||||||
|
// Sale estimates settings.
|
||||||
|
settings.set({ group: 'sales_estimates', key: 'next_number', value: '00001' });
|
||||||
|
settings.set({ group: 'sales_estimates', key: 'number_prefix', value: 'EST-' });
|
||||||
|
settings.set({ group: 'sales_estimates', key: 'auto_increment', value: true });
|
||||||
|
|
||||||
|
// Payment receives settings.
|
||||||
|
settings.set({ group: 'payment_receives', key: 'number_prefix', value: 'PAY-' });
|
||||||
|
settings.set({ group: 'payment_receives', key: 'next_number', value: '00001' });
|
||||||
|
settings.set({ group: 'payment_receives', key: 'auto_increment', value: true });
|
||||||
|
|
||||||
return settings.save();
|
return settings.save();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ import { IJournalEntry } from './Journal';
|
|||||||
import { ISystemUser } from './User';
|
import { ISystemUser } from './User';
|
||||||
|
|
||||||
export interface IManualJournal {
|
export interface IManualJournal {
|
||||||
id: number;
|
id?: number;
|
||||||
date: Date | string;
|
date: Date | string;
|
||||||
journalNumber: number;
|
journalNumber: string;
|
||||||
journalType: string;
|
journalType: string;
|
||||||
reference: string;
|
reference: string;
|
||||||
amount: number;
|
amount: number;
|
||||||
@@ -37,7 +37,7 @@ export interface IManualJournalEntryDTO {
|
|||||||
|
|
||||||
export interface IManualJournalDTO {
|
export interface IManualJournalDTO {
|
||||||
date: Date;
|
date: Date;
|
||||||
journalNumber: number;
|
journalNumber: string;
|
||||||
journalType: string;
|
journalType: string;
|
||||||
reference?: string;
|
reference?: string;
|
||||||
description?: string;
|
description?: string;
|
||||||
|
|||||||
10
server/src/interfaces/Setup.ts
Normal file
10
server/src/interfaces/Setup.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export interface IOrganizationSetupDTO{
|
||||||
|
organizationName: string,
|
||||||
|
baseCurrency: string,
|
||||||
|
fiscalYear: string,
|
||||||
|
industry: string,
|
||||||
|
timeZone: string,
|
||||||
|
}
|
||||||
@@ -40,3 +40,4 @@ export * from './ARAgingSummaryReport';
|
|||||||
export * from './APAgingSummaryReport';
|
export * from './APAgingSummaryReport';
|
||||||
export * from './Mailable';
|
export * from './Mailable';
|
||||||
export * from './InventoryAdjustment';
|
export * from './InventoryAdjustment';
|
||||||
|
export * from './Setup'
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Inject, Container, Service } from 'typedi';
|
import { Inject, Service } from 'typedi';
|
||||||
|
import Currencies from 'js-money/lib/currency';
|
||||||
import {
|
import {
|
||||||
ICurrencyEditDTO,
|
ICurrencyEditDTO,
|
||||||
ICurrencyDTO,
|
ICurrencyDTO,
|
||||||
@@ -15,6 +16,7 @@ import TenancyService from 'services/Tenancy/TenancyService';
|
|||||||
const ERRORS = {
|
const ERRORS = {
|
||||||
CURRENCY_NOT_FOUND: 'currency_not_found',
|
CURRENCY_NOT_FOUND: 'currency_not_found',
|
||||||
CURRENCY_CODE_EXISTS: 'currency_code_exists',
|
CURRENCY_CODE_EXISTS: 'currency_code_exists',
|
||||||
|
BASE_CURRENCY_INVALID: 'BASE_CURRENCY_INVALID'
|
||||||
};
|
};
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
@@ -129,7 +131,6 @@ export default class CurrenciesService implements ICurrenciesService {
|
|||||||
tenantId,
|
tenantId,
|
||||||
currencyDTO,
|
currencyDTO,
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.validateCurrencyCodeUniquiness(
|
await this.validateCurrencyCodeUniquiness(
|
||||||
tenantId,
|
tenantId,
|
||||||
currencyDTO.currencyCode
|
currencyDTO.currencyCode
|
||||||
@@ -211,4 +212,25 @@ export default class CurrenciesService implements ICurrenciesService {
|
|||||||
});
|
});
|
||||||
return currencies;
|
return currencies;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Seeds the given base currency to the currencies list.
|
||||||
|
* @param {number} tenantId
|
||||||
|
* @param {string} baseCurrency
|
||||||
|
*/
|
||||||
|
public async seedBaseCurrency(tenantId: number, baseCurrency: string) {
|
||||||
|
const { Currency } = this.tenancy.models(tenantId);
|
||||||
|
const currencyMeta = Currencies[baseCurrency];
|
||||||
|
|
||||||
|
const foundBaseCurrency = await Currency.query().findOne(
|
||||||
|
'currency_code',
|
||||||
|
baseCurrency
|
||||||
|
);
|
||||||
|
if (!foundBaseCurrency) {
|
||||||
|
await Currency.query().insert({
|
||||||
|
currency_code: currencyMeta.code,
|
||||||
|
currency_name: currencyMeta.name,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -244,11 +244,11 @@ export default class ItemsService implements IItemsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate item type in edit item mode, cannot change item inventory type.
|
* Validate edit item type from inventory to another type that not allowed.
|
||||||
* @param {IItemDTO} itemDTO
|
* @param {IItemDTO} itemDTO
|
||||||
* @param {IItem} oldItem
|
* @param {IItem} oldItem
|
||||||
*/
|
*/
|
||||||
private validateEditItemInventoryType(itemDTO: IItemDTO, oldItem: IItem) {
|
private validateEditItemFromInventory(itemDTO: IItemDTO, oldItem: IItem) {
|
||||||
if (
|
if (
|
||||||
itemDTO.type &&
|
itemDTO.type &&
|
||||||
oldItem.type === 'inventory' &&
|
oldItem.type === 'inventory' &&
|
||||||
@@ -258,6 +258,36 @@ export default class ItemsService implements IItemsService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates edit item type from service/non-inventory to inventory.
|
||||||
|
* Should item has no any relations with accounts transactions.
|
||||||
|
* @param {number} tenantId - Tenant id.
|
||||||
|
* @param {number} itemId - Item id.
|
||||||
|
*/
|
||||||
|
private async validateEditItemTypeToInventory(
|
||||||
|
tenantId: number,
|
||||||
|
oldItem: IItem,
|
||||||
|
newItemDTO: IItemDTO
|
||||||
|
) {
|
||||||
|
const { AccountTransaction } = this.tenancy.models(tenantId);
|
||||||
|
|
||||||
|
// We have no problem in case the item type not modified.
|
||||||
|
if (newItemDTO.type === oldItem.type || oldItem.type === 'inventory') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Retrieve all transactions that associated to the given item id.
|
||||||
|
const itemTransactionsCount = await AccountTransaction.query()
|
||||||
|
.where('item_id', oldItem.id)
|
||||||
|
.count('item_id', { as: 'transactions' })
|
||||||
|
.first();
|
||||||
|
|
||||||
|
if (itemTransactionsCount.transactions > 0) {
|
||||||
|
throw new ServiceError(
|
||||||
|
ERRORS.TYPE_CANNOT_CHANGE_WITH_ITEM_HAS_TRANSACTIONS
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new item.
|
* Creates a new item.
|
||||||
* @param {number} tenantId DTO
|
* @param {number} tenantId DTO
|
||||||
@@ -319,7 +349,11 @@ export default class ItemsService implements IItemsService {
|
|||||||
// Validates the given item existance on the storage.
|
// Validates the given item existance on the storage.
|
||||||
const oldItem = await this.getItemOrThrowError(tenantId, itemId);
|
const oldItem = await this.getItemOrThrowError(tenantId, itemId);
|
||||||
|
|
||||||
this.validateEditItemInventoryType(itemDTO, oldItem);
|
// Validate edit item type from inventory type.
|
||||||
|
this.validateEditItemFromInventory(itemDTO, oldItem);
|
||||||
|
|
||||||
|
// Validate edit item type to inventory type.
|
||||||
|
await this.validateEditItemTypeToInventory(tenantId, oldItem, itemDTO);
|
||||||
|
|
||||||
// Transform the edit item DTO to model.
|
// Transform the edit item DTO to model.
|
||||||
const itemModel = this.transformEditItemDTOToModel(itemDTO, oldItem);
|
const itemModel = this.transformEditItemDTOToModel(itemDTO, oldItem);
|
||||||
@@ -580,8 +614,7 @@ export default class ItemsService implements IItemsService {
|
|||||||
const { ItemEntry } = this.tenancy.models(tenantId);
|
const { ItemEntry } = this.tenancy.models(tenantId);
|
||||||
|
|
||||||
const ids = Array.isArray(itemId) ? itemId : [itemId];
|
const ids = Array.isArray(itemId) ? itemId : [itemId];
|
||||||
const foundItemEntries = await ItemEntry.query()
|
const foundItemEntries = await ItemEntry.query().whereIn('item_id', ids);
|
||||||
.whereIn('item_id', ids);
|
|
||||||
|
|
||||||
if (foundItemEntries.length > 0) {
|
if (foundItemEntries.length > 0) {
|
||||||
throw new ServiceError(
|
throw new ServiceError(
|
||||||
|
|||||||
@@ -18,4 +18,5 @@ export const ERRORS = {
|
|||||||
ITEM_HAS_ASSOCIATED_INVENTORY_ADJUSTMENT:
|
ITEM_HAS_ASSOCIATED_INVENTORY_ADJUSTMENT:
|
||||||
'ITEM_HAS_ASSOCIATED_INVENTORY_ADJUSTMENT',
|
'ITEM_HAS_ASSOCIATED_INVENTORY_ADJUSTMENT',
|
||||||
ITEM_CANNOT_CHANGE_INVENTORY_TYPE: 'ITEM_CANNOT_CHANGE_INVENTORY_TYPE',
|
ITEM_CANNOT_CHANGE_INVENTORY_TYPE: 'ITEM_CANNOT_CHANGE_INVENTORY_TYPE',
|
||||||
|
TYPE_CANNOT_CHANGE_WITH_ITEM_HAS_TRANSACTIONS: 'TYPE_CANNOT_CHANGE_WITH_ITEM_HAS_TRANSACTIONS',
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import {
|
|||||||
import JournalPoster from 'services/Accounting/JournalPoster';
|
import JournalPoster from 'services/Accounting/JournalPoster';
|
||||||
import JournalCommands from 'services/Accounting/JournalCommands';
|
import JournalCommands from 'services/Accounting/JournalCommands';
|
||||||
import JournalPosterService from 'services/Sales/JournalPosterService';
|
import JournalPosterService from 'services/Sales/JournalPosterService';
|
||||||
|
import AutoIncrementOrdersService from 'services/Sales/AutoIncrementOrdersService';
|
||||||
import { ERRORS } from './constants';
|
import { ERRORS } from './constants';
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
@@ -40,6 +41,9 @@ export default class ManualJournalsService implements IManualJournalsService {
|
|||||||
@EventDispatcher()
|
@EventDispatcher()
|
||||||
eventDispatcher: EventDispatcherInterface;
|
eventDispatcher: EventDispatcherInterface;
|
||||||
|
|
||||||
|
@Inject()
|
||||||
|
autoIncrementOrdersService: AutoIncrementOrdersService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates the manual journal existance.
|
* Validates the manual journal existance.
|
||||||
* @param {number} tenantId
|
* @param {number} tenantId
|
||||||
@@ -157,18 +161,18 @@ export default class ManualJournalsService implements IManualJournalsService {
|
|||||||
*/
|
*/
|
||||||
private async validateManualJournalNoUnique(
|
private async validateManualJournalNoUnique(
|
||||||
tenantId: number,
|
tenantId: number,
|
||||||
manualJournalDTO: IManualJournalDTO,
|
journalNumber: string,
|
||||||
notId?: number
|
notId?: number
|
||||||
) {
|
) {
|
||||||
const { ManualJournal } = this.tenancy.models(tenantId);
|
const { ManualJournal } = this.tenancy.models(tenantId);
|
||||||
const journalNumber = await ManualJournal.query()
|
const journals = await ManualJournal.query()
|
||||||
.where('journal_number', manualJournalDTO.journalNumber)
|
.where('journal_number', journalNumber)
|
||||||
.onBuild((builder) => {
|
.onBuild((builder) => {
|
||||||
if (notId) {
|
if (notId) {
|
||||||
builder.whereNot('id', notId);
|
builder.whereNot('id', notId);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (journalNumber.length > 0) {
|
if (journals.length > 0) {
|
||||||
throw new ServiceError(ERRORS.JOURNAL_NUMBER_EXISTS);
|
throw new ServiceError(ERRORS.JOURNAL_NUMBER_EXISTS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -206,7 +210,7 @@ export default class ManualJournalsService implements IManualJournalsService {
|
|||||||
);
|
);
|
||||||
// Throw error in case one of entries that has invalid contact type.
|
// Throw error in case one of entries that has invalid contact type.
|
||||||
if (entriesNoContact.length > 0) {
|
if (entriesNoContact.length > 0) {
|
||||||
const indexes = entriesNoContact.map(e => e.index);
|
const indexes = entriesNoContact.map((e) => e.index);
|
||||||
|
|
||||||
throw new ServiceError(ERRORS.ENTRIES_SHOULD_ASSIGN_WITH_CONTACT, '', {
|
throw new ServiceError(ERRORS.ENTRIES_SHOULD_ASSIGN_WITH_CONTACT, '', {
|
||||||
accountSlug: accountBySlug,
|
accountSlug: accountBySlug,
|
||||||
@@ -291,18 +295,58 @@ export default class ManualJournalsService implements IManualJournalsService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the next journal number.
|
||||||
|
*/
|
||||||
|
getNextJournalNumber(tenantId: number): string {
|
||||||
|
return this.autoIncrementOrdersService.getNextTransactionNumber(
|
||||||
|
tenantId,
|
||||||
|
'manual_journals'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Increment the manual journal number.
|
||||||
|
* @param {number} tenantId
|
||||||
|
*/
|
||||||
|
incrementNextJournalNumber(tenantId: number) {
|
||||||
|
return this.autoIncrementOrdersService.incrementSettingsNextNumber(
|
||||||
|
tenantId,
|
||||||
|
'manual_journals'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates the manual journal number require.
|
||||||
|
* @param {string} journalNumber
|
||||||
|
*/
|
||||||
|
private validateJournalNoRequire(journalNumber: string) {
|
||||||
|
if (!journalNumber) {
|
||||||
|
throw new ServiceError(ERRORS.MANUAL_JOURNAL_NO_REQUIRED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transform the new manual journal DTO to upsert graph operation.
|
* Transform the new manual journal DTO to upsert graph operation.
|
||||||
* @param {IManualJournalDTO} manualJournalDTO - Manual jorunal DTO.
|
* @param {IManualJournalDTO} manualJournalDTO - Manual jorunal DTO.
|
||||||
* @param {ISystemUser} authorizedUser
|
* @param {ISystemUser} authorizedUser
|
||||||
*/
|
*/
|
||||||
private transformNewDTOToModel(
|
private transformNewDTOToModel(
|
||||||
|
tenantId,
|
||||||
manualJournalDTO: IManualJournalDTO,
|
manualJournalDTO: IManualJournalDTO,
|
||||||
authorizedUser: ISystemUser
|
authorizedUser: ISystemUser
|
||||||
) {
|
) {
|
||||||
const amount = sumBy(manualJournalDTO.entries, 'credit') || 0;
|
const amount = sumBy(manualJournalDTO.entries, 'credit') || 0;
|
||||||
const date = moment(manualJournalDTO.date).format('YYYY-MM-DD');
|
const date = moment(manualJournalDTO.date).format('YYYY-MM-DD');
|
||||||
|
|
||||||
|
// Retrieve the next manual journal number.
|
||||||
|
const autoNextNumber = this.getNextJournalNumber(tenantId);
|
||||||
|
|
||||||
|
const journalNumber = manualJournalDTO.journalNumber || autoNextNumber;
|
||||||
|
|
||||||
|
// Validate manual journal number require.
|
||||||
|
this.validateJournalNoRequire(journalNumber);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...omit(manualJournalDTO, ['publish']),
|
...omit(manualJournalDTO, ['publish']),
|
||||||
...(manualJournalDTO.publish
|
...(manualJournalDTO.publish
|
||||||
@@ -310,6 +354,7 @@ export default class ManualJournalsService implements IManualJournalsService {
|
|||||||
: {}),
|
: {}),
|
||||||
amount,
|
amount,
|
||||||
date,
|
date,
|
||||||
|
journalNumber,
|
||||||
userId: authorizedUser.id,
|
userId: authorizedUser.id,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -350,6 +395,12 @@ export default class ManualJournalsService implements IManualJournalsService {
|
|||||||
): Promise<{ manualJournal: IManualJournal }> {
|
): Promise<{ manualJournal: IManualJournal }> {
|
||||||
const { ManualJournal } = this.tenancy.models(tenantId);
|
const { ManualJournal } = this.tenancy.models(tenantId);
|
||||||
|
|
||||||
|
// Transformes the next DTO to model.
|
||||||
|
const manualJournalObj = this.transformNewDTOToModel(
|
||||||
|
tenantId,
|
||||||
|
manualJournalDTO,
|
||||||
|
authorizedUser
|
||||||
|
);
|
||||||
// Validate the total credit should equals debit.
|
// Validate the total credit should equals debit.
|
||||||
this.valdiateCreditDebitTotalEquals(manualJournalDTO);
|
this.valdiateCreditDebitTotalEquals(manualJournalDTO);
|
||||||
|
|
||||||
@@ -360,8 +411,10 @@ export default class ManualJournalsService implements IManualJournalsService {
|
|||||||
await this.validateAccountsExistance(tenantId, manualJournalDTO);
|
await this.validateAccountsExistance(tenantId, manualJournalDTO);
|
||||||
|
|
||||||
// Validate manual journal uniquiness on the storage.
|
// Validate manual journal uniquiness on the storage.
|
||||||
await this.validateManualJournalNoUnique(tenantId, manualJournalDTO);
|
await this.validateManualJournalNoUnique(
|
||||||
|
tenantId,
|
||||||
|
manualJournalObj.journalNumber
|
||||||
|
);
|
||||||
// Validate accounts with contact type from the given config.
|
// Validate accounts with contact type from the given config.
|
||||||
await this.dynamicValidateAccountsWithContactType(
|
await this.dynamicValidateAccountsWithContactType(
|
||||||
tenantId,
|
tenantId,
|
||||||
@@ -371,10 +424,7 @@ export default class ManualJournalsService implements IManualJournalsService {
|
|||||||
'[manual_journal] trying to save manual journal to the storage.',
|
'[manual_journal] trying to save manual journal to the storage.',
|
||||||
{ tenantId, manualJournalDTO }
|
{ tenantId, manualJournalDTO }
|
||||||
);
|
);
|
||||||
const manualJournalObj = this.transformNewDTOToModel(
|
|
||||||
manualJournalDTO,
|
|
||||||
authorizedUser
|
|
||||||
);
|
|
||||||
const manualJournal = await ManualJournal.query().upsertGraph({
|
const manualJournal = await ManualJournal.query().upsertGraph({
|
||||||
...manualJournalObj,
|
...manualJournalObj,
|
||||||
});
|
});
|
||||||
@@ -415,6 +465,11 @@ export default class ManualJournalsService implements IManualJournalsService {
|
|||||||
tenantId,
|
tenantId,
|
||||||
manualJournalId
|
manualJournalId
|
||||||
);
|
);
|
||||||
|
// Transform manual journal DTO to model.
|
||||||
|
const manualJournalObj = this.transformEditDTOToModel(
|
||||||
|
manualJournalDTO,
|
||||||
|
oldManualJournal
|
||||||
|
);
|
||||||
// Validates the total credit and debit to be equals.
|
// Validates the total credit and debit to be equals.
|
||||||
this.valdiateCreditDebitTotalEquals(manualJournalDTO);
|
this.valdiateCreditDebitTotalEquals(manualJournalDTO);
|
||||||
|
|
||||||
@@ -425,21 +480,19 @@ export default class ManualJournalsService implements IManualJournalsService {
|
|||||||
await this.validateAccountsExistance(tenantId, manualJournalDTO);
|
await this.validateAccountsExistance(tenantId, manualJournalDTO);
|
||||||
|
|
||||||
// Validates the manual journal number uniquiness.
|
// Validates the manual journal number uniquiness.
|
||||||
|
if (manualJournalDTO.journalNumber) {
|
||||||
await this.validateManualJournalNoUnique(
|
await this.validateManualJournalNoUnique(
|
||||||
tenantId,
|
tenantId,
|
||||||
manualJournalDTO,
|
manualJournalDTO.journalNumber,
|
||||||
manualJournalId
|
manualJournalId
|
||||||
);
|
);
|
||||||
|
}
|
||||||
// Validate accounts with contact type from the given config.
|
// Validate accounts with contact type from the given config.
|
||||||
await this.dynamicValidateAccountsWithContactType(
|
await this.dynamicValidateAccountsWithContactType(
|
||||||
tenantId,
|
tenantId,
|
||||||
manualJournalDTO.entries
|
manualJournalDTO.entries
|
||||||
);
|
);
|
||||||
// Transform manual journal DTO to model.
|
|
||||||
const manualJournalObj = this.transformEditDTOToModel(
|
|
||||||
manualJournalDTO,
|
|
||||||
oldManualJournal
|
|
||||||
);
|
|
||||||
await ManualJournal.query().upsertGraph({
|
await ManualJournal.query().upsertGraph({
|
||||||
...manualJournalObj,
|
...manualJournalObj,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ export const ERRORS = {
|
|||||||
CONTACTS_NOT_FOUND: 'contacts_not_found',
|
CONTACTS_NOT_FOUND: 'contacts_not_found',
|
||||||
ENTRIES_CONTACTS_NOT_FOUND: 'ENTRIES_CONTACTS_NOT_FOUND',
|
ENTRIES_CONTACTS_NOT_FOUND: 'ENTRIES_CONTACTS_NOT_FOUND',
|
||||||
MANUAL_JOURNAL_ALREADY_PUBLISHED: 'MANUAL_JOURNAL_ALREADY_PUBLISHED',
|
MANUAL_JOURNAL_ALREADY_PUBLISHED: 'MANUAL_JOURNAL_ALREADY_PUBLISHED',
|
||||||
|
MANUAL_JOURNAL_NO_REQUIRED: 'MANUAL_JOURNAL_NO_REQUIRED'
|
||||||
};
|
};
|
||||||
|
|
||||||
export const CONTACTS_CONFIG = [
|
export const CONTACTS_CONFIG = [
|
||||||
|
|||||||
@@ -172,6 +172,15 @@ export default class BillPaymentsService {
|
|||||||
if (notFoundBillsIds.length > 0) {
|
if (notFoundBillsIds.length > 0) {
|
||||||
throw new ServiceError(ERRORS.BILL_ENTRIES_IDS_NOT_FOUND);
|
throw new ServiceError(ERRORS.BILL_ENTRIES_IDS_NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate the not opened bills.
|
||||||
|
const notOpenedBills = storedBills.filter((bill) => !bill.openedAt);
|
||||||
|
|
||||||
|
if (notOpenedBills.length > 0) {
|
||||||
|
throw new ServiceError(ERRORS.BILLS_NOT_OPENED_YET, null, {
|
||||||
|
notOpenedBills
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -9,4 +9,5 @@ export const ERRORS = {
|
|||||||
BILL_PAYMENT_ENTRIES_NOT_FOUND: 'BILL_PAYMENT_ENTRIES_NOT_FOUND',
|
BILL_PAYMENT_ENTRIES_NOT_FOUND: 'BILL_PAYMENT_ENTRIES_NOT_FOUND',
|
||||||
INVALID_BILL_PAYMENT_AMOUNT: 'INVALID_BILL_PAYMENT_AMOUNT',
|
INVALID_BILL_PAYMENT_AMOUNT: 'INVALID_BILL_PAYMENT_AMOUNT',
|
||||||
PAYMENT_NUMBER_SHOULD_NOT_MODIFY: 'PAYMENT_NUMBER_SHOULD_NOT_MODIFY',
|
PAYMENT_NUMBER_SHOULD_NOT_MODIFY: 'PAYMENT_NUMBER_SHOULD_NOT_MODIFY',
|
||||||
|
BILLS_NOT_OPENED_YET: 'BILLS_NOT_OPENED_YET'
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -139,6 +139,26 @@ export default class BillsService extends SalesInvoicesCost {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate the bill has no payment entries.
|
||||||
|
* @param {number} tenantId
|
||||||
|
* @param {number} billId - Bill id.
|
||||||
|
*/
|
||||||
|
private async validateBillHasNoEntries(
|
||||||
|
tenantId,
|
||||||
|
billId: number,
|
||||||
|
) {
|
||||||
|
const { BillPaymentEntry } = this.tenancy.models(tenantId);
|
||||||
|
|
||||||
|
// Retireve the bill associate payment made entries.
|
||||||
|
const entries = await BillPaymentEntry.query().where('bill_id', billId);
|
||||||
|
|
||||||
|
if (entries.length > 0) {
|
||||||
|
throw new ServiceError(ERRORS.BILL_HAS_ASSOCIATED_PAYMENT_ENTRIES);
|
||||||
|
}
|
||||||
|
return entries;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate the bill number require.
|
* Validate the bill number require.
|
||||||
* @param {string} billNo -
|
* @param {string} billNo -
|
||||||
@@ -354,6 +374,9 @@ export default class BillsService extends SalesInvoicesCost {
|
|||||||
// Retrieve the given bill or throw not found error.
|
// Retrieve the given bill or throw not found error.
|
||||||
const oldBill = await this.getBillOrThrowError(tenantId, billId);
|
const oldBill = await this.getBillOrThrowError(tenantId, billId);
|
||||||
|
|
||||||
|
// Validate the purchase bill has no assocaited payments transactions.
|
||||||
|
await this.validateBillHasNoEntries(tenantId, billId);
|
||||||
|
|
||||||
// Delete all associated bill entries.
|
// Delete all associated bill entries.
|
||||||
const deleteBillEntriesOper = ItemEntry.query()
|
const deleteBillEntriesOper = ItemEntry.query()
|
||||||
.where('reference_type', 'Bill')
|
.where('reference_type', 'Bill')
|
||||||
|
|||||||
@@ -7,5 +7,6 @@ export const ERRORS = {
|
|||||||
BILL_ENTRIES_IDS_NOT_FOUND: 'BILL_ENTRIES_IDS_NOT_FOUND',
|
BILL_ENTRIES_IDS_NOT_FOUND: 'BILL_ENTRIES_IDS_NOT_FOUND',
|
||||||
NOT_PURCHASE_ABLE_ITEMS: 'NOT_PURCHASE_ABLE_ITEMS',
|
NOT_PURCHASE_ABLE_ITEMS: 'NOT_PURCHASE_ABLE_ITEMS',
|
||||||
BILL_ALREADY_OPEN: 'BILL_ALREADY_OPEN',
|
BILL_ALREADY_OPEN: 'BILL_ALREADY_OPEN',
|
||||||
BILL_NO_IS_REQUIRED: 'BILL_NO_IS_REQUIRED'
|
BILL_NO_IS_REQUIRED: 'BILL_NO_IS_REQUIRED',
|
||||||
|
BILL_HAS_ASSOCIATED_PAYMENT_ENTRIES: 'BILL_HAS_ASSOCIATED_PAYMENT_ENTRIES'
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ export default class AutoIncrementOrdersService {
|
|||||||
|
|
||||||
// Settings service transaction number and prefix.
|
// Settings service transaction number and prefix.
|
||||||
const autoIncrement = settings.get({ group, key: 'auto_increment' }, false);
|
const autoIncrement = settings.get({ group, key: 'auto_increment' }, false);
|
||||||
|
|
||||||
const settingNo = settings.get({ group, key: 'next_number' }, '');
|
const settingNo = settings.get({ group, key: 'next_number' }, '');
|
||||||
const settingPrefix = settings.get({ group, key: 'number_prefix' }, '');
|
const settingPrefix = settings.get({ group, key: 'number_prefix' }, '');
|
||||||
|
|
||||||
@@ -37,11 +38,12 @@ export default class AutoIncrementOrdersService {
|
|||||||
*/
|
*/
|
||||||
async incrementSettingsNextNumber(tenantId: number, group: string) {
|
async incrementSettingsNextNumber(tenantId: number, group: string) {
|
||||||
const settings = this.tenancy.settings(tenantId);
|
const settings = this.tenancy.settings(tenantId);
|
||||||
|
|
||||||
const settingNo = settings.get({ group, key: 'next_number' });
|
const settingNo = settings.get({ group, key: 'next_number' });
|
||||||
const autoIncrement = settings.get({ group, key: 'auto_increment' });
|
const autoIncrement = settings.get({ group, key: 'auto_increment' });
|
||||||
|
|
||||||
// Can't continue if the auto-increment of the service was disabled.
|
// Can't continue if the auto-increment of the service was disabled.
|
||||||
if (!autoIncrement) return;
|
if (!autoIncrement) { return; }
|
||||||
|
|
||||||
settings.set(
|
settings.set(
|
||||||
{ group, key: 'next_number' },
|
{ group, key: 'next_number' },
|
||||||
|
|||||||
@@ -522,7 +522,7 @@ export default class PaymentReceiveService {
|
|||||||
public async deletePaymentReceive(
|
public async deletePaymentReceive(
|
||||||
tenantId: number,
|
tenantId: number,
|
||||||
paymentReceiveId: number,
|
paymentReceiveId: number,
|
||||||
authorizedUser: ISystemUser
|
authorizedUser: ISystemUser,
|
||||||
) {
|
) {
|
||||||
const { PaymentReceive, PaymentReceiveEntry } = this.tenancy.models(
|
const { PaymentReceive, PaymentReceiveEntry } = this.tenancy.models(
|
||||||
tenantId
|
tenantId
|
||||||
@@ -541,6 +541,7 @@ export default class PaymentReceiveService {
|
|||||||
// Deletes the payment receive transaction.
|
// Deletes the payment receive transaction.
|
||||||
await PaymentReceive.query().findById(paymentReceiveId).delete();
|
await PaymentReceive.query().findById(paymentReceiveId).delete();
|
||||||
|
|
||||||
|
// Triggers `onPaymentReceiveDeleted` event.
|
||||||
await this.eventDispatcher.dispatch(events.paymentReceive.onDeleted, {
|
await this.eventDispatcher.dispatch(events.paymentReceive.onDeleted, {
|
||||||
tenantId,
|
tenantId,
|
||||||
paymentReceiveId,
|
paymentReceiveId,
|
||||||
|
|||||||
120
server/src/services/Setup/SetupService.ts
Normal file
120
server/src/services/Setup/SetupService.ts
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
import { Service, Inject } from 'typedi';
|
||||||
|
import Currencies from 'js-money/lib/currency';
|
||||||
|
import HasTenancyService from 'services/Tenancy/TenancyService';
|
||||||
|
import { IOrganizationSetupDTO, ITenant } from 'interfaces';
|
||||||
|
|
||||||
|
import CurrenciesService from 'services/Currencies/CurrenciesService';
|
||||||
|
import TenantsManagerService from 'services/Tenancy/TenantsManager';
|
||||||
|
import { ServiceError } from 'exceptions';
|
||||||
|
|
||||||
|
const ERRORS = {
|
||||||
|
TENANT_IS_ALREADY_SETUPED: 'TENANT_IS_ALREADY_SETUPED',
|
||||||
|
BASE_CURRENCY_INVALID: 'BASE_CURRENCY_INVALID',
|
||||||
|
};
|
||||||
|
|
||||||
|
@Service()
|
||||||
|
export default class SetupService {
|
||||||
|
@Inject()
|
||||||
|
tenancy: HasTenancyService;
|
||||||
|
|
||||||
|
@Inject()
|
||||||
|
currenciesService: CurrenciesService;
|
||||||
|
|
||||||
|
@Inject()
|
||||||
|
tenantsManager: TenantsManagerService;
|
||||||
|
|
||||||
|
@Inject('repositories')
|
||||||
|
sysRepositories: any;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transformes the setup DTO to settings.
|
||||||
|
* @param {IOrganizationSetupDTO} setupDTO
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
private transformSetupDTOToOptions(setupDTO: IOrganizationSetupDTO) {
|
||||||
|
return [
|
||||||
|
{ key: 'name', value: setupDTO.organizationName },
|
||||||
|
{ key: 'base_currency', value: setupDTO.baseCurrency },
|
||||||
|
{ key: 'time_zone', value: setupDTO.timeZone },
|
||||||
|
{ key: 'industry', value: setupDTO.industry },
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets organization setup settings.
|
||||||
|
* @param {number} tenantId
|
||||||
|
* @param {IOrganizationSetupDTO} organizationSetupDTO
|
||||||
|
*/
|
||||||
|
private setOrganizationSetupSettings(
|
||||||
|
tenantId: number,
|
||||||
|
organizationSetupDTO: IOrganizationSetupDTO
|
||||||
|
) {
|
||||||
|
const settings = this.tenancy.settings(tenantId);
|
||||||
|
|
||||||
|
// Can't continue if app is already configured.
|
||||||
|
if (settings.get('app_configured')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
settings.set([
|
||||||
|
...this.transformSetupDTOToOptions(organizationSetupDTO)
|
||||||
|
.filter((option) => typeof option.value !== 'undefined')
|
||||||
|
.map((option) => ({
|
||||||
|
...option,
|
||||||
|
group: 'organization',
|
||||||
|
})),
|
||||||
|
{ key: 'app_configured', value: true },
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates the base currency code.
|
||||||
|
* @param {string} baseCurrency
|
||||||
|
*/
|
||||||
|
public validateBaseCurrencyCode(baseCurrency: string) {
|
||||||
|
if (typeof Currencies[baseCurrency] === 'undefined') {
|
||||||
|
throw new ServiceError(ERRORS.BASE_CURRENCY_INVALID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Organization setup DTO.
|
||||||
|
* @param {IOrganizationSetupDTO} organizationSetupDTO
|
||||||
|
* @return {Promise<void>}
|
||||||
|
*/
|
||||||
|
public async organizationSetup(
|
||||||
|
tenantId: number,
|
||||||
|
organizationSetupDTO: IOrganizationSetupDTO,
|
||||||
|
): Promise<void> {
|
||||||
|
const { tenantRepository } = this.sysRepositories;
|
||||||
|
|
||||||
|
// Find tenant model by the given id.
|
||||||
|
const tenant = await tenantRepository.findOneById(tenantId);
|
||||||
|
|
||||||
|
// Validate base currency code.
|
||||||
|
this.validateBaseCurrencyCode(organizationSetupDTO.baseCurrency);
|
||||||
|
|
||||||
|
// Validate tenant not already seeded.
|
||||||
|
this.validateTenantNotSeeded(tenant);
|
||||||
|
|
||||||
|
// Seeds the base currency to the currencies list.
|
||||||
|
this.currenciesService.seedBaseCurrency(
|
||||||
|
tenantId,
|
||||||
|
organizationSetupDTO.baseCurrency
|
||||||
|
);
|
||||||
|
// Sets organization setup settings.
|
||||||
|
await this.setOrganizationSetupSettings(tenantId, organizationSetupDTO);
|
||||||
|
|
||||||
|
// Seed tenant.
|
||||||
|
await this.tenantsManager.seedTenant(tenant);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates tenant not seeded.
|
||||||
|
* @param {ITenant} tenant
|
||||||
|
*/
|
||||||
|
private validateTenantNotSeeded(tenant: ITenant) {
|
||||||
|
if (tenant.seededAt) {
|
||||||
|
throw new ServiceError(ERRORS.TENANT_IS_ALREADY_SETUPED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,6 +30,14 @@ export class ManualJournalSubscriber {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the manual journal next number increment once the journal be created.
|
||||||
|
*/
|
||||||
|
@On(events.manualJournals.onCreated)
|
||||||
|
public async handleJournalNumberIncrement({ tenantId }) {
|
||||||
|
await this.manualJournalsService.incrementNextJournalNumber(tenantId);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle manual journal edited event.
|
* Handle manual journal edited event.
|
||||||
*/
|
*/
|
||||||
@@ -107,16 +115,4 @@ export class ManualJournalSubscriber {
|
|||||||
manualJournalsIds
|
manualJournalsIds
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle increment next number of manual journal once be created.
|
|
||||||
*/
|
|
||||||
@On(events.manualJournals.onCreated)
|
|
||||||
public async handleJournalNextNumberIncrement({ tenantId }) {
|
|
||||||
const query = {
|
|
||||||
group: 'manual_journals',
|
|
||||||
key: 'next_number',
|
|
||||||
};
|
|
||||||
await this.settingsService.incrementNextNumber(tenantId, query);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user