Compare commits

...

43 Commits

Author SHA1 Message Date
Ahmed Bouhuolia
f9e5028e0d feat: add sample sheet to accounts and bank transactions 2024-03-28 00:57:01 +02:00
Ahmed Bouhuolia
7a3e121942 feat: import customers/vendors 2024-03-28 00:05:02 +02:00
Ahmed Bouhuolia
fc1d123c6b Merge pull request #388 from bigcapitalhq/fix-import-bugs
fix: import resources improvements
2024-03-27 04:10:18 +02:00
Ahmed Bouhuolia
ad4e51d81d fix: import resource imporements 2024-03-27 04:01:01 +02:00
Ahmed Bouhuolia
973d1832bd fix: import accounts issue 2024-03-22 20:45:05 +02:00
Ahmed Bouhuolia
858e3541cb Merge pull request #386 from bigcapitalhq/frontend-import-resource
feat(webapp): import resource UI
2024-03-22 01:45:19 +02:00
Ahmed Bouhuolia
a5ab535d3b feat(webapp): import preview page 2024-03-22 00:05:10 +02:00
Ahmed Bouhuolia
1d8cec5069 feat: wip import resource UI 2024-03-20 04:55:35 +02:00
Ahmed Bouhuolia
aba06991d4 Merge branch 'develop' into frontend-import-resource 2024-03-19 03:58:41 +02:00
Ahmed Bouhuolia
ff5730d8a7 feat(webapp): import resource UI 2024-03-19 03:57:57 +02:00
Ahmed Bouhuolia
a27c877321 Merge pull request #382 from bigcapitalhq/draft-import-resources
Import resources from csv/xlsx
2024-03-15 00:26:58 +02:00
Ahmed Bouhuolia
c5063fc5b5 chore: update the frozen pnpm-lock.yaml file 2024-03-15 00:26:23 +02:00
Ahmed Bouhuolia
ab4c0ab7a7 feat: wip import resources 2024-03-15 00:18:41 +02:00
Ahmed Bouhuolia
084d9d3d10 feat: import resource 2024-03-14 22:18:12 +02:00
Ahmed Bouhuolia
daa1e3a6bd feat: wip import resource 2024-03-13 02:14:25 +02:00
Ahmed Bouhuolia
4270d66928 feat(server): wip import resources 2024-03-11 20:05:12 +02:00
Ahmed Bouhuolia
90b4f3ef6d feat: import resources from csv/xlsx 2024-03-11 00:21:36 +02:00
Ahmed Bouhuolia
1fc6445123 Merge branch 'develop' into draft-import-resources 2024-03-10 14:54:32 +02:00
Ahmed Bouhuolia
b1d5390bfc WIP 2024-03-10 14:53:10 +02:00
Ahmed Bouhuolia
1ba26a3b85 Merge pull request #381 from bigcapitalhq/big-152-uncategorize-the-cashflow-transaction
feat: uncategorize the cashflow transaction
2024-03-10 03:09:43 +02:00
Ahmed Bouhuolia
2c98376162 feat: uncategorize transaciton catch validation errors 2024-03-10 03:08:24 +02:00
Ahmed Bouhuolia
b71c79fef5 feat: uncategorize the cashflow transaction 2024-03-10 02:53:57 +02:00
Ahmed Bouhuolia
2baf407814 Merge pull request #377 from bigcapitalhq/big-150-categorized-the-bank-synced-transactions
feat: Categorize the bank synced transactions
2024-03-07 20:59:42 +02:00
Ahmed Bouhuolia
83fbb7225d feat: remove uncategorized transaction from expenses 2024-03-07 20:58:44 +02:00
Ahmed Bouhuolia
b9a00418fa feat: abstract the uncategorized and all transactions boot wrappers 2024-03-07 14:31:59 +02:00
Ahmed Bouhuolia
62d3e386dd feat(server): move all cashflow under application service 2024-03-07 14:19:11 +02:00
Ahmed Bouhuolia
d87d674aba feat: wip categorize the cashflow transactions 2024-03-06 22:15:31 +02:00
Ahmed Bouhuolia
a17b4f6a8a feat: categorize cashflow transaction drawer 2024-03-05 22:27:42 +02:00
Ahmed Bouhuolia
db839137d0 feat(server): change the Plaid synced bank name. 2024-03-05 16:36:35 +02:00
Ahmed Bouhuolia
b602f28696 Merge branch 'develop' into big-150-categorized-the-bank-synced-transactions 2024-03-04 21:03:11 +02:00
Ahmed Bouhuolia
68f2f4ee84 feat: wip categorized transactions 2024-03-04 21:01:36 +02:00
Ahmed Bouhuolia
f23e8d98f6 feat(server): sync Plaid transactions to uncategorized transactions 2024-03-04 13:42:17 +02:00
Ahmed Bouhuolia
9db03350e0 feat(webapp): categorize the cashflow uncategorized transactions 2024-03-04 13:41:15 +02:00
Ahmed Bouhuolia
0273714a07 feat(webapp): Filter account transactions by categorized/uncategorized transactions 2024-03-02 17:01:58 +02:00
Ahmed Bouhuolia
ea497bfdea Merge pull request #378 from bigcapitalhq/picking-the-sync-bank-service-provider
feat(webapp): Dialog to choose the bank service provider
2024-03-01 17:56:45 +02:00
Ahmed Bouhuolia
685a6150e6 feat(webapp): add the text of connect bank dialog 2024-03-01 17:55:21 +02:00
Ahmed Bouhuolia
daf87a8ec7 feat(webapp): Dialog to choose the bank service provider 2024-03-01 17:22:28 +02:00
Ahmed Bouhuolia
5b4ddadcf6 feat(server): categorize the synced bank transactions 2024-03-01 17:12:56 +02:00
Ahmed Bouhuolia
ea8c5458ff feat: Categorize the bank synced transactions 2024-02-29 23:53:26 +02:00
Ahmed Bouhuolia
0833baabda Merge pull request #361 from ANasouf/BIG-111-add-Convert-to-invoice-button-on-estimate-drawer-toolbar
Big 111 add convert to invoice button on estimate drawer toolbar
2024-02-26 15:32:19 +02:00
Ahmed Bouhuolia
ab7eb40ea9 feat(webapp): add icon to convert to invoice button 2024-02-26 15:29:51 +02:00
a.nasouf
a8671a8d99 feat(webapp): add convert to invoice on estimate drawer toolbar 2024-02-10 22:18:13 +02:00
a.nasouf
cd8f64dfdc feat(webapp): add mark as delivered to action bar of invoice details drawer 2024-02-10 21:04:54 +02:00
208 changed files with 9156 additions and 452 deletions

View File

@@ -25,6 +25,7 @@
"@types/i18n": "^0.8.7",
"@types/knex": "^0.16.1",
"@types/mathjs": "^6.0.12",
"@types/yup": "^0.29.13",
"accepts": "^1.3.7",
"accounting": "^0.4.1",
"agenda": "^4.2.1",
@@ -53,7 +54,6 @@
"express": "^4.17.1",
"express-basic-auth": "^1.2.0",
"express-boom": "^3.0.0",
"express-fileupload": "^1.1.7-alpha.3",
"express-oauth-server": "^2.0.0",
"express-validator": "^6.12.2",
"form-data": "^4.0.0",
@@ -77,6 +77,7 @@
"moment-timezone": "^0.5.43",
"mongodb": "^6.1.0",
"mongoose": "^5.10.0",
"multer": "1.4.5-lts.1",
"mustache": "^3.0.3",
"mysql": "^2.17.1",
"mysql2": "^1.6.5",
@@ -105,7 +106,8 @@
"typedi": "^0.8.0",
"uniqid": "^5.2.0",
"winston": "^3.2.1",
"xlsx": "^0.18.5"
"xlsx": "^0.18.5",
"yup": "^0.28.1"
},
"devDependencies": {
"@types/lodash": "^4.14.158",

View File

@@ -242,7 +242,7 @@
"account.field.normal.credit": "دائن",
"account.field.normal.debit": "مدين",
"account.field.type": "نوع الحساب",
"account.field.active": "Activity",
"account.field.active": "Active",
"account.field.balance": "الرصيد",
"account.field.created_at": "أنشئت في",
"item.field.type": "نوع الصنف",

View File

@@ -241,7 +241,8 @@
"account.field.normal.credit": "Credit",
"account.field.normal.debit": "Debit",
"account.field.type": "Type",
"account.field.active": "Activity",
"account.field.active": "Active",
"account.field.currency": "Currency",
"account.field.balance": "Balance",
"account.field.created_at": "Created at",
"item.field.type": "Item type",
@@ -376,8 +377,8 @@
"customer.field.last_name": "Last name",
"customer.field.display_name": "Display name",
"customer.field.email": "Email",
"customer.field.work_phone": "Work phone",
"customer.field.personal_phone": "Personal phone",
"customer.field.work_phone": "Work Phone Number",
"customer.field.personal_phone": "Personal Phone Number",
"customer.field.company_name": "Company name",
"customer.field.website": "Website",
"customer.field.opening_balance_at": "Opening balance at",
@@ -385,7 +386,7 @@
"customer.field.created_at": "Created at",
"customer.field.balance": "Balance",
"customer.field.status": "Status",
"customer.field.currency": "Curreny",
"customer.field.currency": "Currency",
"customer.field.status.active": "Active",
"customer.field.status.inactive": "Inactive",
"customer.field.status.overdue": "Overdue",
@@ -394,8 +395,8 @@
"vendor.field.last_name": "Last name",
"vendor.field.display_name": "Display name",
"vendor.field.email": "Email",
"vendor.field.work_phone": "Work phone",
"vendor.field.personal_phone": "Personal phone",
"vendor.field.work_phone": "Work Phone Number",
"vendor.field.personal_phone": "Personal Phone Number",
"vendor.field.company_name": "Company name",
"vendor.field.website": "Website",
"vendor.field.opening_balance_at": "Opening balance at",
@@ -403,7 +404,7 @@
"vendor.field.created_at": "Created at",
"vendor.field.balance": "Balance",
"vendor.field.status": "Status",
"vendor.field.currency": "Curreny",
"vendor.field.currency": "Currency",
"vendor.field.status.active": "Active",
"vendor.field.status.inactive": "Inactive",
"vendor.field.status.overdue": "Overdue",

View File

@@ -27,7 +27,7 @@ export default class AccountsController extends BaseController {
/**
* Router constructor method.
*/
router() {
public router() {
const router = Router();
router.get(
@@ -98,7 +98,7 @@ export default class AccountsController extends BaseController {
/**
* Create account DTO Schema validation.
*/
get createAccountDTOSchema() {
private get createAccountDTOSchema() {
return [
check('name')
.exists()
@@ -131,7 +131,7 @@ export default class AccountsController extends BaseController {
/**
* Account DTO Schema validation.
*/
get editAccountDTOSchema() {
private get editAccountDTOSchema() {
return [
check('name')
.exists()
@@ -160,14 +160,14 @@ export default class AccountsController extends BaseController {
];
}
get accountParamSchema() {
private get accountParamSchema() {
return [param('id').exists().isNumeric().toInt()];
}
/**
* Accounts list validation schema.
*/
get accountsListSchema() {
private get accountsListSchema() {
return [
query('view_slug').optional({ nullable: true }).isString().trim(),
query('stringified_filter_roles').optional().isJSON(),
@@ -349,7 +349,7 @@ export default class AccountsController extends BaseController {
// Filter query.
const filter = {
sortOrder: 'desc',
columnSortBy: 'created_at',
columnSortBy: 'createdAt',
inactiveMode: false,
structure: IAccountsStructureType.Tree,
...this.matchedQueryData(req),

View File

@@ -13,9 +13,9 @@ export default class CashflowController {
router() {
const router = Router();
router.use(Container.get(CommandCashflowTransaction).router());
router.use(Container.get(GetCashflowTransaction).router());
router.use(Container.get(GetCashflowAccounts).router());
router.use(Container.get(CommandCashflowTransaction).router());
router.use(Container.get(DeleteCashflowTransaction).router());
return router;

View File

@@ -3,14 +3,15 @@ import { Router, Request, Response, NextFunction } from 'express';
import { param } from 'express-validator';
import BaseController from '../BaseController';
import { ServiceError } from '@/exceptions';
import DeleteCashflowTransactionService from '../../../services/Cashflow/DeleteCashflowTransactionService';
import CheckPolicies from '@/api/middleware/CheckPolicies';
import { AbilitySubject, CashflowAction } from '@/interfaces';
import { CashflowApplication } from '@/services/Cashflow/CashflowApplication';
@Service()
export default class DeleteCashflowTransaction extends BaseController {
export default class DeleteCashflowTransactionController extends BaseController {
@Inject()
deleteCashflowService: DeleteCashflowTransactionService;
private cashflowApplication: CashflowApplication;
/**
* Controller router.
@@ -44,7 +45,7 @@ export default class DeleteCashflowTransaction extends BaseController {
try {
const { oldCashflowTransaction } =
await this.deleteCashflowService.deleteCashflowTransaction(
await this.cashflowApplication.deleteTransaction(
tenantId,
transactionId
);
@@ -92,6 +93,19 @@ export default class DeleteCashflowTransaction extends BaseController {
],
});
}
if (
error.errorType ===
'CANNOT_DELETE_TRANSACTION_CONVERTED_FROM_UNCATEGORIZED'
) {
return res.boom.badRequest(null, {
errors: [
{
type: 'CANNOT_DELETE_TRANSACTION_CONVERTED_FROM_UNCATEGORIZED',
code: 4100,
},
],
});
}
}
next(error);
}

View File

@@ -1,20 +1,16 @@
import { Service, Inject } from 'typedi';
import { Router, Request, Response, NextFunction } from 'express';
import { param, query } from 'express-validator';
import GetCashflowAccountsService from '@/services/Cashflow/GetCashflowAccountsService';
import { query } from 'express-validator';
import BaseController from '../BaseController';
import GetCashflowTransactionsService from '@/services/Cashflow/GetCashflowTransactionsService';
import { ServiceError } from '@/exceptions';
import CheckPolicies from '@/api/middleware/CheckPolicies';
import { AbilitySubject, CashflowAction } from '@/interfaces';
import { CashflowApplication } from '@/services/Cashflow/CashflowApplication';
@Service()
export default class GetCashflowAccounts extends BaseController {
@Inject()
getCashflowAccountsService: GetCashflowAccountsService;
@Inject()
getCashflowTransactionsService: GetCashflowTransactionsService;
private cashflowApplication: CashflowApplication;
/**
* Controller router.
@@ -62,10 +58,7 @@ export default class GetCashflowAccounts extends BaseController {
try {
const cashflowAccounts =
await this.getCashflowAccountsService.getCashflowAccounts(
tenantId,
filter
);
await this.cashflowApplication.getCashflowAccounts(tenantId, filter);
return res.status(200).send({
cashflow_accounts: this.transfromToResponse(cashflowAccounts),

View File

@@ -2,15 +2,15 @@ import { Service, Inject } from 'typedi';
import { Router, Request, Response, NextFunction } from 'express';
import { param } from 'express-validator';
import BaseController from '../BaseController';
import GetCashflowTransactionsService from '@/services/Cashflow/GetCashflowTransactionsService';
import { ServiceError } from '@/exceptions';
import CheckPolicies from '@/api/middleware/CheckPolicies';
import { AbilitySubject, CashflowAction } from '@/interfaces';
import { CashflowApplication } from '@/services/Cashflow/CashflowApplication';
@Service()
export default class GetCashflowAccounts extends BaseController {
@Inject()
getCashflowTransactionsService: GetCashflowTransactionsService;
private cashflowApplication: CashflowApplication;
/**
* Controller router.
@@ -43,11 +43,10 @@ export default class GetCashflowAccounts extends BaseController {
const { transactionId } = req.params;
try {
const cashflowTransaction =
await this.getCashflowTransactionsService.getCashflowTransaction(
tenantId,
transactionId
);
const cashflowTransaction = await this.cashflowApplication.getTransaction(
tenantId,
transactionId
);
return res.status(200).send({
cashflow_transaction: this.transfromToResponse(cashflowTransaction),

View File

@@ -1,16 +1,16 @@
import { Service, Inject } from 'typedi';
import { check } from 'express-validator';
import { ValidationChain, check, param, query } from 'express-validator';
import { Router, Request, Response, NextFunction } from 'express';
import BaseController from '../BaseController';
import { ServiceError } from '@/exceptions';
import NewCashflowTransactionService from '@/services/Cashflow/NewCashflowTransactionService';
import CheckPolicies from '@/api/middleware/CheckPolicies';
import { AbilitySubject, CashflowAction } from '@/interfaces';
import { CashflowApplication } from '@/services/Cashflow/CashflowApplication';
@Service()
export default class NewCashflowTransactionController extends BaseController {
@Inject()
private newCashflowTranscationService: NewCashflowTransactionService;
private cashflowApplication: CashflowApplication;
/**
* Router constructor.
@@ -18,6 +18,18 @@ export default class NewCashflowTransactionController extends BaseController {
public router() {
const router = Router();
router.get(
'/transactions/uncategorized/:id',
this.asyncMiddleware(this.getUncategorizedCashflowTransaction),
this.catchServiceErrors
);
router.get(
'/transactions/:id/uncategorized',
this.getUncategorizedTransactionsValidationSchema,
this.validationResult,
this.asyncMiddleware(this.getUncategorizedCashflowTransactions),
this.catchServiceErrors
);
router.post(
'/transactions',
CheckPolicies(CashflowAction.Create, AbilitySubject.Cashflow),
@@ -26,13 +38,72 @@ export default class NewCashflowTransactionController extends BaseController {
this.asyncMiddleware(this.newCashflowTransaction),
this.catchServiceErrors
);
router.post(
'/transactions/:id/uncategorize',
this.revertCategorizedCashflowTransaction,
this.catchServiceErrors
);
router.post(
'/transactions/:id/categorize',
this.categorizeCashflowTransactionValidationSchema,
this.validationResult,
this.categorizeCashflowTransaction,
this.catchServiceErrors
);
router.post(
'/transaction/:id/categorize/expense',
this.categorizeAsExpenseValidationSchema,
this.validationResult,
this.categorizesCashflowTransactionAsExpense,
this.catchServiceErrors
);
return router;
}
/**
* Getting uncategorized transactions validation schema.
* @returns {ValidationChain}
*/
public get getUncategorizedTransactionsValidationSchema() {
return [
param('id').exists().isNumeric().toInt(),
query('page').optional().isNumeric().toInt(),
query('page_size').optional().isNumeric().toInt(),
];
}
/**
* Categorize as expense validation schema.
*/
public get categorizeAsExpenseValidationSchema() {
return [
check('expense_account_id').exists(),
check('date').isISO8601().exists(),
check('reference_no').optional(),
check('exchange_rate').optional().isFloat({ gt: 0 }).toFloat(),
];
}
/**
* Categorize cashflow tranasction validation schema.
*/
public get categorizeCashflowTransactionValidationSchema() {
return [
check('date').exists().isISO8601().toDate(),
check('credit_account_id').exists().isInt().toInt(),
check('transaction_number').optional(),
check('transaction_type').exists(),
check('reference_no').optional(),
check('exchange_rate').optional().isFloat({ gt: 0 }).toFloat(),
check('description').optional(),
check('branch_id').optional({ nullable: true }).isNumeric().toInt(),
];
}
/**
* New cashflow transaction validation schema.
*/
get newTransactionValidationSchema() {
public get newTransactionValidationSchema() {
return [
check('date').exists().isISO8601().toDate(),
check('reference_no').optional({ nullable: true }).trim().escape(),
@@ -48,9 +119,7 @@ export default class NewCashflowTransactionController extends BaseController {
check('credit_account_id').exists().isInt().toInt(),
check('exchange_rate').optional().isFloat({ gt: 0 }).toFloat(),
check('branch_id').optional({ nullable: true }).isNumeric().toInt(),
check('publish').default(false).isBoolean().toBoolean(),
];
}
@@ -70,13 +139,12 @@ export default class NewCashflowTransactionController extends BaseController {
const ownerContributionDTO = this.matchedBodyData(req);
try {
const { cashflowTransaction } =
await this.newCashflowTranscationService.newCashflowTransaction(
const cashflowTransaction =
await this.cashflowApplication.createTransaction(
tenantId,
ownerContributionDTO,
userId
);
return res.status(200).send({
id: cashflowTransaction.id,
message: 'New cashflow transaction has been created successfully.',
@@ -86,11 +154,147 @@ export default class NewCashflowTransactionController extends BaseController {
}
};
/**
* Revert the categorized cashflow transaction.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
private revertCategorizedCashflowTransaction = async (
req: Request,
res: Response,
next: NextFunction
) => {
const { tenantId } = req;
const { id: cashflowTransactionId } = req.params;
try {
const data = await this.cashflowApplication.uncategorizeTransaction(
tenantId,
cashflowTransactionId
);
return res.status(200).send({ data });
} catch (error) {
next(error);
}
};
/**
* Categorize the cashflow transaction.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
private categorizeCashflowTransaction = async (
req: Request,
res: Response,
next: NextFunction
) => {
const { tenantId } = req;
const { id: cashflowTransactionId } = req.params;
const cashflowTransaction = this.matchedBodyData(req);
try {
await this.cashflowApplication.categorizeTransaction(
tenantId,
cashflowTransactionId,
cashflowTransaction
);
return res.status(200).send({
message: 'The cashflow transaction has been created successfully.',
});
} catch (error) {
next(error);
}
};
/**
* Categorize the transaction as expense transaction.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
private categorizesCashflowTransactionAsExpense = async (
req: Request,
res: Response,
next: NextFunction
) => {
const { tenantId } = req;
const { id: cashflowTransactionId } = req.params;
const cashflowTransaction = this.matchedBodyData(req);
try {
await this.cashflowApplication.categorizeAsExpense(
tenantId,
cashflowTransactionId,
cashflowTransaction
);
return res.status(200).send({
message: 'The cashflow transaction has been created successfully.',
});
} catch (error) {
next(error);
}
};
/**
* Retrieves the uncategorized cashflow transactions.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
public getUncategorizedCashflowTransaction = async (
req: Request,
res: Response,
next: NextFunction
) => {
const { tenantId } = req;
const { id: transactionId } = req.params;
try {
const data = await this.cashflowApplication.getUncategorizedTransaction(
tenantId,
transactionId
);
return res.status(200).send({ data });
} catch (error) {
next(error);
}
};
/**
* Retrieves the uncategorized cashflow transactions.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
public getUncategorizedCashflowTransactions = async (
req: Request,
res: Response,
next: NextFunction
) => {
const { tenantId } = req;
const { id: accountId } = req.params;
const query = this.matchedQueryData(req);
try {
const data = await this.cashflowApplication.getUncategorizedTransactions(
tenantId,
accountId,
query
);
return res.status(200).send(data);
} catch (error) {
next(error);
}
};
/**
* Handle the service errors.
* @param error
* @param req
* @param res
* @param {Request} req
* @param {res
* @param next
* @returns
*/
@@ -140,6 +344,16 @@ export default class NewCashflowTransactionController extends BaseController {
],
});
}
if (error.errorType === 'UNCATEGORIZED_TRANSACTION_TYPE_INVALID') {
return res.boom.badRequest(null, {
errors: [
{
type: 'UNCATEGORIZED_TRANSACTION_TYPE_INVALID',
code: 4100,
},
],
});
}
}
next(error);
}

View File

@@ -160,10 +160,8 @@ export default class CustomersController extends ContactsController {
try {
const contact = await this.customersApplication.createCustomer(
tenantId,
contactDTO,
user
contactDTO
);
return res.status(200).send({
id: contact.id,
message: 'The customer has been created successfully.',
@@ -291,7 +289,7 @@ export default class CustomersController extends ContactsController {
const filter = {
inactiveMode: false,
sortOrder: 'desc',
columnSortBy: 'created_at',
columnSortBy: 'createdAt',
page: 1,
pageSize: 12,
...this.matchedQueryData(req),

View File

@@ -272,7 +272,7 @@ export default class VendorsController extends ContactsController {
const vendorsFilter: IVendorsFilter = {
inactiveMode: false,
sortOrder: 'desc',
columnSortBy: 'created_at',
columnSortBy: 'createdAt',
page: 1,
pageSize: 12,
...this.matchedQueryData(req),

View File

@@ -0,0 +1,248 @@
import { Inject, Service } from 'typedi';
import { Router, Request, Response, NextFunction } from 'express';
import { body, param, query } from 'express-validator';
import { defaultTo } from 'lodash';
import BaseController from '@/api/controllers/BaseController';
import { ServiceError } from '@/exceptions';
import { ImportResourceApplication } from '@/services/Import/ImportResourceApplication';
import { uploadImportFile } from './_utils';
import { parseJsonSafe } from '@/utils/parse-json-safe';
@Service()
export class ImportController extends BaseController {
@Inject()
private importResourceApp: ImportResourceApplication;
/**
* Router constructor method.
*/
router() {
const router = Router();
router.post(
'/file',
uploadImportFile.single('file'),
this.importValidationSchema,
this.validationResult,
this.asyncMiddleware(this.fileUpload.bind(this)),
this.catchServiceErrors
);
router.post(
'/:import_id/import',
this.asyncMiddleware(this.import.bind(this)),
this.catchServiceErrors
);
router.post(
'/:import_id/mapping',
[
param('import_id').exists().isString(),
body('mapping').exists().isArray({ min: 1 }),
body('mapping.*.from').exists(),
body('mapping.*.to').exists(),
],
this.validationResult,
this.asyncMiddleware(this.mapping.bind(this)),
this.catchServiceErrors
);
router.get(
'/sample',
[query('resource').exists(), query('format').optional()],
this.downloadImportSample.bind(this),
this.catchServiceErrors
);
router.get(
'/:import_id',
this.asyncMiddleware(this.getImportFileMeta.bind(this)),
this.catchServiceErrors
);
router.get(
'/:import_id/preview',
this.asyncMiddleware(this.preview.bind(this)),
this.catchServiceErrors
);
return router;
}
/**
* Import validation schema.
* @returns {ValidationSchema[]}
*/
private get importValidationSchema() {
return [body('resource').exists(), body('params').optional()];
}
/**
* Imports xlsx/csv to the given resource type.
* @param {Request} req -
* @param {Response} res -
* @param {NextFunction} next -
*/
private async fileUpload(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
const body = this.matchedBodyData(req);
const params = defaultTo(parseJsonSafe(body.params), {});
try {
const data = await this.importResourceApp.import(
tenantId,
body.resource,
req.file.filename,
params
);
return res.status(200).send(data);
} catch (error) {
next(error);
}
}
/**
* Maps the columns of the imported file.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
private async mapping(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
const { import_id: importId } = req.params;
const body = this.matchedBodyData(req);
try {
const mapping = await this.importResourceApp.mapping(
tenantId,
importId,
body?.mapping
);
return res.status(200).send(mapping);
} catch (error) {
next(error);
}
}
/**
* Preview the imported file before actual importing.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
private async preview(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
const { import_id: importId } = req.params;
try {
const preview = await this.importResourceApp.preview(tenantId, importId);
return res.status(200).send(preview);
} catch (error) {
next(error);
}
}
/**
* Importing the imported file to the application storage.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
private async import(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
const { import_id: importId } = req.params;
try {
const result = await this.importResourceApp.process(tenantId, importId);
return res.status(200).send(result);
} catch (error) {
next(error);
}
}
/**
* Retrieves the csv/xlsx sample sheet of the given resource name.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
private async downloadImportSample(
req: Request,
res: Response,
next: NextFunction
) {
const { tenantId } = req;
const { format, resource } = this.matchedQueryData(req);
try {
const result = this.importResourceApp.sample(tenantId, resource, format);
return res.status(200).send(result);
} catch (error) {
next(error);
}
}
/**
* Retrieves the import file meta.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
private async getImportFileMeta(
req: Request,
res: Response,
next: NextFunction
) {
const { tenantId } = req;
const { import_id: importId } = req.params;
try {
const result = await this.importResourceApp.importMeta(
tenantId,
importId
);
return res.status(200).send(result);
} catch (error) {
next(error);
}
}
/**
* Transforms service errors to response.
* @param {Error}
* @param {Request} req
* @param {Response} res
* @param {ServiceError} error
*/
private catchServiceErrors(
error,
req: Request,
res: Response,
next: NextFunction
) {
if (error instanceof ServiceError) {
if (error.errorType === 'INVALID_MAP_ATTRS') {
return res.status(400).send({
errors: [{ type: 'INVALID_MAP_ATTRS' }],
});
}
if (error.errorType === 'DUPLICATED_FROM_MAP_ATTR') {
return res.status(400).send({
errors: [{ type: 'DUPLICATED_FROM_MAP_ATTR' }],
});
}
if (error.errorType === 'DUPLICATED_TO_MAP_ATTR') {
return res.status(400).send({
errors: [{ type: 'DUPLICATED_TO_MAP_ATTR' }],
});
}
if (error.errorType === 'IMPORTED_FILE_EXTENSION_INVALID') {
return res.status(400).send({
errors: [{ type: 'IMPORTED_FILE_EXTENSION_INVALID' }],
});
}
return res.status(400).send({
errors: [{ type: error.errorType }],
});
}
next(error);
}
}

View File

@@ -0,0 +1,21 @@
import Multer from 'multer';
import { ServiceError } from '@/exceptions';
export function allowSheetExtensions(req, file, cb) {
if (
file.mimetype !== 'text/csv' &&
file.mimetype !== 'application/vnd.ms-excel' &&
file.mimetype !== 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
) {
cb(new ServiceError('IMPORTED_FILE_EXTENSION_INVALID'));
return;
}
cb(null, true);
}
export const uploadImportFile = Multer({
dest: './public/imports',
limits: { fileSize: 5 * 1024 * 1024 },
fileFilter: allowSheetExtensions,
});

View File

@@ -56,6 +56,7 @@ import { ProjectsController } from './controllers/Projects/Projects';
import { ProjectTasksController } from './controllers/Projects/Tasks';
import { ProjectTimesController } from './controllers/Projects/Times';
import { TaxRatesController } from './controllers/TaxRates/TaxRates';
import { ImportController } from './controllers/Import/ImportController';
import { BankingController } from './controllers/Banking/BankingController';
import { Webhooks } from './controllers/Webhooks/Webhooks';
@@ -135,6 +136,9 @@ export default () => {
dashboard.use('/warehouses', Container.get(WarehousesController).router());
dashboard.use('/projects', Container.get(ProjectsController).router());
dashboard.use('/tax-rates', Container.get(TaxRatesController).router());
dashboard.use('/import', Container.get(ImportController).router());
dashboard.use('/', Container.get(ProjectTasksController).router());
dashboard.use('/', Container.get(ProjectTimesController).router());

View File

@@ -0,0 +1,16 @@
exports.up = function (knex) {
return knex.schema.createTable('imports', (table) => {
table.increments();
table.string('filename');
table.string('import_id');
table.string('resource');
table.json('columns');
table.json('mapping');
table.json('params');
table.timestamps();
});
};
exports.down = function (knex) {
return knex.schema.dropTableIfExists('imports');
};

View File

@@ -0,0 +1,28 @@
exports.up = function (knex) {
return knex.schema.createTable(
'uncategorized_cashflow_transactions',
(table) => {
table.increments('id');
table.date('date').index();
table.decimal('amount');
table.string('currency_code');
table.string('reference_no').index();
table.string('payee');
table
.integer('account_id')
.unsigned()
.references('id')
.inTable('accounts');
table.string('description');
table.string('categorize_ref_type');
table.integer('categorize_ref_id').unsigned();
table.boolean('categorized').defaultTo(false);
table.string('plaid_transaction_id');
table.timestamps();
}
);
};
exports.down = function (knex) {
return knex.schema.dropTableIfExists('uncategorized_cashflow_transactions');
};

View File

@@ -0,0 +1,10 @@
exports.up = function (knex) {
return knex.schema.table('accounts', (table) => {
table.integer('uncategorized_transactions').defaultTo(0);
table.boolean('is_system_account').defaultTo(true);
table.boolean('is_feeds_active').defaultTo(false);
table.datetime('last_feeds_updated_at').nullable();
});
};
exports.down = function (knex) {};

View File

@@ -0,0 +1,15 @@
exports.up = function (knex) {
return knex.schema.table('cashflow_transactions', (table) => {
table
.integer('uncategorized_transaction_id')
.unsigned()
.references('id')
.inTable('uncategorized_cashflow_transactions');
});
};
exports.down = function (knex) {
return knex.schema.table('cashflow_transactions', (table) => {
table.dropColumn('uncategorized_transaction_id');
});
};

View File

@@ -233,3 +233,38 @@ export interface ICashflowTransactionSchema {
}
export interface ICashflowTransactionInput extends ICashflowTransactionSchema {}
export interface ICategorizeCashflowTransactioDTO {
creditAccountId: number;
referenceNo: string;
transactionNumber: string;
transactionType: string;
exchangeRate: number;
description: string;
branchId: number;
}
export interface IUncategorizedCashflowTransaction {
id?: number;
amount: number;
date: Date;
currencyCode: string;
accountId: number;
description: string;
referenceNo: string;
categorizeRefType: string;
categorizeRefId: number;
categorized: boolean;
}
export interface CreateUncategorizedTransactionDTO {
date: Date | string;
accountId: number;
amount: number;
currencyCode: string;
payee?: string;
description?: string;
referenceNo?: string | null;
plaidTransactionId?: string | null;
}

View File

@@ -1,5 +1,6 @@
import { Knex } from 'knex';
import { IAccount } from './Account';
import { IUncategorizedCashflowTransaction } from './CashFlow';
export interface ICashflowAccountTransactionsFilter {
page: number;
@@ -50,6 +51,7 @@ export interface ICashflowCommandDTO {
export interface ICashflowNewCommandDTO extends ICashflowCommandDTO {
plaidAccountId?: string;
uncategorizedTransactionId?: number;
}
export interface ICashflowTransaction {
@@ -82,6 +84,8 @@ export interface ICashflowTransaction {
isCashDebit?: boolean;
isCashCredit?: boolean;
uncategorizedTransactionId?: number;
}
export interface ICashflowTransactionLine {
@@ -124,8 +128,39 @@ export interface ICommandCashflowDeletedPayload {
trx: Knex.Transaction;
}
export interface ICashflowTransactionCategorizedPayload {
tenantId: number;
cashflowTransactionId: number;
cashflowTransaction: ICashflowTransaction;
trx: Knex.Transaction;
}
export interface ICashflowTransactionUncategorizingPayload {
tenantId: number;
uncategorizedTransaction: IUncategorizedCashflowTransaction;
trx: Knex.Transaction;
}
export interface ICashflowTransactionUncategorizedPayload {
tenantId: number;
uncategorizedTransaction: IUncategorizedCashflowTransaction;
oldUncategorizedTransaction: IUncategorizedCashflowTransaction;
trx: Knex.Transaction;
}
export enum CashflowAction {
Create = 'Create',
Delete = 'Delete',
View = 'View',
}
export interface CategorizeTransactionAsExpenseDTO {
expenseAccountId: number;
exchangeRate: number;
referenceNo: string;
description: string;
branchId?: number;
}
export interface IGetUncategorizedTransactionsQuery {
page?: number;
pageSize?: number;
}

View File

@@ -34,20 +34,40 @@ export interface IModelMetaFieldCommon {
columnable?: boolean;
fieldType: IModelColumnType;
customQuery?: Function;
required?: boolean;
importHint?: string;
order?: number;
}
export interface IModelMetaFieldNumber {
fieldType: 'number';
export interface IModelMetaFieldText {
fieldType: 'text';
minLength?: number;
maxLength?: number;
}
export interface IModelMetaFieldOther {
fieldType: 'text' | 'boolean';
export interface IModelMetaFieldBoolean {
fieldType: 'boolean';
}
export interface IModelMetaFieldNumber {
fieldType: 'number';
min?: number;
max?: number;
}
export interface IModelMetaFieldDate {
fieldType: 'date';
}
export interface IModelMetaFieldUrl {
fieldType: 'url';
}
export type IModelMetaField = IModelMetaFieldCommon &
(IModelMetaFieldOther | IModelMetaEnumerationField | IModelMetaRelationField);
(
| IModelMetaFieldText
| IModelMetaFieldNumber
| IModelMetaFieldBoolean
| IModelMetaFieldDate
| IModelMetaFieldUrl
| IModelMetaEnumerationField
| IModelMetaRelationField
);
export interface IModelMetaEnumerationOption {
key: string;
@@ -70,12 +90,12 @@ export interface IModelMetaRelationEnumerationField {
relationEntityKey: string;
}
export type IModelMetaRelationField = IModelMetaRelationFieldCommon & (
IModelMetaRelationEnumerationField
);
export type IModelMetaRelationField = IModelMetaRelationFieldCommon &
IModelMetaRelationEnumerationField;
export interface IModelMeta {
defaultFilterField: string;
defaultSort: IModelMetaDefaultSort;
importable?: boolean;
fields: { [key: string]: IModelMetaField };
}

View File

@@ -38,6 +38,7 @@ export interface PlaidTransaction {
iso_currency_code: string;
transaction_id: string;
transaction_type: string;
payment_meta: { reference_number: string | null; payee: string | null };
}
export interface PlaidFetchedTransactionsUpdates {

View File

@@ -88,6 +88,8 @@ import { PlaidUpdateTransactionsOnItemCreatedSubscriber } from '@/services/Banki
import { InvoiceChangeStatusOnMailSentSubscriber } from '@/services/Sales/Invoices/subscribers/InvoiceChangeStatusOnMailSentSubscriber';
import { SaleReceiptMarkClosedOnMailSentSubcriber } from '@/services/Sales/Receipts/subscribers/SaleReceiptMarkClosedOnMailSentSubcriber';
import { SaleEstimateMarkApprovedOnMailSent } from '@/services/Sales/Estimates/subscribers/SaleEstimateMarkApprovedOnMailSent';
import { DeleteCashflowTransactionOnUncategorize } from '@/services/Cashflow/subscribers/DeleteCashflowTransactionOnUncategorize';
import { PreventDeleteTransactionOnDelete } from '@/services/Cashflow/subscribers/PreventDeleteTransactionsOnDelete'; }
export default () => {
return new EventPublisher();
@@ -212,6 +214,10 @@ export const susbcribers = () => {
SyncItemTaxRateOnEditTaxSubscriber,
// Plaid
PlaidUpdateTransactionsOnItemCreatedSubscriber
PlaidUpdateTransactionsOnItemCreatedSubscriber,
// Cashflow
DeleteCashflowTransactionOnUncategorize,
PreventDeleteTransactionOnDelete
];
};

View File

@@ -4,7 +4,6 @@ import helmet from 'helmet';
import boom from 'express-boom';
import errorHandler from 'errorhandler';
import bodyParser from 'body-parser';
import fileUpload from 'express-fileupload';
import { Server } from 'socket.io';
import Container from 'typedi';
import routes from 'api';
@@ -47,13 +46,6 @@ export default ({ app }) => {
app.use('/public', express.static(path.join(global.__storage_dir)));
// Handle multi-media requests.
app.use(
fileUpload({
createParentPath: true,
})
);
// Logger middleware.
app.use(LoggerMiddleware);

View File

@@ -61,7 +61,9 @@ import Task from 'models/Task';
import TaxRate from 'models/TaxRate';
import TaxRateTransaction from 'models/TaxRateTransaction';
import Attachment from 'models/Attachment';
import Import from 'models/Import';
import PlaidItem from 'models/PlaidItem';
import UncategorizedCashflowTransaction from 'models/UncategorizedCashflowTransaction';
export default (knex) => {
const models = {
@@ -126,7 +128,9 @@ export default (knex) => {
TaxRate,
TaxRateTransaction,
Attachment,
PlaidItem
Import,
PlaidItem,
UncategorizedCashflowTransaction
};
return mapValues(models, (model) => model.bindKnex(knex));
};

View File

@@ -6,16 +6,24 @@ export default {
sortOrder: 'DESC',
sortField: 'name',
},
importable: true,
fields: {
name: {
name: 'account.field.name',
column: 'name',
fieldType: 'text',
unique: true,
required: true,
importable: true,
exportable: true,
order: 1,
},
description: {
name: 'account.field.description',
column: 'description',
fieldType: 'text',
importable: true,
exportable: true,
},
slug: {
name: 'account.field.slug',
@@ -23,13 +31,19 @@ export default {
fieldType: 'text',
columnable: false,
filterable: false,
importable: false,
},
code: {
name: 'account.field.code',
column: 'code',
fieldType: 'text',
exportable: true,
importable: true,
minLength: 3,
maxLength: 6,
importHint: 'Unique number to identify the account.',
},
root_type: {
rootType: {
name: 'account.field.root_type',
fieldType: 'enumeration',
options: [
@@ -41,6 +55,7 @@ export default {
],
filterCustomQuery: RootTypeFieldFilterQuery,
sortable: false,
importable: false,
},
normal: {
name: 'account.field.normal',
@@ -51,37 +66,56 @@ export default {
],
filterCustomQuery: NormalTypeFieldFilterQuery,
sortable: false,
importable: false,
},
type: {
accountType: {
name: 'account.field.type',
column: 'account_type',
fieldType: 'enumeration',
options: ACCOUNT_TYPES.map((accountType) => ({
label: accountType.label,
key: accountType.key
key: accountType.key,
})),
required: true,
importable: true,
exportable: true,
order: 2,
},
active: {
name: 'account.field.active',
column: 'active',
fieldType: 'boolean',
filterable: false,
exportable: true,
importable: true,
},
balance: {
name: 'account.field.balance',
column: 'amount',
fieldType: 'number',
importable: false,
},
currency: {
currencyCode: {
name: 'account.field.currency',
column: 'currency_code',
fieldType: 'text',
filterable: false,
importable: true,
exportable: true,
},
created_at: {
parentAccount: {
name: 'account.field.parent_account',
column: 'parent_account_id',
fieldType: 'relation',
to: { model: 'Account', to: 'id' },
importable: false,
},
createdAt: {
name: 'account.field.created_at',
column: 'created_at',
fieldType: 'date',
importable: false,
exportable: true,
},
},
};

View File

@@ -196,6 +196,7 @@ export default class Account extends mixin(TenantModel, [
const Expense = require('models/Expense');
const ExpenseEntry = require('models/ExpenseCategory');
const ItemEntry = require('models/ItemEntry');
const UncategorizedTransaction = require('models/UncategorizedCashflowTransaction');
return {
/**
@@ -305,6 +306,21 @@ export default class Account extends mixin(TenantModel, [
to: 'items_entries.sellAccountId',
},
},
/**
* Associated uncategorized transactions.
*/
uncategorizedTransactions: {
relation: Model.HasManyRelation,
modelClass: UncategorizedTransaction.default,
join: {
from: 'accounts.id',
to: 'uncategorized_cashflow_transactions.accountId',
},
filter: (query) => {
query.where('categorized', false);
},
},
};
}

View File

@@ -12,6 +12,8 @@ export default class CashflowTransaction extends TenantModel {
transactionType: string;
amount: number;
exchangeRate: number;
uncategorize: boolean;
uncategorizedTransaction!: boolean;
/**
* Table name.
@@ -85,6 +87,14 @@ export default class CashflowTransaction extends TenantModel {
return this.typeMeta?.direction === CASHFLOW_DIRECTION.IN;
}
/**
* Detarmines whether the transaction imported from uncategorized transaction.
* @returns {boolean}
*/
get isCategroizedTranasction() {
return !!this.uncategorizedTransaction;
}
/**
* Relationship mapping.
*/

View File

@@ -1,70 +1,112 @@
export default {
importable: true,
defaultFilterField: 'displayName',
defaultSort: {
sortOrder: 'DESC',
sortField: 'createdAt',
},
fields: {
first_name: {
customerType: {
name: 'Customer Type',
column: 'contact_type',
fieldType: 'enumeration',
options: [
{ key: 'business', label: 'Business' },
{ key: 'individual', label: 'Individual' },
],
importable: true,
required: true,
},
firstName: {
name: 'customer.field.first_name',
column: 'first_name',
fieldType: 'text',
importable: true,
},
last_name: {
lastName: {
name: 'customer.field.last_name',
column: 'last_name',
fieldType: 'text',
importable: true,
},
display_name: {
displayName: {
name: 'customer.field.display_name',
column: 'display_name',
fieldType: 'text',
required: true,
importable: true,
},
email: {
name: 'customer.field.email',
column: 'email',
fieldType: 'text',
importable: true,
},
work_phone: {
workPhone: {
name: 'customer.field.work_phone',
column: 'work_phone',
fieldType: 'text',
importable: true,
},
personal_phone: {
personalPhone: {
name: 'customer.field.personal_phone',
column: 'personal_phone',
fieldType: 'text',
importable: true,
},
company_name: {
companyName: {
name: 'customer.field.company_name',
column: 'company_name',
fieldType: 'text',
importable: true,
},
website: {
name: 'customer.field.website',
column: 'website',
fieldType: 'text',
},
created_at: {
name: 'customer.field.created_at',
column: 'created_at',
fieldType: 'date',
fieldType: 'url',
importable: true,
},
balance: {
name: 'customer.field.balance',
column: 'balance',
fieldType: 'number',
},
opening_balance: {
openingBalance: {
name: 'customer.field.opening_balance',
column: 'opening_balance',
fieldType: 'number',
importable: true,
},
opening_balance_at: {
openingBalanceAt: {
name: 'customer.field.opening_balance_at',
column: 'opening_balance_at',
filterable: false,
fieldType: 'date',
importable: true,
},
currency_code: {
openingBalanceExchangeRate: {
name: 'Opening Balance Ex. Rate',
column: 'opening_balance_exchange_rate',
fieldType: 'number',
importable: true,
},
currencyCode: {
name: 'customer.field.currency',
column: 'currency_code',
fieldType: 'text',
importable: true,
},
note: {
name: 'Note',
column: 'note',
fieldType: 'text',
importable: true,
},
active: {
name: 'Active',
column: 'active',
fieldType: 'boolean',
importable: true,
},
status: {
name: 'customer.field.status',
@@ -77,6 +119,98 @@ export default {
],
filterCustomQuery: statusFieldFilterQuery,
},
// Billing Address
billingAddress1: {
name: 'Billing Address 1',
column: 'billing_address1',
fieldType: 'text',
importable: true,
},
billingAddress2: {
name: 'Billing Address 2',
column: 'billing_address2',
fieldType: 'text',
importable: true,
},
billingAddressCity: {
name: 'Billing Address City',
column: 'billing_address_city',
fieldType: 'text',
importable: true,
},
billingAddressCountry: {
name: 'Billing Address Country',
column: 'billing_address_country',
fieldType: 'text',
importable: true,
},
billingAddressPostcode: {
name: 'Billing Address Postcode',
column: 'billing_address_postcode',
fieldType: 'text',
importable: true,
},
billingAddressState: {
name: 'Billing Address State',
column: 'billing_address_state',
fieldType: 'text',
importable: true,
},
billingAddressPhone: {
name: 'Billing Address Phone',
column: 'billing_address_phone',
fieldType: 'text',
importable: true,
},
// Shipping Address
shippingAddress1: {
name: 'Shipping Address 1',
column: 'shipping_address1',
fieldType: 'text',
importable: true,
},
shippingAddress2: {
name: 'Shipping Address 2',
column: 'shipping_address2',
fieldType: 'text',
importable: true,
},
shippingAddressCity: {
name: 'Shipping Address City',
column: 'shipping_address_city',
fieldType: 'text',
importable: true,
},
shippingAddressCountry: {
name: 'Shipping Address Country',
column: 'shipping_address_country',
fieldType: 'text',
importable: true,
},
shippingAddressPostcode: {
name: 'Shipping Address Postcode',
column: 'shipping_address_postcode',
fieldType: 'text',
importable: true,
},
shippingAddressPhone: {
name: 'Shipping Address Phone',
column: 'shipping_address_phone',
fieldType: 'text',
importable: true,
},
shippingAddressState: {
name: 'Shipping Address State',
column: 'shipping_address_state',
fieldType: 'text',
importable: true,
},
//
createdAt: {
name: 'customer.field.created_at',
column: 'created_at',
fieldType: 'date',
},
},
};

View File

@@ -215,6 +215,10 @@ export default class Expense extends mixin(TenantModel, [
to: 'branches.id',
},
},
/**
*
*/
media: {
relation: Model.ManyToManyRelation,
modelClass: Media.default,

View File

@@ -0,0 +1,69 @@
import TenantModel from 'models/TenantModel';
export default class Import extends TenantModel {
resource!: string;
mapping!: string;
columns!: string;
params!: Record<string, any>;
/**
* Table name.
*/
static get tableName() {
return 'imports';
}
/**
* Virtual attributes.
*/
static get virtualAttributes() {
return ['mappingParsed'];
}
/**
* Timestamps columns.
*/
get timestamps() {
return [];
}
/**
* Relationship mapping.
*/
static get relationMappings() {
return {};
}
/**
* Detarmines whether the import is mapped.
* @returns {boolean}
*/
public get isMapped() {
return Boolean(this.mapping);
}
public get columnsParsed() {
try {
return JSON.parse(this.columns);
} catch {
return [];
}
}
public get paramsParsed() {
try {
return JSON.parse(this.params);
} catch {
return [];
}
}
public get mappingParsed() {
try {
return JSON.parse(this.mapping);
} catch {
return [];
}
}
}

View File

@@ -1,51 +1,59 @@
export default {
importable: true,
defaultFilterField: 'name',
defaultSort: {
sortField: 'name',
sortOrder: 'DESC',
},
fields: {
'type': {
type: {
name: 'item.field.type',
column: 'type',
fieldType: 'enumeration',
options: [
{ key: 'inventory', label: 'item.field.type.inventory', },
{ key: 'inventory', label: 'item.field.type.inventory' },
{ key: 'service', label: 'item.field.type.service' },
{ key: 'non-inventory', label: 'item.field.type.non-inventory', },
{ key: 'non-inventory', label: 'item.field.type.non-inventory' },
],
importable: true,
},
'name': {
name: {
name: 'item.field.name',
column: 'name',
fieldType: 'text',
importable: true,
},
'code': {
code: {
name: 'item.field.code',
column: 'code',
fieldType: 'text',
importable: true,
},
'sellable': {
sellable: {
name: 'item.field.sellable',
column: 'sellable',
fieldType: 'boolean',
importable: true,
},
'purchasable': {
purchasable: {
name: 'item.field.purchasable',
column: 'purchasable',
fieldType: 'boolean',
importable: true,
},
'sell_price': {
sellPrice: {
name: 'item.field.cost_price',
column: 'sell_price',
fieldType: 'number',
importable: true,
},
'cost_price': {
costPrice: {
name: 'item.field.cost_account',
column: 'cost_price',
fieldType: 'number',
importable: true,
},
'cost_account': {
costAccount: {
name: 'item.field.sell_account',
column: 'cost_account_id',
fieldType: 'relation',
@@ -55,8 +63,10 @@ export default {
relationEntityLabel: 'name',
relationEntityKey: 'slug',
importable: true,
},
'sell_account': {
sellAccount: {
name: 'item.field.sell_description',
column: 'sell_account_id',
fieldType: 'relation',
@@ -66,8 +76,10 @@ export default {
relationEntityLabel: 'name',
relationEntityKey: 'slug',
importable: true,
},
'inventory_account': {
inventoryAccount: {
name: 'item.field.inventory_account',
column: 'inventory_account_id',
@@ -76,28 +88,34 @@ export default {
relationEntityLabel: 'name',
relationEntityKey: 'slug',
importable: true,
},
'sell_description': {
sellDescription: {
name: 'Sell description',
column: 'sell_description',
fieldType: 'text',
importable: true,
},
'purchase_description': {
purchaseDescription: {
name: 'Purchase description',
column: 'purchase_description',
fieldType: 'text',
importable: true,
},
'quantity_on_hand': {
quantityOnHand: {
name: 'item.field.quantity_on_hand',
column: 'quantity_on_hand',
fieldType: 'number',
importable: true,
},
'note': {
note: {
name: 'item.field.note',
column: 'note',
fieldType: 'text',
importable: true,
},
'category': {
category: {
name: 'item.field.category',
column: 'category_id',
@@ -106,14 +124,15 @@ export default {
relationEntityLabel: 'name',
relationEntityKey: 'id',
importable: true,
},
'active': {
active: {
name: 'item.field.active',
column: 'active',
fieldType: 'boolean',
filterable: false,
importable: true,
},
'created_at': {
createdAt: {
name: 'item.field.created_at',
column: 'created_at',
columnType: 'date',

View File

@@ -0,0 +1,54 @@
export default {
defaultFilterField: 'createdAt',
defaultSort: {
sortOrder: 'DESC',
sortField: 'createdAt',
},
importable: true,
fields: {
date: {
name: 'Date',
column: 'date',
fieldType: 'date',
importable: true,
required: true,
},
payee: {
name: 'Payee',
column: 'payee',
fieldType: 'text',
importable: true,
},
description: {
name: 'Description',
column: 'description',
fieldType: 'text',
importable: true,
},
referenceNo: {
name: 'Reference No.',
column: 'reference_no',
fieldType: 'text',
importable: true,
},
amount: {
name: 'Amount',
column: 'Amount',
fieldType: 'numeric',
required: true,
importable: true,
},
account: {
name: 'Account',
column: 'account_id',
fieldType: 'relation',
to: { model: 'Account', to: 'id' },
},
createdAt: {
name: 'Created At',
column: 'createdAt',
fieldType: 'date',
importable: false,
},
},
};

View File

@@ -0,0 +1,150 @@
/* eslint-disable global-require */
import * as R from 'ramda';
import { Model, ModelOptions, QueryContext, mixin } from 'objection';
import TenantModel from 'models/TenantModel';
import ModelSettings from './ModelSetting';
import Account from './Account';
import UncategorizedCashflowTransactionMeta from './UncategorizedCashflowTransaction.meta';
export default class UncategorizedCashflowTransaction extends mixin(
TenantModel,
[ModelSettings]
) {
id!: number;
amount!: number;
categorized!: boolean;
accountId!: number;
/**
* Table name.
*/
static get tableName() {
return 'uncategorized_cashflow_transactions';
}
/**
* Timestamps columns.
*/
static get timestamps() {
return ['createdAt', 'updatedAt'];
}
/**
* Virtual attributes.
*/
static get virtualAttributes() {
return [
'withdrawal',
'deposit',
'isDepositTransaction',
'isWithdrawalTransaction',
];
}
static get meta() {
return UncategorizedCashflowTransactionMeta;
}
/**
* Retrieves the withdrawal amount.
* @returns {number}
*/
public get withdrawal() {
return this.amount < 0 ? Math.abs(this.amount) : 0;
}
/**
* Retrieves the deposit amount.
* @returns {number}
*/
public get deposit(): number {
return this.amount > 0 ? Math.abs(this.amount) : 0;
}
/**
* Detarmines whether the transaction is deposit transaction.
*/
public get isDepositTransaction(): boolean {
return 0 < this.deposit;
}
/**
* Detarmines whether the transaction is withdrawal transaction.
*/
public get isWithdrawalTransaction(): boolean {
return 0 < this.withdrawal;
}
/**
* Relationship mapping.
*/
static get relationMappings() {
const Account = require('models/Account');
return {
/**
* Transaction may has associated to account.
*/
account: {
relation: Model.BelongsToOneRelation,
modelClass: Account.default,
join: {
from: 'uncategorized_cashflow_transactions.accountId',
to: 'accounts.id',
},
},
};
}
/**
* Updates the count of uncategorized transactions for the associated account
* based on the specified operation.
* @param {QueryContext} queryContext - The query context for the transaction.
* @param {boolean} increment - Indicates whether to increment or decrement the count.
*/
private async updateUncategorizedTransactionCount(
queryContext: QueryContext,
increment: boolean
) {
const operation = increment ? 'increment' : 'decrement';
const amount = increment ? 1 : -1;
await Account.query(queryContext.transaction)
.findById(this.accountId)
[operation]('uncategorized_transactions', amount);
}
/**
* Runs after insert.
* @param {QueryContext} queryContext
*/
public async $afterInsert(queryContext) {
await super.$afterInsert(queryContext);
await this.updateUncategorizedTransactionCount(queryContext, true);
}
/**
* Runs after update.
* @param {ModelOptions} opt
* @param {QueryContext} queryContext
*/
public async $afterUpdate(
opt: ModelOptions,
queryContext: QueryContext
): Promise<any> {
await super.$afterUpdate(opt, queryContext);
if (this.id && this.categorized) {
await this.updateUncategorizedTransactionCount(queryContext, false);
}
}
/**
* Runs after delete.
* @param {QueryContext} queryContext
*/
public async $afterDelete(queryContext: QueryContext) {
await super.$afterDelete(queryContext);
await this.updateUncategorizedTransactionCount(queryContext, false);
}
}

View File

@@ -1,74 +1,100 @@
export default {
defaultFilterField: 'display_name',
defaultFilterField: 'displayName',
defaultSort: {
sortOrder: 'DESC',
sortField: 'created_at',
sortField: 'createdAt',
},
importable: true,
fields: {
first_name: {
firstName: {
name: 'vendor.field.first_name',
column: 'first_name',
fieldType: 'text',
importable: true,
},
last_name: {
lastName: {
name: 'vendor.field.last_name',
column: 'last_name',
fieldType: 'text',
importable: true,
},
display_name: {
displayName: {
name: 'vendor.field.display_name',
column: 'display_name',
fieldType: 'text',
required: true,
importable: true,
},
email: {
name: 'vendor.field.email',
column: 'email',
fieldType: 'text',
importable: true,
},
work_phone: {
workPhone: {
name: 'vendor.field.work_phone',
column: 'work_phone',
fieldType: 'text',
importable: true,
},
personal_phone: {
name: 'vendor.field.personal_pone',
personalPhone: {
name: 'vendor.field.personal_phone',
column: 'personal_phone',
fieldType: 'text',
importable: true,
},
company_name: {
companyName: {
name: 'vendor.field.company_name',
column: 'company_name',
fieldType: 'text',
importable: true,
},
website: {
name: 'vendor.field.website',
column: 'website',
fieldType: 'text',
},
created_at: {
name: 'vendor.field.created_at',
column: 'created_at',
fieldType: 'date',
importable: true,
},
balance: {
name: 'vendor.field.balance',
column: 'balance',
fieldType: 'number',
},
opening_balance: {
openingBalance: {
name: 'vendor.field.opening_balance',
column: 'opening_balance',
fieldType: 'number',
importable: true,
},
opening_balance_at: {
openingBalanceAt: {
name: 'vendor.field.opening_balance_at',
column: 'opening_balance_at',
fieldType: 'date',
importable: true,
},
currency_code: {
openingBalanceExchangeRate: {
name: 'Opening Balance Ex. Rate',
column: 'opening_balance_exchange_rate',
fieldType: 'number',
importable: true,
},
currencyCode: {
name: 'vendor.field.currency',
column: 'currency_code',
fieldType: 'text',
importable: true,
},
note: {
name: 'Note',
column: 'note',
fieldType: 'text',
importable: true,
},
active: {
name: 'Active',
column: 'active',
fieldType: 'boolean',
importable: true,
},
status: {
name: 'vendor.field.status',
@@ -88,5 +114,96 @@ export default {
}
},
},
// Billing Address
billingAddress1: {
name: 'Billing Address 1',
column: 'billing_address1',
fieldType: 'text',
importable: true,
},
billingAddress2: {
name: 'Billing Address 2',
column: 'billing_address2',
fieldType: 'text',
importable: true,
},
billingAddressCity: {
name: 'Billing Address City',
column: 'billing_address_city',
fieldType: 'text',
importable: true,
},
billingAddressCountry: {
name: 'Billing Address Country',
column: 'billing_address_country',
fieldType: 'text',
importable: true,
},
billingAddressPostcode: {
name: 'Billing Address Postcode',
column: 'billing_address_postcode',
fieldType: 'text',
importable: true,
},
billingAddressState: {
name: 'Billing Address State',
column: 'billing_address_state',
fieldType: 'text',
importable: true,
},
billingAddressPhone: {
name: 'Billing Address Phone',
column: 'billing_address_phone',
fieldType: 'text',
importable: true,
},
// Shipping Address
shippingAddress1: {
name: 'Shipping Address 1',
column: 'shipping_address1',
fieldType: 'text',
importable: true,
},
shippingAddress2: {
name: 'Shipping Address 2',
column: 'shipping_address2',
fieldType: 'text',
importable: true,
},
shippingAddressCity: {
name: 'Shipping Address City',
column: 'shipping_address_city',
fieldType: 'text',
importable: true,
},
shippingAddressCountry: {
name: 'Shipping Address Country',
column: 'shipping_address_country',
fieldType: 'text',
importable: true,
},
shippingAddressPostcode: {
name: 'Shipping Address Postcode',
column: 'shipping_address_postcode',
fieldType: 'text',
importable: true,
},
shippingAddressState: {
name: 'Shipping Address State',
column: 'shipping_address_state',
fieldType: 'text',
importable: true,
},
shippingAddressPhone: {
name: 'Shipping Address Phone',
column: 'shipping_address_phone',
fieldType: 'text',
importable: true,
},
createdAt: {
name: 'vendor.field.created_at',
column: 'created_at',
fieldType: 'date',
},
},
};

View File

@@ -16,6 +16,7 @@ import { ActivateAccount } from './ActivateAccount';
import { GetAccounts } from './GetAccounts';
import { GetAccount } from './GetAccount';
import { GetAccountTransactions } from './GetAccountTransactions';
import { Knex } from 'knex';
@Service()
export class AccountsApplication {
@@ -48,9 +49,10 @@ export class AccountsApplication {
*/
public createAccount = (
tenantId: number,
accountDTO: IAccountCreateDTO
accountDTO: IAccountCreateDTO,
trx?: Knex.Transaction
): Promise<IAccount> => {
return this.createAccountService.createAccount(tenantId, accountDTO);
return this.createAccountService.createAccount(tenantId, accountDTO, trx);
};
/**

View File

@@ -0,0 +1,50 @@
export const AccountsSampleData = [
{
'Account Name': 'Utilities Expense',
'Account Code': 9000,
Type: 'Expense',
Description: 'Omnis voluptatum consequatur.',
Active: 'T',
'Currency Code': '',
},
{
'Account Name': 'Unearned Revenue',
'Account Code': 9010,
Type: 'Long Term Liability',
Description: 'Autem odit voluptas nihil unde.',
Active: 'T',
'Currency Code': '',
},
{
'Account Name': 'Long-Term Debt',
'Account Code': 9020,
Type: 'Long Term Liability',
Description: 'In voluptas cumque exercitationem.',
Active: 'T',
'Currency Code': '',
},
{
'Account Name': 'Salaries and Wages Expense',
'Account Code': 9030,
Type: 'Expense',
Description: 'Assumenda aspernatur soluta aliquid perspiciatis quasi.',
Active: 'T',
'Currency Code': '',
},
{
'Account Name': 'Rental Income',
'Account Code': 9040,
Type: 'Income',
Description: 'Omnis possimus amet occaecati inventore.',
Active: 'T',
'Currency Code': '',
},
{
'Account Name': 'Paypal',
'Account Code': 9050,
Type: 'Bank',
Description: 'In voluptas cumque exercitationem.',
Active: 'T',
'Currency Code': '',
},
];

View File

@@ -0,0 +1,45 @@
import { Inject, Service } from 'typedi';
import { Knex } from 'knex';
import { IAccountCreateDTO } from '@/interfaces';
import { CreateAccount } from './CreateAccount';
import { Importable } from '../Import/Importable';
import { AccountsSampleData } from './AccountsImportable.SampleData';
@Service()
export class AccountsImportable extends Importable {
@Inject()
private createAccountService: CreateAccount;
/**
* Importing to account service.
* @param {number} tenantId
* @param {IAccountCreateDTO} createAccountDTO
* @returns
*/
public importable(
tenantId: number,
createAccountDTO: IAccountCreateDTO,
trx?: Knex.Transaction
) {
return this.createAccountService.createAccount(
tenantId,
createAccountDTO,
trx
);
}
/**
* Concurrrency controlling of the importing process.
* @returns {number}
*/
public get concurrency() {
return 1;
}
/**
* Retrieves the sample data that used to download accounts sample sheet.
*/
public sampleData(): any[] {
return AccountsSampleData;
}
}

View File

@@ -97,13 +97,14 @@ export class CreateAccount {
/**
* Creates a new account on the storage.
* @param {number} tenantId
* @param {IAccountCreateDTO} accountDTO
* @param {number} tenantId
* @param {IAccountCreateDTO} accountDTO
* @returns {Promise<IAccount>}
*/
public createAccount = async (
tenantId: number,
accountDTO: IAccountCreateDTO
accountDTO: IAccountCreateDTO,
trx?: Knex.Transaction
): Promise<IAccount> => {
const { Account } = this.tenancy.models(tenantId);
@@ -119,27 +120,31 @@ export class CreateAccount {
tenantMeta.baseCurrency
);
// Creates a new account with associated transactions under unit-of-work envirement.
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Triggers `onAccountCreating` event.
await this.eventPublisher.emitAsync(events.accounts.onCreating, {
tenantId,
accountDTO,
trx,
} as IAccountEventCreatingPayload);
return this.uow.withTransaction(
tenantId,
async (trx: Knex.Transaction) => {
// Triggers `onAccountCreating` event.
await this.eventPublisher.emitAsync(events.accounts.onCreating, {
tenantId,
accountDTO,
trx,
} as IAccountEventCreatingPayload);
// Inserts account to the storage.
const account = await Account.query(trx).insertAndFetch({
...accountInputModel,
});
// Triggers `onAccountCreated` event.
await this.eventPublisher.emitAsync(events.accounts.onCreated, {
tenantId,
account,
accountId: account.id,
trx,
} as IAccountEventCreatedPayload);
// Inserts account to the storage.
const account = await Account.query(trx).insertAndFetch({
...accountInputModel,
});
// Triggers `onAccountCreated` event.
await this.eventPublisher.emitAsync(events.accounts.onCreated, {
tenantId,
account,
accountId: account.id,
trx,
} as IAccountEventCreatedPayload);
return account;
});
return account;
},
trx
);
};
}

View File

@@ -8,9 +8,9 @@ import {
transformPlaidAccountToCreateAccount,
transformPlaidTrxsToCashflowCreate,
} from './utils';
import NewCashflowTransactionService from '@/services/Cashflow/NewCashflowTransactionService';
import DeleteCashflowTransactionService from '@/services/Cashflow/DeleteCashflowTransactionService';
import { DeleteCashflowTransaction } from '@/services/Cashflow/DeleteCashflowTransactionService';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { CashflowApplication } from '@/services/Cashflow/CashflowApplication';
const CONCURRENCY_ASYNC = 10;
@@ -23,10 +23,10 @@ export class PlaidSyncDb {
private createAccountService: CreateAccount;
@Inject()
private createCashflowTransactionService: NewCashflowTransactionService;
private cashflowApp: CashflowApplication;
@Inject()
private deleteCashflowTransactionService: DeleteCashflowTransactionService;
private deleteCashflowTransactionService: DeleteCashflowTransaction;
/**
* Syncs the plaid accounts to the system accounts.
@@ -36,11 +36,14 @@ export class PlaidSyncDb {
*/
public async syncBankAccounts(
tenantId: number,
plaidAccounts: PlaidAccount[]
plaidAccounts: PlaidAccount[],
institution: any
): Promise<void> {
const accountCreateDTOs = R.map(transformPlaidAccountToCreateAccount)(
plaidAccounts
);
const transformToPlaidAccounts =
transformPlaidAccountToCreateAccount(institution);
const accountCreateDTOs = R.map(transformToPlaidAccounts)(plaidAccounts);
await bluebird.map(
accountCreateDTOs,
(createAccountDTO: any) =>
@@ -75,17 +78,18 @@ export class PlaidSyncDb {
cashflowAccount.id,
openingEquityBalance.id
);
const accountsCashflowDTO = R.map(transformTransaction)(plaidTranasctions);
const uncategorizedTransDTOs =
R.map(transformTransaction)(plaidTranasctions);
// Creating account transaction queue.
await bluebird.map(
accountsCashflowDTO,
(cashflowDTO) =>
this.createCashflowTransactionService.newCashflowTransaction(
uncategorizedTransDTOs,
(uncategoriedDTO) =>
this.cashflowApp.createUncategorizedTransaction(
tenantId,
cashflowDTO
uncategoriedDTO
),
{ concurrency: CONCURRENCY_ASYNC }
{ concurrency: 1 }
);
}
@@ -157,4 +161,38 @@ export class PlaidSyncDb {
await PlaidItem.query().findOne({ plaidItemId }).patch({ lastCursor });
}
/**
* Updates the last feeds updated at of the given Plaid accounts ids.
* @param {number} tenantId
* @param {string[]} plaidAccountIds
*/
public async updateLastFeedsUpdatedAt(
tenantId: number,
plaidAccountIds: string[]
) {
const { Account } = this.tenancy.models(tenantId);
await Account.query().whereIn('plaid_account_id', plaidAccountIds).patch({
lastFeedsUpdatedAt: new Date(),
});
}
/**
* Updates the accounts feed active status of the given Plaid accounts ids.
* @param {number} tenantId
* @param {number[]} plaidAccountIds
* @param {boolean} isFeedsActive
*/
public async updateAccountsFeedsActive(
tenantId: number,
plaidAccountIds: string[],
isFeedsActive: boolean = true
) {
const { Account } = this.tenancy.models(tenantId);
await Account.query().whereIn('plaid_account_id', plaidAccountIds).patch({
isFeedsActive,
});
}
}

View File

@@ -25,11 +25,19 @@ export class PlaidUpdateTransactions {
const request = { access_token: accessToken };
const plaidInstance = new PlaidClientWrapper();
const {
data: { accounts },
data: { accounts, item },
} = await plaidInstance.accountsGet(request);
const plaidAccountsIds = accounts.map((a) => a.account_id);
const {
data: { institution },
} = await plaidInstance.institutionsGetById({
institution_id: item.institution_id,
country_codes: ['US', 'UK'],
});
// Update the DB.
await this.plaidSync.syncBankAccounts(tenantId, accounts);
await this.plaidSync.syncBankAccounts(tenantId, accounts, institution);
await this.plaidSync.syncAccountsTransactions(
tenantId,
added.concat(modified)
@@ -37,6 +45,12 @@ export class PlaidUpdateTransactions {
await this.plaidSync.syncRemoveTransactions(tenantId, removed);
await this.plaidSync.syncTransactionsCursor(tenantId, plaidItemId, cursor);
// Update the last feeds updated at of the updated accounts.
await this.plaidSync.updateLastFeedsUpdatedAt(tenantId, plaidAccountsIds);
// Turn on the accounts feeds flag.
await this.plaidSync.updateAccountsFeedsActive(tenantId, plaidAccountsIds);
return {
addedCount: added.length,
modifiedCount: modified.length,

View File

@@ -1,7 +1,7 @@
import * as R from 'ramda';
import {
CreateUncategorizedTransactionDTO,
IAccountCreateDTO,
ICashflowNewCommandDTO,
PlaidAccount,
PlaidTransaction,
} from '@/interfaces';
@@ -11,51 +11,44 @@ import {
* @param {PlaidAccount} plaidAccount
* @returns {IAccountCreateDTO}
*/
export const transformPlaidAccountToCreateAccount = (
plaidAccount: PlaidAccount
): IAccountCreateDTO => {
return {
name: plaidAccount.name,
code: '',
description: plaidAccount.official_name,
currencyCode: plaidAccount.balances.iso_currency_code,
accountType: 'cash',
active: true,
plaidAccountId: plaidAccount.account_id,
bankBalance: plaidAccount.balances.current,
accountMask: plaidAccount.mask,
};
};
export const transformPlaidAccountToCreateAccount = R.curry(
(institution: any, plaidAccount: PlaidAccount): IAccountCreateDTO => {
return {
name: `${institution.name} - ${plaidAccount.name}`,
code: '',
description: plaidAccount.official_name,
currencyCode: plaidAccount.balances.iso_currency_code,
accountType: 'cash',
active: true,
plaidAccountId: plaidAccount.account_id,
bankBalance: plaidAccount.balances.current,
accountMask: plaidAccount.mask,
};
}
);
/**
* Transformes the plaid transaction to cashflow create DTO.
* @param {number} cashflowAccountId - Cashflow account ID.
* @param {number} creditAccountId - Credit account ID.
* @param {PlaidTransaction} plaidTranasction - Plaid transaction.
* @returns {ICashflowNewCommandDTO}
* @returns {CreateUncategorizedTransactionDTO}
*/
export const transformPlaidTrxsToCashflowCreate = R.curry(
(
cashflowAccountId: number,
creditAccountId: number,
plaidTranasction: PlaidTransaction
): ICashflowNewCommandDTO => {
): CreateUncategorizedTransactionDTO => {
return {
date: plaidTranasction.date,
transactionType: 'OwnerContribution',
description: plaidTranasction.name,
amount: plaidTranasction.amount,
exchangeRate: 1,
description: plaidTranasction.name,
payee: plaidTranasction.payment_meta?.payee,
currencyCode: plaidTranasction.iso_currency_code,
creditAccountId,
cashflowAccountId,
// transactionNumber: string;
// referenceNo: string;
accountId: cashflowAccountId,
referenceNo: plaidTranasction.payment_meta?.reference_number,
plaidTransactionId: plaidTranasction.transaction_id,
publish: true,
};
}
);

View File

@@ -0,0 +1,213 @@
import { Inject, Service } from 'typedi';
import { DeleteCashflowTransaction } from './DeleteCashflowTransactionService';
import { UncategorizeCashflowTransaction } from './UncategorizeCashflowTransaction';
import { CategorizeCashflowTransaction } from './CategorizeCashflowTransaction';
import {
CategorizeTransactionAsExpenseDTO,
CreateUncategorizedTransactionDTO,
ICashflowAccountsFilter,
ICashflowNewCommandDTO,
ICategorizeCashflowTransactioDTO,
IGetUncategorizedTransactionsQuery,
} from '@/interfaces';
import { CategorizeTransactionAsExpense } from './CategorizeTransactionAsExpense';
import { GetUncategorizedTransactions } from './GetUncategorizedTransactions';
import { CreateUncategorizedTransaction } from './CreateUncategorizedTransaction';
import { GetUncategorizedTransaction } from './GetUncategorizedTransaction';
import NewCashflowTransactionService from './NewCashflowTransactionService';
import GetCashflowAccountsService from './GetCashflowAccountsService';
import { GetCashflowTransactionService } from './GetCashflowTransactionsService';
@Service()
export class CashflowApplication {
@Inject()
private createTransactionService: NewCashflowTransactionService;
@Inject()
private deleteTransactionService: DeleteCashflowTransaction;
@Inject()
private getCashflowAccountsService: GetCashflowAccountsService;
@Inject()
private getCashflowTransactionService: GetCashflowTransactionService;
@Inject()
private uncategorizeTransactionService: UncategorizeCashflowTransaction;
@Inject()
private categorizeTransactionService: CategorizeCashflowTransaction;
@Inject()
private categorizeAsExpenseService: CategorizeTransactionAsExpense;
@Inject()
private getUncategorizedTransactionsService: GetUncategorizedTransactions;
@Inject()
private getUncategorizedTransactionService: GetUncategorizedTransaction;
@Inject()
private createUncategorizedTransactionService: CreateUncategorizedTransaction;
/**
* Creates a new cashflow transaction.
* @param {number} tenantId
* @param {ICashflowNewCommandDTO} transactionDTO
* @param {number} userId
* @returns
*/
public createTransaction(
tenantId: number,
transactionDTO: ICashflowNewCommandDTO,
userId?: number
) {
return this.createTransactionService.newCashflowTransaction(
tenantId,
transactionDTO,
userId
);
}
/**
* Deletes the given cashflow transaction.
* @param {number} tenantId
* @param {number} cashflowTransactionId
* @returns
*/
public deleteTransaction(tenantId: number, cashflowTransactionId: number) {
return this.deleteTransactionService.deleteCashflowTransaction(
tenantId,
cashflowTransactionId
);
}
/**
* Retrieves specific cashflow transaction.
* @param {number} tenantId
* @param {number} cashflowTransactionId
* @returns
*/
public getTransaction(tenantId: number, cashflowTransactionId: number) {
return this.getCashflowTransactionService.getCashflowTransaction(
tenantId,
cashflowTransactionId
);
}
/**
* Retrieves the cashflow accounts.
* @param {number} tenantId
* @param {ICashflowAccountsFilter} filterDTO
* @returns
*/
public getCashflowAccounts(
tenantId: number,
filterDTO: ICashflowAccountsFilter
) {
return this.getCashflowAccountsService.getCashflowAccounts(
tenantId,
filterDTO
);
}
/**
* Creates a new uncategorized cash transaction.
* @param {number} tenantId
* @param {CreateUncategorizedTransactionDTO} createUncategorizedTransactionDTO
* @returns {IUncategorizedCashflowTransaction}
*/
public createUncategorizedTransaction(
tenantId: number,
createUncategorizedTransactionDTO: CreateUncategorizedTransactionDTO
) {
return this.createUncategorizedTransactionService.create(
tenantId,
createUncategorizedTransactionDTO
);
}
/**
* Uncategorize the given cashflow transaction.
* @param {number} tenantId
* @param {number} cashflowTransactionId
* @returns
*/
public uncategorizeTransaction(
tenantId: number,
cashflowTransactionId: number
) {
return this.uncategorizeTransactionService.uncategorize(
tenantId,
cashflowTransactionId
);
}
/**
* Categorize the given cashflow transaction.
* @param {number} tenantId
* @param {number} cashflowTransactionId
* @param {ICategorizeCashflowTransactioDTO} categorizeDTO
* @returns
*/
public categorizeTransaction(
tenantId: number,
cashflowTransactionId: number,
categorizeDTO: ICategorizeCashflowTransactioDTO
) {
return this.categorizeTransactionService.categorize(
tenantId,
cashflowTransactionId,
categorizeDTO
);
}
/**
* Categorizes the given cashflow transaction as expense transaction.
* @param {number} tenantId
* @param {number} cashflowTransactionId
* @param {CategorizeTransactionAsExpenseDTO} transactionDTO
*/
public categorizeAsExpense(
tenantId: number,
cashflowTransactionId: number,
transactionDTO: CategorizeTransactionAsExpenseDTO
) {
return this.categorizeAsExpenseService.categorize(
tenantId,
cashflowTransactionId,
transactionDTO
);
}
/**
* Retrieves the uncategorized cashflow transactions.
* @param {number} tenantId
*/
public getUncategorizedTransactions(
tenantId: number,
accountId: number,
query: IGetUncategorizedTransactionsQuery
) {
return this.getUncategorizedTransactionsService.getTransactions(
tenantId,
accountId,
query
);
}
/**
* Retrieves specific uncategorized transaction.
* @param {number} tenantId
* @param {number} uncategorizedTransactionId
*/
public getUncategorizedTransaction(
tenantId: number,
uncategorizedTransactionId: number
) {
return this.getUncategorizedTransactionService.getTransaction(
tenantId,
uncategorizedTransactionId
);
}
}

View File

@@ -1,11 +1,9 @@
import { Inject, Service } from 'typedi';
import { Knex } from 'knex';
import * as R from 'ramda';
import {
ILedgerEntry,
ICashflowTransaction,
AccountNormal,
ICashflowTransactionLine,
} from '../../interfaces';
import {
transformCashflowTransactionType,

View File

@@ -0,0 +1,101 @@
import { Inject, Service } from 'typedi';
import HasTenancyService from '../Tenancy/TenancyService';
import events from '@/subscribers/events';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import UnitOfWork from '../UnitOfWork';
import {
ICashflowTransactionCategorizedPayload,
ICashflowTransactionUncategorizingPayload,
ICategorizeCashflowTransactioDTO,
} from '@/interfaces';
import { Knex } from 'knex';
import { transformCategorizeTransToCashflow } from './utils';
import { CommandCashflowValidator } from './CommandCasflowValidator';
import NewCashflowTransactionService from './NewCashflowTransactionService';
import { TransferAuthorizationGuaranteeDecision } from 'plaid';
@Service()
export class CategorizeCashflowTransaction {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private eventPublisher: EventPublisher;
@Inject()
private uow: UnitOfWork;
@Inject()
private commandValidators: CommandCashflowValidator;
@Inject()
private createCashflow: NewCashflowTransactionService;
/**
* Categorize the given cashflow transaction.
* @param {number} tenantId
* @param {ICategorizeCashflowTransactioDTO} categorizeDTO
*/
public async categorize(
tenantId: number,
uncategorizedTransactionId: number,
categorizeDTO: ICategorizeCashflowTransactioDTO
) {
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
// Retrieves the uncategorized transaction or throw an error.
const transaction = await UncategorizedCashflowTransaction.query()
.findById(uncategorizedTransactionId)
.throwIfNotFound();
// Validates the transaction shouldn't be categorized before.
this.commandValidators.validateTransactionShouldNotCategorized(transaction);
// Validate the uncateogirzed transaction if it's deposit the transaction direction
// should `IN` and the same thing if it's withdrawal the direction should be OUT.
this.commandValidators.validateUncategorizeTransactionType(
transaction,
categorizeDTO.transactionType
);
// Edits the cashflow transaction under UOW env.
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Triggers `onTransactionCategorizing` event.
await this.eventPublisher.emitAsync(
events.cashflow.onTransactionCategorizing,
{
tenantId,
trx,
} as ICashflowTransactionUncategorizingPayload
);
// Transformes the categorize DTO to the cashflow transaction.
const cashflowTransactionDTO = transformCategorizeTransToCashflow(
transaction,
categorizeDTO
);
// Creates a new cashflow transaction.
const cashflowTransaction =
await this.createCashflow.newCashflowTransaction(
tenantId,
cashflowTransactionDTO
);
// Updates the uncategorized transaction as categorized.
await UncategorizedCashflowTransaction.query(trx).patchAndFetchById(
uncategorizedTransactionId,
{
categorized: true,
categorizeRefType: 'CashflowTransaction',
categorizeRefId: cashflowTransaction.id,
}
);
// Triggers `onCashflowTransactionCategorized` event.
await this.eventPublisher.emitAsync(
events.cashflow.onTransactionCategorized,
{
tenantId,
// cashflowTransaction,
trx,
} as ICashflowTransactionCategorizedPayload
);
});
}
}

View File

@@ -0,0 +1,80 @@
import {
CategorizeTransactionAsExpenseDTO,
ICashflowTransactionCategorizedPayload,
} from '@/interfaces';
import { Inject, Service } from 'typedi';
import UnitOfWork from '../UnitOfWork';
import events from '@/subscribers/events';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import HasTenancyService from '../Tenancy/TenancyService';
import { Knex } from 'knex';
import { CreateExpense } from '../Expenses/CRUD/CreateExpense';
@Service()
export class CategorizeTransactionAsExpense {
@Inject()
private uow: UnitOfWork;
@Inject()
private tenancy: HasTenancyService;
@Inject()
private eventPublisher: EventPublisher;
@Inject()
private createExpenseService: CreateExpense;
/**
* Categorize the transaction as expense transaction.
* @param {number} tenantId
* @param {number} cashflowTransactionId
* @param {CategorizeTransactionAsExpenseDTO} transactionDTO
*/
public async categorize(
tenantId: number,
cashflowTransactionId: number,
transactionDTO: CategorizeTransactionAsExpenseDTO
) {
const { CashflowTransaction } = this.tenancy.models(tenantId);
const transaction = await CashflowTransaction.query()
.findById(cashflowTransactionId)
.throwIfNotFound();
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Triggers `onTransactionUncategorizing` event.
await this.eventPublisher.emitAsync(
events.cashflow.onTransactionCategorizingAsExpense,
{
tenantId,
trx,
} as ICashflowTransactionCategorizedPayload
);
// Creates a new expense transaction.
const expenseTransaction = await this.createExpenseService.newExpense(
tenantId,
{
},
1
);
// Updates the item on the storage and fetches the updated once.
const cashflowTransaction = await CashflowTransaction.query(
trx
).patchAndFetchById(cashflowTransactionId, {
categorizeRefType: 'Expense',
categorizeRefId: expenseTransaction.id,
uncategorized: true,
});
// Triggers `onTransactionUncategorized` event.
await this.eventPublisher.emitAsync(
events.cashflow.onTransactionCategorizedAsExpense,
{
tenantId,
cashflowTransaction,
trx,
} as ICashflowTransactionUncategorizedPayload
);
});
}
}

View File

@@ -1,9 +1,14 @@
import { Service } from 'typedi';
import { includes, camelCase, upperFirst } from 'lodash';
import { IAccount } from '@/interfaces';
import { IAccount, IUncategorizedCashflowTransaction } from '@/interfaces';
import { getCashflowTransactionType } from './utils';
import { ServiceError } from '@/exceptions';
import { CASHFLOW_TRANSACTION_TYPE, ERRORS } from './constants';
import {
CASHFLOW_DIRECTION,
CASHFLOW_TRANSACTION_TYPE,
ERRORS,
} from './constants';
import CashflowTransaction from '@/models/CashflowTransaction';
@Service()
export class CommandCashflowValidator {
@@ -46,4 +51,52 @@ export class CommandCashflowValidator {
}
return transformedType;
};
/**
* Validate the given transaction should be categorized.
* @param {CashflowTransaction} cashflowTransaction
*/
public validateTransactionShouldCategorized(
cashflowTransaction: CashflowTransaction
) {
if (!cashflowTransaction.uncategorize) {
throw new ServiceError(ERRORS.TRANSACTION_ALREADY_CATEGORIZED);
}
}
/**
* Validate the given transcation shouldn't be categorized.
* @param {CashflowTransaction} cashflowTransaction
*/
public validateTransactionShouldNotCategorized(
cashflowTransaction: CashflowTransaction
) {
if (cashflowTransaction.uncategorize) {
throw new ServiceError(ERRORS.TRANSACTION_ALREADY_CATEGORIZED);
}
}
/**
*
* @param {uncategorizeTransaction}
* @param {string} transactionType
* @throws {ServiceError(ERRORS.UNCATEGORIZED_TRANSACTION_TYPE_INVALID)}
*/
public validateUncategorizeTransactionType(
uncategorizeTransaction: IUncategorizedCashflowTransaction,
transactionType: string
) {
const type = getCashflowTransactionType(
upperFirst(camelCase(transactionType)) as CASHFLOW_TRANSACTION_TYPE
);
if (
(type.direction === CASHFLOW_DIRECTION.IN &&
uncategorizeTransaction.isDepositTransaction) ||
(type.direction === CASHFLOW_DIRECTION.OUT &&
uncategorizeTransaction.isWithdrawalTransaction)
) {
return;
}
throw new ServiceError(ERRORS.UNCATEGORIZED_TRANSACTION_TYPE_INVALID);
}
}

View File

@@ -0,0 +1,40 @@
import { Knex } from 'knex';
import { Inject, Service } from 'typedi';
import HasTenancyService from '../Tenancy/TenancyService';
import UnitOfWork from '../UnitOfWork';
import { CreateUncategorizedTransactionDTO } from '@/interfaces';
@Service()
export class CreateUncategorizedTransaction {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private uow: UnitOfWork;
/**
* Creates an uncategorized cashflow transaction.
* @param {number} tenantId
* @param {CreateUncategorizedTransactionDTO} createDTO
*/
public create(
tenantId: number,
createDTO: CreateUncategorizedTransactionDTO,
trx?: Knex.Transaction
) {
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
return this.uow.withTransaction(
tenantId,
async (trx: Knex.Transaction) => {
const transaction = await UncategorizedCashflowTransaction.query(
trx
).insertAndFetch({
...createDTO,
});
return transaction;
},
trx
);
}
}

View File

@@ -13,15 +13,15 @@ import UnitOfWork from '@/services/UnitOfWork';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
@Service()
export default class CommandCashflowTransactionService {
export class DeleteCashflowTransaction {
@Inject()
tenancy: HasTenancyService;
private tenancy: HasTenancyService;
@Inject()
eventPublisher: EventPublisher;
private eventPublisher: EventPublisher;
@Inject()
uow: UnitOfWork;
private uow: UnitOfWork;
/**
* Deletes the cashflow transaction with associated journal entries.

View File

@@ -4,17 +4,13 @@ import { CashflowTransactionTransformer } from './CashflowTransactionTransformer
import { ERRORS } from './constants';
import { ICashflowTransaction } from '@/interfaces';
import { ServiceError } from '@/exceptions';
import I18nService from '@/services/I18n/I18nService';
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
@Service()
export default class GetCashflowTransactionsService {
export class GetCashflowTransactionService {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private i18nService: I18nService;
@Inject()
private transfromer: TransformerInjectable;
@@ -35,6 +31,7 @@ export default class GetCashflowTransactionsService {
.withGraphFetched('entries.cashflowAccount')
.withGraphFetched('entries.creditAccount')
.withGraphFetched('transactions.account')
.orderBy('date', 'DESC')
.throwIfNotFound();
this.throwErrorCashflowTranscationNotFound(cashflowTransaction);

View File

@@ -0,0 +1,36 @@
import { Inject, Service } from 'typedi';
import HasTenancyService from '../Tenancy/TenancyService';
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
import { UncategorizedTransactionTransformer } from './UncategorizedTransactionTransformer';
@Service()
export class GetUncategorizedTransaction {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private transformer: TransformerInjectable;
/**
* Retrieves specific uncategorized cashflow transaction.
* @param {number} tenantId - Tenant id.
* @param {number} uncategorizedTransactionId - Uncategorized transaction id.
*/
public async getTransaction(
tenantId: number,
uncategorizedTransactionId: number
) {
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
const transaction = await UncategorizedCashflowTransaction.query()
.findById(uncategorizedTransactionId)
.withGraphFetched('account')
.throwIfNotFound();
return this.transformer.transform(
tenantId,
transaction,
new UncategorizedTransactionTransformer()
);
}
}

View File

@@ -0,0 +1,51 @@
import { Inject, Service } from 'typedi';
import HasTenancyService from '../Tenancy/TenancyService';
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
import { UncategorizedTransactionTransformer } from './UncategorizedTransactionTransformer';
import { IGetUncategorizedTransactionsQuery } from '@/interfaces';
@Service()
export class GetUncategorizedTransactions {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private transformer: TransformerInjectable;
/**
* Retrieves the uncategorized cashflow transactions.
* @param {number} tenantId - Tenant id.
* @param {number} accountId - Account Id.
*/
public async getTransactions(
tenantId: number,
accountId: number,
query: IGetUncategorizedTransactionsQuery
) {
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
// Parsed query with default values.
const _query = {
page: 1,
pageSize: 20,
...query,
};
const { results, pagination } =
await UncategorizedCashflowTransaction.query()
.where('accountId', accountId)
.where('categorized', false)
.withGraphFetched('account')
.orderBy('date', 'DESC')
.pagination(_query.page - 1, _query.pageSize);
const data = await this.transformer.transform(
tenantId,
results,
new UncategorizedTransactionTransformer()
);
return {
data,
pagination,
};
}
}

View File

@@ -1,11 +1,10 @@
import { Service, Inject } from 'typedi';
import { isEmpty, pick } from 'lodash';
import { pick } from 'lodash';
import { Knex } from 'knex';
import * as R from 'ramda';
import {
ICashflowNewCommandDTO,
ICashflowTransaction,
ICashflowTransactionLine,
ICommandCashflowCreatedPayload,
ICommandCashflowCreatingPayload,
ICashflowTransactionInput,
@@ -87,6 +86,7 @@ export default class NewCashflowTransactionService {
'creditAccountId',
'branchId',
'plaidTransactionId',
'uncategorizedTransactionId',
]);
// Retreive the next invoice number.
const autoNextNumber =
@@ -126,7 +126,7 @@ export default class NewCashflowTransactionService {
tenantId: number,
newTransactionDTO: ICashflowNewCommandDTO,
userId?: number
): Promise<{ cashflowTransaction: ICashflowTransaction }> => {
): Promise<ICashflowTransaction> => {
const { CashflowTransaction, Account } = this.tenancy.models(tenantId);
// Retrieves the cashflow account or throw not found error.
@@ -175,7 +175,7 @@ export default class NewCashflowTransactionService {
trx,
} as ICommandCashflowCreatedPayload
);
return { cashflowTransaction };
return cashflowTransaction;
});
};
}

View File

@@ -0,0 +1,72 @@
import { Knex } from 'knex';
import { Inject, Service } from 'typedi';
import HasTenancyService from '../Tenancy/TenancyService';
import UnitOfWork from '../UnitOfWork';
import events from '@/subscribers/events';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import {
ICashflowTransactionUncategorizedPayload,
ICashflowTransactionUncategorizingPayload,
} from '@/interfaces';
@Service()
export class UncategorizeCashflowTransaction {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private eventPublisher: EventPublisher;
@Inject()
private uow: UnitOfWork;
/**
* Uncategorizes the given cashflow transaction.
* @param {number} tenantId
* @param {number} cashflowTransactionId
*/
public async uncategorize(
tenantId: number,
uncategorizedTransactionId: number
) {
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
const oldUncategorizedTransaction =
await UncategorizedCashflowTransaction.query()
.findById(uncategorizedTransactionId)
.throwIfNotFound();
// Updates the transaction under UOW.
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Triggers `onTransactionUncategorizing` event.
await this.eventPublisher.emitAsync(
events.cashflow.onTransactionUncategorizing,
{
tenantId,
trx,
} as ICashflowTransactionUncategorizingPayload
);
// Removes the ref relation with the related transaction.
const uncategorizedTransaction =
await UncategorizedCashflowTransaction.query(trx).updateAndFetchById(
uncategorizedTransactionId,
{
categorized: false,
categorizeRefId: null,
categorizeRefType: null,
}
);
// Triggers `onTransactionUncategorized` event.
await this.eventPublisher.emitAsync(
events.cashflow.onTransactionUncategorized,
{
tenantId,
uncategorizedTransaction,
oldUncategorizedTransaction,
trx,
} as ICashflowTransactionUncategorizedPayload
);
return uncategorizedTransaction;
});
}
}

View File

@@ -0,0 +1,65 @@
import { Transformer } from '@/lib/Transformer/Transformer';
import { formatNumber } from '@/utils';
export class UncategorizedTransactionTransformer extends Transformer {
/**
* Include these attributes to sale invoice object.
* @returns {string[]}
*/
public includeAttributes = (): string[] => {
return [
'formattedAmount',
'formattedDate',
'formattetDepositAmount',
'formattedWithdrawalAmount',
];
};
/**
* Formattes the transaction date.
* @param transaction
* @returns {string}
*/
public formattedDate(transaction) {
return this.formatDate(transaction.date);
}
/**
* Formatted amount.
* @param transaction
* @returns {string}
*/
public formattedAmount(transaction) {
return formatNumber(transaction.amount, {
currencyCode: transaction.currencyCode,
});
}
/**
* Formatted deposit amount.
* @param transaction
* @returns {string}
*/
protected formattetDepositAmount(transaction) {
if (transaction.isDepositTransaction) {
return formatNumber(transaction.deposit, {
currencyCode: transaction.currencyCode,
});
}
return '';
}
/**
* Formatted withdrawal amount.
* @param transaction
* @returns {string}
*/
protected formattedWithdrawalAmount(transaction) {
if (transaction.isWithdrawalTransaction) {
return formatNumber(transaction.withdrawal, {
currencyCode: transaction.currencyCode,
});
}
return '';
}
}

View File

@@ -0,0 +1,82 @@
import { Inject, Service } from 'typedi';
import { Knex } from 'knex';
import * as yup from 'yup';
import { Importable } from '../Import/Importable';
import { CreateUncategorizedTransaction } from './CreateUncategorizedTransaction';
import { CreateUncategorizedTransactionDTO } from '@/interfaces';
import { ImportableContext } from '../Import/interfaces';
import HasTenancyService from '../Tenancy/TenancyService';
import { BankTransactionsSampleData } from './constants';
@Service()
export class UncategorizedTransactionsImportable extends Importable {
@Inject()
private createUncategorizedTransaction: CreateUncategorizedTransaction;
@Inject()
private tenancy: HasTenancyService;
/**
* Passing the sheet DTO to create uncategorized transaction.
* @param {number} tenantId
* @param {number} tenantId
* @param {any} createDTO
* @param {Knex.Transaction} trx
*/
public async importable(
tenantId: number,
createDTO: CreateUncategorizedTransactionDTO,
trx?: Knex.Transaction
) {
return this.createUncategorizedTransaction.create(tenantId, createDTO, trx);
}
/**
* Transformes the DTO before validating and importing.
* @param {CreateUncategorizedTransactionDTO} createDTO
* @param {ImportableContext} context
* @returns {CreateUncategorizedTransactionDTO}
*/
public transform(
createDTO: CreateUncategorizedTransactionDTO,
context?: ImportableContext
): CreateUncategorizedTransactionDTO {
return {
...createDTO,
accountId: context.import.paramsParsed.accountId,
};
}
/**
* Sample data used to download sample sheet.
* @returns {Record<string, any>[]}
*/
public sampleData(): Record<string, any>[] {
return BankTransactionsSampleData;
}
/**
* Params validation schema.
* @returns {ValidationSchema[]}
*/
public paramsValidationSchema() {
return yup.object().shape({
accountId: yup.number().required(),
});
}
/**
* Validates the params existance asyncly.
* @param {number} tenantId -
* @param {Record<string, any>} params -
*/
public async validateParams(
tenantId: number,
params: Record<string, any>
): Promise<void> {
const { Account } = this.tenancy.models(tenantId);
if (params.accountId) {
await Account.query().findById(params.accountId).throwIfNotFound({});
}
}
}

View File

@@ -8,7 +8,13 @@ export const ERRORS = {
CREDIT_ACCOUNTS_IDS_NOT_FOUND: 'CREDIT_ACCOUNTS_IDS_NOT_FOUND',
CREDIT_ACCOUNTS_HAS_INVALID_TYPE: 'CREDIT_ACCOUNTS_HAS_INVALID_TYPE',
ACCOUNT_ID_HAS_INVALID_TYPE: 'ACCOUNT_ID_HAS_INVALID_TYPE',
ACCOUNT_HAS_ASSOCIATED_TRANSACTIONS: 'account_has_associated_transactions'
ACCOUNT_HAS_ASSOCIATED_TRANSACTIONS: 'account_has_associated_transactions',
TRANSACTION_ALREADY_CATEGORIZED: 'TRANSACTION_ALREADY_CATEGORIZED',
TRANSACTION_ALREADY_UNCATEGORIZED: 'TRANSACTION_ALREADY_UNCATEGORIZED',
UNCATEGORIZED_TRANSACTION_TYPE_INVALID:
'UNCATEGORIZED_TRANSACTION_TYPE_INVALID',
CANNOT_DELETE_TRANSACTION_CONVERTED_FROM_UNCATEGORIZED:
'CANNOT_DELETE_TRANSACTION_CONVERTED_FROM_UNCATEGORIZED',
};
export enum CASHFLOW_DIRECTION {
@@ -71,3 +77,29 @@ export interface ICashflowTransactionTypeMeta {
direction: CASHFLOW_DIRECTION;
creditType: string[];
}
export const BankTransactionsSampleData = [
[
{
Amount: '6,410.19',
Date: '2024-03-26',
Payee: 'MacGyver and Sons',
'Reference No.': 'REF-1',
Description: 'Commodi quo labore.',
},
{
Amount: '8,914.17',
Date: '2024-01-05',
Payee: 'Eichmann - Bergnaum',
'Reference No.': 'REF-1',
Description: 'Quia enim et.',
},
{
Amount: '6,200.88',
Date: '2024-02-17',
Payee: 'Luettgen, Mraz and Legros',
'Reference No.': 'REF-1',
Description: 'Occaecati consequuntur cum impedit illo.',
},
],
];

View File

@@ -0,0 +1,42 @@
import { Inject, Service } from 'typedi';
import events from '@/subscribers/events';
import { ICashflowTransactionUncategorizedPayload } from '@/interfaces';
import { DeleteCashflowTransaction } from '../DeleteCashflowTransactionService';
import HasTenancyService from '@/services/Tenancy/TenancyService';
@Service()
export class DeleteCashflowTransactionOnUncategorize {
@Inject()
private deleteCashflowTransactionService: DeleteCashflowTransaction;
/**
* Attaches events with handlers.
*/
public attach = (bus) => {
bus.subscribe(
events.cashflow.onTransactionUncategorized,
this.deleteCashflowTransactionOnUncategorize.bind(this)
);
};
/**
* Deletes the cashflow transaction on uncategorize transaction.
* @param {ICashflowTransactionUncategorizedPayload} payload
*/
public async deleteCashflowTransactionOnUncategorize({
tenantId,
oldUncategorizedTransaction,
trx,
}: ICashflowTransactionUncategorizedPayload) {
// Deletes the cashflow transaction.
if (
oldUncategorizedTransaction.categorizeRefType === 'CashflowTransaction'
) {
await this.deleteCashflowTransactionService.deleteCashflowTransaction(
tenantId,
oldUncategorizedTransaction.categorizeRefId
);
}
}
}

View File

@@ -0,0 +1,52 @@
import { Inject, Service } from 'typedi';
import events from '@/subscribers/events';
import { ICommandCashflowDeletingPayload } from '@/interfaces';
import { ServiceError } from '@/exceptions';
import { ERRORS } from '../constants';
import HasTenancyService from '@/services/Tenancy/TenancyService';
@Service()
export class PreventDeleteTransactionOnDelete {
@Inject()
private tenancy: HasTenancyService;
/**
* Attaches events with handlers.
*/
public attach = (bus) => {
bus.subscribe(
events.cashflow.onTransactionDeleting,
this.preventDeleteCashflowTransactionHasUncategorizedTransaction.bind(
this
)
);
};
/**
* Prevent delete cashflow transaction has converted from uncategorized transaction.
* @param {ICommandCashflowDeletingPayload} payload
*/
public async preventDeleteCashflowTransactionHasUncategorizedTransaction({
tenantId,
oldCashflowTransaction,
trx,
}: ICommandCashflowDeletingPayload) {
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
if (oldCashflowTransaction.uncategorizedTransactionId) {
const foundTransactions = await UncategorizedCashflowTransaction.query(
trx
).where({
categorized: true,
categorizeRefId: oldCashflowTransaction.id,
categorizeRefType: 'CashflowTransaction',
});
// Throw the error if the cashflow transaction still linked to uncategorized transaction.
if (foundTransactions.length > 0) {
throw new ServiceError(
ERRORS.CANNOT_DELETE_TRANSACTION_CONVERTED_FROM_UNCATEGORIZED,
'Cannot delete cashflow transaction converted from uncategorized transaction.'
);
}
}
}
}

View File

@@ -1,13 +1,19 @@
import { upperFirst, camelCase } from 'lodash';
import { upperFirst, camelCase, omit } from 'lodash';
import {
CASHFLOW_TRANSACTION_TYPE,
CASHFLOW_TRANSACTION_TYPE_META,
ICashflowTransactionTypeMeta,
} from './constants';
import {
ICashflowNewCommandDTO,
ICashflowTransaction,
ICategorizeCashflowTransactioDTO,
IUncategorizedCashflowTransaction,
} from '@/interfaces';
/**
* Ensures the given transaction type to transformed to appropriate format.
* @param {string} type
* @param {string} type
* @returns {string}
*/
export const transformCashflowTransactionType = (type) => {
@@ -32,3 +38,30 @@ export function getCashflowTransactionType(
export const getCashflowAccountTransactionsTypes = () => {
return Object.values(CASHFLOW_TRANSACTION_TYPE_META).map((meta) => meta.type);
};
/**
* Tranasformes the given uncategorized transaction and categorized DTO
* to cashflow create DTO.
* @param {IUncategorizedCashflowTransaction} uncategorizeModel
* @param {ICategorizeCashflowTransactioDTO} categorizeDTO
* @returns {ICashflowNewCommandDTO}
*/
export const transformCategorizeTransToCashflow = (
uncategorizeModel: IUncategorizedCashflowTransaction,
categorizeDTO: ICategorizeCashflowTransactioDTO
): ICashflowNewCommandDTO => {
return {
date: uncategorizeModel.date,
referenceNo: categorizeDTO.referenceNo || uncategorizeModel.referenceNo,
description: categorizeDTO.description || uncategorizeModel.description,
cashflowAccountId: uncategorizeModel.accountId,
creditAccountId: categorizeDTO.creditAccountId,
exchangeRate: categorizeDTO.exchangeRate || 1,
currencyCode: uncategorizeModel.currencyCode,
amount: uncategorizeModel.amount,
transactionNumber: categorizeDTO.transactionNumber,
transactionType: categorizeDTO.transactionType,
uncategorizedTransactionId: uncategorizeModel.id,
publish: true,
};
};

View File

@@ -36,7 +36,7 @@ export class CreateCustomer {
public async createCustomer(
tenantId: number,
customerDTO: ICustomerNewDTO,
authorizedUser: ISystemUser
trx?: Knex.Transaction
): Promise<ICustomer> {
const { Contact } = this.tenancy.models(tenantId);
@@ -46,28 +46,31 @@ export class CreateCustomer {
customerDTO
);
// Creates a new customer under unit-of-work envirement.
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Triggers `onCustomerCreating` event.
await this.eventPublisher.emitAsync(events.customers.onCreating, {
tenantId,
customerDTO,
trx,
} as ICustomerEventCreatingPayload);
return this.uow.withTransaction(
tenantId,
async (trx: Knex.Transaction) => {
// Triggers `onCustomerCreating` event.
await this.eventPublisher.emitAsync(events.customers.onCreating, {
tenantId,
customerDTO,
trx,
} as ICustomerEventCreatingPayload);
// Creates a new contact as customer.
const customer = await Contact.query(trx).insertAndFetch({
...customerObj,
});
// Triggers `onCustomerCreated` event.
await this.eventPublisher.emitAsync(events.customers.onCreated, {
customer,
tenantId,
customerId: customer.id,
authorizedUser,
trx,
} as ICustomerEventCreatedPayload);
// Creates a new contact as customer.
const customer = await Contact.query(trx).insertAndFetch({
...customerObj,
});
// Triggers `onCustomerCreated` event.
await this.eventPublisher.emitAsync(events.customers.onCreated, {
customer,
tenantId,
customerId: customer.id,
trx,
} as ICustomerEventCreatedPayload);
return customer;
});
return customer;
},
trx
);
}
}

View File

@@ -1,6 +1,6 @@
import moment from 'moment';
import { defaultTo, omit, isEmpty } from 'lodash';
import { Service, Inject } from 'typedi';
import { Service } from 'typedi';
import {
ContactService,
ICustomer,
@@ -51,6 +51,10 @@ export class CreateEditCustomerDTO {
).toMySqlDateTime(),
}
: {}),
openingBalanceExchangeRate: defaultTo(
customerDTO.openingBalanceExchangeRate,
1
),
};
};

View File

@@ -4,7 +4,6 @@ import {
ICustomerEditDTO,
ICustomerEventEditedPayload,
ICustomerEventEditingPayload,
ISystemUser,
} from '@/interfaces';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import UnitOfWork from '@/services/UnitOfWork';

View File

@@ -53,13 +53,8 @@ export class CustomersApplication {
public createCustomer = (
tenantId: number,
customerDTO: ICustomerNewDTO,
authorizedUser: ISystemUser
) => {
return this.createCustomerService.createCustomer(
tenantId,
customerDTO,
authorizedUser
);
return this.createCustomerService.createCustomer(tenantId, customerDTO);
};
/**

View File

@@ -0,0 +1,34 @@
import { Inject, Service } from 'typedi';
import { Importable } from '@/services/Import/Importable';
import { CreateCustomer } from './CRUD/CreateCustomer';
import { Knex } from 'knex';
import { ICustomer, ICustomerNewDTO } from '@/interfaces';
import { CustomersSampleData } from './_SampleData';
@Service()
export class CustomersImportable extends Importable {
@Inject()
private createCustomerService: CreateCustomer;
/**
* Mapps the imported data to create a new customer service.
* @param {number} tenantId
* @param {ICustomerNewDTO} createDTO
* @param {Knex.Transaction} trx
* @returns {Promise<void>}
*/
public async importable(
tenantId: number,
createDTO: ICustomerNewDTO,
trx?: Knex.Transaction<any, any[]>
): Promise<void> {
await this.createCustomerService.createCustomer(tenantId, createDTO, trx);
}
/**
* Retrieves the sample data of customers used to download sample sheet.
*/
public sampleData(): any[] {
return CustomersSampleData;
}
}

View File

@@ -0,0 +1,158 @@
export const CustomersSampleData = [
{
"Customer Type": "Business",
"First Name": "Nicolette",
"Last Name": "Schamberger",
"Company Name": "Homenick - Hane",
"Display Name": "Rowland Rowe",
"Email": "cicero86@yahoo.com",
"Personal Phone Number": "811-603-2235",
"Work Phone Number": "906-993-5190",
"Website": "http://google.com",
"Opening Balance": 54302.23,
"Opening Balance At": "2022-02-02",
"Opening Balance Ex. Rate": 2,
"Currency": "LYD",
"Active": "F",
"Note": "Doloribus autem optio temporibus dolores mollitia sit.",
"Billing Address 1": "862 Jessika Well",
"Billing Address 2": "1091 Dorthy Mount",
"Billing Address City": "Deckowfort",
"Billing Address Country": "Ghana",
"Billing Address Phone": "825-011-5207",
"Billing Address Postcode": "38228",
"Billing Address State": "Oregon",
"Shipping Address 1": "37626 Thiel Villages",
"Shipping Address 2": "132 Batz Avenue",
"Shipping Address City": "Pagacburgh",
"Shipping Address Country": "Albania",
"Shipping Address Phone": "171-546-3701",
"Shipping Address Postcode": "13709",
"Shipping Address State": "Georgia"
},
{
"Customer Type": "Business",
"First Name": "Hermann",
"Last Name": "Crooks",
"Company Name": "Veum - Schaefer",
"Display Name": "Harley Veum",
"Email": "immanuel56@hotmail.com",
"Personal Phone Number": "449-780-9999",
"Work Phone Number": "970-473-5785",
"Website": "http://google.com",
"Opening Balance": 54302.23,
"Opening Balance At": "2022-02-02",
"Opening Balance Ex. Rate": 2,
"Currency": "LYD",
"Active": "T",
"Note": "Doloribus dolore dolor dicta vitae in fugit nisi quibusdam.",
"Billing Address 1": "532 Simonis Spring",
"Billing Address 2": "3122 Nicolas Inlet",
"Billing Address City": "East Matteofort",
"Billing Address Country": "Holy See (Vatican City State)",
"Billing Address Phone": "366-084-8629",
"Billing Address Postcode": "41607",
"Billing Address State": "Montana",
"Shipping Address 1": "2889 Tremblay Plaza",
"Shipping Address 2": "71355 Kutch Isle",
"Shipping Address City": "D'Amorehaven",
"Shipping Address Country": "Monaco",
"Shipping Address Phone": "614-189-3328",
"Shipping Address Postcode": "09634-0435",
"Shipping Address State": "Nevada"
},
{
"Customer Type": "Business",
"First Name": "Nellie",
"Last Name": "Gulgowski",
"Company Name": "Boyle, Heller and Jones",
"Display Name": "Randall Kohler",
"Email": "anibal_frami@yahoo.com",
"Personal Phone Number": "498-578-0740",
"Work Phone Number": "394-550-6827",
"Website": "http://google.com",
"Opening Balance": 54302.23,
"Opening Balance At": "2022-02-02",
"Opening Balance Ex. Rate": 2,
"Currency": "LYD",
"Active": "T",
"Note": "Vero quibusdam rem fugit aperiam est modi.",
"Billing Address 1": "214 Sauer Villages",
"Billing Address 2": "30687 Kacey Square",
"Billing Address City": "Jayceborough",
"Billing Address Country": "Benin",
"Billing Address Phone": "332-820-1127",
"Billing Address Postcode": "16425-3887",
"Billing Address State": "Mississippi",
"Shipping Address 1": "562 Diamond Loaf",
"Shipping Address 2": "9595 Satterfield Trafficway",
"Shipping Address City": "Alexandrinefort",
"Shipping Address Country": "Puerto Rico",
"Shipping Address Phone": "776-500-8456",
"Shipping Address Postcode": "30258",
"Shipping Address State": "South Dakota"
},
{
"Customer Type": "Business",
"First Name": "Stone",
"Last Name": "Jerde",
"Company Name": "Cassin, Casper and Maggio",
"Display Name": "Clint McLaughlin",
"Email": "nathanael22@yahoo.com",
"Personal Phone Number": "562-790-6059",
"Work Phone Number": "686-838-0027",
"Website": "http://google.com",
"Opening Balance": 54302.23,
"Opening Balance At": "2022-02-02",
"Opening Balance Ex. Rate": 2,
"Currency": "LYD",
"Active": "F",
"Note": "Quis cumque molestias rerum.",
"Billing Address 1": "22590 Cathy Harbor",
"Billing Address 2": "24493 Brycen Brooks",
"Billing Address City": "Elnorashire",
"Billing Address Country": "Andorra",
"Billing Address Phone": "701-852-8005",
"Billing Address Postcode": "5680",
"Billing Address State": "Nevada",
"Shipping Address 1": "5355 Erdman Bridge",
"Shipping Address 2": "421 Jeanette Camp",
"Shipping Address City": "East Philip",
"Shipping Address Country": "Venezuela",
"Shipping Address Phone": "426-119-0858",
"Shipping Address Postcode": "34929-0501",
"Shipping Address State": "Tennessee"
},
{
"Customer Type": "Individual",
"First Name": "Lempi",
"Last Name": "Kling",
"Company Name": "Schamberger, O'Connell and Bechtelar",
"Display Name": "Alexie Barton",
"Email": "eulah.kreiger@hotmail.com",
"Personal Phone Number": "745-756-1063",
"Work Phone Number": "965-150-1945",
"Website": "http://google.com",
"Opening Balance": 54302.23,
"Opening Balance At": "2022-02-02",
"Opening Balance Ex. Rate": 2,
"Currency": "LYD",
"Active": "F",
"Note": "Maxime laboriosam hic voluptate maiores est officia.",
"Billing Address 1": "0851 Jones Flat",
"Billing Address 2": "845 Bailee Drives",
"Billing Address City": "Kamrenport",
"Billing Address Country": "Niger",
"Billing Address Phone": "220-125-0608",
"Billing Address Postcode": "30311",
"Billing Address State": "Delaware",
"Shipping Address 1": "929 Ferry Row",
"Shipping Address 2": "020 Adam Plaza",
"Shipping Address City": "West Carmellaside",
"Shipping Address Country": "Ghana",
"Shipping Address Phone": "053-333-6679",
"Shipping Address Postcode": "79221-4681",
"Shipping Address State": "Illinois"
}
]

View File

@@ -49,6 +49,10 @@ export class CreateEditVendorDTO {
).toMySqlDateTime(),
}
: {}),
openingBalanceExchangeRate: defaultTo(
vendorDTO.openingBalanceExchangeRate,
1
),
};
};

View File

@@ -35,7 +35,7 @@ export class CreateVendor {
public async createVendor(
tenantId: number,
vendorDTO: IVendorNewDTO,
authorizedUser: ISystemUser
trx?: Knex.Transaction
) {
const { Contact } = this.tenancy.models(tenantId);
@@ -45,28 +45,31 @@ export class CreateVendor {
vendorDTO
);
// Creates vendor contact under unit-of-work evnirement.
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Triggers `onVendorCreating` event.
await this.eventPublisher.emitAsync(events.vendors.onCreating, {
tenantId,
vendorDTO,
trx,
} as IVendorEventCreatingPayload);
return this.uow.withTransaction(
tenantId,
async (trx: Knex.Transaction) => {
// Triggers `onVendorCreating` event.
await this.eventPublisher.emitAsync(events.vendors.onCreating, {
tenantId,
vendorDTO,
trx,
} as IVendorEventCreatingPayload);
// Creates a new contact as vendor.
const vendor = await Contact.query(trx).insertAndFetch({
...vendorObject,
});
// Triggers `onVendorCreated` event.
await this.eventPublisher.emitAsync(events.vendors.onCreated, {
tenantId,
vendorId: vendor.id,
vendor,
authorizedUser,
trx,
} as IVendorEventCreatedPayload);
// Creates a new contact as vendor.
const vendor = await Contact.query(trx).insertAndFetch({
...vendorObject,
});
// Triggers `onVendorCreated` event.
await this.eventPublisher.emitAsync(events.vendors.onCreated, {
tenantId,
vendorId: vendor.id,
vendor,
trx,
} as IVendorEventCreatedPayload);
return vendor;
});
return vendor;
},
trx
);
}
}

View File

@@ -0,0 +1,32 @@
import { Importable } from '@/services/Import/Importable';
import { CreateVendor } from './CRUD/CreateVendor';
import { Knex } from 'knex';
import { Inject, Service } from 'typedi';
import { VendorsSampleData } from './_SampleData';
@Service()
export class VendorsImportable extends Importable {
@Inject()
private createVendorService: CreateVendor;
/**
* Maps the imported data to create a new vendor service.
* @param {number} tenantId
* @param {} createDTO
* @param {Knex.Transaction} trx
*/
public async importable(
tenantId: number,
createDTO: any,
trx?: Knex.Transaction<any, any[]>
): Promise<void> {
await this.createVendorService.createVendor(tenantId, createDTO, trx);
}
/**
* Retrieves the sample data of vendors sample sheet.
*/
public sampleData(): any[] {
return VendorsSampleData;
}
}

View File

@@ -0,0 +1,122 @@
export const VendorsSampleData = [
{
"First Name": "Nicolette",
"Last Name": "Schamberger",
"Company Name": "Homenick - Hane",
"Display Name": "Rowland Rowe",
"Email": "cicero86@yahoo.com",
"Personal Phone Number": "811-603-2235",
"Work Phone Number": "906-993-5190",
"Website": "http://google.com",
"Opening Balance": 54302.23,
"Opening Balance At": "2022-02-02",
"Opening Balance Ex. Rate": 2,
"Currency": "LYD",
"Active": "F",
"Note": "Doloribus autem optio temporibus dolores mollitia sit.",
"Billing Address 1": "862 Jessika Well",
"Billing Address 2": "1091 Dorthy Mount",
"Billing Address City": "Deckowfort",
"Billing Address Country": "Ghana",
"Billing Address Phone": "825-011-5207",
"Billing Address Postcode": "38228",
"Billing Address State": "Oregon",
"Shipping Address 1": "37626 Thiel Villages",
"Shipping Address 2": "132 Batz Avenue",
"Shipping Address City": "Pagacburgh",
"Shipping Address Country": "Albania",
"Shipping Address Phone": "171-546-3701",
"Shipping Address Postcode": "13709",
"Shipping Address State": "Georgia"
},
{
"First Name": "Hermann",
"Last Name": "Crooks",
"Company Name": "Veum - Schaefer",
"Display Name": "Harley Veum",
"Email": "immanuel56@hotmail.com",
"Personal Phone Number": "449-780-9999",
"Work Phone Number": "970-473-5785",
"Website": "http://google.com",
"Opening Balance": 54302.23,
"Opening Balance At": "2022-02-02",
"Opening Balance Ex. Rate": 2,
"Currency": "LYD",
"Active": "F",
"Note": "Doloribus dolore dolor dicta vitae in fugit nisi quibusdam.",
"Billing Address 1": "532 Simonis Spring",
"Billing Address 2": "3122 Nicolas Inlet",
"Billing Address City": "East Matteofort",
"Billing Address Country": "Holy See (Vatican City State)",
"Billing Address Phone": "366-084-8629",
"Billing Address Postcode": "41607",
"Billing Address State": "Montana",
"Shipping Address 1": "2889 Tremblay Plaza",
"Shipping Address 2": "71355 Kutch Isle",
"Shipping Address City": "D'Amorehaven",
"Shipping Address Country": "Monaco",
"Shipping Address Phone": "614-189-3328",
"Shipping Address Postcode": "09634-0435",
"Shipping Address State": "Nevada"
},
{
"First Name": "Nellie",
"Last Name": "Gulgowski",
"Company Name": "Boyle, Heller and Jones",
"Display Name": "Randall Kohler",
"Email": "anibal_frami@yahoo.com",
"Personal Phone Number": "498-578-0740",
"Work Phone Number": "394-550-6827",
"Website": "http://google.com",
"Opening Balance": 54302.23,
"Opening Balance At": "2022-02-02",
"Opening Balance Ex. Rate": 2,
"Currency": "LYD",
"Active": "F",
"Note": "Vero quibusdam rem fugit aperiam est modi.",
"Billing Address 1": "214 Sauer Villages",
"Billing Address 2": "30687 Kacey Square",
"Billing Address City": "Jayceborough",
"Billing Address Country": "Benin",
"Billing Address Phone": "332-820-1127",
"Billing Address Postcode": "16425-3887",
"Billing Address State": "Mississippi",
"Shipping Address 1": "562 Diamond Loaf",
"Shipping Address 2": "9595 Satterfield Trafficway",
"Shipping Address City": "Alexandrinefort",
"Shipping Address Country": "Puerto Rico",
"Shipping Address Phone": "776-500-8456",
"Shipping Address Postcode": "30258",
"Shipping Address State": "South Dakota"
},
{
"First Name": "Stone",
"Last Name": "Jerde",
"Company Name": "Cassin, Casper and Maggio",
"Display Name": "Clint McLaughlin",
"Email": "nathanael22@yahoo.com",
"Personal Phone Number": "562-790-6059",
"Work Phone Number": "686-838-0027",
"Website": "http://google.com",
"Opening Balance": 54302.23,
"Opening Balance At": "2022-02-02",
"Opening Balance Ex. Rate": 2,
"Currency": "LYD",
"Active": "F",
"Note": "Quis cumque molestias rerum.",
"Billing Address 1": "22590 Cathy Harbor",
"Billing Address 2": "24493 Brycen Brooks",
"Billing Address City": "Elnorashire",
"Billing Address Country": "Andorra",
"Billing Address Phone": "701-852-8005",
"Billing Address Postcode": "5680",
"Billing Address State": "Nevada",
"Shipping Address 1": "5355 Erdman Bridge",
"Shipping Address 2": "421 Jeanette Camp",
"Shipping Address City": "East Philip",
"Shipping Address Country": "Venezuela",
"Shipping Address Phone": "426-119-0858",
"Shipping Address Postcode": "34929-0501",
"Shipping Address State": "Tennessee"
}
]

View File

@@ -0,0 +1,205 @@
import fs from 'fs/promises';
import XLSX from 'xlsx';
import bluebird from 'bluebird';
import * as R from 'ramda';
import { Inject, Service } from 'typedi';
import { first } from 'lodash';
import { ImportFileDataValidator } from './ImportFileDataValidator';
import { Knex } from 'knex';
import {
ImportOperError,
ImportOperSuccess,
ImportableContext,
} from './interfaces';
import { ServiceError } from '@/exceptions';
import { trimObject } from './_utils';
import { ImportableResources } from './ImportableResources';
import ResourceService from '../Resource/ResourceService';
import HasTenancyService from '../Tenancy/TenancyService';
import Import from '@/models/Import';
@Service()
export class ImportFileCommon {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private importFileValidator: ImportFileDataValidator;
@Inject()
private importable: ImportableResources;
@Inject()
private resource: ResourceService;
/**
* Maps the columns of the imported data based on the provided mapping attributes.
* @param {Record<string, any>[]} body - The array of data objects to map.
* @param {ImportMappingAttr[]} map - The mapping attributes.
* @returns {Record<string, any>[]} - The mapped data objects.
*/
public parseXlsxSheet(buffer: Buffer): Record<string, unknown>[] {
const workbook = XLSX.read(buffer, { type: 'buffer', raw: true });
const firstSheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[firstSheetName];
return XLSX.utils.sheet_to_json(worksheet, {});
}
/**
* Reads the import file.
* @param {string} filename
* @returns {Promise<Buffer>}
*/
public readImportFile(filename: string) {
return fs.readFile(`public/imports/${filename}`);
}
/**
* Imports the given parsed data to the resource storage through registered importable service.
* @param {number} tenantId -
* @param {string} resourceName - Resource name.
* @param {Record<string, any>} parsedData - Parsed data.
* @param {Knex.Transaction} trx - Knex transaction.
* @returns {Promise<[ImportOperSuccess[], ImportOperError[]]>}
*/
public async import(
tenantId: number,
importFile: Import,
parsedData: Record<string, any>[],
trx?: Knex.Transaction
): Promise<[ImportOperSuccess[], ImportOperError[]]> {
const importableFields = this.resource.getResourceImportableFields(
tenantId,
importFile.resource
);
const ImportableRegistry = this.importable.registry;
const importable = ImportableRegistry.getImportable(importFile.resource);
const concurrency = importable.concurrency || 10;
const success: ImportOperSuccess[] = [];
const failed: ImportOperError[] = [];
const importAsync = async (objectDTO, index: number): Promise<void> => {
const context: ImportableContext = {
rowIndex: index,
import: importFile,
};
const transformedDTO = importable.transform(objectDTO, context);
try {
// Validate the DTO object before passing it to the service layer.
await this.importFileValidator.validateData(
importableFields,
transformedDTO
);
try {
// Run the importable function and listen to the errors.
const data = await importable.importable(
tenantId,
transformedDTO,
trx
);
success.push({ index, data });
} catch (err) {
if (err instanceof ServiceError) {
const error = [
{
errorCode: 'ValidationError',
errorMessage: err.message || err.errorType,
rowNumber: index + 1,
},
];
failed.push({ index, error });
}
}
} catch (errors) {
const error = errors.map((er) => ({ ...er, rowNumber: index + 1 }));
failed.push({ index, error });
}
};
await bluebird.map(parsedData, importAsync, { concurrency });
return [success, failed];
}
/**
*
* @param {string} resourceName
* @param {Record<string, any>} params
*/
public async validateParamsSchema(
resourceName: string,
params: Record<string, any>
) {
const ImportableRegistry = this.importable.registry;
const importable = ImportableRegistry.getImportable(resourceName);
const yupSchema = importable.paramsValidationSchema();
try {
await yupSchema.validate(params, { abortEarly: false });
} catch (validationError) {
const errors = validationError.inner.map((error) => ({
errorCode: 'ParamsValidationError',
errorMessage: error.errors,
}));
throw errors;
}
}
/**
*
* @param {string} resourceName
* @param {Record<string, any>} params
*/
public async validateParams(
tenantId: number,
resourceName: string,
params: Record<string, any>
) {
const ImportableRegistry = this.importable.registry;
const importable = ImportableRegistry.getImportable(resourceName);
await importable.validateParams(tenantId, params);
}
/**
*
* @param {string} resourceName
* @param {Record<string, any>} params
* @returns
*/
public transformParams(resourceName: string, params: Record<string, any>) {
const ImportableRegistry = this.importable.registry;
const importable = ImportableRegistry.getImportable(resourceName);
return importable.transformParams(params);
}
/**
* Retrieves the sheet columns from the given sheet data.
* @param {unknown[]} json
* @returns {string[]}
*/
public parseSheetColumns(json: unknown[]): string[] {
return R.compose(Object.keys, trimObject, first)(json);
}
/**
* Deletes the imported file from the storage and database.
* @param {number} tenantId
* @param {} importFile
*/
public async deleteImportFile(tenantId: number, importFile: any) {
const { Import } = this.tenancy.models(tenantId);
// Deletes the import row.
await Import.query().findById(importFile.id).delete();
// Deletes the imported file.
await fs.unlink(`public/imports/${importFile.filename}`);
}
}

View File

@@ -0,0 +1,101 @@
import { Service } from 'typedi';
import * as R from 'ramda';
import { isUndefined, get, chain } from 'lodash';
import { ImportMappingAttr, ResourceMetaFieldsMap } from './interfaces';
import { parseBoolean } from '@/utils';
import { trimObject } from './_utils';
@Service()
export class ImportFileDataTransformer {
/**
*
* @param {number} tenantId -
* @param {}
*/
public parseSheetData(
importFile: any,
importableFields: any,
data: Record<string, unknown>[]
) {
// Sanitize the sheet data.
const sanitizedData = this.sanitizeSheetData(data);
// Map the sheet columns key with the given map.
const mappedDTOs = this.mapSheetColumns(
sanitizedData,
importFile.mappingParsed
);
// Parse the mapped sheet values.
const parsedValues = this.parseExcelValues(importableFields, mappedDTOs);
return parsedValues;
}
/**
* Sanitizes the data in the imported sheet by trimming object keys.
* @param json - The JSON data representing the imported sheet.
* @returns {string[][]} - The sanitized data with trimmed object keys.
*/
public sanitizeSheetData(json) {
return R.compose(R.map(trimObject))(json);
}
/**
* Maps the columns of the imported data based on the provided mapping attributes.
* @param {Record<string, any>[]} body - The array of data objects to map.
* @param {ImportMappingAttr[]} map - The mapping attributes.
* @returns {Record<string, any>[]} - The mapped data objects.
*/
public mapSheetColumns(
body: Record<string, any>[],
map: ImportMappingAttr[]
): Record<string, any>[] {
return body.map((item) => {
const newItem = {};
map
.filter((mapping) => !isUndefined(item[mapping.from]))
.forEach((mapping) => {
newItem[mapping.to] = item[mapping.from];
});
return newItem;
});
}
/**
* Parses sheet values before passing to the service layer.
* @param {ResourceMetaFieldsMap} fields -
* @param {Record<string, any>} valueDTOS -
* @returns {Record<string, any>}
*/
public parseExcelValues(
fields: ResourceMetaFieldsMap,
valueDTOs: Record<string, any>[]
): Record<string, any> {
const parser = (value, key) => {
let _value = value;
// Parses the boolean value.
if (fields[key].fieldType === 'boolean') {
_value = parseBoolean(value, false);
// Parses the enumeration value.
} else if (fields[key].fieldType === 'enumeration') {
const field = fields[key];
const option = get(field, 'options', []).find(
(option) => option.label === value
);
_value = get(option, 'key');
// Prases the numeric value.
} else if (fields[key].fieldType === 'number') {
_value = parseFloat(value);
}
return _value;
};
return valueDTOs.map((DTO) => {
return chain(DTO)
.pickBy((value, key) => !isUndefined(fields[key]))
.mapValues(parser)
.value();
});
}
}

View File

@@ -0,0 +1,42 @@
import { Service } from 'typedi';
import { ImportInsertError, ResourceMetaFieldsMap } from './interfaces';
import { ERRORS, convertFieldsToYupValidation } from './_utils';
import { IModelMeta } from '@/interfaces';
import { ServiceError } from '@/exceptions';
@Service()
export class ImportFileDataValidator {
/**
* Validates the given resource is importable.
* @param {IModelMeta} resourceMeta
*/
public validateResourceImportable(resourceMeta: IModelMeta) {
// Throw service error if the resource does not support importing.
if (!resourceMeta.importable) {
throw new ServiceError(ERRORS.RESOURCE_NOT_IMPORTABLE);
}
}
/**
* Validates the given mapped DTOs and returns errors with their index.
* @param {Record<string, any>} mappedDTOs
* @returns {Promise<void | ImportInsertError[]>}
*/
public async validateData(
importableFields: ResourceMetaFieldsMap,
data: Record<string, any>
): Promise<void | ImportInsertError[]> {
const YupSchema = convertFieldsToYupValidation(importableFields);
const _data = { ...data };
try {
await YupSchema.validate(_data, { abortEarly: false });
} catch (validationError) {
const errors = validationError.inner.map((error) => ({
errorCode: 'ValidationError',
errorMessage: error.errors,
}));
throw errors;
}
}
}

View File

@@ -0,0 +1,145 @@
import { fromPairs } from 'lodash';
import { Inject, Service } from 'typedi';
import HasTenancyService from '../Tenancy/TenancyService';
import {
ImportDateFormats,
ImportFileMapPOJO,
ImportMappingAttr,
} from './interfaces';
import ResourceService from '../Resource/ResourceService';
import { ServiceError } from '@/exceptions';
import { ERRORS } from './_utils';
@Service()
export class ImportFileMapping {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private resource: ResourceService;
/**
* Mapping the excel sheet columns with resource columns.
* @param {number} tenantId
* @param {number} importId
* @param {ImportMappingAttr} maps
*/
public async mapping(
tenantId: number,
importId: number,
maps: ImportMappingAttr[]
): Promise<ImportFileMapPOJO> {
const { Import } = this.tenancy.models(tenantId);
const importFile = await Import.query()
.findOne('filename', importId)
.throwIfNotFound();
// Invalidate the from/to map attributes.
this.validateMapsAttrs(tenantId, importFile, maps);
// Validate the diplicated relations of map attrs.
this.validateDuplicatedMapAttrs(maps);
// Validate the date format mapping.
this.validateDateFormatMapping(tenantId, importFile.resource, maps);
const mappingStringified = JSON.stringify(maps);
await Import.query().findById(importFile.id).patch({
mapping: mappingStringified,
});
return {
import: {
importId: importFile.importId,
resource: importFile.resource,
},
};
}
/**
* Validate the mapping attributes.
* @param {number} tenantId -
* @param {} importFile -
* @param {ImportMappingAttr[]} maps
* @throws {ServiceError(ERRORS.INVALID_MAP_ATTRS)}
*/
private validateMapsAttrs(
tenantId: number,
importFile: any,
maps: ImportMappingAttr[]
) {
const fields = this.resource.getResourceImportableFields(
tenantId,
importFile.resource
);
const columnsMap = fromPairs(
importFile.columnsParsed.map((field) => [field, ''])
);
const invalid = [];
maps.forEach((map) => {
if (
'undefined' === typeof fields[map.to] ||
'undefined' === typeof columnsMap[map.from]
) {
invalid.push(map);
}
});
if (invalid.length > 0) {
throw new ServiceError(ERRORS.INVALID_MAP_ATTRS);
}
}
/**
* Validate the map attrs relation should be one-to-one relation only.
* @param {ImportMappingAttr[]} maps
*/
private validateDuplicatedMapAttrs(maps: ImportMappingAttr[]) {
const fromMap = {};
const toMap = {};
maps.forEach((map) => {
if (fromMap[map.from]) {
throw new ServiceError(ERRORS.DUPLICATED_FROM_MAP_ATTR);
} else {
fromMap[map.from] = true;
}
if (toMap[map.to]) {
throw new ServiceError(ERRORS.DUPLICATED_TO_MAP_ATTR);
} else {
toMap[map.to] = true;
}
});
}
/**
* Validates the date format mapping.
* @param {number} tenantId
* @param {string} resource
* @param {ImportMappingAttr[]} maps
*/
private validateDateFormatMapping(
tenantId: number,
resource: string,
maps: ImportMappingAttr[]
) {
const fields = this.resource.getResourceImportableFields(
tenantId,
resource
);
maps.forEach((map) => {
if (
typeof fields[map.to] !== 'undefined' &&
fields[map.to].fieldType === 'date'
) {
if (
typeof map.dateFormat !== 'undefined' &&
ImportDateFormats.indexOf(map.dateFormat) === -1
) {
throw new ServiceError(ERRORS.INVALID_MAP_DATE_FORMAT);
}
}
});
}
}

View File

@@ -0,0 +1,32 @@
import { Inject, Service } from 'typedi';
import HasTenancyService from '../Tenancy/TenancyService';
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
import { ImportFileMetaTransformer } from './ImportFileMetaTransformer';
@Service()
export class ImportFileMeta {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private transformer: TransformerInjectable;
/**
*
* @param {number} tenantId
* @param {number} importId
* @returns {}
*/
async getImportMeta(tenantId: number, importId: string) {
const { Import } = this.tenancy.models(tenantId);
const importFile = await Import.query().findOne('importId', importId);
// Retrieves the transformed accounts collection.
return this.transformer.transform(
tenantId,
importFile,
new ImportFileMetaTransformer()
);
}
}

View File

@@ -0,0 +1,19 @@
import { Transformer } from '@/lib/Transformer/Transformer';
export class ImportFileMetaTransformer extends Transformer {
/**
* Include these attributes to sale invoice object.
* @returns {Array}
*/
public includeAttributes = (): string[] => {
return ['map'];
};
public excludeAttributes = (): string[] => {
return ['id', 'filename', 'columns', 'mappingParsed', 'mapping'];
}
map(importFile) {
return importFile.mappingParsed;
}
}

View File

@@ -0,0 +1,34 @@
import { Inject, Service } from 'typedi';
import HasTenancyService from '../Tenancy/TenancyService';
import { ImportFilePreviewPOJO } from './interfaces';
import { ImportFileProcess } from './ImportFileProcess';
@Service()
export class ImportFilePreview {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private importFile: ImportFileProcess;
/**
* Preview the imported file results before commiting the transactions.
* @param {number} tenantId
* @param {number} importId
* @returns {Promise<ImportFilePreviewPOJO>}
*/
public async preview(
tenantId: number,
importId: number
): Promise<ImportFilePreviewPOJO> {
const knex = this.tenancy.knex(tenantId);
const trx = await knex.transaction({ isolationLevel: 'read uncommitted' });
const meta = await this.importFile.import(tenantId, importId, trx);
// Rollback the successed transaction.
await trx.rollback();
return meta;
}
}

View File

@@ -0,0 +1,96 @@
import { Inject, Service } from 'typedi';
import { chain } from 'lodash';
import { Knex } from 'knex';
import { ServiceError } from '@/exceptions';
import { ERRORS, getSheetColumns, getUnmappedSheetColumns } from './_utils';
import HasTenancyService from '../Tenancy/TenancyService';
import { ImportFileCommon } from './ImportFileCommon';
import { ImportFileDataTransformer } from './ImportFileDataTransformer';
import ResourceService from '../Resource/ResourceService';
import UnitOfWork from '../UnitOfWork';
import { ImportFilePreviewPOJO } from './interfaces';
@Service()
export class ImportFileProcess {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private resource: ResourceService;
@Inject()
private importCommon: ImportFileCommon;
@Inject()
private importParser: ImportFileDataTransformer;
@Inject()
private uow: UnitOfWork;
/**
* Preview the imported file results before commiting the transactions.
* @param {number} tenantId
* @param {number} importId
* @returns {Promise<ImportFilePreviewPOJO>}
*/
public async import(
tenantId: number,
importId: number,
trx?: Knex.Transaction
): Promise<ImportFilePreviewPOJO> {
const { Import } = this.tenancy.models(tenantId);
const importFile = await Import.query()
.findOne('importId', importId)
.throwIfNotFound();
// Throw error if the import file is not mapped yet.
if (!importFile.isMapped) {
throw new ServiceError(ERRORS.IMPORT_FILE_NOT_MAPPED);
}
// Read the imported file.
const buffer = await this.importCommon.readImportFile(importFile.filename);
const sheetData = this.importCommon.parseXlsxSheet(buffer);
const header = getSheetColumns(sheetData);
const importableFields = this.resource.getResourceImportableFields(
tenantId,
importFile.resource
);
// Prases the sheet json data.
const parsedData = this.importParser.parseSheetData(
importFile,
importableFields,
sheetData
);
// Runs the importing operation with ability to return errors that will happen.
const [successedImport, failedImport] = await this.uow.withTransaction(
tenantId,
(trx: Knex.Transaction) =>
this.importCommon.import(tenantId, importFile, parsedData, trx),
trx
);
const mapping = importFile.mappingParsed;
const errors = chain(failedImport)
.map((oper) => oper.error)
.flatten()
.value();
const unmappedColumns = getUnmappedSheetColumns(header, mapping);
const totalCount = parsedData.length;
const createdCount = successedImport.length;
const errorsCount = failedImport.length;
const skippedCount = errorsCount;
return {
createdCount,
skippedCount,
totalCount,
errorsCount,
errors,
unmappedColumns: unmappedColumns,
unmappedColumnsCount: unmappedColumns.length,
};
}
}

View File

@@ -0,0 +1,111 @@
import { Inject, Service } from 'typedi';
import HasTenancyService from '../Tenancy/TenancyService';
import { sanitizeResourceName } from './_utils';
import ResourceService from '../Resource/ResourceService';
import { IModelMetaField } from '@/interfaces';
import { ImportFileCommon } from './ImportFileCommon';
import { ImportFileDataValidator } from './ImportFileDataValidator';
import { ImportFileUploadPOJO } from './interfaces';
import { ServiceError } from '@/exceptions';
@Service()
export class ImportFileUploadService {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private resourceService: ResourceService;
@Inject()
private importFileCommon: ImportFileCommon;
@Inject()
private importValidator: ImportFileDataValidator;
/**
* Reads the imported file and stores the import file meta under unqiue id.
* @param {number} tenantId - Tenant id.
* @param {string} resource - Resource name.
* @param {string} filePath - File path.
* @param {string} fileName - File name.
* @returns {Promise<ImportFileUploadPOJO>}
*/
public async import(
tenantId: number,
resourceName: string,
filename: string,
params: Record<string, number | string>
): Promise<ImportFileUploadPOJO> {
const { Import } = this.tenancy.models(tenantId);
const resource = sanitizeResourceName(resourceName);
const resourceMeta = this.resourceService.getResourceMeta(
tenantId,
resource
);
// Throw service error if the resource does not support importing.
this.importValidator.validateResourceImportable(resourceMeta);
// Reads the imported file into buffer.
const buffer = await this.importFileCommon.readImportFile(filename);
// Parse the buffer file to array data.
const sheetData = this.importFileCommon.parseXlsxSheet(buffer);
const sheetColumns = this.importFileCommon.parseSheetColumns(sheetData);
const coumnsStringified = JSON.stringify(sheetColumns);
try {
// Validates the params Yup schema.
await this.importFileCommon.validateParamsSchema(resource, params);
// Validates importable params asyncly.
await this.importFileCommon.validateParams(tenantId, resource, params);
} catch (error) {
throw error;
}
const _params = this.importFileCommon.transformParams(resource, params);
const paramsStringified = JSON.stringify(_params);
// Store the import model with related metadata.
const importFile = await Import.query().insert({
filename,
resource,
importId: filename,
columns: coumnsStringified,
params: paramsStringified,
});
const resourceColumnsMap = this.resourceService.getResourceImportableFields(
tenantId,
resource
);
const resourceColumns = this.getResourceColumns(resourceColumnsMap);
return {
import: {
importId: importFile.importId,
resource: importFile.resource,
},
sheetColumns,
resourceColumns,
};
}
getResourceColumns(resourceColumns: { [key: string]: IModelMetaField }) {
return Object.entries(resourceColumns)
.map(
([key, { name, importHint, required, order }]: [
string,
IModelMetaField
]) => ({
key,
name,
required,
hint: importHint,
order,
})
)
.sort((a, b) =>
a.order && b.order ? a.order - b.order : a.order ? -1 : b.order ? 1 : 0
);
}
}

View File

@@ -0,0 +1,102 @@
import { Inject } from 'typedi';
import { ImportFileUploadService } from './ImportFileUpload';
import { ImportFileMapping } from './ImportFileMapping';
import { ImportMappingAttr } from './interfaces';
import { ImportFileProcess } from './ImportFileProcess';
import { ImportFilePreview } from './ImportFilePreview';
import { ImportSampleService } from './ImportSample';
import { ImportFileMeta } from './ImportFileMeta';
@Inject()
export class ImportResourceApplication {
@Inject()
private importFileService: ImportFileUploadService;
@Inject()
private importMappingService: ImportFileMapping;
@Inject()
private importProcessService: ImportFileProcess;
@Inject()
private ImportFilePreviewService: ImportFilePreview;
@Inject()
private importSampleService: ImportSampleService;
@Inject()
private importMetaService: ImportFileMeta;
/**
* Reads the imported file and stores the import file meta under unqiue id.
* @param {number} tenantId -
* @param {string} resource - Resource name.
* @param {string} fileName - File name.
* @returns {Promise<ImportFileUploadPOJO>}
*/
public async import(
tenantId: number,
resource: string,
filename: string,
params: Record<string, any>
) {
return this.importFileService.import(tenantId, resource, filename, params);
}
/**
* Mapping the excel sheet columns with resource columns.
* @param {number} tenantId
* @param {number} importId - Import id.
* @param {ImportMappingAttr} maps
*/
public async mapping(
tenantId: number,
importId: number,
maps: ImportMappingAttr[]
) {
return this.importMappingService.mapping(tenantId, importId, maps);
}
/**
* Preview the mapped results before process importing.
* @param {number} tenantId
* @param {number} importId - Import id.
* @returns {Promise<ImportFilePreviewPOJO>}
*/
public async preview(tenantId: number, importId: number) {
return this.ImportFilePreviewService.preview(tenantId, importId);
}
/**
* Process the import file sheet through service for creating entities.
* @param {number} tenantId
* @param {number} importId
* @returns {Promise<ImportFilePreviewPOJO>}
*/
public async process(tenantId: number, importId: number) {
return this.importProcessService.import(tenantId, importId);
}
/**
* Retrieves the import meta of the given import id.
* @param {number} tenantId -
* @param {string} importId - Import id.
* @returns {}
*/
public importMeta(tenantId: number, importId: string) {
return this.importMetaService.getImportMeta(tenantId, importId);
}
/**
* Retrieves the csv/xlsx sample sheet of the given
* @param {number} tenantId
* @param {number} resource - Resource name.
*/
public sample(
tenantId: number,
resource: string,
format: 'csv' | 'xlsx' = 'csv'
) {
return this.importSampleService.sample(tenantId, resource, format);
}
}

View File

@@ -0,0 +1,46 @@
import XLSX from 'xlsx';
import { Inject, Service } from 'typedi';
import { ImportableResources } from './ImportableResources';
import { sanitizeResourceName } from './_utils';
@Service()
export class ImportSampleService {
@Inject()
private importable: ImportableResources;
/**
* Retrieves the sample sheet of the given resource.
* @param {number} tenantId
* @param {string} resource
* @param {string} format
* @returns {Buffer | string}
*/
public sample(
tenantId: number,
resource: string,
format: 'csv' | 'xlsx'
): Buffer | string {
const _resource = sanitizeResourceName(resource);
const ImportableRegistry = this.importable.registry;
const importable = ImportableRegistry.getImportable(_resource);
const data = importable.sampleData();
const workbook = XLSX.utils.book_new();
const worksheet = XLSX.utils.json_to_sheet(data);
XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
// Determine the output format
if (format === 'csv') {
const csvOutput = XLSX.utils.sheet_to_csv(worksheet);
return csvOutput;
} else {
const xlsxOutput = XLSX.write(workbook, {
bookType: 'xlsx',
type: 'buffer',
});
return xlsxOutput;
}
}
}

View File

@@ -0,0 +1,72 @@
import { Knex } from 'knex';
import * as Yup from 'yup';
import { ImportableContext } from './interfaces';
export abstract class Importable {
/**
*
* @param {number} tenantId
* @param {any} createDTO
* @param {Knex.Transaction} trx
*/
public importable(tenantId: number, createDTO: any, trx?: Knex.Transaction) {
throw new Error(
'The `importable` function is not defined in service importable.'
);
}
/**
* Transformes the DTO before passing it to importable and validation.
* @param {Record<string, any>} createDTO
* @param {ImportableContext} context
* @returns {Record<string, any>}
*/
public transform(createDTO: Record<string, any>, context: ImportableContext) {
return createDTO;
}
/**
* Concurrency controlling of the importing process.
* @returns {number}
*/
public get concurrency() {
return 10;
}
/**
* Retrieves the sample data of importable.
* @returns {Array<any>}
*/
public sampleData(): Array<any> {
return [];
}
// ------------------
// # Params
// ------------------
/**
* Params Yup validation schema.
* @returns {Yup.ObjectSchema<object, object>}
*/
public paramsValidationSchema(): Yup.ObjectSchema<object, object> {
return Yup.object().nullable();
}
/**
* Validates the params of the importable service.
* @param {Record<string, any>}
* @returns {Promise<boolean>} - True means passed and false failed.
*/
public async validateParams(
tenantId: number,
params: Record<string, any>
): Promise<void> {}
/**
* Transformes the import params before storing them.
* @param {Record<string, any>} parmas
*/
public transformParams(parmas: Record<string, any>) {
return parmas;
}
}

View File

@@ -0,0 +1,46 @@
import { camelCase, upperFirst } from 'lodash';
import { Importable } from './Importable';
export class ImportableRegistry {
private static instance: ImportableRegistry;
private importables: Record<string, Importable>;
private constructor() {
this.importables = {};
}
/**
* Gets singleton instance of registry.
* @returns {ImportableRegistry}
*/
public static getInstance(): ImportableRegistry {
if (!ImportableRegistry.instance) {
ImportableRegistry.instance = new ImportableRegistry();
}
return ImportableRegistry.instance;
}
/**
* Registers the given importable service.
* @param {string} resource
* @param {Importable} importable
*/
public registerImportable(resource: string, importable: Importable): void {
const _resource = this.sanitizeResourceName(resource);
this.importables[_resource] = importable;
}
/**
* Retrieves the importable service instance of the given resource name.
* @param {string} name
* @returns {Importable}
*/
public getImportable(name: string): Importable {
const _name = this.sanitizeResourceName(name);
return this.importables[_name];
}
private sanitizeResourceName(resource: string) {
return upperFirst(camelCase(resource));
}
}

View File

@@ -0,0 +1,47 @@
import Container, { Service } from 'typedi';
import { AccountsImportable } from '../Accounts/AccountsImportable';
import { ImportableRegistry } from './ImportableRegistry';
import { UncategorizedTransactionsImportable } from '../Cashflow/UncategorizedTransactionsImportable';
import { CustomersImportable } from '../Contacts/Customers/CustomersImportable';
import { VendorsImportable } from '../Contacts/Vendors/VendorsImportable';
@Service()
export class ImportableResources {
private static registry: ImportableRegistry;
constructor() {
this.boot();
}
/**
* Importable instances.
*/
private importables = [
{ resource: 'Account', importable: AccountsImportable },
{
resource: 'UncategorizedCashflowTransaction',
importable: UncategorizedTransactionsImportable,
},
{ resource: 'Customer', importable: CustomersImportable },
{ resource: 'Vendor', importable: VendorsImportable },
];
public get registry() {
return ImportableResources.registry;
}
/**
* Boots all the registered importables.
*/
public boot() {
if (!ImportableResources.registry) {
const instance = ImportableRegistry.getInstance();
this.importables.forEach((importable) => {
const importableInstance = Container.get(importable.importable);
instance.registerImportable(importable.resource, importableInstance);
});
ImportableResources.registry = instance;
}
}
}

View File

@@ -0,0 +1,103 @@
import * as Yup from 'yup';
import { upperFirst, camelCase, first, isUndefined } from 'lodash';
import pluralize from 'pluralize';
import { ResourceMetaFieldsMap } from './interfaces';
import { IModelMetaField } from '@/interfaces';
import moment from 'moment';
export const ERRORS = {
RESOURCE_NOT_IMPORTABLE: 'RESOURCE_NOT_IMPORTABLE',
INVALID_MAP_ATTRS: 'INVALID_MAP_ATTRS',
DUPLICATED_FROM_MAP_ATTR: 'DUPLICATED_FROM_MAP_ATTR',
DUPLICATED_TO_MAP_ATTR: 'DUPLICATED_TO_MAP_ATTR',
IMPORT_FILE_NOT_MAPPED: 'IMPORT_FILE_NOT_MAPPED',
INVALID_MAP_DATE_FORMAT: 'INVALID_MAP_DATE_FORMAT',
MAP_DATE_FORMAT_NOT_DEFINED: 'MAP_DATE_FORMAT_NOT_DEFINED',
};
export function trimObject(obj) {
return Object.entries(obj).reduce((acc, [key, value]) => {
// Trim the key
const trimmedKey = key.trim();
// Trim the value if it's a string, otherwise leave it as is
const trimmedValue = typeof value === 'string' ? value.trim() : value;
// Assign the trimmed key and value to the accumulator object
return { ...acc, [trimmedKey]: trimmedValue };
}, {});
}
export const convertFieldsToYupValidation = (fields: ResourceMetaFieldsMap) => {
const yupSchema = {};
Object.keys(fields).forEach((fieldName: string) => {
const field = fields[fieldName] as IModelMetaField;
let fieldSchema;
fieldSchema = Yup.string().label(field.name);
if (field.fieldType === 'text') {
if (!isUndefined(field.minLength)) {
fieldSchema = fieldSchema.min(
field.minLength,
`Minimum length is ${field.minLength} characters`
);
}
if (!isUndefined(field.maxLength)) {
fieldSchema = fieldSchema.max(
field.maxLength,
`Maximum length is ${field.maxLength} characters`
);
}
} else if (field.fieldType === 'number') {
fieldSchema = Yup.number().label(field.name);
if (!isUndefined(field.max)) {
fieldSchema = fieldSchema.max(field.max);
}
if (!isUndefined(field.min)) {
fieldSchema = fieldSchema.min(field.min);
}
} else if (field.fieldType === 'boolean') {
fieldSchema = Yup.boolean().label(field.name);
} else if (field.fieldType === 'enumeration') {
const options = field.options.reduce((acc, option) => {
acc[option.key] = option.label;
return acc;
}, {});
fieldSchema = Yup.string().oneOf(Object.keys(options)).label(field.name);
// Validate date field type.
} else if (field.fieldType === 'date') {
fieldSchema = fieldSchema.test(
'date validation',
'Invalid date or format. The string should be a valid YYYY-MM-DD format.',
(val) => {
if (!val) {
return true;
}
return moment(val, 'YYYY-MM-DD', true).isValid();
}
);
} else if (field.fieldType === 'url') {
fieldSchema = fieldSchema.url();
}
if (field.required) {
fieldSchema = fieldSchema.required();
}
yupSchema[fieldName] = fieldSchema;
});
return Yup.object().shape(yupSchema);
};
export const getUnmappedSheetColumns = (columns, mapping) => {
return columns.filter(
(column) => !mapping.some((map) => map.from === column)
);
};
export const sanitizeResourceName = (resourceName: string) => {
return upperFirst(camelCase(pluralize.singular(resourceName)));
};
export const getSheetColumns = (sheetData: unknown[]) => {
return Object.keys(first(sheetData));
};

View File

@@ -0,0 +1,76 @@
import { IModelMetaField } from '@/interfaces';
import Import from '@/models/Import';
export interface ImportMappingAttr {
from: string;
to: string;
dateFormat?: string;
}
export interface ImportValidationError {
index: number;
property: string;
constraints: Record<string, string>;
}
export type ResourceMetaFieldsMap = { [key: string]: IModelMetaField };
export interface ImportInsertError {
rowNumber: number;
errorCode: string;
errorMessage: string;
}
export interface ImportFileUploadPOJO {
import: {
importId: string;
resource: string;
};
sheetColumns: string[];
resourceColumns: {
key: string;
name: string;
required?: boolean;
hint?: string;
}[];
}
export interface ImportFileMapPOJO {
import: {
importId: string;
resource: string;
};
}
export interface ImportFilePreviewPOJO {
createdCount: number;
skippedCount: number;
totalCount: number;
errorsCount: number;
errors: ImportInsertError[];
unmappedColumns: string[];
unmappedColumnsCount: number;
}
export interface ImportOperSuccess {
data: unknown;
index: number;
}
export interface ImportOperError {
error: ImportInsertError;
index: number;
}
export interface ImportableContext {
import: Import,
rowIndex: number;
}
export const ImportDateFormats = [
'yyyy-MM-dd',
'dd.MM.yy',
'MM/dd/yy',
'dd/MMM/yyyy'
]

View File

@@ -21,25 +21,19 @@ import { ERRORS } from './constants';
@Service()
export default class OrganizationService {
@Inject()
eventPublisher: EventPublisher;
@Inject('logger')
logger: any;
@Inject('repositories')
sysRepositories: any;
private eventPublisher: EventPublisher;
@Inject()
tenantsManager: TenantsManager;
private tenantsManager: TenantsManager;
@Inject('agenda')
agenda: any;
private agenda: any;
@Inject()
baseCurrencyMutateLocking: OrganizationBaseCurrencyLocking;
private baseCurrencyMutateLocking: OrganizationBaseCurrencyLocking;
@Inject()
tenancy: HasTenancyService;
private tenancy: HasTenancyService;
/**
* Builds the database schema and seed data of the given organization id.

View File

@@ -1,12 +1,11 @@
import { Service, Inject } from 'typedi';
import { camelCase, upperFirst } from 'lodash';
import { camelCase, upperFirst, pickBy } from 'lodash';
import * as qim from 'qim';
import pluralize from 'pluralize';
import { IModelMeta } from '@/interfaces';
import { IModelMeta, IModelMetaField } from '@/interfaces';
import TenancyService from '@/services/Tenancy/TenancyService';
import { ServiceError } from '@/exceptions';
import I18nService from '@/services/I18n/I18nService';
import { tenantKnexConfig } from 'config/knexConfig';
const ERRORS = {
RESOURCE_MODEL_NOT_FOUND: 'RESOURCE_MODEL_NOT_FOUND',
@@ -24,7 +23,7 @@ export default class ResourceService {
* Transform resource to model name.
* @param {string} resourceName
*/
private resourceToModelName(resourceName: string): string {
public resourceToModelName(resourceName: string): string {
return upperFirst(camelCase(pluralize.singular(resourceName)));
}
@@ -63,6 +62,33 @@ export default class ResourceService {
return this.getResourceMetaLocalized(resourceMeta, tenantId);
}
/**
*
*/
public getResourceFields(
tenantId: number,
modelName: string
): { [key: string]: IModelMetaField } {
const meta = this.getResourceMeta(tenantId, modelName);
return meta.fields;
}
/**
*
* @param {number} tenantId
* @param {string} modelName
* @returns
*/
public getResourceImportableFields(
tenantId: number,
modelName: string
): { [key: string]: IModelMetaField } {
const fields = this.getResourceFields(tenantId, modelName);
return pickBy(fields, (field) => field.importable);
}
/**
* Retrieve the resource meta localized based on the current user language.
*/

View File

@@ -1,5 +1,6 @@
import { Service, Inject } from 'typedi';
import TenancyService from '@/services/Tenancy/TenancyService';
import { Transaction } from 'objection';
/**
* Enumeration that represents transaction isolation levels for use with the {@link Transactional} annotation
@@ -38,18 +39,26 @@ export default class UnitOfWork {
public withTransaction = async (
tenantId: number,
work,
trx?: Transaction,
isolationLevel: IsolationLevel = IsolationLevel.READ_UNCOMMITTED
) => {
const knex = this.tenancy.knex(tenantId);
const trx = await knex.transaction({ isolationLevel });
let _trx = trx;
if (!_trx) {
_trx = await knex.transaction({ isolationLevel });
}
try {
const result = await work(trx);
trx.commit();
const result = await work(_trx);
if (!trx) {
_trx.commit();
}
return result;
} catch (error) {
trx.rollback();
if (!trx) {
_trx.rollback();
}
throw error;
}
};

View File

@@ -392,6 +392,15 @@ export default {
onTransactionDeleting: 'onCashflowTransactionDeleting',
onTransactionDeleted: 'onCashflowTransactionDeleted',
onTransactionCategorizing: 'onTransactionCategorizing',
onTransactionCategorized: 'onCashflowTransactionCategorized',
onTransactionUncategorizing: 'onTransactionUncategorizing',
onTransactionUncategorized: 'onTransactionUncategorized',
onTransactionCategorizingAsExpense: 'onTransactionCategorizingAsExpense',
onTransactionCategorizedAsExpense: 'onTransactionCategorizedAsExpense',
},
/**

View File

@@ -0,0 +1,7 @@
export const parseJsonSafe = (value: string) => {
try {
return JSON.parse(value);
} catch {
return null;
}
};

View File

@@ -20,11 +20,11 @@
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.4.0",
"@testing-library/user-event": "^7.2.1",
"@tiptap/extension-color": "latest",
"@tiptap/extension-text-style": "2.1.13",
"@tiptap/core": "2.1.13",
"@tiptap/pm": "2.1.13",
"@tiptap/extension-color": "latest",
"@tiptap/extension-list-item": "2.1.13",
"@tiptap/extension-text-style": "2.1.13",
"@tiptap/pm": "2.1.13",
"@tiptap/react": "2.1.13",
"@tiptap/starter-kit": "2.1.13",
"@types/jest": "^26.0.15",
@@ -38,9 +38,9 @@
"@types/react-redux": "^7.1.24",
"@types/react-router-dom": "^5.3.3",
"@types/react-transition-group": "^4.4.5",
"@types/socket.io-client": "^3.0.0",
"@types/styled-components": "^5.1.25",
"@types/yup": "^0.29.13",
"@types/socket.io-client": "^3.0.0",
"@typescript-eslint/eslint-plugin": "^2.10.0",
"@typescript-eslint/parser": "^2.10.0",
"@welldone-software/why-did-you-render": "^6.0.0-rc.1",
@@ -69,10 +69,9 @@
"moment": "^2.24.0",
"moment-timezone": "^0.5.33",
"path-browserify": "^1.0.1",
"prop-types": "15.8.1",
"plaid": "^9.3.0",
"plaid-threads": "^11.4.3",
"react-plaid-link": "^3.2.1",
"prop-types": "15.8.1",
"query-string": "^7.1.1",
"ramda": "^0.27.1",
"react": "^18.2.0",
@@ -82,11 +81,13 @@
"react-dev-utils": "^11.0.4",
"react-dom": "^18.2.0",
"react-dropzone": "^11.0.1",
"react-dropzone-esm": "^15.0.1",
"react-error-boundary": "^3.0.2",
"react-error-overlay": "^6.0.9",
"react-hotkeys-hook": "^3.0.3",
"react-intl-universal": "^2.4.7",
"react-loadable": "^5.5.0",
"react-plaid-link": "^3.2.1",
"react-query": "^3.6.0",
"react-query-devtools": "^2.1.1",
"react-redux": "^7.2.9",
@@ -112,10 +113,10 @@
"rtl-detect": "^1.0.3",
"sass": "^1.68.0",
"semver": "6.3.0",
"socket.io-client": "^4.7.4",
"style-loader": "0.23.1",
"styled-components": "^5.3.1",
"stylis-rtlcss": "^2.1.1",
"socket.io-client": "^4.7.4",
"typescript": "^4.8.3",
"yup": "^0.28.1"
},

View File

@@ -0,0 +1,111 @@
// @ts-nocheck
import React from 'react';
import styled from 'styled-components';
import { useUncontrolled } from '@/hooks/useUncontrolled';
const ContentTabsRoot = styled('div')`
display: flex;
gap: 10px;
`;
interface ContentTabItemRootProps {
active?: boolean;
}
const ContentTabItemRoot = styled.button<ContentTabItemRootProps>`
flex: 1 0;
background: #fff;
border: 1px solid #e1e2e8;
border-radius: 5px;
padding: 11px;
text-align: left;
cursor: pointer;
${(props) =>
props.active &&
`
border-color: #1552c8;
box-shadow: 0 0 0 0.25px #1552c8;
${ContentTabTitle} {
color: #1552c8;
font-weight: 500;
}
${ContentTabDesc} {
color: #1552c8;
}
`}
&:hover,
&:active {
border-color: #1552c8;
}
`;
const ContentTabTitle = styled('h3')`
font-size: 14px;
font-weight: 400;
color: #2f343c;
`;
const ContentTabDesc = styled('p')`
margin: 0;
color: #5f6b7c;
margin-top: 4px;
font-size: 12px;
`;
interface ContentTabsItemProps {
id: string;
title?: React.ReactNode;
description?: React.ReactNode;
active?: boolean;
}
const ContentTabsItem = ({
title,
description,
active,
onClick,
}: ContentTabsItemProps) => {
return (
<ContentTabItemRoot active={active} onClick={onClick}>
<ContentTabTitle>{title}</ContentTabTitle>
<ContentTabDesc>{description}</ContentTabDesc>
</ContentTabItemRoot>
);
};
interface ContentTabsProps {
initialValue?: string;
value?: string;
onChange?: (value: string) => void;
children?: React.ReactNode;
className?: string;
}
export function ContentTabs({
initialValue,
value,
onChange,
children,
className,
}: ContentTabsProps) {
const [localValue, handleItemChange] = useUncontrolled<string>({
initialValue,
value,
onChange,
finalValue: '',
});
const tabs = React.Children.toArray(children);
return (
<ContentTabsRoot className={className}>
{tabs.map((tab) => (
<ContentTabsItem
key={tab.key}
{...tab.props}
active={localValue === tab.props.id}
onClick={() => handleItemChange(tab.props?.id)}
/>
))}
</ContentTabsRoot>
);
}
ContentTabs.Tab = ContentTabsItem;

View File

@@ -0,0 +1 @@
export * from './ContentTabs';

View File

@@ -1,5 +1,6 @@
// @ts-nocheck
import React from 'react';
import clsx from 'classnames';
import { Classes } from '@blueprintjs/core';
import { LoadingIndicator } from '../Indicator';
@@ -11,8 +12,8 @@ export function DrawerLoading({ loading, mount = false, children }) {
);
}
export function DrawerBody({ children }) {
return <div className={Classes.DRAWER_BODY}>{children}</div>;
export function DrawerBody({ children, className }) {
return <div className={clsx(Classes.DRAWER_BODY, className)}>{children}</div>;
}
export * from './DrawerActionsBar';

View File

@@ -23,6 +23,7 @@ import WarehouseTransferDetailDrawer from '@/containers/Drawers/WarehouseTransfe
import TaxRateDetailsDrawer from '@/containers/TaxRates/drawers/TaxRateDetailsDrawer/TaxRateDetailsDrawer';
import { DRAWERS } from '@/constants/drawers';
import CategorizeTransactionDrawer from '@/containers/CashFlow/CategorizeTransaction/drawers/CategorizeTransactionDrawer/CategorizeTransactionDrawer';
/**
* Drawers container of the dashboard.
@@ -61,6 +62,7 @@ export default function DrawersContainer() {
name={DRAWERS.WAREHOUSE_TRANSFER_DETAILS}
/>
<TaxRateDetailsDrawer name={DRAWERS.TAX_RATE_DETAILS} />
<CategorizeTransactionDrawer name={DRAWERS.CATEGORIZE_TRANSACTION} />
</div>
);
}

View File

@@ -0,0 +1,12 @@
.root {
padding: 20px;
border: 2px dotted #c5cbd3;
border-radius: 6px;
min-height: 200px;
display: flex;
flex-direction: column;
background: #fff;
position: relative;
}

View File

@@ -0,0 +1,291 @@
// @ts-nocheck
import React from 'react';
import { Ref, useCallback } from 'react';
import clsx from 'classnames';
import {
Accept,
DropEvent,
FileError,
FileRejection,
FileWithPath,
useDropzone,
} from 'react-dropzone-esm';
import { DropzoneProvider } from './DropzoneProvider';
import { DropzoneAccept, DropzoneIdle, DropzoneReject } from './DropzoneStatus';
import { Box } from '../Layout';
import styles from './Dropzone.module.css';
import { CloudLoadingIndicator } from '../Indicator';
export type DropzoneStylesNames = 'root' | 'inner';
export type DropzoneVariant = 'filled' | 'light';
export type DropzoneCssVariables = {
root:
| '--dropzone-radius'
| '--dropzone-accept-color'
| '--dropzone-accept-bg'
| '--dropzone-reject-color'
| '--dropzone-reject-bg';
};
export interface DropzoneProps {
/** Key of `theme.colors` or any valid CSS color to set colors of `Dropzone.Accept`, `theme.primaryColor` by default */
acceptColor?: MantineColor;
/** Key of `theme.colors` or any valid CSS color to set colors of `Dropzone.Reject`, `'red'` by default */
rejectColor?: MantineColor;
/** Key of `theme.radius` or any valid CSS value to set `border-radius`, numbers are converted to rem, `theme.defaultRadius` by default */
radius?: MantineRadius;
/** Determines whether files capturing should be disabled, `false` by default */
disabled?: boolean;
/** Called when any files are dropped to the dropzone */
onDropAny?: (files: FileWithPath[], fileRejections: FileRejection[]) => void;
/** Called when valid files are dropped to the dropzone */
onDrop: (files: FileWithPath[]) => void;
/** Called when dropped files do not meet file restrictions */
onReject?: (fileRejections: FileRejection[]) => void;
/** Determines whether a loading overlay should be displayed over the dropzone, `false` by default */
loading?: boolean;
/** Mime types of the files that dropzone can accepts. By default, dropzone accepts all file types. */
accept?: Accept | string[];
/** A ref function which when called opens the file system file picker */
openRef?: React.ForwardedRef<() => void | undefined>;
/** Determines whether multiple files can be dropped to the dropzone or selected from file system picker, `true` by default */
multiple?: boolean;
/** Maximum file size in bytes */
maxSize?: number;
/** Name of the form control. Submitted with the form as part of a name/value pair. */
name?: string;
/** Maximum number of files that can be picked at once */
maxFiles?: number;
/** Set to autofocus the root element */
autoFocus?: boolean;
/** If `false`, disables click to open the native file selection dialog */
activateOnClick?: boolean;
/** If `false`, disables drag 'n' drop */
activateOnDrag?: boolean;
/** If `false`, disables Space/Enter to open the native file selection dialog. Note that it also stops tracking the focus state. */
activateOnKeyboard?: boolean;
/** If `false`, stops drag event propagation to parents */
dragEventsBubbling?: boolean;
/** Called when the `dragenter` event occurs */
onDragEnter?: (event: React.DragEvent<HTMLElement>) => void;
/** Called when the `dragleave` event occurs */
onDragLeave?: (event: React.DragEvent<HTMLElement>) => void;
/** Called when the `dragover` event occurs */
onDragOver?: (event: React.DragEvent<HTMLElement>) => void;
/** Called when user closes the file selection dialog with no selection */
onFileDialogCancel?: () => void;
/** Called when user opens the file selection dialog */
onFileDialogOpen?: () => void;
/** If `false`, allow dropped items to take over the current browser window */
preventDropOnDocument?: boolean;
/** Set to true to use the File System Access API to open the file picker instead of using an <input type="file"> click event, defaults to true */
useFsAccessApi?: boolean;
/** Use this to provide a custom file aggregator */
getFilesFromEvent?: (
event: DropEvent,
) => Promise<Array<File | DataTransferItem>>;
/** Custom validation function. It must return null if there's no errors. */
validator?: <T extends File>(file: T) => FileError | FileError[] | null;
/** Determines whether pointer events should be enabled on the inner element, `false` by default */
enablePointerEvents?: boolean;
/** Props passed down to the Loader component */
loaderProps?: LoaderProps;
/** Props passed down to the internal Input component */
inputProps?: React.InputHTMLAttributes<HTMLInputElement>;
}
export type DropzoneFactory = Factory<{
props: DropzoneProps;
ref: HTMLDivElement;
stylesNames: DropzoneStylesNames;
vars: DropzoneCssVariables;
staticComponents: {
Accept: typeof DropzoneAccept;
Idle: typeof DropzoneIdle;
Reject: typeof DropzoneReject;
};
}>;
const defaultProps: Partial<DropzoneProps> = {
loading: false,
multiple: true,
maxSize: Infinity,
autoFocus: false,
activateOnClick: true,
activateOnDrag: true,
dragEventsBubbling: true,
activateOnKeyboard: true,
useFsAccessApi: true,
variant: 'light',
rejectColor: 'red',
};
export const Dropzone = (_props: DropzoneProps) => {
const {
// classNames,
// className,
// style,
// styles,
// unstyled,
// vars,
radius,
disabled,
loading,
multiple,
maxSize,
accept,
children,
onDropAny,
onDrop,
onReject,
openRef,
name,
maxFiles,
autoFocus,
activateOnClick,
activateOnDrag,
dragEventsBubbling,
activateOnKeyboard,
onDragEnter,
onDragLeave,
onDragOver,
onFileDialogCancel,
onFileDialogOpen,
preventDropOnDocument,
useFsAccessApi,
getFilesFromEvent,
validator,
rejectColor,
acceptColor,
enablePointerEvents,
loaderProps,
inputProps,
// mod,
classNames,
...others
} = {
...defaultProps,
..._props,
};
const { getRootProps, getInputProps, isDragAccept, isDragReject, open } =
useDropzone({
onDrop: onDropAny,
onDropAccepted: onDrop,
onDropRejected: onReject,
disabled: disabled || loading,
accept: Array.isArray(accept)
? accept.reduce((r, key) => ({ ...r, [key]: [] }), {})
: accept,
multiple,
maxSize,
maxFiles,
autoFocus,
noClick: !activateOnClick,
noDrag: !activateOnDrag,
noDragEventsBubbling: !dragEventsBubbling,
noKeyboard: !activateOnKeyboard,
onDragEnter,
onDragLeave,
onDragOver,
onFileDialogCancel,
onFileDialogOpen,
preventDropOnDocument,
useFsAccessApi,
validator,
...(getFilesFromEvent ? { getFilesFromEvent } : null),
});
const isIdle = !isDragAccept && !isDragReject;
assignRef(openRef, open);
return (
<DropzoneProvider
value={{ accept: isDragAccept, reject: isDragReject, idle: isIdle }}
>
<Box
{...getRootProps({
className: clsx(styles.root, classNames?.root),
})}
// {...getStyles('root', { focusable: true })}
{...others}
mod={[
{
accept: isDragAccept,
reject: isDragReject,
idle: isIdle,
loading,
'activate-on-click': activateOnClick,
},
// mod,
]}
>
<input {...getInputProps(inputProps)} name={name} />
<div
data-enable-pointer-events={enablePointerEvents || undefined}
className={classNames?.content}
>
{children}
</div>
</Box>
</DropzoneProvider>
);
};
Dropzone.displayName = '@mantine/dropzone/Dropzone';
Dropzone.Accept = DropzoneAccept;
Dropzone.Idle = DropzoneIdle;
Dropzone.Reject = DropzoneReject;
type PossibleRef<T> = Ref<T> | undefined;
export function assignRef<T>(ref: PossibleRef<T>, value: T) {
if (typeof ref === 'function') {
ref(value);
} else if (typeof ref === 'object' && ref !== null && 'current' in ref) {
(ref as React.MutableRefObject<T>).current = value;
}
}
export function mergeRefs<T>(...refs: PossibleRef<T>[]) {
return (node: T | null) => {
refs.forEach((ref) => assignRef(ref, node));
};
}
export function useMergedRef<T>(...refs: PossibleRef<T>[]) {
return useCallback(mergeRefs(...refs), refs);
}

View File

@@ -0,0 +1,12 @@
import { createSafeContext } from './create-safe-context';
export interface DropzoneContextValue {
idle: boolean;
accept: boolean;
reject: boolean;
}
export const [DropzoneProvider, useDropzoneContext] =
createSafeContext<DropzoneContextValue>(
'Dropzone component was not found in tree',
);

View File

@@ -0,0 +1,36 @@
import React, { cloneElement } from 'react';
import { upperFirst } from 'lodash';
import { DropzoneContextValue, useDropzoneContext } from './DropzoneProvider';
import { isElement } from '@/utils/is-element';
export interface DropzoneStatusProps {
children: React.ReactNode;
}
type DropzoneStatusComponent = React.FC<DropzoneStatusProps>;
function createDropzoneStatus(status: keyof DropzoneContextValue) {
const Component: DropzoneStatusComponent = (props) => {
const { children, ...others } = props;
const ctx = useDropzoneContext();
const _children = isElement(children) ? children : <span>{children}</span>;
if (ctx[status]) {
return cloneElement(_children as JSX.Element, others);
}
return null;
};
Component.displayName = `@bigcapital/core/dropzone/${upperFirst(status)}`;
return Component;
}
export const DropzoneAccept = createDropzoneStatus('accept');
export const DropzoneReject = createDropzoneStatus('reject');
export const DropzoneIdle = createDropzoneStatus('idle');
export type DropzoneAcceptProps = DropzoneStatusProps;
export type DropzoneRejectProps = DropzoneStatusProps;
export type DropzoneIdleProps = DropzoneStatusProps;

Some files were not shown because too many files have changed in this diff Show More