mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-22 15:50:32 +00:00
Compare commits
1 Commits
reconcile-
...
BIG-213
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
882fc20ac1 |
@@ -1,49 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { NextFunction, Request, Response, Router } from 'express';
|
|
||||||
import BaseController from '@/api/controllers/BaseController';
|
|
||||||
import { CashflowApplication } from '@/services/Cashflow/CashflowApplication';
|
|
||||||
import { GetBankAccountSummary } from '@/services/Banking/BankAccounts/GetBankAccountSummary';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class BankAccountsController extends BaseController {
|
|
||||||
@Inject()
|
|
||||||
private getBankAccountSummaryService: GetBankAccountSummary;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Router constructor.
|
|
||||||
*/
|
|
||||||
router() {
|
|
||||||
const router = Router();
|
|
||||||
|
|
||||||
router.get('/:bankAccountId/meta', this.getBankAccountSummary.bind(this));
|
|
||||||
|
|
||||||
return router;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the bank account meta summary.
|
|
||||||
* @param {Request} req
|
|
||||||
* @param {Response} res
|
|
||||||
* @param {NextFunction} next
|
|
||||||
* @returns {Promise<Response|null>}
|
|
||||||
*/
|
|
||||||
async getBankAccountSummary(
|
|
||||||
req: Request<{ bankAccountId: number }>,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
) {
|
|
||||||
const { bankAccountId } = req.params;
|
|
||||||
const { tenantId } = req;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const data =
|
|
||||||
await this.getBankAccountSummaryService.getBankAccountSummary(
|
|
||||||
tenantId,
|
|
||||||
bankAccountId
|
|
||||||
);
|
|
||||||
return res.status(200).send({ data });
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,103 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { NextFunction, Request, Response, Router } from 'express';
|
|
||||||
import BaseController from '@/api/controllers/BaseController';
|
|
||||||
import { MatchBankTransactionsApplication } from '@/services/Banking/Matching/MatchBankTransactionsApplication';
|
|
||||||
import { body, param } from 'express-validator';
|
|
||||||
import {
|
|
||||||
GetMatchedTransactionsFilter,
|
|
||||||
IMatchTransactionsDTO,
|
|
||||||
} from '@/services/Banking/Matching/types';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class BankTransactionsMatchingController extends BaseController {
|
|
||||||
@Inject()
|
|
||||||
private bankTransactionsMatchingApp: MatchBankTransactionsApplication;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Router constructor.
|
|
||||||
*/
|
|
||||||
public router() {
|
|
||||||
const router = Router();
|
|
||||||
|
|
||||||
router.post(
|
|
||||||
'/:transactionId',
|
|
||||||
[
|
|
||||||
param('transactionId').exists(),
|
|
||||||
body('matchedTransactions').isArray({ min: 1 }),
|
|
||||||
body('matchedTransactions.*.reference_type').exists(),
|
|
||||||
body('matchedTransactions.*.reference_id').isNumeric().toInt(),
|
|
||||||
],
|
|
||||||
this.validationResult,
|
|
||||||
this.matchBankTransaction.bind(this)
|
|
||||||
);
|
|
||||||
router.post(
|
|
||||||
'/unmatch/:transactionId',
|
|
||||||
[param('transactionId').exists()],
|
|
||||||
this.validationResult,
|
|
||||||
this.unmatchMatchedBankTransaction.bind(this)
|
|
||||||
);
|
|
||||||
return router;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Matches the given bank transaction.
|
|
||||||
* @param {Request} req
|
|
||||||
* @param {Response} res
|
|
||||||
* @param {NextFunction} next
|
|
||||||
* @returns {Promise<Response|null>}
|
|
||||||
*/
|
|
||||||
private async matchBankTransaction(
|
|
||||||
req: Request<{ transactionId: number }>,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
) {
|
|
||||||
const { tenantId } = req;
|
|
||||||
const { transactionId } = req.params;
|
|
||||||
const matchTransactionDTO = this.matchedBodyData(
|
|
||||||
req
|
|
||||||
) as IMatchTransactionsDTO;
|
|
||||||
|
|
||||||
try {
|
|
||||||
await this.bankTransactionsMatchingApp.matchTransaction(
|
|
||||||
tenantId,
|
|
||||||
transactionId,
|
|
||||||
matchTransactionDTO
|
|
||||||
);
|
|
||||||
return res.status(200).send({
|
|
||||||
id: transactionId,
|
|
||||||
message: 'The bank transaction has been matched.',
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unmatches the matched bank transaction.
|
|
||||||
* @param {Request} req
|
|
||||||
* @param {Response} res
|
|
||||||
* @param {NextFunction} next
|
|
||||||
* @returns {Promise<Response|null>}
|
|
||||||
*/
|
|
||||||
private async unmatchMatchedBankTransaction(
|
|
||||||
req: Request<{ transactionId: number }>,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
) {
|
|
||||||
const { tenantId } = req;
|
|
||||||
const { transactionId } = req.params;
|
|
||||||
|
|
||||||
try {
|
|
||||||
await this.bankTransactionsMatchingApp.unmatchMatchedTransaction(
|
|
||||||
tenantId,
|
|
||||||
transactionId
|
|
||||||
);
|
|
||||||
return res.status(200).send({
|
|
||||||
id: transactionId,
|
|
||||||
message: 'The bank matched transaction has been unmatched.',
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,33 +2,17 @@ import Container, { Inject, Service } from 'typedi';
|
|||||||
import { Router } from 'express';
|
import { Router } from 'express';
|
||||||
import BaseController from '@/api/controllers/BaseController';
|
import BaseController from '@/api/controllers/BaseController';
|
||||||
import { PlaidBankingController } from './PlaidBankingController';
|
import { PlaidBankingController } from './PlaidBankingController';
|
||||||
import { BankingRulesController } from './BankingRulesController';
|
|
||||||
import { BankTransactionsMatchingController } from './BankTransactionsMatchingController';
|
|
||||||
import { RecognizedTransactionsController } from './RecognizedTransactionsController';
|
|
||||||
import { BankAccountsController } from './BankAccountsController';
|
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export class BankingController extends BaseController {
|
export class BankingController extends BaseController {
|
||||||
/**
|
/**
|
||||||
* Router constructor.
|
* Router constructor.
|
||||||
*/
|
*/
|
||||||
public router() {
|
router() {
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
router.use('/plaid', Container.get(PlaidBankingController).router());
|
router.use('/plaid', Container.get(PlaidBankingController).router());
|
||||||
router.use('/rules', Container.get(BankingRulesController).router());
|
|
||||||
router.use(
|
|
||||||
'/matches',
|
|
||||||
Container.get(BankTransactionsMatchingController).router()
|
|
||||||
);
|
|
||||||
router.use(
|
|
||||||
'/recognized',
|
|
||||||
Container.get(RecognizedTransactionsController).router()
|
|
||||||
);
|
|
||||||
router.use(
|
|
||||||
'/bank_accounts',
|
|
||||||
Container.get(BankAccountsController).router()
|
|
||||||
);
|
|
||||||
return router;
|
return router;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,214 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { NextFunction, Request, Response, Router } from 'express';
|
|
||||||
import BaseController from '@/api/controllers/BaseController';
|
|
||||||
import { BankRulesApplication } from '@/services/Banking/Rules/BankRulesApplication';
|
|
||||||
import { body, param } from 'express-validator';
|
|
||||||
import {
|
|
||||||
ICreateBankRuleDTO,
|
|
||||||
IEditBankRuleDTO,
|
|
||||||
} from '@/services/Banking/Rules/types';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class BankingRulesController extends BaseController {
|
|
||||||
@Inject()
|
|
||||||
private bankRulesApplication: BankRulesApplication;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Bank rule DTO validation schema.
|
|
||||||
*/
|
|
||||||
private get bankRuleValidationSchema() {
|
|
||||||
return [
|
|
||||||
body('name').isString().exists(),
|
|
||||||
body('order').isInt({ min: 0 }),
|
|
||||||
|
|
||||||
// Apply to if transaction is.
|
|
||||||
body('apply_if_account_id')
|
|
||||||
.isInt({ min: 0 })
|
|
||||||
.optional({ nullable: true }),
|
|
||||||
body('apply_if_transaction_type').isIn(['deposit', 'withdrawal']),
|
|
||||||
|
|
||||||
// Conditions
|
|
||||||
body('conditions_type').isString().isIn(['and', 'or']).default('and'),
|
|
||||||
body('conditions').isArray({ min: 1 }),
|
|
||||||
body('conditions.*.field').exists().isIn(['description', 'amount']),
|
|
||||||
body('conditions.*.comparator')
|
|
||||||
.exists()
|
|
||||||
.isIn(['equals', 'contains', 'not_contain'])
|
|
||||||
.default('contain'),
|
|
||||||
body('conditions.*.value').exists(),
|
|
||||||
|
|
||||||
// Assign
|
|
||||||
body('assign_category')
|
|
||||||
.isString()
|
|
||||||
.isIn([
|
|
||||||
'interest_income',
|
|
||||||
'other_income',
|
|
||||||
'deposit',
|
|
||||||
'expense',
|
|
||||||
'owner_drawings',
|
|
||||||
]),
|
|
||||||
body('assign_account_id').isInt({ min: 0 }),
|
|
||||||
body('assign_payee').isString().optional({ nullable: true }),
|
|
||||||
body('assign_memo').isString().optional({ nullable: true }),
|
|
||||||
|
|
||||||
body('recognition').isBoolean().toBoolean().optional({ nullable: true }),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Router constructor.
|
|
||||||
*/
|
|
||||||
public router() {
|
|
||||||
const router = Router();
|
|
||||||
|
|
||||||
router.post(
|
|
||||||
'/',
|
|
||||||
[...this.bankRuleValidationSchema],
|
|
||||||
this.validationResult,
|
|
||||||
this.createBankRule.bind(this)
|
|
||||||
);
|
|
||||||
router.post(
|
|
||||||
'/:id',
|
|
||||||
[param('id').toInt().exists(), ...this.bankRuleValidationSchema],
|
|
||||||
this.validationResult,
|
|
||||||
this.editBankRule.bind(this)
|
|
||||||
);
|
|
||||||
router.delete(
|
|
||||||
'/:id',
|
|
||||||
[param('id').toInt().exists()],
|
|
||||||
this.validationResult,
|
|
||||||
this.deleteBankRule.bind(this)
|
|
||||||
);
|
|
||||||
router.get(
|
|
||||||
'/:id',
|
|
||||||
[param('id').toInt().exists()],
|
|
||||||
this.validationResult,
|
|
||||||
this.getBankRule.bind(this)
|
|
||||||
);
|
|
||||||
router.get(
|
|
||||||
'/',
|
|
||||||
[param('id').toInt().exists()],
|
|
||||||
this.validationResult,
|
|
||||||
this.getBankRules.bind(this)
|
|
||||||
);
|
|
||||||
return router;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new bank rule.
|
|
||||||
* @param {Request} req
|
|
||||||
* @param {Response} res
|
|
||||||
* @param {NextFunction} next
|
|
||||||
*/
|
|
||||||
private async createBankRule(
|
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
) {
|
|
||||||
const { tenantId } = req;
|
|
||||||
const createBankRuleDTO = this.matchedBodyData(req) as ICreateBankRuleDTO;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const bankRule = await this.bankRulesApplication.createBankRule(
|
|
||||||
tenantId,
|
|
||||||
createBankRuleDTO
|
|
||||||
);
|
|
||||||
return res.status(200).send({
|
|
||||||
id: bankRule.id,
|
|
||||||
message: 'The bank rule has been created successfully.',
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Edits the given bank rule.
|
|
||||||
* @param {Request} req
|
|
||||||
* @param {Response} res
|
|
||||||
* @param {NextFunction} next
|
|
||||||
*/
|
|
||||||
private async editBankRule(req: Request, res: Response, next: NextFunction) {
|
|
||||||
const { tenantId } = req;
|
|
||||||
const { id: ruleId } = req.params;
|
|
||||||
const editBankRuleDTO = this.matchedBodyData(req) as IEditBankRuleDTO;
|
|
||||||
|
|
||||||
try {
|
|
||||||
await this.bankRulesApplication.editBankRule(
|
|
||||||
tenantId,
|
|
||||||
ruleId,
|
|
||||||
editBankRuleDTO
|
|
||||||
);
|
|
||||||
return res.status(200).send({
|
|
||||||
id: ruleId,
|
|
||||||
message: 'The bank rule has been updated successfully.',
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Deletes the given bank rule.
|
|
||||||
* @param {Request} req
|
|
||||||
* @param {Response} res
|
|
||||||
* @param {NextFunction} next
|
|
||||||
*/
|
|
||||||
private async deleteBankRule(
|
|
||||||
req: Request<{ id: number }>,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
) {
|
|
||||||
const { id: ruleId } = req.params;
|
|
||||||
const { tenantId } = req;
|
|
||||||
|
|
||||||
try {
|
|
||||||
await this.bankRulesApplication.deleteBankRule(tenantId, ruleId);
|
|
||||||
|
|
||||||
return res
|
|
||||||
.status(200)
|
|
||||||
.send({ message: 'The bank rule has been deleted.', id: ruleId });
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the given bank rule.
|
|
||||||
* @param {Request} req
|
|
||||||
* @param {Response} res
|
|
||||||
* @param {NextFunction} next
|
|
||||||
*/
|
|
||||||
private async getBankRule(req: Request, res: Response, next: NextFunction) {
|
|
||||||
const { id: ruleId } = req.params;
|
|
||||||
const { tenantId } = req;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const bankRule = await this.bankRulesApplication.getBankRule(
|
|
||||||
tenantId,
|
|
||||||
ruleId
|
|
||||||
);
|
|
||||||
|
|
||||||
return res.status(200).send({ bankRule });
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the bank rules.
|
|
||||||
* @param {Request} req
|
|
||||||
* @param {Response} res
|
|
||||||
* @param {NextFunction} next
|
|
||||||
*/
|
|
||||||
private async getBankRules(req: Request, res: Response, next: NextFunction) {
|
|
||||||
const { tenantId } = req;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const bankRules = await this.bankRulesApplication.getBankRules(tenantId);
|
|
||||||
return res.status(200).send({ bankRules });
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,124 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { param } from 'express-validator';
|
|
||||||
import { NextFunction, Request, Response, Router, query } from 'express';
|
|
||||||
import BaseController from '../BaseController';
|
|
||||||
import { ExcludeBankTransactionsApplication } from '@/services/Banking/Exclude/ExcludeBankTransactionsApplication';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class ExcludeBankTransactionsController extends BaseController {
|
|
||||||
@Inject()
|
|
||||||
private excludeBankTransactionApp: ExcludeBankTransactionsApplication;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Router constructor.
|
|
||||||
*/
|
|
||||||
public router() {
|
|
||||||
const router = Router();
|
|
||||||
|
|
||||||
router.put(
|
|
||||||
'/transactions/:transactionId/exclude',
|
|
||||||
[param('transactionId').exists()],
|
|
||||||
this.validationResult,
|
|
||||||
this.excludeBankTransaction.bind(this)
|
|
||||||
);
|
|
||||||
router.put(
|
|
||||||
'/transactions/:transactionId/unexclude',
|
|
||||||
[param('transactionId').exists()],
|
|
||||||
this.validationResult,
|
|
||||||
this.unexcludeBankTransaction.bind(this)
|
|
||||||
);
|
|
||||||
router.get(
|
|
||||||
'/excluded',
|
|
||||||
[],
|
|
||||||
this.validationResult,
|
|
||||||
this.getExcludedBankTransactions.bind(this)
|
|
||||||
);
|
|
||||||
return router;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Marks a bank transaction as excluded.
|
|
||||||
* @param {Request} req
|
|
||||||
* @param {Response} res
|
|
||||||
* @param {NextFunction} next
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
private async excludeBankTransaction(
|
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
): Promise<Response | void> {
|
|
||||||
const { tenantId } = req;
|
|
||||||
const { transactionId } = req.params;
|
|
||||||
|
|
||||||
try {
|
|
||||||
await this.excludeBankTransactionApp.excludeBankTransaction(
|
|
||||||
tenantId,
|
|
||||||
transactionId
|
|
||||||
);
|
|
||||||
return res.status(200).send({
|
|
||||||
message: 'The bank transaction has been excluded.',
|
|
||||||
id: transactionId,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Marks a bank transaction as not excluded.
|
|
||||||
* @param {Request} req
|
|
||||||
* @param {Response} res
|
|
||||||
* @param {NextFunction} next
|
|
||||||
* @returns {Promise<Response|void>}
|
|
||||||
*/
|
|
||||||
private async unexcludeBankTransaction(
|
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
): Promise<Response | void> {
|
|
||||||
const { tenantId } = req;
|
|
||||||
const { transactionId } = req.params;
|
|
||||||
|
|
||||||
try {
|
|
||||||
await this.excludeBankTransactionApp.unexcludeBankTransaction(
|
|
||||||
tenantId,
|
|
||||||
transactionId
|
|
||||||
);
|
|
||||||
return res.status(200).send({
|
|
||||||
message: 'The bank transaction has been unexcluded.',
|
|
||||||
id: transactionId,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the excluded uncategorized bank transactions.
|
|
||||||
* @param {Request} req
|
|
||||||
* @param {Response} res
|
|
||||||
* @param {NextFunction} next
|
|
||||||
* @returns {Promise<Response|null>}
|
|
||||||
*/
|
|
||||||
private async getExcludedBankTransactions(
|
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
): Promise<Response | void> {
|
|
||||||
const { tenantId } = req;
|
|
||||||
const filter = this.matchedBodyData(req);
|
|
||||||
|
|
||||||
console.log('123');
|
|
||||||
try {
|
|
||||||
const data =
|
|
||||||
await this.excludeBankTransactionApp.getExcludedBankTransactions(
|
|
||||||
tenantId,
|
|
||||||
filter
|
|
||||||
);
|
|
||||||
return res.status(200).send(data);
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,77 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { NextFunction, Request, Response, Router } from 'express';
|
|
||||||
import BaseController from '@/api/controllers/BaseController';
|
|
||||||
import { CashflowApplication } from '@/services/Cashflow/CashflowApplication';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class RecognizedTransactionsController extends BaseController {
|
|
||||||
@Inject()
|
|
||||||
private cashflowApplication: CashflowApplication;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Router constructor.
|
|
||||||
*/
|
|
||||||
router() {
|
|
||||||
const router = Router();
|
|
||||||
|
|
||||||
router.get('/', this.getRecognizedTransactions.bind(this));
|
|
||||||
router.get(
|
|
||||||
'/transactions/:uncategorizedTransactionId',
|
|
||||||
this.getRecognizedTransaction.bind(this)
|
|
||||||
);
|
|
||||||
|
|
||||||
return router;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the recognized bank transactions.
|
|
||||||
* @param {Request} req
|
|
||||||
* @param {Response} res
|
|
||||||
* @param {NextFunction} next
|
|
||||||
* @returns {Promise<Response|null>}
|
|
||||||
*/
|
|
||||||
async getRecognizedTransactions(
|
|
||||||
req: Request<{ accountId: number }>,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
) {
|
|
||||||
const filter = this.matchedQueryData(req);
|
|
||||||
const { tenantId } = req;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const data = await this.cashflowApplication.getRecognizedTransactions(
|
|
||||||
tenantId,
|
|
||||||
filter
|
|
||||||
);
|
|
||||||
return res.status(200).send(data);
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the recognized transaction of the ginen uncategorized transaction.
|
|
||||||
* @param {Request} req
|
|
||||||
* @param {Response} res
|
|
||||||
* @param {NextFunction} next
|
|
||||||
* @returns {Promise<Response|null>}
|
|
||||||
*/
|
|
||||||
async getRecognizedTransaction(
|
|
||||||
req: Request<{ uncategorizedTransactionId: number }>,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
) {
|
|
||||||
const { tenantId } = req;
|
|
||||||
const { uncategorizedTransactionId } = req.params;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const data = await this.cashflowApplication.getRecognizedTransaction(
|
|
||||||
tenantId,
|
|
||||||
uncategorizedTransactionId
|
|
||||||
);
|
|
||||||
return res.status(200).send({ data });
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,7 +4,6 @@ import CommandCashflowTransaction from './NewCashflowTransaction';
|
|||||||
import DeleteCashflowTransaction from './DeleteCashflowTransaction';
|
import DeleteCashflowTransaction from './DeleteCashflowTransaction';
|
||||||
import GetCashflowTransaction from './GetCashflowTransaction';
|
import GetCashflowTransaction from './GetCashflowTransaction';
|
||||||
import GetCashflowAccounts from './GetCashflowAccounts';
|
import GetCashflowAccounts from './GetCashflowAccounts';
|
||||||
import { ExcludeBankTransactionsController } from '../Banking/ExcludeBankTransactionsController';
|
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export default class CashflowController {
|
export default class CashflowController {
|
||||||
@@ -15,7 +14,6 @@ export default class CashflowController {
|
|||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
router.use(Container.get(CommandCashflowTransaction).router());
|
router.use(Container.get(CommandCashflowTransaction).router());
|
||||||
router.use(Container.get(ExcludeBankTransactionsController).router());
|
|
||||||
router.use(Container.get(GetCashflowTransaction).router());
|
router.use(Container.get(GetCashflowTransaction).router());
|
||||||
router.use(Container.get(GetCashflowAccounts).router());
|
router.use(Container.get(GetCashflowAccounts).router());
|
||||||
router.use(Container.get(DeleteCashflowTransaction).router());
|
router.use(Container.get(DeleteCashflowTransaction).router());
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ export default class GetCashflowAccounts extends BaseController {
|
|||||||
query('search_keyword').optional({ nullable: true }).isString().trim(),
|
query('search_keyword').optional({ nullable: true }).isString().trim(),
|
||||||
],
|
],
|
||||||
this.asyncMiddleware(this.getCashflowAccounts),
|
this.asyncMiddleware(this.getCashflowAccounts),
|
||||||
|
this.catchServiceErrors
|
||||||
);
|
);
|
||||||
return router;
|
return router;
|
||||||
}
|
}
|
||||||
@@ -66,4 +67,22 @@ export default class GetCashflowAccounts extends BaseController {
|
|||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Catches the service errors.
|
||||||
|
* @param {Error} error - Error.
|
||||||
|
* @param {Request} req - Request.
|
||||||
|
* @param {Response} res - Response.
|
||||||
|
* @param {NextFunction} next -
|
||||||
|
*/
|
||||||
|
private catchServiceErrors(
|
||||||
|
error,
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
) {
|
||||||
|
if (error instanceof ServiceError) {
|
||||||
|
}
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,27 +6,18 @@ import { ServiceError } from '@/exceptions';
|
|||||||
import CheckPolicies from '@/api/middleware/CheckPolicies';
|
import CheckPolicies from '@/api/middleware/CheckPolicies';
|
||||||
import { AbilitySubject, CashflowAction } from '@/interfaces';
|
import { AbilitySubject, CashflowAction } from '@/interfaces';
|
||||||
import { CashflowApplication } from '@/services/Cashflow/CashflowApplication';
|
import { CashflowApplication } from '@/services/Cashflow/CashflowApplication';
|
||||||
import { GetMatchedTransactionsFilter } from '@/services/Banking/Matching/types';
|
|
||||||
import { MatchBankTransactionsApplication } from '@/services/Banking/Matching/MatchBankTransactionsApplication';
|
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export default class GetCashflowAccounts extends BaseController {
|
export default class GetCashflowAccounts extends BaseController {
|
||||||
@Inject()
|
@Inject()
|
||||||
private cashflowApplication: CashflowApplication;
|
private cashflowApplication: CashflowApplication;
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private bankTransactionsMatchingApp: MatchBankTransactionsApplication;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Controller router.
|
* Controller router.
|
||||||
*/
|
*/
|
||||||
public router() {
|
public router() {
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
router.get(
|
|
||||||
'/transactions/:transactionId/matches',
|
|
||||||
this.getMatchedTransactions.bind(this)
|
|
||||||
);
|
|
||||||
router.get(
|
router.get(
|
||||||
'/transactions/:transactionId',
|
'/transactions/:transactionId',
|
||||||
CheckPolicies(CashflowAction.View, AbilitySubject.Cashflow),
|
CheckPolicies(CashflowAction.View, AbilitySubject.Cashflow),
|
||||||
@@ -56,6 +47,7 @@ export default class GetCashflowAccounts extends BaseController {
|
|||||||
tenantId,
|
tenantId,
|
||||||
transactionId
|
transactionId
|
||||||
);
|
);
|
||||||
|
|
||||||
return res.status(200).send({
|
return res.status(200).send({
|
||||||
cashflow_transaction: this.transfromToResponse(cashflowTransaction),
|
cashflow_transaction: this.transfromToResponse(cashflowTransaction),
|
||||||
});
|
});
|
||||||
@@ -64,34 +56,6 @@ export default class GetCashflowAccounts extends BaseController {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the matched transactions.
|
|
||||||
* @param {Request} req
|
|
||||||
* @param {Response} res
|
|
||||||
* @param {NextFunction} next
|
|
||||||
*/
|
|
||||||
private async getMatchedTransactions(
|
|
||||||
req: Request<{ transactionId: number }>,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
) {
|
|
||||||
const { tenantId } = req;
|
|
||||||
const { transactionId } = req.params;
|
|
||||||
const filter = this.matchedQueryData(req) as GetMatchedTransactionsFilter;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const data =
|
|
||||||
await this.bankTransactionsMatchingApp.getMatchedTransactions(
|
|
||||||
tenantId,
|
|
||||||
transactionId,
|
|
||||||
filter
|
|
||||||
);
|
|
||||||
return res.status(200).send(data);
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Catches the service errors.
|
* Catches the service errors.
|
||||||
* @param {Error} error - Error.
|
* @param {Error} error - Error.
|
||||||
|
|||||||
@@ -1,45 +0,0 @@
|
|||||||
exports.up = function (knex) {
|
|
||||||
return knex.schema
|
|
||||||
.createTable('bank_rules', (table) => {
|
|
||||||
table.increments('id').primary();
|
|
||||||
table.string('name');
|
|
||||||
table.integer('order').unsigned();
|
|
||||||
|
|
||||||
table
|
|
||||||
.integer('apply_if_account_id')
|
|
||||||
.unsigned()
|
|
||||||
.references('id')
|
|
||||||
.inTable('accounts');
|
|
||||||
table.string('apply_if_transaction_type');
|
|
||||||
|
|
||||||
table.string('assign_category');
|
|
||||||
table
|
|
||||||
.integer('assign_account_id')
|
|
||||||
.unsigned()
|
|
||||||
.references('id')
|
|
||||||
.inTable('accounts');
|
|
||||||
table.string('assign_payee');
|
|
||||||
table.string('assign_memo');
|
|
||||||
|
|
||||||
table.string('conditions_type');
|
|
||||||
|
|
||||||
table.timestamps();
|
|
||||||
})
|
|
||||||
.createTable('bank_rule_conditions', (table) => {
|
|
||||||
table.increments('id').primary();
|
|
||||||
table
|
|
||||||
.integer('rule_id')
|
|
||||||
.unsigned()
|
|
||||||
.references('id')
|
|
||||||
.inTable('bank_rules');
|
|
||||||
table.string('field');
|
|
||||||
table.string('comparator');
|
|
||||||
table.string('value');
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.down = function (knex) {
|
|
||||||
return knex.schema
|
|
||||||
.dropTableIfExists('bank_rules')
|
|
||||||
.dropTableIfExists('bank_rule_conditions');
|
|
||||||
};
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
exports.up = function (knex) {
|
|
||||||
return knex.schema.createTable('recognized_bank_transactions', (table) => {
|
|
||||||
table.increments('id');
|
|
||||||
table
|
|
||||||
.integer('uncategorized_transaction_id')
|
|
||||||
.unsigned()
|
|
||||||
.references('id')
|
|
||||||
.inTable('uncategorized_cashflow_transactions');
|
|
||||||
table
|
|
||||||
.integer('bank_rule_id')
|
|
||||||
.unsigned()
|
|
||||||
.references('id')
|
|
||||||
.inTable('bank_rules');
|
|
||||||
|
|
||||||
table.string('assigned_category');
|
|
||||||
table
|
|
||||||
.integer('assigned_account_id')
|
|
||||||
.unsigned()
|
|
||||||
.references('id')
|
|
||||||
.inTable('accounts');
|
|
||||||
table.string('assigned_payee');
|
|
||||||
table.string('assigned_memo');
|
|
||||||
|
|
||||||
table.timestamps();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.down = function (knex) {
|
|
||||||
return knex.schema.dropTableIfExists('recognized_bank_transactions');
|
|
||||||
};
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
exports.up = function (knex) {
|
|
||||||
return knex.schema.table('uncategorized_cashflow_transactions', (table) => {
|
|
||||||
table.integer('recognized_transaction_id').unsigned();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.down = function (knex) {
|
|
||||||
return knex.schema.table('uncategorized_cashflow_transactions', (table) => {
|
|
||||||
table.dropColumn('recognized_transaction_id');
|
|
||||||
});
|
|
||||||
};
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
exports.up = function (knex) {
|
|
||||||
return knex.schema.createTable('matched_bank_transactions', (table) => {
|
|
||||||
table.increments('id');
|
|
||||||
table.integer('uncategorized_transaction_id').unsigned();
|
|
||||||
table.string('reference_type');
|
|
||||||
table.integer('reference_id').unsigned();
|
|
||||||
table.decimal('amount');
|
|
||||||
table.timestamps();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.down = function (knex) {
|
|
||||||
return knex.schema.dropTableIfExists('matched_bank_transactions');
|
|
||||||
};
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
exports.up = function (knex) {
|
|
||||||
return knex.schema.table('uncategorized_cashflow_transactions', (table) => {
|
|
||||||
table.datetime('excluded_at');
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.down = function (knex) {
|
|
||||||
return knex.schema.table('uncategorized_cashflow_transactions', (table) => {
|
|
||||||
table.dropColumn('excluded_at');
|
|
||||||
});
|
|
||||||
};
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
exports.up = function (knex) {
|
|
||||||
return knex.schema.table('uncategorized_cashflow_transactions', (table) => {
|
|
||||||
table.string('batch');
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.down = function (knex) {
|
|
||||||
return knex.schema.table('uncategorized_cashflow_transactions', (table) => {
|
|
||||||
table.dropColumn('batch');
|
|
||||||
});
|
|
||||||
};
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
exports.up = function (knex) {
|
|
||||||
return knex.schema.table('settings', (table) => {
|
|
||||||
table.text('value').alter();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.down = (knex) => {
|
|
||||||
return knex.schema.table('settings', (table) => {
|
|
||||||
table.string('value').alter();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
@@ -267,5 +267,4 @@ export interface CreateUncategorizedTransactionDTO {
|
|||||||
description?: string;
|
description?: string;
|
||||||
referenceNo?: string | null;
|
referenceNo?: string | null;
|
||||||
plaidTransactionId?: string | null;
|
plaidTransactionId?: string | null;
|
||||||
batch?: string;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -164,10 +164,3 @@ export interface IGetUncategorizedTransactionsQuery {
|
|||||||
page?: number;
|
page?: number;
|
||||||
pageSize?: number;
|
pageSize?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export interface IGetRecognizedTransactionsQuery {
|
|
||||||
page?: number;
|
|
||||||
pageSize?: number;
|
|
||||||
accountId?: number;
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
import { Knex } from "knex";
|
|
||||||
|
|
||||||
export interface IPlaidItemCreatedEventPayload {
|
export interface IPlaidItemCreatedEventPayload {
|
||||||
tenantId: number;
|
tenantId: number;
|
||||||
plaidAccessToken: string;
|
plaidAccessToken: string;
|
||||||
@@ -56,10 +54,3 @@ export interface SyncAccountsTransactionsTask {
|
|||||||
plaidAccountId: number;
|
plaidAccountId: number;
|
||||||
plaidTransactions: PlaidTransaction[];
|
plaidTransactions: PlaidTransaction[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IPlaidTransactionsSyncedEventPayload {
|
|
||||||
tenantId: number;
|
|
||||||
plaidAccountId: number;
|
|
||||||
batch: string;
|
|
||||||
trx?: Knex.Transaction
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -169,8 +169,8 @@ export class Transformer {
|
|||||||
* @param number
|
* @param number
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
protected formatNumber(number, props?) {
|
protected formatNumber(number) {
|
||||||
return formatNumber(number, { money: false, ...props });
|
return formatNumber(number, { money: false });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -102,14 +102,6 @@ import { AttachmentsOnVendorCredits } from '@/services/Attachments/events/Attach
|
|||||||
import { AttachmentsOnCreditNote } from '@/services/Attachments/events/AttachmentsOnCreditNote';
|
import { AttachmentsOnCreditNote } from '@/services/Attachments/events/AttachmentsOnCreditNote';
|
||||||
import { AttachmentsOnBillPayments } from '@/services/Attachments/events/AttachmentsOnPaymentsMade';
|
import { AttachmentsOnBillPayments } from '@/services/Attachments/events/AttachmentsOnPaymentsMade';
|
||||||
import { AttachmentsOnSaleEstimates } from '@/services/Attachments/events/AttachmentsOnSaleEstimates';
|
import { AttachmentsOnSaleEstimates } from '@/services/Attachments/events/AttachmentsOnSaleEstimates';
|
||||||
import { TriggerRecognizedTransactions } from '@/services/Banking/RegonizeTranasctions/events/TriggerRecognizedTransactions';
|
|
||||||
import { ValidateMatchingOnExpenseDelete } from '@/services/Banking/Matching/events/ValidateMatchingOnExpenseDelete';
|
|
||||||
import { ValidateMatchingOnManualJournalDelete } from '@/services/Banking/Matching/events/ValidateMatchingOnManualJournalDelete';
|
|
||||||
import { ValidateMatchingOnPaymentReceivedDelete } from '@/services/Banking/Matching/events/ValidateMatchingOnPaymentReceivedDelete';
|
|
||||||
import { ValidateMatchingOnPaymentMadeDelete } from '@/services/Banking/Matching/events/ValidateMatchingOnPaymentMadeDelete';
|
|
||||||
import { ValidateMatchingOnCashflowDelete } from '@/services/Banking/Matching/events/ValidateMatchingOnCashflowDelete';
|
|
||||||
import { RecognizeSyncedBankTranasctions } from '@/services/Banking/Plaid/subscribers/RecognizeSyncedBankTransactions';
|
|
||||||
import { UnlinkBankRuleOnDeleteBankRule } from '@/services/Banking/Rules/events/UnlinkBankRuleOnDeleteBankRule';
|
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
return new EventPublisher();
|
return new EventPublisher();
|
||||||
@@ -254,19 +246,5 @@ export const susbcribers = () => {
|
|||||||
AttachmentsOnBillPayments,
|
AttachmentsOnBillPayments,
|
||||||
AttachmentsOnManualJournals,
|
AttachmentsOnManualJournals,
|
||||||
AttachmentsOnExpenses,
|
AttachmentsOnExpenses,
|
||||||
|
|
||||||
// Bank Rules
|
|
||||||
TriggerRecognizedTransactions,
|
|
||||||
UnlinkBankRuleOnDeleteBankRule,
|
|
||||||
|
|
||||||
// Validate matching
|
|
||||||
ValidateMatchingOnCashflowDelete,
|
|
||||||
ValidateMatchingOnExpenseDelete,
|
|
||||||
ValidateMatchingOnManualJournalDelete,
|
|
||||||
ValidateMatchingOnPaymentReceivedDelete,
|
|
||||||
ValidateMatchingOnPaymentMadeDelete,
|
|
||||||
|
|
||||||
// Plaid
|
|
||||||
RecognizeSyncedBankTranasctions,
|
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import { PaymentReceiveMailNotificationJob } from '@/services/Sales/PaymentRecei
|
|||||||
import { PlaidFetchTransactionsJob } from '@/services/Banking/Plaid/PlaidFetchTransactionsJob';
|
import { PlaidFetchTransactionsJob } from '@/services/Banking/Plaid/PlaidFetchTransactionsJob';
|
||||||
import { ImportDeleteExpiredFilesJobs } from '@/services/Import/jobs/ImportDeleteExpiredFilesJob';
|
import { ImportDeleteExpiredFilesJobs } from '@/services/Import/jobs/ImportDeleteExpiredFilesJob';
|
||||||
import { SendVerifyMailJob } from '@/services/Authentication/jobs/SendVerifyMailJob';
|
import { SendVerifyMailJob } from '@/services/Authentication/jobs/SendVerifyMailJob';
|
||||||
import { RegonizeTransactionsJob } from '@/services/Banking/RegonizeTranasctions/RecognizeTransactionsJob';
|
|
||||||
|
|
||||||
export default ({ agenda }: { agenda: Agenda }) => {
|
export default ({ agenda }: { agenda: Agenda }) => {
|
||||||
new ResetPasswordMailJob(agenda);
|
new ResetPasswordMailJob(agenda);
|
||||||
@@ -30,7 +29,6 @@ export default ({ agenda }: { agenda: Agenda }) => {
|
|||||||
new PlaidFetchTransactionsJob(agenda);
|
new PlaidFetchTransactionsJob(agenda);
|
||||||
new ImportDeleteExpiredFilesJobs(agenda);
|
new ImportDeleteExpiredFilesJobs(agenda);
|
||||||
new SendVerifyMailJob(agenda);
|
new SendVerifyMailJob(agenda);
|
||||||
new RegonizeTransactionsJob(agenda);
|
|
||||||
|
|
||||||
agenda.start().then(() => {
|
agenda.start().then(() => {
|
||||||
agenda.every('1 hours', 'delete-expired-imported-files', {});
|
agenda.every('1 hours', 'delete-expired-imported-files', {});
|
||||||
|
|||||||
@@ -64,10 +64,6 @@ import PlaidItem from 'models/PlaidItem';
|
|||||||
import UncategorizedCashflowTransaction from 'models/UncategorizedCashflowTransaction';
|
import UncategorizedCashflowTransaction from 'models/UncategorizedCashflowTransaction';
|
||||||
import Document from '@/models/Document';
|
import Document from '@/models/Document';
|
||||||
import DocumentLink from '@/models/DocumentLink';
|
import DocumentLink from '@/models/DocumentLink';
|
||||||
import { BankRule } from '@/models/BankRule';
|
|
||||||
import { BankRuleCondition } from '@/models/BankRuleCondition';
|
|
||||||
import { RecognizedBankTransaction } from '@/models/RecognizedBankTransaction';
|
|
||||||
import { MatchedBankTransaction } from '@/models/MatchedBankTransaction';
|
|
||||||
|
|
||||||
export default (knex) => {
|
export default (knex) => {
|
||||||
const models = {
|
const models = {
|
||||||
@@ -135,10 +131,6 @@ export default (knex) => {
|
|||||||
DocumentLink,
|
DocumentLink,
|
||||||
PlaidItem,
|
PlaidItem,
|
||||||
UncategorizedCashflowTransaction,
|
UncategorizedCashflowTransaction,
|
||||||
BankRule,
|
|
||||||
BankRuleCondition,
|
|
||||||
RecognizedBankTransaction,
|
|
||||||
MatchedBankTransaction,
|
|
||||||
};
|
};
|
||||||
return mapValues(models, (model) => model.bindKnex(knex));
|
return mapValues(models, (model) => model.bindKnex(knex));
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,70 +0,0 @@
|
|||||||
import TenantModel from 'models/TenantModel';
|
|
||||||
import { Model } from 'objection';
|
|
||||||
|
|
||||||
export class BankRule extends TenantModel {
|
|
||||||
id!: number;
|
|
||||||
name!: string;
|
|
||||||
order!: number;
|
|
||||||
applyIfAccountId!: number;
|
|
||||||
applyIfTransactionType!: string;
|
|
||||||
assignCategory!: string;
|
|
||||||
assignAccountId!: number;
|
|
||||||
assignPayee!: string;
|
|
||||||
assignMemo!: string;
|
|
||||||
conditionsType!: string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Table name
|
|
||||||
*/
|
|
||||||
static get tableName() {
|
|
||||||
return 'bank_rules';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Timestamps columns.
|
|
||||||
*/
|
|
||||||
get timestamps() {
|
|
||||||
return ['created_at', 'updated_at'];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Virtual attributes.
|
|
||||||
*/
|
|
||||||
static get virtualAttributes() {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Relationship mapping.
|
|
||||||
*/
|
|
||||||
static get relationMappings() {
|
|
||||||
const { BankRuleCondition } = require('models/BankRuleCondition');
|
|
||||||
const Account = require('models/Account');
|
|
||||||
|
|
||||||
return {
|
|
||||||
/**
|
|
||||||
* Sale invoice associated entries.
|
|
||||||
*/
|
|
||||||
conditions: {
|
|
||||||
relation: Model.HasManyRelation,
|
|
||||||
modelClass: BankRuleCondition,
|
|
||||||
join: {
|
|
||||||
from: 'bank_rules.id',
|
|
||||||
to: 'bank_rule_conditions.ruleId',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Bank rule may associated to the assign account.
|
|
||||||
*/
|
|
||||||
assignAccount: {
|
|
||||||
relation: Model.BelongsToOneRelation,
|
|
||||||
modelClass: Account.default,
|
|
||||||
join: {
|
|
||||||
from: 'bank_rules.assignAccountId',
|
|
||||||
to: 'accounts.id',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
import TenantModel from 'models/TenantModel';
|
|
||||||
|
|
||||||
export class BankRuleCondition extends TenantModel {
|
|
||||||
/**
|
|
||||||
* Table name.
|
|
||||||
*/
|
|
||||||
static get tableName() {
|
|
||||||
return 'bank_rule_conditions';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Timestamps columns.
|
|
||||||
*/
|
|
||||||
get timestamps() {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Virtual attributes.
|
|
||||||
*/
|
|
||||||
static get virtualAttributes() {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -404,7 +404,6 @@ export default class Bill extends mixin(TenantModel, [
|
|||||||
const Branch = require('models/Branch');
|
const Branch = require('models/Branch');
|
||||||
const TaxRateTransaction = require('models/TaxRateTransaction');
|
const TaxRateTransaction = require('models/TaxRateTransaction');
|
||||||
const Document = require('models/Document');
|
const Document = require('models/Document');
|
||||||
const { MatchedBankTransaction } = require('models/MatchedBankTransaction');
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
vendor: {
|
vendor: {
|
||||||
@@ -486,21 +485,6 @@ export default class Bill extends mixin(TenantModel, [
|
|||||||
query.where('model_ref', 'Bill');
|
query.where('model_ref', 'Bill');
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
|
||||||
* Bill may belongs to matched bank transaction.
|
|
||||||
*/
|
|
||||||
matchedBankTransaction: {
|
|
||||||
relation: Model.HasManyRelation,
|
|
||||||
modelClass: MatchedBankTransaction,
|
|
||||||
join: {
|
|
||||||
from: 'bills.id',
|
|
||||||
to: 'matched_bank_transactions.referenceId',
|
|
||||||
},
|
|
||||||
filter(query) {
|
|
||||||
query.where('reference_type', 'Bill');
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -102,7 +102,6 @@ export default class CashflowTransaction extends TenantModel {
|
|||||||
const CashflowTransactionLine = require('models/CashflowTransactionLine');
|
const CashflowTransactionLine = require('models/CashflowTransactionLine');
|
||||||
const AccountTransaction = require('models/AccountTransaction');
|
const AccountTransaction = require('models/AccountTransaction');
|
||||||
const Account = require('models/Account');
|
const Account = require('models/Account');
|
||||||
const { MatchedBankTransaction } = require('models/MatchedBankTransaction');
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
/**
|
/**
|
||||||
@@ -159,22 +158,6 @@ export default class CashflowTransaction extends TenantModel {
|
|||||||
to: 'accounts.id',
|
to: 'accounts.id',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
|
||||||
* Cashflow transaction may belongs to matched bank transaction.
|
|
||||||
*/
|
|
||||||
matchedBankTransaction: {
|
|
||||||
relation: Model.HasManyRelation,
|
|
||||||
modelClass: MatchedBankTransaction,
|
|
||||||
join: {
|
|
||||||
from: 'cashflow_transactions.id',
|
|
||||||
to: 'matched_bank_transactions.referenceId',
|
|
||||||
},
|
|
||||||
filter: (query) => {
|
|
||||||
const referenceTypes = getCashflowAccountTransactionsTypes();
|
|
||||||
query.whereIn('reference_type', referenceTypes);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -182,7 +182,6 @@ export default class Expense extends mixin(TenantModel, [
|
|||||||
const ExpenseCategory = require('models/ExpenseCategory');
|
const ExpenseCategory = require('models/ExpenseCategory');
|
||||||
const Document = require('models/Document');
|
const Document = require('models/Document');
|
||||||
const Branch = require('models/Branch');
|
const Branch = require('models/Branch');
|
||||||
const { MatchedBankTransaction } = require('models/MatchedBankTransaction');
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
paymentAccount: {
|
paymentAccount: {
|
||||||
@@ -235,21 +234,6 @@ export default class Expense extends mixin(TenantModel, [
|
|||||||
query.where('model_ref', 'Expense');
|
query.where('model_ref', 'Expense');
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
|
||||||
* Expense may belongs to matched bank transaction.
|
|
||||||
*/
|
|
||||||
matchedBankTransaction: {
|
|
||||||
relation: Model.HasManyRelation,
|
|
||||||
modelClass: MatchedBankTransaction,
|
|
||||||
join: {
|
|
||||||
from: 'expenses_transactions.id',
|
|
||||||
to: 'matched_bank_transactions.referenceId',
|
|
||||||
},
|
|
||||||
filter(query) {
|
|
||||||
query.where('reference_type', 'Expense');
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -97,7 +97,6 @@ export default class ManualJournal extends mixin(TenantModel, [
|
|||||||
const AccountTransaction = require('models/AccountTransaction');
|
const AccountTransaction = require('models/AccountTransaction');
|
||||||
const ManualJournalEntry = require('models/ManualJournalEntry');
|
const ManualJournalEntry = require('models/ManualJournalEntry');
|
||||||
const Document = require('models/Document');
|
const Document = require('models/Document');
|
||||||
const { MatchedBankTransaction } = require('models/MatchedBankTransaction');
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
entries: {
|
entries: {
|
||||||
@@ -141,21 +140,6 @@ export default class ManualJournal extends mixin(TenantModel, [
|
|||||||
query.where('model_ref', 'ManualJournal');
|
query.where('model_ref', 'ManualJournal');
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
|
||||||
* Manual journal may belongs to matched bank transaction.
|
|
||||||
*/
|
|
||||||
matchedBankTransaction: {
|
|
||||||
relation: Model.BelongsToOneRelation,
|
|
||||||
modelClass: MatchedBankTransaction,
|
|
||||||
join: {
|
|
||||||
from: 'manual_journals.id',
|
|
||||||
to: 'matched_bank_transactions.referenceId',
|
|
||||||
},
|
|
||||||
filter(query) {
|
|
||||||
query.where('reference_type', 'ManualJournal');
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,32 +0,0 @@
|
|||||||
import TenantModel from 'models/TenantModel';
|
|
||||||
import { Model } from 'objection';
|
|
||||||
|
|
||||||
export class MatchedBankTransaction extends TenantModel {
|
|
||||||
/**
|
|
||||||
* Table name.
|
|
||||||
*/
|
|
||||||
static get tableName() {
|
|
||||||
return 'matched_bank_transactions';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Timestamps columns.
|
|
||||||
*/
|
|
||||||
get timestamps() {
|
|
||||||
return ['createdAt', 'updatedAt'];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Virtual attributes.
|
|
||||||
*/
|
|
||||||
static get virtualAttributes() {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Relationship mapping.
|
|
||||||
*/
|
|
||||||
static get relationMappings() {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
import TenantModel from 'models/TenantModel';
|
|
||||||
import { Model } from 'objection';
|
|
||||||
|
|
||||||
export class RecognizedBankTransaction extends TenantModel {
|
|
||||||
/**
|
|
||||||
* Table name.
|
|
||||||
*/
|
|
||||||
static get tableName() {
|
|
||||||
return 'recognized_bank_transactions';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Timestamps columns.
|
|
||||||
*/
|
|
||||||
get timestamps() {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Virtual attributes.
|
|
||||||
*/
|
|
||||||
static get virtualAttributes() {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Relationship mapping.
|
|
||||||
*/
|
|
||||||
static get relationMappings() {
|
|
||||||
const UncategorizedCashflowTransaction = require('./UncategorizedCashflowTransaction');
|
|
||||||
const Account = require('./Account');
|
|
||||||
const { BankRule } = require('./BankRule');
|
|
||||||
|
|
||||||
return {
|
|
||||||
/**
|
|
||||||
* Recognized bank transaction may belongs to uncategorized transactions.
|
|
||||||
*/
|
|
||||||
uncategorizedTransactions: {
|
|
||||||
relation: Model.HasManyRelation,
|
|
||||||
modelClass: UncategorizedCashflowTransaction.default,
|
|
||||||
join: {
|
|
||||||
from: 'recognized_bank_transactions.uncategorizedTransactionId',
|
|
||||||
to: 'uncategorized_cashflow_transactions.id',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Recognized bank transaction may belongs to assign account.
|
|
||||||
*/
|
|
||||||
assignAccount: {
|
|
||||||
relation: Model.BelongsToOneRelation,
|
|
||||||
modelClass: Account.default,
|
|
||||||
join: {
|
|
||||||
from: 'recognized_bank_transactions.assignedAccountId',
|
|
||||||
to: 'accounts.id',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Recognized bank transaction may belongs to bank rule.
|
|
||||||
*/
|
|
||||||
bankRule: {
|
|
||||||
relation: Model.BelongsToOneRelation,
|
|
||||||
modelClass: BankRule,
|
|
||||||
join: {
|
|
||||||
from: 'recognized_bank_transactions.bankRuleId',
|
|
||||||
to: 'bank_rules.id',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -411,7 +411,6 @@ export default class SaleInvoice extends mixin(TenantModel, [
|
|||||||
const Account = require('models/Account');
|
const Account = require('models/Account');
|
||||||
const TaxRateTransaction = require('models/TaxRateTransaction');
|
const TaxRateTransaction = require('models/TaxRateTransaction');
|
||||||
const Document = require('models/Document');
|
const Document = require('models/Document');
|
||||||
const { MatchedBankTransaction } = require('models/MatchedBankTransaction');
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
/**
|
/**
|
||||||
@@ -544,21 +543,6 @@ export default class SaleInvoice extends mixin(TenantModel, [
|
|||||||
query.where('model_ref', 'SaleInvoice');
|
query.where('model_ref', 'SaleInvoice');
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
|
||||||
* Sale invocie may belongs to matched bank transaction.
|
|
||||||
*/
|
|
||||||
matchedBankTransaction: {
|
|
||||||
relation: Model.HasManyRelation,
|
|
||||||
modelClass: MatchedBankTransaction,
|
|
||||||
join: {
|
|
||||||
from: 'sales_invoices.id',
|
|
||||||
to: "matched_bank_transactions.referenceId",
|
|
||||||
},
|
|
||||||
filter(query) {
|
|
||||||
query.where('reference_type', 'SaleInvoice');
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,15 +11,9 @@ export default class UncategorizedCashflowTransaction extends mixin(
|
|||||||
[ModelSettings]
|
[ModelSettings]
|
||||||
) {
|
) {
|
||||||
id!: number;
|
id!: number;
|
||||||
date!: Date | string;
|
|
||||||
amount!: number;
|
amount!: number;
|
||||||
categorized!: boolean;
|
categorized!: boolean;
|
||||||
accountId!: number;
|
accountId!: number;
|
||||||
referenceNo!: string;
|
|
||||||
payee!: string;
|
|
||||||
description!: string;
|
|
||||||
plaidTransactionId!: string;
|
|
||||||
recognizedTransactionId!: number;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Table name.
|
* Table name.
|
||||||
@@ -44,7 +38,6 @@ export default class UncategorizedCashflowTransaction extends mixin(
|
|||||||
'deposit',
|
'deposit',
|
||||||
'isDepositTransaction',
|
'isDepositTransaction',
|
||||||
'isWithdrawalTransaction',
|
'isWithdrawalTransaction',
|
||||||
'isRecognized',
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,43 +75,11 @@ export default class UncategorizedCashflowTransaction extends mixin(
|
|||||||
return 0 < this.withdrawal;
|
return 0 < this.withdrawal;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Detarmines whether the transaction is recognized.
|
|
||||||
*/
|
|
||||||
public get isRecognized(): boolean {
|
|
||||||
return !!this.recognizedTransactionId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Model modifiers.
|
|
||||||
*/
|
|
||||||
static get modifiers() {
|
|
||||||
return {
|
|
||||||
/**
|
|
||||||
* Filters the not excluded transactions.
|
|
||||||
*/
|
|
||||||
notExcluded(query) {
|
|
||||||
query.whereNull('excluded_at');
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filters the excluded transactions.
|
|
||||||
*/
|
|
||||||
excluded(query) {
|
|
||||||
query.whereNotNull('excluded_at')
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Relationship mapping.
|
* Relationship mapping.
|
||||||
*/
|
*/
|
||||||
static get relationMappings() {
|
static get relationMappings() {
|
||||||
const Account = require('models/Account');
|
const Account = require('models/Account');
|
||||||
const {
|
|
||||||
RecognizedBankTransaction,
|
|
||||||
} = require('models/RecognizedBankTransaction');
|
|
||||||
const { MatchedBankTransaction } = require('models/MatchedBankTransaction');
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
/**
|
/**
|
||||||
@@ -132,30 +93,6 @@ export default class UncategorizedCashflowTransaction extends mixin(
|
|||||||
to: 'accounts.id',
|
to: 'accounts.id',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
|
||||||
* Transaction may has association to recognized transaction.
|
|
||||||
*/
|
|
||||||
recognizedTransaction: {
|
|
||||||
relation: Model.HasOneRelation,
|
|
||||||
modelClass: RecognizedBankTransaction,
|
|
||||||
join: {
|
|
||||||
from: 'uncategorized_cashflow_transactions.recognizedTransactionId',
|
|
||||||
to: 'recognized_bank_transactions.id',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Uncategorized transaction may has association to matched transaction.
|
|
||||||
*/
|
|
||||||
matchedBankTransactions: {
|
|
||||||
relation: Model.HasManyRelation,
|
|
||||||
modelClass: MatchedBankTransaction,
|
|
||||||
join: {
|
|
||||||
from: 'uncategorized_cashflow_transactions.id',
|
|
||||||
to: 'matched_bank_transactions.uncategorizedTransactionId',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,55 +0,0 @@
|
|||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
import { Server } from 'socket.io';
|
|
||||||
import { Inject, Service } from 'typedi';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class GetBankAccountSummary {
|
|
||||||
@Inject()
|
|
||||||
private tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the bank account meta summary
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} bankAccountId
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
public async getBankAccountSummary(tenantId: number, bankAccountId: number) {
|
|
||||||
const {
|
|
||||||
Account,
|
|
||||||
UncategorizedCashflowTransaction,
|
|
||||||
RecognizedBankTransaction,
|
|
||||||
} = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const bankAccount = await Account.query()
|
|
||||||
.findById(bankAccountId)
|
|
||||||
.throwIfNotFound();
|
|
||||||
|
|
||||||
// Retrieves the uncategorized transactions count of the given bank account.
|
|
||||||
const uncategorizedTranasctionsCount =
|
|
||||||
await UncategorizedCashflowTransaction.query()
|
|
||||||
.where('accountId', bankAccountId)
|
|
||||||
.count('id as total')
|
|
||||||
.first();
|
|
||||||
|
|
||||||
// Retrieves the recognized transactions count of the given bank account.
|
|
||||||
const recognizedTransactionsCount = await RecognizedBankTransaction.query()
|
|
||||||
.whereExists(
|
|
||||||
UncategorizedCashflowTransaction.query().where(
|
|
||||||
'accountId',
|
|
||||||
bankAccountId
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.count('id as total')
|
|
||||||
.first();
|
|
||||||
|
|
||||||
const totalUncategorizedTransactions =
|
|
||||||
uncategorizedTranasctionsCount?.total;
|
|
||||||
const totalRecognizedTransactions = recognizedTransactionsCount?.total;
|
|
||||||
|
|
||||||
return {
|
|
||||||
name: bankAccount.name,
|
|
||||||
totalUncategorizedTransactions,
|
|
||||||
totalRecognizedTransactions,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
import UnitOfWork from '@/services/UnitOfWork';
|
|
||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { validateTransactionNotCategorized } from './utils';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class ExcludeBankTransaction {
|
|
||||||
@Inject()
|
|
||||||
private tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private uow: UnitOfWork;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Marks the given bank transaction as excluded.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} bankTransactionId
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
public async excludeBankTransaction(
|
|
||||||
tenantId: number,
|
|
||||||
uncategorizedTransactionId: number
|
|
||||||
) {
|
|
||||||
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const oldUncategorizedTransaction =
|
|
||||||
await UncategorizedCashflowTransaction.query()
|
|
||||||
.findById(uncategorizedTransactionId)
|
|
||||||
.throwIfNotFound();
|
|
||||||
|
|
||||||
validateTransactionNotCategorized(oldUncategorizedTransaction);
|
|
||||||
|
|
||||||
return this.uow.withTransaction(tenantId, async (trx) => {
|
|
||||||
await UncategorizedCashflowTransaction.query(trx)
|
|
||||||
.findById(uncategorizedTransactionId)
|
|
||||||
.patch({
|
|
||||||
excludedAt: new Date(),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,59 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { ExcludeBankTransaction } from './ExcludeBankTransaction';
|
|
||||||
import { UnexcludeBankTransaction } from './UnexcludeBankTransaction';
|
|
||||||
import { GetExcludedBankTransactionsService } from './GetExcludedBankTransactions';
|
|
||||||
import { ExcludedBankTransactionsQuery } from './_types';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class ExcludeBankTransactionsApplication {
|
|
||||||
@Inject()
|
|
||||||
private excludeBankTransactionService: ExcludeBankTransaction;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private unexcludeBankTransactionService: UnexcludeBankTransaction;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private getExcludedBankTransactionsService: GetExcludedBankTransactionsService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Marks a bank transaction as excluded.
|
|
||||||
* @param {number} tenantId - The ID of the tenant.
|
|
||||||
* @param {number} bankTransactionId - The ID of the bank transaction to exclude.
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
public excludeBankTransaction(tenantId: number, bankTransactionId: number) {
|
|
||||||
return this.excludeBankTransactionService.excludeBankTransaction(
|
|
||||||
tenantId,
|
|
||||||
bankTransactionId
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Marks a bank transaction as not excluded.
|
|
||||||
* @param {number} tenantId - The ID of the tenant.
|
|
||||||
* @param {number} bankTransactionId - The ID of the bank transaction to exclude.
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
public unexcludeBankTransaction(tenantId: number, bankTransactionId: number) {
|
|
||||||
return this.unexcludeBankTransactionService.unexcludeBankTransaction(
|
|
||||||
tenantId,
|
|
||||||
bankTransactionId
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the excluded bank transactions.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {ExcludedBankTransactionsQuery} filter
|
|
||||||
* @returns {}
|
|
||||||
*/
|
|
||||||
public getExcludedBankTransactions(
|
|
||||||
tenantId: number,
|
|
||||||
filter: ExcludedBankTransactionsQuery
|
|
||||||
) {
|
|
||||||
return this.getExcludedBankTransactionsService.getExcludedBankTransactions(
|
|
||||||
tenantId,
|
|
||||||
filter
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { ExcludedBankTransactionsQuery } from './_types';
|
|
||||||
import { UncategorizedTransactionTransformer } from '@/services/Cashflow/UncategorizedTransactionTransformer';
|
|
||||||
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class GetExcludedBankTransactionsService {
|
|
||||||
@Inject()
|
|
||||||
private tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private transformer: TransformerInjectable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the excluded uncategorized bank transactions.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {ExcludedBankTransactionsQuery} filter
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
public async getExcludedBankTransactions(
|
|
||||||
tenantId: number,
|
|
||||||
filter: ExcludedBankTransactionsQuery
|
|
||||||
) {
|
|
||||||
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
// Parsed query with default values.
|
|
||||||
const _query = {
|
|
||||||
page: 1,
|
|
||||||
pageSize: 20,
|
|
||||||
...filter,
|
|
||||||
};
|
|
||||||
const { results, pagination } =
|
|
||||||
await UncategorizedCashflowTransaction.query()
|
|
||||||
.onBuild((q) => {
|
|
||||||
q.modify('excluded');
|
|
||||||
q.orderBy('date', 'DESC');
|
|
||||||
|
|
||||||
if (_query.accountId) {
|
|
||||||
q.where('account_id', _query.accountId);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.pagination(_query.page - 1, _query.pageSize);
|
|
||||||
|
|
||||||
const data = await this.transformer.transform(
|
|
||||||
tenantId,
|
|
||||||
results,
|
|
||||||
new UncategorizedTransactionTransformer()
|
|
||||||
);
|
|
||||||
return { data, pagination };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
import UnitOfWork from '@/services/UnitOfWork';
|
|
||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { validateTransactionNotCategorized } from './utils';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class UnexcludeBankTransaction {
|
|
||||||
@Inject()
|
|
||||||
private tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private uow: UnitOfWork;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Marks the given bank transaction as excluded.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} bankTransactionId
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
public async unexcludeBankTransaction(
|
|
||||||
tenantId: number,
|
|
||||||
uncategorizedTransactionId: number
|
|
||||||
) {
|
|
||||||
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const oldUncategorizedTransaction =
|
|
||||||
await UncategorizedCashflowTransaction.query()
|
|
||||||
.findById(uncategorizedTransactionId)
|
|
||||||
.throwIfNotFound();
|
|
||||||
|
|
||||||
validateTransactionNotCategorized(oldUncategorizedTransaction);
|
|
||||||
|
|
||||||
return this.uow.withTransaction(tenantId, async (trx) => {
|
|
||||||
await UncategorizedCashflowTransaction.query(trx)
|
|
||||||
.findById(uncategorizedTransactionId)
|
|
||||||
.patch({
|
|
||||||
excludedAt: null,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
|
|
||||||
export interface ExcludedBankTransactionsQuery {
|
|
||||||
page?: number;
|
|
||||||
pageSize?: number;
|
|
||||||
accountId?: number;
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
import { ServiceError } from '@/exceptions';
|
|
||||||
import UncategorizedCashflowTransaction from '@/models/UncategorizedCashflowTransaction';
|
|
||||||
|
|
||||||
const ERRORS = {
|
|
||||||
TRANSACTION_ALREADY_CATEGORIZED: 'TRANSACTION_ALREADY_CATEGORIZED',
|
|
||||||
};
|
|
||||||
|
|
||||||
export const validateTransactionNotCategorized = (
|
|
||||||
transaction: UncategorizedCashflowTransaction
|
|
||||||
) => {
|
|
||||||
if (transaction.categorized) {
|
|
||||||
throw new ServiceError(ERRORS.TRANSACTION_ALREADY_CATEGORIZED);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -1,103 +0,0 @@
|
|||||||
import { Transformer } from '@/lib/Transformer/Transformer';
|
|
||||||
|
|
||||||
export class GetMatchedTransactionBillsTransformer extends Transformer {
|
|
||||||
/**
|
|
||||||
* Include these attributes to sale credit note object.
|
|
||||||
* @returns {Array}
|
|
||||||
*/
|
|
||||||
public includeAttributes = (): string[] => {
|
|
||||||
return [
|
|
||||||
'referenceNo',
|
|
||||||
'amount',
|
|
||||||
'amountFormatted',
|
|
||||||
'transactionNo',
|
|
||||||
'date',
|
|
||||||
'dateFormatted',
|
|
||||||
'transactionId',
|
|
||||||
'transactionNo',
|
|
||||||
'transactionType',
|
|
||||||
'transsactionTypeFormatted',
|
|
||||||
];
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Exclude all attributes.
|
|
||||||
* @returns {Array<string>}
|
|
||||||
*/
|
|
||||||
public excludeAttributes = (): string[] => {
|
|
||||||
return ['*'];
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the reference number of the bill.
|
|
||||||
* @param {Object} bill - The bill object.
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected referenceNo(bill) {
|
|
||||||
return bill.referenceNo;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the amount of the bill.
|
|
||||||
* @param {Object} bill - The bill object.
|
|
||||||
* @returns {number}
|
|
||||||
*/
|
|
||||||
protected amount(bill) {
|
|
||||||
return bill.amount;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the formatted amount of the bill.
|
|
||||||
* @param {Object} bill - The bill object.
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected amountFormatted(bill) {
|
|
||||||
return this.formatNumber(bill.amount, {
|
|
||||||
currencyCode: bill.currencyCode,
|
|
||||||
money: true,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the date of the bill.
|
|
||||||
* @param {Object} bill - The bill object.
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected date(bill) {
|
|
||||||
return bill.billDate;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the formatted date of the bill.
|
|
||||||
* @param {Object} bill - The bill object.
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected dateFormatted(bill) {
|
|
||||||
return this.formatDate(bill.billDate);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the transcation id of the bill.
|
|
||||||
* @param {Object} bill - The bill object.
|
|
||||||
* @returns {number}
|
|
||||||
*/
|
|
||||||
protected transactionId(bill) {
|
|
||||||
return bill.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the manual journal transaction type.
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected transactionType() {
|
|
||||||
return 'Bill';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the manual journal formatted transaction type.
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected transsactionTypeFormatted() {
|
|
||||||
return 'Bill';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,114 +0,0 @@
|
|||||||
import { Transformer } from '@/lib/Transformer/Transformer';
|
|
||||||
|
|
||||||
export class GetMatchedTransactionExpensesTransformer extends Transformer {
|
|
||||||
/**
|
|
||||||
* Include these attributes to sale credit note object.
|
|
||||||
* @returns {Array}
|
|
||||||
*/
|
|
||||||
public includeAttributes = (): string[] => {
|
|
||||||
return [
|
|
||||||
'referenceNo',
|
|
||||||
'amount',
|
|
||||||
'amountFormatted',
|
|
||||||
'transactionNo',
|
|
||||||
'date',
|
|
||||||
'dateFormatted',
|
|
||||||
'transactionId',
|
|
||||||
'transactionNo',
|
|
||||||
'transactionType',
|
|
||||||
'transsactionTypeFormatted',
|
|
||||||
];
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Exclude all attributes.
|
|
||||||
* @returns {Array<string>}
|
|
||||||
*/
|
|
||||||
public excludeAttributes = (): string[] => {
|
|
||||||
return ['*'];
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the expense reference number.
|
|
||||||
* @param expense
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected referenceNo(expense) {
|
|
||||||
return expense.referenceNo;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the expense amount.
|
|
||||||
* @param expense
|
|
||||||
* @returns {number}
|
|
||||||
*/
|
|
||||||
protected amount(expense) {
|
|
||||||
return expense.totalAmount;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Formats the amount of the expense.
|
|
||||||
* @param expense
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected amountFormatted(expense) {
|
|
||||||
return this.formatNumber(expense.totalAmount, {
|
|
||||||
currencyCode: expense.currencyCode,
|
|
||||||
money: true,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the date of the expense.
|
|
||||||
* @param expense
|
|
||||||
* @returns {Date}
|
|
||||||
*/
|
|
||||||
protected date(expense) {
|
|
||||||
return expense.paymentDate;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Formats the date of the expense.
|
|
||||||
* @param expense
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected dateFormatted(expense) {
|
|
||||||
return this.formatDate(expense.paymentDate);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the transaction ID of the expense.
|
|
||||||
* @param expense
|
|
||||||
* @returns {number}
|
|
||||||
*/
|
|
||||||
protected transactionId(expense) {
|
|
||||||
return expense.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the expense transaction number.
|
|
||||||
* @param expense
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected transactionNo(expense) {
|
|
||||||
return expense.expenseNo;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the expense transaction type.
|
|
||||||
* @param expense
|
|
||||||
* @returns {String}
|
|
||||||
*/
|
|
||||||
protected transactionType() {
|
|
||||||
return 'Expense';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the formatted transaction type of the expense.
|
|
||||||
* @param expense
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected transsactionTypeFormatted() {
|
|
||||||
return 'Expense';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,111 +0,0 @@
|
|||||||
import { Transformer } from '@/lib/Transformer/Transformer';
|
|
||||||
|
|
||||||
export class GetMatchedTransactionInvoicesTransformer extends Transformer {
|
|
||||||
/**
|
|
||||||
* Include these attributes to sale credit note object.
|
|
||||||
* @returns {Array}
|
|
||||||
*/
|
|
||||||
public includeAttributes = (): string[] => {
|
|
||||||
return [
|
|
||||||
'referenceNo',
|
|
||||||
'amount',
|
|
||||||
'amountFormatted',
|
|
||||||
'transactionNo',
|
|
||||||
'date',
|
|
||||||
'dateFormatted',
|
|
||||||
'transactionId',
|
|
||||||
'transactionNo',
|
|
||||||
'transactionType',
|
|
||||||
'transsactionTypeFormatted',
|
|
||||||
];
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Exclude all attributes.
|
|
||||||
* @returns {Array<string>}
|
|
||||||
*/
|
|
||||||
public excludeAttributes = (): string[] => {
|
|
||||||
return ['*'];
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the invoice reference number.
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected referenceNo(invoice) {
|
|
||||||
return invoice.referenceNo;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the invoice amount.
|
|
||||||
* @param invoice
|
|
||||||
* @returns {number}
|
|
||||||
*/
|
|
||||||
protected amount(invoice) {
|
|
||||||
return invoice.dueAmount;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Format the amount of the invoice.
|
|
||||||
* @param invoice
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected formatAmount(invoice) {
|
|
||||||
return this.formatNumber(invoice.dueAmount, {
|
|
||||||
currencyCode: invoice.currencyCode,
|
|
||||||
money: true,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the date of the invoice.
|
|
||||||
* @param invoice
|
|
||||||
* @returns {Date}
|
|
||||||
*/
|
|
||||||
protected date(invoice) {
|
|
||||||
return invoice.invoiceDate;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Format the date of the invoice.
|
|
||||||
* @param invoice
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected dateFormatted(invoice) {
|
|
||||||
return this.formatDate(invoice.invoiceDate);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the transaction ID of the invoice.
|
|
||||||
* @param invoice
|
|
||||||
* @returns {number}
|
|
||||||
*/
|
|
||||||
protected getTransactionId(invoice) {
|
|
||||||
return invoice.id;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Retrieve the invoice transaction number.
|
|
||||||
* @param invoice
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected transactionNo(invoice) {
|
|
||||||
return invoice.invoiceNo;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the invoice transaction type.
|
|
||||||
* @param invoice
|
|
||||||
* @returns {String}
|
|
||||||
*/
|
|
||||||
protected transactionType(invoice) {
|
|
||||||
return 'SaleInvoice';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the invoice formatted transaction type.
|
|
||||||
* @param invoice
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected transsactionTypeFormatted(invoice) {
|
|
||||||
return 'Sale invoice';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,111 +0,0 @@
|
|||||||
import { Transformer } from '@/lib/Transformer/Transformer';
|
|
||||||
|
|
||||||
export class GetMatchedTransactionManualJournalsTransformer extends Transformer {
|
|
||||||
/**
|
|
||||||
* Include these attributes to sale credit note object.
|
|
||||||
* @returns {Array}
|
|
||||||
*/
|
|
||||||
public includeAttributes = (): string[] => {
|
|
||||||
return [
|
|
||||||
'referenceNo',
|
|
||||||
'amount',
|
|
||||||
'amountFormatted',
|
|
||||||
'transactionNo',
|
|
||||||
'date',
|
|
||||||
'dateFormatted',
|
|
||||||
'transactionId',
|
|
||||||
'transactionNo',
|
|
||||||
'transactionType',
|
|
||||||
'transsactionTypeFormatted',
|
|
||||||
];
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Exclude all attributes.
|
|
||||||
* @returns {Array<string>}
|
|
||||||
*/
|
|
||||||
public excludeAttributes = (): string[] => {
|
|
||||||
return ['*'];
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the manual journal reference no.
|
|
||||||
* @param manualJournal
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected referenceNo(manualJournal) {
|
|
||||||
return manualJournal.referenceNo;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the manual journal amount.
|
|
||||||
* @param manualJournal
|
|
||||||
* @returns {number}
|
|
||||||
*/
|
|
||||||
protected amount(manualJournal) {
|
|
||||||
return manualJournal.amount;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the manual journal formatted amount.
|
|
||||||
* @param manualJournal
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected amountFormatted(manualJournal) {
|
|
||||||
return this.formatNumber(manualJournal.amount, {
|
|
||||||
currencyCode: manualJournal.currencyCode,
|
|
||||||
money: true,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retreives the manual journal date.
|
|
||||||
* @param manualJournal
|
|
||||||
* @returns {Date}
|
|
||||||
*/
|
|
||||||
protected date(manualJournal) {
|
|
||||||
return manualJournal.date;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the manual journal formatted date.
|
|
||||||
* @param manualJournal
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected dateFormatted(manualJournal) {
|
|
||||||
return this.formatDate(manualJournal.date);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the manual journal transaction id.
|
|
||||||
* @returns {number}
|
|
||||||
*/
|
|
||||||
protected transactionId(manualJournal) {
|
|
||||||
return manualJournal.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the manual journal transaction number.
|
|
||||||
* @param manualJournal
|
|
||||||
*/
|
|
||||||
protected transactionNo(manualJournal) {
|
|
||||||
return manualJournal.journalNumber;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the manual journal transaction type.
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected transactionType() {
|
|
||||||
return 'ManualJournal';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the manual journal formatted transaction type.
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected transsactionTypeFormatted() {
|
|
||||||
return 'Manual Journal';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,107 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import * as R from 'ramda';
|
|
||||||
import moment from 'moment';
|
|
||||||
import { PromisePool } from '@supercharge/promise-pool';
|
|
||||||
import { GetMatchedTransactionsFilter, MatchedTransactionsPOJO } from './types';
|
|
||||||
import { GetMatchedTransactionsByExpenses } from './GetMatchedTransactionsByExpenses';
|
|
||||||
import { GetMatchedTransactionsByBills } from './GetMatchedTransactionsByBills';
|
|
||||||
import { GetMatchedTransactionsByManualJournals } from './GetMatchedTransactionsByManualJournals';
|
|
||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
import { sortClosestMatchTransactions } from './_utils';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class GetMatchedTransactions {
|
|
||||||
@Inject()
|
|
||||||
private tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private getMatchedInvoicesService: GetMatchedTransactionsByExpenses;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private getMatchedBillsService: GetMatchedTransactionsByBills;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private getMatchedManualJournalService: GetMatchedTransactionsByManualJournals;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private getMatchedExpensesService: GetMatchedTransactionsByExpenses;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Registered matched transactions types.
|
|
||||||
*/
|
|
||||||
get registered() {
|
|
||||||
return [
|
|
||||||
{ type: 'SaleInvoice', service: this.getMatchedInvoicesService },
|
|
||||||
{ type: 'Bill', service: this.getMatchedBillsService },
|
|
||||||
{ type: 'Expense', service: this.getMatchedExpensesService },
|
|
||||||
{ type: 'ManualJournal', service: this.getMatchedManualJournalService },
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the matched transactions.
|
|
||||||
* @param {number} tenantId -
|
|
||||||
* @param {GetMatchedTransactionsFilter} filter -
|
|
||||||
* @returns {Promise<MatchedTransactionsPOJO>}
|
|
||||||
*/
|
|
||||||
public async getMatchedTransactions(
|
|
||||||
tenantId: number,
|
|
||||||
uncategorizedTransactionId: number,
|
|
||||||
filter: GetMatchedTransactionsFilter
|
|
||||||
): Promise<MatchedTransactionsPOJO> {
|
|
||||||
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const uncategorizedTransaction =
|
|
||||||
await UncategorizedCashflowTransaction.query()
|
|
||||||
.findById(uncategorizedTransactionId)
|
|
||||||
.throwIfNotFound();
|
|
||||||
|
|
||||||
const filtered = filter.transactionType
|
|
||||||
? this.registered.filter((item) => item.type === filter.transactionType)
|
|
||||||
: this.registered;
|
|
||||||
|
|
||||||
const matchedTransactions = await PromisePool.withConcurrency(2)
|
|
||||||
.for(filtered)
|
|
||||||
.process(async ({ type, service }) => {
|
|
||||||
return service.getMatchedTransactions(tenantId, filter);
|
|
||||||
});
|
|
||||||
|
|
||||||
const { perfectMatches, possibleMatches } = this.groupMatchedResults(
|
|
||||||
uncategorizedTransaction,
|
|
||||||
matchedTransactions
|
|
||||||
);
|
|
||||||
return {
|
|
||||||
perfectMatches,
|
|
||||||
possibleMatches,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Groups the given results for getting perfect and possible matches
|
|
||||||
* based on the given uncategorized transaction.
|
|
||||||
* @param uncategorizedTransaction
|
|
||||||
* @param matchedTransactions
|
|
||||||
* @returns {MatchedTransactionsPOJO}
|
|
||||||
*/
|
|
||||||
private groupMatchedResults(
|
|
||||||
uncategorizedTransaction,
|
|
||||||
matchedTransactions
|
|
||||||
): MatchedTransactionsPOJO {
|
|
||||||
const results = R.compose(R.flatten)(matchedTransactions?.results);
|
|
||||||
|
|
||||||
// Sort the results based on amount, date, and transaction type
|
|
||||||
const closestResullts = sortClosestMatchTransactions(
|
|
||||||
uncategorizedTransaction,
|
|
||||||
results
|
|
||||||
);
|
|
||||||
const perfectMatches = R.filter(
|
|
||||||
(match) =>
|
|
||||||
match.amount === uncategorizedTransaction.amount &&
|
|
||||||
moment(match.date).isSame(uncategorizedTransaction.date, 'day'),
|
|
||||||
closestResullts
|
|
||||||
);
|
|
||||||
const possibleMatches = R.difference(closestResullts, perfectMatches);
|
|
||||||
|
|
||||||
return { perfectMatches, possibleMatches };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
|
||||||
import { GetMatchedTransactionBillsTransformer } from './GetMatchedTransactionBillsTransformer';
|
|
||||||
import { GetMatchedTransactionsFilter, MatchedTransactionPOJO } from './types';
|
|
||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
import { GetMatchedTransactionsByType } from './GetMatchedTransactionsByType';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class GetMatchedTransactionsByBills extends GetMatchedTransactionsByType {
|
|
||||||
@Inject()
|
|
||||||
private tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private transformer: TransformerInjectable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the matched transactions.
|
|
||||||
* @param {number} tenantId -
|
|
||||||
* @param {GetMatchedTransactionsFilter} filter -
|
|
||||||
*/
|
|
||||||
public async getMatchedTransactions(
|
|
||||||
tenantId: number,
|
|
||||||
filter: GetMatchedTransactionsFilter
|
|
||||||
) {
|
|
||||||
const { Bill } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const bills = await Bill.query().onBuild((q) => {
|
|
||||||
q.whereNotExists(Bill.relatedQuery('matchedBankTransaction'));
|
|
||||||
});
|
|
||||||
|
|
||||||
return this.transformer.transform(
|
|
||||||
tenantId,
|
|
||||||
bills,
|
|
||||||
new GetMatchedTransactionBillsTransformer()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the given bill matched transaction.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} transactionId
|
|
||||||
* @returns {Promise<MatchedTransactionPOJO>}
|
|
||||||
*/
|
|
||||||
public async getMatchedTransaction(
|
|
||||||
tenantId: number,
|
|
||||||
transactionId: number
|
|
||||||
): Promise<MatchedTransactionPOJO> {
|
|
||||||
const { Bill } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const bill = await Bill.query().findById(transactionId).throwIfNotFound();
|
|
||||||
|
|
||||||
return this.transformer.transform(
|
|
||||||
tenantId,
|
|
||||||
bill,
|
|
||||||
new GetMatchedTransactionBillsTransformer()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { GetMatchedTransactionsFilter, MatchedTransactionPOJO } from './types';
|
|
||||||
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
|
||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
import { GetMatchedTransactionsByType } from './GetMatchedTransactionsByType';
|
|
||||||
import { GetMatchedTransactionExpensesTransformer } from './GetMatchedTransactionExpensesTransformer';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class GetMatchedTransactionsByExpenses extends GetMatchedTransactionsByType {
|
|
||||||
@Inject()
|
|
||||||
protected tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
protected transformer: TransformerInjectable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the matched transactions of expenses.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {GetMatchedTransactionsFilter} filter
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
async getMatchedTransactions(
|
|
||||||
tenantId: number,
|
|
||||||
filter: GetMatchedTransactionsFilter
|
|
||||||
) {
|
|
||||||
const { Expense } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const expenses = await Expense.query().onBuild((query) => {
|
|
||||||
query.whereNotExists(Expense.relatedQuery('matchedBankTransaction'));
|
|
||||||
if (filter.fromDate) {
|
|
||||||
query.where('payment_date', '>=', filter.fromDate);
|
|
||||||
}
|
|
||||||
if (filter.toDate) {
|
|
||||||
query.where('payment_date', '<=', filter.toDate);
|
|
||||||
}
|
|
||||||
if (filter.minAmount) {
|
|
||||||
query.where('total_amount', '>=', filter.minAmount);
|
|
||||||
}
|
|
||||||
if (filter.maxAmount) {
|
|
||||||
query.where('total_amount', '<=', filter.maxAmount);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return this.transformer.transform(
|
|
||||||
tenantId,
|
|
||||||
expenses,
|
|
||||||
new GetMatchedTransactionExpensesTransformer()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the given matched expense transaction.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} transactionId
|
|
||||||
* @returns {GetMatchedTransactionExpensesTransformer-}
|
|
||||||
*/
|
|
||||||
public async getMatchedTransaction(
|
|
||||||
tenantId: number,
|
|
||||||
transactionId: number
|
|
||||||
): Promise<MatchedTransactionPOJO> {
|
|
||||||
const { Expense } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const expense = await Expense.query()
|
|
||||||
.findById(transactionId)
|
|
||||||
.throwIfNotFound();
|
|
||||||
|
|
||||||
return this.transformer.transform(
|
|
||||||
tenantId,
|
|
||||||
expense,
|
|
||||||
new GetMatchedTransactionExpensesTransformer()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
|
||||||
import { GetMatchedTransactionInvoicesTransformer } from './GetMatchedTransactionInvoicesTransformer';
|
|
||||||
import {
|
|
||||||
GetMatchedTransactionsFilter,
|
|
||||||
MatchedTransactionPOJO,
|
|
||||||
MatchedTransactionsPOJO,
|
|
||||||
} from './types';
|
|
||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { GetMatchedTransactionsByType } from './GetMatchedTransactionsByType';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class GetMatchedTransactionsByInvoices extends GetMatchedTransactionsByType {
|
|
||||||
@Inject()
|
|
||||||
protected tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
protected transformer: TransformerInjectable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the matched transactions.
|
|
||||||
* @param {number} tenantId -
|
|
||||||
* @param {GetMatchedTransactionsFilter} filter -
|
|
||||||
* @returns {Promise<MatchedTransactionsPOJO>}
|
|
||||||
*/
|
|
||||||
public async getMatchedTransactions(
|
|
||||||
tenantId: number,
|
|
||||||
filter: GetMatchedTransactionsFilter
|
|
||||||
): Promise<MatchedTransactionsPOJO> {
|
|
||||||
const { SaleInvoice } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const invoices = await SaleInvoice.query().onBuild((q) => {
|
|
||||||
q.whereNotExists(SaleInvoice.relatedQuery('matchedBankTransaction'));
|
|
||||||
});
|
|
||||||
|
|
||||||
return this.transformer.transform(
|
|
||||||
tenantId,
|
|
||||||
invoices,
|
|
||||||
new GetMatchedTransactionInvoicesTransformer()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the matched transaction.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} transactionId
|
|
||||||
* @returns {Promise<MatchedTransactionPOJO>}
|
|
||||||
*/
|
|
||||||
public async getMatchedTransaction(
|
|
||||||
tenantId: number,
|
|
||||||
transactionId: number
|
|
||||||
): Promise<MatchedTransactionPOJO> {
|
|
||||||
const { SaleInvoice } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const invoice = await SaleInvoice.query().findById(transactionId);
|
|
||||||
|
|
||||||
return this.transformer.transform(
|
|
||||||
tenantId,
|
|
||||||
invoice,
|
|
||||||
new GetMatchedTransactionInvoicesTransformer()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
|
||||||
import { GetMatchedTransactionManualJournalsTransformer } from './GetMatchedTransactionManualJournalsTransformer';
|
|
||||||
import { GetMatchedTransactionsByType } from './GetMatchedTransactionsByType';
|
|
||||||
import { GetMatchedTransactionsFilter } from './types';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class GetMatchedTransactionsByManualJournals extends GetMatchedTransactionsByType {
|
|
||||||
@Inject()
|
|
||||||
private transformer: TransformerInjectable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the matched transactions of manual journals.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {GetMatchedTransactionsFilter} filter
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
async getMatchedTransactions(
|
|
||||||
tenantId: number,
|
|
||||||
filter: Omit<GetMatchedTransactionsFilter, 'transactionType'>
|
|
||||||
) {
|
|
||||||
const { ManualJournal } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const manualJournals = await ManualJournal.query().onBuild((query) => {
|
|
||||||
query.whereNotExists(
|
|
||||||
ManualJournal.relatedQuery('matchedBankTransaction')
|
|
||||||
);
|
|
||||||
if (filter.fromDate) {
|
|
||||||
query.where('date', '>=', filter.fromDate);
|
|
||||||
}
|
|
||||||
if (filter.toDate) {
|
|
||||||
query.where('date', '<=', filter.toDate);
|
|
||||||
}
|
|
||||||
if (filter.minAmount) {
|
|
||||||
query.where('amount', '>=', filter.minAmount);
|
|
||||||
}
|
|
||||||
if (filter.maxAmount) {
|
|
||||||
query.where('amount', '<=', filter.maxAmount);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return this.transformer.transform(
|
|
||||||
tenantId,
|
|
||||||
manualJournals,
|
|
||||||
new GetMatchedTransactionManualJournalsTransformer()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the matched transaction of manual journals.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} transactionId
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
async getMatchedTransaction(tenantId: number, transactionId: number) {
|
|
||||||
const { ManualJournal } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const manualJournal = await ManualJournal.query()
|
|
||||||
.findById(transactionId)
|
|
||||||
.whereNotExists(ManualJournal.relatedQuery('matchedBankTransaction'))
|
|
||||||
.throwIfNotFound();
|
|
||||||
|
|
||||||
return this.transformer.transform(
|
|
||||||
tenantId,
|
|
||||||
manualJournal,
|
|
||||||
new GetMatchedTransactionManualJournalsTransformer()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,66 +0,0 @@
|
|||||||
import { Knex } from 'knex';
|
|
||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
import {
|
|
||||||
GetMatchedTransactionsFilter,
|
|
||||||
IMatchTransactionDTO,
|
|
||||||
MatchedTransactionPOJO,
|
|
||||||
MatchedTransactionsPOJO,
|
|
||||||
} from './types';
|
|
||||||
import { Inject, Service } from 'typedi';
|
|
||||||
|
|
||||||
export abstract class GetMatchedTransactionsByType {
|
|
||||||
@Inject()
|
|
||||||
protected tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the matched transactions.
|
|
||||||
* @param {number} tenantId -
|
|
||||||
* @param {GetMatchedTransactionsFilter} filter -
|
|
||||||
* @returns {Promise<MatchedTransactionsPOJO>}
|
|
||||||
*/
|
|
||||||
public async getMatchedTransactions(
|
|
||||||
tenantId: number,
|
|
||||||
filter: GetMatchedTransactionsFilter
|
|
||||||
): Promise<MatchedTransactionsPOJO> {
|
|
||||||
throw new Error(
|
|
||||||
'The `getMatchedTransactions` method is not defined for the transaction type.'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the matched transaction details.
|
|
||||||
* @param {number} tenantId -
|
|
||||||
* @param {number} transactionId -
|
|
||||||
* @returns {Promise<MatchedTransactionPOJO>}
|
|
||||||
*/
|
|
||||||
public async getMatchedTransaction(
|
|
||||||
tenantId: number,
|
|
||||||
transactionId: number
|
|
||||||
): Promise<MatchedTransactionPOJO> {
|
|
||||||
throw new Error(
|
|
||||||
'The `getMatchedTransaction` method is not defined for the transaction type.'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} uncategorizedTransactionId
|
|
||||||
* @param {IMatchTransactionDTO} matchTransactionDTO
|
|
||||||
* @param {Knex.Transaction} trx
|
|
||||||
*/
|
|
||||||
public async createMatchedTransaction(
|
|
||||||
tenantId: number,
|
|
||||||
uncategorizedTransactionId: number,
|
|
||||||
matchTransactionDTO: IMatchTransactionDTO,
|
|
||||||
trx?: Knex.Transaction
|
|
||||||
) {
|
|
||||||
const { MatchedBankTransaction } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
await MatchedBankTransaction.query(trx).insert({
|
|
||||||
uncategorizedTransactionId,
|
|
||||||
referenceType: matchTransactionDTO.referenceType,
|
|
||||||
referenceId: matchTransactionDTO.referenceId,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { GetMatchedTransactions } from './GetMatchedTransactions';
|
|
||||||
import { MatchBankTransactions } from './MatchTransactions';
|
|
||||||
import { UnmatchMatchedBankTransaction } from './UnmatchMatchedTransaction';
|
|
||||||
import { GetMatchedTransactionsFilter, IMatchTransactionsDTO } from './types';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class MatchBankTransactionsApplication {
|
|
||||||
@Inject()
|
|
||||||
private getMatchedTransactionsService: GetMatchedTransactions;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private matchTransactionService: MatchBankTransactions;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private unmatchMatchedTransactionService: UnmatchMatchedBankTransaction;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the matched transactions.
|
|
||||||
* @param {number} tenantId -
|
|
||||||
* @param {GetMatchedTransactionsFilter} filter -
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
public getMatchedTransactions(
|
|
||||||
tenantId: number,
|
|
||||||
uncategorizedTransactionId: number,
|
|
||||||
filter: GetMatchedTransactionsFilter
|
|
||||||
) {
|
|
||||||
return this.getMatchedTransactionsService.getMatchedTransactions(
|
|
||||||
tenantId,
|
|
||||||
uncategorizedTransactionId,
|
|
||||||
filter
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Matches the given uncategorized transaction with the given system transaction.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} uncategorizedTransactionId
|
|
||||||
* @param {IMatchTransactionDTO} matchTransactionsDTO
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
public matchTransaction(
|
|
||||||
tenantId: number,
|
|
||||||
uncategorizedTransactionId: number,
|
|
||||||
matchTransactionsDTO: IMatchTransactionsDTO
|
|
||||||
): Promise<void> {
|
|
||||||
return this.matchTransactionService.matchTransaction(
|
|
||||||
tenantId,
|
|
||||||
uncategorizedTransactionId,
|
|
||||||
matchTransactionsDTO
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unmatch the given matched transaction.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} uncategorizedTransactionId
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
public unmatchMatchedTransaction(
|
|
||||||
tenantId: number,
|
|
||||||
uncategorizedTransactionId: number
|
|
||||||
) {
|
|
||||||
return this.unmatchMatchedTransactionService.unmatchMatchedTransaction(
|
|
||||||
tenantId,
|
|
||||||
uncategorizedTransactionId
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,157 +0,0 @@
|
|||||||
import { isEmpty, sumBy } from 'lodash';
|
|
||||||
import { Knex } from 'knex';
|
|
||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { PromisePool } from '@supercharge/promise-pool';
|
|
||||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
|
||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
import UnitOfWork from '@/services/UnitOfWork';
|
|
||||||
import events from '@/subscribers/events';
|
|
||||||
import {
|
|
||||||
ERRORS,
|
|
||||||
IBankTransactionMatchedEventPayload,
|
|
||||||
IBankTransactionMatchingEventPayload,
|
|
||||||
IMatchTransactionsDTO,
|
|
||||||
} from './types';
|
|
||||||
import { MatchTransactionsTypes } from './MatchTransactionsTypes';
|
|
||||||
import { ServiceError } from '@/exceptions';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class MatchBankTransactions {
|
|
||||||
@Inject()
|
|
||||||
private tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private uow: UnitOfWork;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private eventPublisher: EventPublisher;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private matchedBankTransactions: MatchTransactionsTypes;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validates the match bank transactions DTO.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} uncategorizedTransactionId
|
|
||||||
* @param {IMatchTransactionsDTO} matchTransactionsDTO
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
async validate(
|
|
||||||
tenantId: number,
|
|
||||||
uncategorizedTransactionId: number,
|
|
||||||
matchTransactionsDTO: IMatchTransactionsDTO
|
|
||||||
) {
|
|
||||||
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
|
|
||||||
const { matchedTransactions } = matchTransactionsDTO;
|
|
||||||
|
|
||||||
// Validates the uncategorized transaction existance.
|
|
||||||
const uncategorizedTransaction =
|
|
||||||
await UncategorizedCashflowTransaction.query()
|
|
||||||
.findById(uncategorizedTransactionId)
|
|
||||||
.withGraphFetched('matchedBankTransactions')
|
|
||||||
.throwIfNotFound();
|
|
||||||
|
|
||||||
// Validates the uncategorized transaction is not already matched.
|
|
||||||
if (!isEmpty(uncategorizedTransaction.matchedBankTransactions)) {
|
|
||||||
throw new ServiceError(ERRORS.TRANSACTION_ALREADY_MATCHED);
|
|
||||||
}
|
|
||||||
// Validate the uncategorized transaction is not excluded.
|
|
||||||
if (uncategorizedTransaction.excluded) {
|
|
||||||
throw new ServiceError(ERRORS.CANNOT_MATCH_EXCLUDED_TRANSACTION);
|
|
||||||
}
|
|
||||||
// Validates the given matched transaction.
|
|
||||||
const validateMatchedTransaction = async (matchedTransaction) => {
|
|
||||||
const getMatchedTransactionsService =
|
|
||||||
this.matchedBankTransactions.registry.get(
|
|
||||||
matchedTransaction.referenceType
|
|
||||||
);
|
|
||||||
if (!getMatchedTransactionsService) {
|
|
||||||
throw new ServiceError(
|
|
||||||
ERRORS.RESOURCE_TYPE_MATCHING_TRANSACTION_INVALID
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const foundMatchedTransaction =
|
|
||||||
await getMatchedTransactionsService.getMatchedTransaction(
|
|
||||||
tenantId,
|
|
||||||
matchedTransaction.referenceId
|
|
||||||
);
|
|
||||||
if (!foundMatchedTransaction) {
|
|
||||||
throw new ServiceError(ERRORS.RESOURCE_ID_MATCHING_TRANSACTION_INVALID);
|
|
||||||
}
|
|
||||||
return foundMatchedTransaction;
|
|
||||||
};
|
|
||||||
// Matches the given transactions under promise pool concurrency controlling.
|
|
||||||
const validatationResult = await PromisePool.withConcurrency(10)
|
|
||||||
.for(matchedTransactions)
|
|
||||||
.process(validateMatchedTransaction);
|
|
||||||
|
|
||||||
if (validatationResult.errors?.length > 0) {
|
|
||||||
const error = validatationResult.errors.map((er) => er.raw)[0];
|
|
||||||
throw new ServiceError(error);
|
|
||||||
}
|
|
||||||
// Calculate the total given matching transactions.
|
|
||||||
const totalMatchedTranasctions = sumBy(
|
|
||||||
validatationResult.results,
|
|
||||||
'amount'
|
|
||||||
);
|
|
||||||
// Validates the total given matching transcations whether is not equal
|
|
||||||
// uncategorized transaction amount.
|
|
||||||
if (totalMatchedTranasctions !== uncategorizedTransaction.amount) {
|
|
||||||
throw new ServiceError(ERRORS.TOTAL_MATCHING_TRANSACTIONS_INVALID);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Matches the given uncategorized transaction to the given references.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} uncategorizedTransactionId
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
public async matchTransaction(
|
|
||||||
tenantId: number,
|
|
||||||
uncategorizedTransactionId: number,
|
|
||||||
matchTransactionsDTO: IMatchTransactionsDTO
|
|
||||||
): Promise<void> {
|
|
||||||
const { matchedTransactions } = matchTransactionsDTO;
|
|
||||||
|
|
||||||
// Validates the given matching transactions DTO.
|
|
||||||
await this.validate(
|
|
||||||
tenantId,
|
|
||||||
uncategorizedTransactionId,
|
|
||||||
matchTransactionsDTO
|
|
||||||
);
|
|
||||||
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
|
|
||||||
// Triggers the event `onBankTransactionMatching`.
|
|
||||||
await this.eventPublisher.emitAsync(events.bankMatch.onMatching, {
|
|
||||||
tenantId,
|
|
||||||
uncategorizedTransactionId,
|
|
||||||
matchTransactionsDTO,
|
|
||||||
trx,
|
|
||||||
} as IBankTransactionMatchingEventPayload);
|
|
||||||
|
|
||||||
// Matches the given transactions under promise pool concurrency controlling.
|
|
||||||
await PromisePool.withConcurrency(10)
|
|
||||||
.for(matchedTransactions)
|
|
||||||
.process(async (matchedTransaction) => {
|
|
||||||
const getMatchedTransactionsService =
|
|
||||||
this.matchedBankTransactions.registry.get(
|
|
||||||
matchedTransaction.referenceType
|
|
||||||
);
|
|
||||||
await getMatchedTransactionsService.createMatchedTransaction(
|
|
||||||
tenantId,
|
|
||||||
uncategorizedTransactionId,
|
|
||||||
matchedTransaction,
|
|
||||||
trx
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Triggers the event `onBankTransactionMatched`.
|
|
||||||
await this.eventPublisher.emitAsync(events.bankMatch.onMatched, {
|
|
||||||
tenantId,
|
|
||||||
uncategorizedTransactionId,
|
|
||||||
matchTransactionsDTO,
|
|
||||||
trx,
|
|
||||||
} as IBankTransactionMatchedEventPayload);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
import Container, { Service } from 'typedi';
|
|
||||||
import { GetMatchedTransactionsByExpenses } from './GetMatchedTransactionsByExpenses';
|
|
||||||
import { GetMatchedTransactionsByBills } from './GetMatchedTransactionsByBills';
|
|
||||||
import { GetMatchedTransactionsByManualJournals } from './GetMatchedTransactionsByManualJournals';
|
|
||||||
import { MatchTransactionsTypesRegistry } from './MatchTransactionsTypesRegistry';
|
|
||||||
import { GetMatchedTransactionsByInvoices } from './GetMatchedTransactionsByInvoices';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class MatchTransactionsTypes {
|
|
||||||
private static registry: MatchTransactionsTypesRegistry;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Consttuctor method.
|
|
||||||
*/
|
|
||||||
constructor() {
|
|
||||||
this.boot();
|
|
||||||
}
|
|
||||||
|
|
||||||
get registered() {
|
|
||||||
return [
|
|
||||||
{ type: 'SaleInvoice', service: GetMatchedTransactionsByInvoices },
|
|
||||||
{ type: 'Bill', service: GetMatchedTransactionsByBills },
|
|
||||||
{ type: 'Expense', service: GetMatchedTransactionsByExpenses },
|
|
||||||
{
|
|
||||||
type: 'ManualJournal',
|
|
||||||
service: GetMatchedTransactionsByManualJournals,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Importable instances.
|
|
||||||
*/
|
|
||||||
private types = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public get registry() {
|
|
||||||
return MatchTransactionsTypes.registry;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Boots all the registered importables.
|
|
||||||
*/
|
|
||||||
public boot() {
|
|
||||||
if (!MatchTransactionsTypes.registry) {
|
|
||||||
const instance = MatchTransactionsTypesRegistry.getInstance();
|
|
||||||
|
|
||||||
this.registered.forEach((registered) => {
|
|
||||||
const serviceInstanace = Container.get(registered.service);
|
|
||||||
instance.register(registered.type, serviceInstanace);
|
|
||||||
});
|
|
||||||
MatchTransactionsTypes.registry = instance;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
import { camelCase, upperFirst } from 'lodash';
|
|
||||||
import { GetMatchedTransactionsByType } from './GetMatchedTransactionsByType';
|
|
||||||
|
|
||||||
export class MatchTransactionsTypesRegistry {
|
|
||||||
private static instance: MatchTransactionsTypesRegistry;
|
|
||||||
private importables: Record<string, GetMatchedTransactionsByType>;
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
this.importables = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets singleton instance of registry.
|
|
||||||
* @returns {MatchTransactionsTypesRegistry}
|
|
||||||
*/
|
|
||||||
public static getInstance(): MatchTransactionsTypesRegistry {
|
|
||||||
if (!MatchTransactionsTypesRegistry.instance) {
|
|
||||||
MatchTransactionsTypesRegistry.instance =
|
|
||||||
new MatchTransactionsTypesRegistry();
|
|
||||||
}
|
|
||||||
return MatchTransactionsTypesRegistry.instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Registers the given importable service.
|
|
||||||
* @param {string} resource
|
|
||||||
* @param {GetMatchedTransactionsByType} importable
|
|
||||||
*/
|
|
||||||
public register(
|
|
||||||
resource: string,
|
|
||||||
importable: GetMatchedTransactionsByType
|
|
||||||
): void {
|
|
||||||
const _resource = this.sanitizeResourceName(resource);
|
|
||||||
this.importables[_resource] = importable;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the importable service instance of the given resource name.
|
|
||||||
* @param {string} name
|
|
||||||
* @returns {GetMatchedTransactionsByType}
|
|
||||||
*/
|
|
||||||
public get(name: string): GetMatchedTransactionsByType {
|
|
||||||
const _name = this.sanitizeResourceName(name);
|
|
||||||
return this.importables[_name];
|
|
||||||
}
|
|
||||||
|
|
||||||
private sanitizeResourceName(resource: string) {
|
|
||||||
return upperFirst(camelCase(resource));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
|
||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
import UnitOfWork from '@/services/UnitOfWork';
|
|
||||||
import events from '@/subscribers/events';
|
|
||||||
import { IBankTransactionUnmatchingEventPayload } from './types';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class UnmatchMatchedBankTransaction {
|
|
||||||
@Inject()
|
|
||||||
private tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private uow: UnitOfWork;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private eventPublisher: EventPublisher;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unmatch the matched the given uncategorized bank transaction.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} uncategorizedTransactionId
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
public unmatchMatchedTransaction(
|
|
||||||
tenantId: number,
|
|
||||||
uncategorizedTransactionId: number
|
|
||||||
): Promise<void> {
|
|
||||||
const { MatchedBankTransaction } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
return this.uow.withTransaction(tenantId, async (trx) => {
|
|
||||||
await this.eventPublisher.emitAsync(events.bankMatch.onUnmatching, {
|
|
||||||
tenantId,
|
|
||||||
trx,
|
|
||||||
} as IBankTransactionUnmatchingEventPayload);
|
|
||||||
|
|
||||||
await MatchedBankTransaction.query(trx)
|
|
||||||
.where('uncategorizedTransactionId', uncategorizedTransactionId)
|
|
||||||
.delete();
|
|
||||||
|
|
||||||
await this.eventPublisher.emitAsync(events.bankMatch.onUnmatched, {
|
|
||||||
tenantId,
|
|
||||||
trx,
|
|
||||||
} as IBankTransactionUnmatchingEventPayload);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
import { ServiceError } from '@/exceptions';
|
|
||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { ERRORS } from './types';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class ValidateTransactionMatched {
|
|
||||||
@Inject()
|
|
||||||
private tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validate the given transaction whether is matched with bank transactions.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {string} referenceType - Transaction reference type.
|
|
||||||
* @param {number} referenceId - Transaction reference id.
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
public async validateTransactionNoMatchLinking(
|
|
||||||
tenantId: number,
|
|
||||||
referenceType: string,
|
|
||||||
referenceId: number
|
|
||||||
) {
|
|
||||||
const { MatchedBankTransaction } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const foundMatchedTransaction =
|
|
||||||
await MatchedBankTransaction.query().findOne({
|
|
||||||
referenceType,
|
|
||||||
referenceId,
|
|
||||||
});
|
|
||||||
if (foundMatchedTransaction) {
|
|
||||||
throw new ServiceError(ERRORS.CANNOT_DELETE_TRANSACTION_MATCHED);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
import moment from 'moment';
|
|
||||||
import * as R from 'ramda';
|
|
||||||
import UncategorizedCashflowTransaction from '@/models/UncategorizedCashflowTransaction';
|
|
||||||
import { MatchedTransactionPOJO } from './types';
|
|
||||||
|
|
||||||
export const sortClosestMatchTransactions = (
|
|
||||||
uncategorizedTransaction: UncategorizedCashflowTransaction,
|
|
||||||
matches: MatchedTransactionPOJO[]
|
|
||||||
) => {
|
|
||||||
return R.sortWith([
|
|
||||||
// Sort by amount difference (closest to uncategorized transaction amount first)
|
|
||||||
R.ascend((match: MatchedTransactionPOJO) =>
|
|
||||||
Math.abs(match.amount - uncategorizedTransaction.amount)
|
|
||||||
),
|
|
||||||
// Sort by date difference (closest to uncategorized transaction date first)
|
|
||||||
R.ascend((match: MatchedTransactionPOJO) =>
|
|
||||||
Math.abs(
|
|
||||||
moment(match.date).diff(moment(uncategorizedTransaction.date), 'days')
|
|
||||||
)
|
|
||||||
),
|
|
||||||
])(matches);
|
|
||||||
};
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { IManualJournalDeletingPayload } from '@/interfaces';
|
|
||||||
import events from '@/subscribers/events';
|
|
||||||
import { ValidateTransactionMatched } from '../ValidateTransactionsMatched';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class ValidateMatchingOnCashflowDelete {
|
|
||||||
@Inject()
|
|
||||||
private validateNoMatchingLinkedService: ValidateTransactionMatched;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor method.
|
|
||||||
*/
|
|
||||||
public attach(bus) {
|
|
||||||
bus.subscribe(
|
|
||||||
events.cashflow.onTransactionDeleting,
|
|
||||||
this.validateMatchingOnCashflowDeleting.bind(this)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validates the cashflow transaction whether matched with bank transaction on deleting.
|
|
||||||
* @param {IManualJournalDeletingPayload}
|
|
||||||
*/
|
|
||||||
public async validateMatchingOnCashflowDeleting({
|
|
||||||
tenantId,
|
|
||||||
oldManualJournal,
|
|
||||||
trx,
|
|
||||||
}: IManualJournalDeletingPayload) {
|
|
||||||
await this.validateNoMatchingLinkedService.validateTransactionNoMatchLinking(
|
|
||||||
tenantId,
|
|
||||||
'ManualJournal',
|
|
||||||
oldManualJournal.id
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { IExpenseEventDeletePayload } from '@/interfaces';
|
|
||||||
import events from '@/subscribers/events';
|
|
||||||
import { ValidateTransactionMatched } from '../ValidateTransactionsMatched';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class ValidateMatchingOnExpenseDelete {
|
|
||||||
@Inject()
|
|
||||||
private validateNoMatchingLinkedService: ValidateTransactionMatched;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor method.
|
|
||||||
*/
|
|
||||||
public attach(bus) {
|
|
||||||
bus.subscribe(
|
|
||||||
events.expenses.onDeleting,
|
|
||||||
this.validateMatchingOnExpenseDeleting.bind(this)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validates the expense transaction whether matched with bank transaction on deleting.
|
|
||||||
* @param {IExpenseEventDeletePayload}
|
|
||||||
*/
|
|
||||||
public async validateMatchingOnExpenseDeleting({
|
|
||||||
tenantId,
|
|
||||||
oldExpense,
|
|
||||||
trx,
|
|
||||||
}: IExpenseEventDeletePayload) {
|
|
||||||
await this.validateNoMatchingLinkedService.validateTransactionNoMatchLinking(
|
|
||||||
tenantId,
|
|
||||||
'Expense',
|
|
||||||
oldExpense.id
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { IManualJournalDeletingPayload } from '@/interfaces';
|
|
||||||
import events from '@/subscribers/events';
|
|
||||||
import { ValidateTransactionMatched } from '../ValidateTransactionsMatched';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class ValidateMatchingOnManualJournalDelete {
|
|
||||||
@Inject()
|
|
||||||
private validateNoMatchingLinkedService: ValidateTransactionMatched;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor method.
|
|
||||||
*/
|
|
||||||
public attach(bus) {
|
|
||||||
bus.subscribe(
|
|
||||||
events.manualJournals.onDeleting,
|
|
||||||
this.validateMatchingOnManualJournalDeleting.bind(this)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validates the manual journal transaction whether matched with bank transaction on deleting.
|
|
||||||
* @param {IManualJournalDeletingPayload}
|
|
||||||
*/
|
|
||||||
public async validateMatchingOnManualJournalDeleting({
|
|
||||||
tenantId,
|
|
||||||
oldManualJournal,
|
|
||||||
trx,
|
|
||||||
}: IManualJournalDeletingPayload) {
|
|
||||||
await this.validateNoMatchingLinkedService.validateTransactionNoMatchLinking(
|
|
||||||
tenantId,
|
|
||||||
'ManualJournal',
|
|
||||||
oldManualJournal.id
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import {
|
|
||||||
IBillPaymentEventDeletedPayload,
|
|
||||||
IPaymentReceiveDeletedPayload,
|
|
||||||
} from '@/interfaces';
|
|
||||||
import { ValidateTransactionMatched } from '../ValidateTransactionsMatched';
|
|
||||||
import events from '@/subscribers/events';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class ValidateMatchingOnPaymentMadeDelete {
|
|
||||||
@Inject()
|
|
||||||
private validateNoMatchingLinkedService: ValidateTransactionMatched;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor method.
|
|
||||||
*/
|
|
||||||
public attach(bus) {
|
|
||||||
bus.subscribe(
|
|
||||||
events.billPayment.onDeleting,
|
|
||||||
this.validateMatchingOnPaymentMadeDeleting.bind(this)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validates the payment made transaction whether matched with bank transaction on deleting.
|
|
||||||
* @param {IPaymentReceiveDeletedPayload}
|
|
||||||
*/
|
|
||||||
public async validateMatchingOnPaymentMadeDeleting({
|
|
||||||
tenantId,
|
|
||||||
oldBillPayment,
|
|
||||||
trx,
|
|
||||||
}: IBillPaymentEventDeletedPayload) {
|
|
||||||
await this.validateNoMatchingLinkedService.validateTransactionNoMatchLinking(
|
|
||||||
tenantId,
|
|
||||||
'PaymentMade',
|
|
||||||
oldBillPayment.id
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { IPaymentReceiveDeletedPayload } from '@/interfaces';
|
|
||||||
import { ValidateTransactionMatched } from '../ValidateTransactionsMatched';
|
|
||||||
import events from '@/subscribers/events';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class ValidateMatchingOnPaymentReceivedDelete {
|
|
||||||
@Inject()
|
|
||||||
private validateNoMatchingLinkedService: ValidateTransactionMatched;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor method.
|
|
||||||
*/
|
|
||||||
public attach(bus) {
|
|
||||||
bus.subscribe(
|
|
||||||
events.paymentReceive.onDeleting,
|
|
||||||
this.validateMatchingOnPaymentReceivedDeleting.bind(this)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validates the payment received transaction whether matched with bank transaction on deleting.
|
|
||||||
* @param {IPaymentReceiveDeletedPayload}
|
|
||||||
*/
|
|
||||||
public async validateMatchingOnPaymentReceivedDeleting({
|
|
||||||
tenantId,
|
|
||||||
oldPaymentReceive,
|
|
||||||
trx,
|
|
||||||
}: IPaymentReceiveDeletedPayload) {
|
|
||||||
await this.validateNoMatchingLinkedService.validateTransactionNoMatchLinking(
|
|
||||||
tenantId,
|
|
||||||
'PaymentReceive',
|
|
||||||
oldPaymentReceive.id
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
import { Knex } from 'knex';
|
|
||||||
|
|
||||||
export interface IBankTransactionMatchingEventPayload {
|
|
||||||
tenantId: number;
|
|
||||||
uncategorizedTransactionId: number;
|
|
||||||
matchTransactionsDTO: IMatchTransactionsDTO;
|
|
||||||
trx?: Knex.Transaction;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IBankTransactionMatchedEventPayload {
|
|
||||||
tenantId: number;
|
|
||||||
uncategorizedTransactionId: number;
|
|
||||||
matchTransactionsDTO: IMatchTransactionsDTO;
|
|
||||||
trx?: Knex.Transaction;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IBankTransactionUnmatchingEventPayload {
|
|
||||||
tenantId: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IBankTransactionUnmatchedEventPayload {
|
|
||||||
tenantId: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IMatchTransactionDTO {
|
|
||||||
referenceType: string;
|
|
||||||
referenceId: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IMatchTransactionsDTO {
|
|
||||||
matchedTransactions: Array<IMatchTransactionDTO>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface GetMatchedTransactionsFilter {
|
|
||||||
fromDate: string;
|
|
||||||
toDate: string;
|
|
||||||
minAmount: number;
|
|
||||||
maxAmount: number;
|
|
||||||
transactionType: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface MatchedTransactionPOJO {
|
|
||||||
amount: number;
|
|
||||||
amountFormatted: string;
|
|
||||||
date: string;
|
|
||||||
dateFormatted: string;
|
|
||||||
referenceNo: string;
|
|
||||||
transactionNo: string;
|
|
||||||
transactionId: number;
|
|
||||||
transactionType: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type MatchedTransactionsPOJO = {
|
|
||||||
perfectMatches: Array<MatchedTransactionPOJO>;
|
|
||||||
possibleMatches: Array<MatchedTransactionPOJO>;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const ERRORS = {
|
|
||||||
RESOURCE_TYPE_MATCHING_TRANSACTION_INVALID:
|
|
||||||
'RESOURCE_TYPE_MATCHING_TRANSACTION_INVALID',
|
|
||||||
RESOURCE_ID_MATCHING_TRANSACTION_INVALID:
|
|
||||||
'RESOURCE_ID_MATCHING_TRANSACTION_INVALID',
|
|
||||||
TOTAL_MATCHING_TRANSACTIONS_INVALID: 'TOTAL_MATCHING_TRANSACTIONS_INVALID',
|
|
||||||
TRANSACTION_ALREADY_MATCHED: 'TRANSACTION_ALREADY_MATCHED',
|
|
||||||
CANNOT_MATCH_EXCLUDED_TRANSACTION: 'CANNOT_MATCH_EXCLUDED_TRANSACTION',
|
|
||||||
CANNOT_DELETE_TRANSACTION_MATCHED: 'CANNOT_DELETE_TRANSACTION_MATCHED',
|
|
||||||
};
|
|
||||||
@@ -5,7 +5,6 @@ import { entries, groupBy } from 'lodash';
|
|||||||
import { CreateAccount } from '@/services/Accounts/CreateAccount';
|
import { CreateAccount } from '@/services/Accounts/CreateAccount';
|
||||||
import {
|
import {
|
||||||
IAccountCreateDTO,
|
IAccountCreateDTO,
|
||||||
IPlaidTransactionsSyncedEventPayload,
|
|
||||||
PlaidAccount,
|
PlaidAccount,
|
||||||
PlaidTransaction,
|
PlaidTransaction,
|
||||||
} from '@/interfaces';
|
} from '@/interfaces';
|
||||||
@@ -17,9 +16,6 @@ import { DeleteCashflowTransaction } from '@/services/Cashflow/DeleteCashflowTra
|
|||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||||
import { CashflowApplication } from '@/services/Cashflow/CashflowApplication';
|
import { CashflowApplication } from '@/services/Cashflow/CashflowApplication';
|
||||||
import { Knex } from 'knex';
|
import { Knex } from 'knex';
|
||||||
import { uniqid } from 'uniqid';
|
|
||||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
|
||||||
import events from '@/subscribers/events';
|
|
||||||
|
|
||||||
const CONCURRENCY_ASYNC = 10;
|
const CONCURRENCY_ASYNC = 10;
|
||||||
|
|
||||||
@@ -37,9 +33,6 @@ export class PlaidSyncDb {
|
|||||||
@Inject()
|
@Inject()
|
||||||
private deleteCashflowTransactionService: DeleteCashflowTransaction;
|
private deleteCashflowTransactionService: DeleteCashflowTransaction;
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private eventPublisher: EventPublisher;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Syncs the Plaid bank account.
|
* Syncs the Plaid bank account.
|
||||||
* @param {number} tenantId
|
* @param {number} tenantId
|
||||||
@@ -99,7 +92,6 @@ export class PlaidSyncDb {
|
|||||||
* @param {number} tenantId - Tenant ID.
|
* @param {number} tenantId - Tenant ID.
|
||||||
* @param {number} plaidAccountId - Plaid account ID.
|
* @param {number} plaidAccountId - Plaid account ID.
|
||||||
* @param {PlaidTransaction[]} plaidTranasctions - Plaid transactions
|
* @param {PlaidTransaction[]} plaidTranasctions - Plaid transactions
|
||||||
* @return {Promise<void>}
|
|
||||||
*/
|
*/
|
||||||
public async syncAccountTranactions(
|
public async syncAccountTranactions(
|
||||||
tenantId: number,
|
tenantId: number,
|
||||||
@@ -109,14 +101,18 @@ export class PlaidSyncDb {
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const { Account } = this.tenancy.models(tenantId);
|
const { Account } = this.tenancy.models(tenantId);
|
||||||
|
|
||||||
const batch = uniqid();
|
|
||||||
const cashflowAccount = await Account.query(trx)
|
const cashflowAccount = await Account.query(trx)
|
||||||
.findOne({ plaidAccountId })
|
.findOne({ plaidAccountId })
|
||||||
.throwIfNotFound();
|
.throwIfNotFound();
|
||||||
|
|
||||||
|
const openingEquityBalance = await Account.query(trx).findOne(
|
||||||
|
'slug',
|
||||||
|
'opening-balance-equity'
|
||||||
|
);
|
||||||
// Transformes the Plaid transactions to cashflow create DTOs.
|
// Transformes the Plaid transactions to cashflow create DTOs.
|
||||||
const transformTransaction = transformPlaidTrxsToCashflowCreate(
|
const transformTransaction = transformPlaidTrxsToCashflowCreate(
|
||||||
cashflowAccount.id
|
cashflowAccount.id,
|
||||||
|
openingEquityBalance.id
|
||||||
);
|
);
|
||||||
const uncategorizedTransDTOs =
|
const uncategorizedTransDTOs =
|
||||||
R.map(transformTransaction)(plaidTranasctions);
|
R.map(transformTransaction)(plaidTranasctions);
|
||||||
@@ -127,28 +123,20 @@ export class PlaidSyncDb {
|
|||||||
(uncategoriedDTO) =>
|
(uncategoriedDTO) =>
|
||||||
this.cashflowApp.createUncategorizedTransaction(
|
this.cashflowApp.createUncategorizedTransaction(
|
||||||
tenantId,
|
tenantId,
|
||||||
{ ...uncategoriedDTO, batch },
|
uncategoriedDTO,
|
||||||
trx
|
trx
|
||||||
),
|
),
|
||||||
{ concurrency: 1 }
|
{ concurrency: 1 }
|
||||||
);
|
);
|
||||||
// Triggers `onPlaidTransactionsSynced` event.
|
|
||||||
await this.eventPublisher.emitAsync(events.plaid.onTransactionsSynced, {
|
|
||||||
tenantId,
|
|
||||||
plaidAccountId,
|
|
||||||
batch,
|
|
||||||
} as IPlaidTransactionsSyncedEventPayload);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Syncs the accounts transactions in paraller under controlled concurrency.
|
* Syncs the accounts transactions in paraller under controlled concurrency.
|
||||||
* @param {number} tenantId
|
* @param {number} tenantId
|
||||||
* @param {PlaidTransaction[]} plaidTransactions
|
* @param {PlaidTransaction[]} plaidTransactions
|
||||||
* @return {Promise<void>}
|
|
||||||
*/
|
*/
|
||||||
public async syncAccountsTransactions(
|
public async syncAccountsTransactions(
|
||||||
tenantId: number,
|
tenantId: number,
|
||||||
batchNo: string,
|
|
||||||
plaidAccountsTransactions: PlaidTransaction[],
|
plaidAccountsTransactions: PlaidTransaction[],
|
||||||
trx?: Knex.Transaction
|
trx?: Knex.Transaction
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
@@ -161,7 +149,6 @@ export class PlaidSyncDb {
|
|||||||
return this.syncAccountTranactions(
|
return this.syncAccountTranactions(
|
||||||
tenantId,
|
tenantId,
|
||||||
plaidAccountId,
|
plaidAccountId,
|
||||||
batchNo,
|
|
||||||
plaidTransactions,
|
plaidTransactions,
|
||||||
trx
|
trx
|
||||||
);
|
);
|
||||||
@@ -205,14 +192,13 @@ export class PlaidSyncDb {
|
|||||||
* @param {number} tenantId - Tenant ID.
|
* @param {number} tenantId - Tenant ID.
|
||||||
* @param {string} itemId - Plaid item ID.
|
* @param {string} itemId - Plaid item ID.
|
||||||
* @param {string} lastCursor - Last transaction cursor.
|
* @param {string} lastCursor - Last transaction cursor.
|
||||||
* @return {Promise<void>}
|
|
||||||
*/
|
*/
|
||||||
public async syncTransactionsCursor(
|
public async syncTransactionsCursor(
|
||||||
tenantId: number,
|
tenantId: number,
|
||||||
plaidItemId: string,
|
plaidItemId: string,
|
||||||
lastCursor: string,
|
lastCursor: string,
|
||||||
trx?: Knex.Transaction
|
trx?: Knex.Transaction
|
||||||
): Promise<void> {
|
) {
|
||||||
const { PlaidItem } = this.tenancy.models(tenantId);
|
const { PlaidItem } = this.tenancy.models(tenantId);
|
||||||
|
|
||||||
await PlaidItem.query(trx).findOne({ plaidItemId }).patch({ lastCursor });
|
await PlaidItem.query(trx).findOne({ plaidItemId }).patch({ lastCursor });
|
||||||
@@ -222,13 +208,12 @@ export class PlaidSyncDb {
|
|||||||
* Updates the last feeds updated at of the given Plaid accounts ids.
|
* Updates the last feeds updated at of the given Plaid accounts ids.
|
||||||
* @param {number} tenantId
|
* @param {number} tenantId
|
||||||
* @param {string[]} plaidAccountIds
|
* @param {string[]} plaidAccountIds
|
||||||
* @return {Promise<void>}
|
|
||||||
*/
|
*/
|
||||||
public async updateLastFeedsUpdatedAt(
|
public async updateLastFeedsUpdatedAt(
|
||||||
tenantId: number,
|
tenantId: number,
|
||||||
plaidAccountIds: string[],
|
plaidAccountIds: string[],
|
||||||
trx?: Knex.Transaction
|
trx?: Knex.Transaction
|
||||||
): Promise<void> {
|
) {
|
||||||
const { Account } = this.tenancy.models(tenantId);
|
const { Account } = this.tenancy.models(tenantId);
|
||||||
|
|
||||||
await Account.query(trx)
|
await Account.query(trx)
|
||||||
@@ -243,14 +228,13 @@ export class PlaidSyncDb {
|
|||||||
* @param {number} tenantId
|
* @param {number} tenantId
|
||||||
* @param {number[]} plaidAccountIds
|
* @param {number[]} plaidAccountIds
|
||||||
* @param {boolean} isFeedsActive
|
* @param {boolean} isFeedsActive
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
*/
|
||||||
public async updateAccountsFeedsActive(
|
public async updateAccountsFeedsActive(
|
||||||
tenantId: number,
|
tenantId: number,
|
||||||
plaidAccountIds: string[],
|
plaidAccountIds: string[],
|
||||||
isFeedsActive: boolean = true,
|
isFeedsActive: boolean = true,
|
||||||
trx?: Knex.Transaction
|
trx?: Knex.Transaction
|
||||||
): Promise<void> {
|
) {
|
||||||
const { Account } = this.tenancy.models(tenantId);
|
const { Account } = this.tenancy.models(tenantId);
|
||||||
|
|
||||||
await Account.query(trx)
|
await Account.query(trx)
|
||||||
|
|||||||
@@ -1,42 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { EventSubscriber } from '@/lib/EventPublisher/EventPublisher';
|
|
||||||
import {
|
|
||||||
IPlaidItemCreatedEventPayload,
|
|
||||||
IPlaidTransactionsSyncedEventPayload,
|
|
||||||
} from '@/interfaces/Plaid';
|
|
||||||
import events from '@/subscribers/events';
|
|
||||||
import { RecognizeTranasctionsService } from '../../RegonizeTranasctions/RecognizeTranasctionsService';
|
|
||||||
import { runAfterTransaction } from '@/services/UnitOfWork/TransactionsHooks';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class RecognizeSyncedBankTranasctions extends EventSubscriber {
|
|
||||||
@Inject()
|
|
||||||
private recognizeTranasctionsService: RecognizeTranasctionsService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor method.
|
|
||||||
*/
|
|
||||||
public attach(bus) {
|
|
||||||
bus.subscribe(
|
|
||||||
events.plaid.onTransactionsSynced,
|
|
||||||
this.handleRecognizeSyncedBankTransactions.bind(this)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates the Plaid item transactions
|
|
||||||
* @param {IPlaidItemCreatedEventPayload} payload - Event payload.
|
|
||||||
*/
|
|
||||||
private handleRecognizeSyncedBankTransactions = async ({
|
|
||||||
tenantId,
|
|
||||||
batch,
|
|
||||||
trx,
|
|
||||||
}: IPlaidTransactionsSyncedEventPayload) => {
|
|
||||||
runAfterTransaction(trx, async () => {
|
|
||||||
await this.recognizeTranasctionsService.recognizeTransactions(
|
|
||||||
tenantId,
|
|
||||||
batch
|
|
||||||
);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,110 +0,0 @@
|
|||||||
import { Knex } from 'knex';
|
|
||||||
import UncategorizedCashflowTransaction from '@/models/UncategorizedCashflowTransaction';
|
|
||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
import { transformToMapBy } from '@/utils';
|
|
||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { PromisePool } from '@supercharge/promise-pool';
|
|
||||||
import { BankRule } from '@/models/BankRule';
|
|
||||||
import { bankRulesMatchTransaction } from './_utils';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class RecognizeTranasctionsService {
|
|
||||||
@Inject()
|
|
||||||
private tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Marks the uncategorized transaction as recognized from the given bank rule.
|
|
||||||
* @param {number} tenantId -
|
|
||||||
* @param {BankRule} bankRule -
|
|
||||||
* @param {UncategorizedCashflowTransaction} transaction -
|
|
||||||
* @param {Knex.Transaction} trx -
|
|
||||||
*/
|
|
||||||
private markBankRuleAsRecognized = async (
|
|
||||||
tenantId: number,
|
|
||||||
bankRule: BankRule,
|
|
||||||
transaction: UncategorizedCashflowTransaction,
|
|
||||||
trx?: Knex.Transaction
|
|
||||||
) => {
|
|
||||||
const { RecognizedBankTransaction, UncategorizedCashflowTransaction } =
|
|
||||||
this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const recognizedTransaction = await RecognizedBankTransaction.query(
|
|
||||||
trx
|
|
||||||
).insert({
|
|
||||||
bankRuleId: bankRule.id,
|
|
||||||
uncategorizedTransactionId: transaction.id,
|
|
||||||
assignedCategory: bankRule.assignCategory,
|
|
||||||
assignedAccountId: bankRule.assignAccountId,
|
|
||||||
assignedPayee: bankRule.assignPayee,
|
|
||||||
assignedMemo: bankRule.assignMemo,
|
|
||||||
});
|
|
||||||
await UncategorizedCashflowTransaction.query(trx)
|
|
||||||
.findById(transaction.id)
|
|
||||||
.patch({
|
|
||||||
recognizedTransactionId: recognizedTransaction.id,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Regonized the uncategorized transactions.
|
|
||||||
* @param {number} tenantId -
|
|
||||||
* @param {Knex.Transaction} trx -
|
|
||||||
*/
|
|
||||||
public async recognizeTransactions(
|
|
||||||
tenantId: number,
|
|
||||||
batch: string = '',
|
|
||||||
trx?: Knex.Transaction
|
|
||||||
) {
|
|
||||||
const { UncategorizedCashflowTransaction, BankRule } =
|
|
||||||
this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const uncategorizedTranasctions =
|
|
||||||
await UncategorizedCashflowTransaction.query().onBuild((query) => {
|
|
||||||
query.where('recognized_transaction_id', null);
|
|
||||||
query.where('categorized', false);
|
|
||||||
|
|
||||||
if (batch) query.where('batch', batch);
|
|
||||||
});
|
|
||||||
const bankRules = await BankRule.query().withGraphFetched('conditions');
|
|
||||||
const bankRulesByAccountId = transformToMapBy(
|
|
||||||
bankRules,
|
|
||||||
'applyIfAccountId'
|
|
||||||
);
|
|
||||||
// Try to recognize the transaction.
|
|
||||||
const regonizeTransaction = async (
|
|
||||||
transaction: UncategorizedCashflowTransaction
|
|
||||||
) => {
|
|
||||||
const allAccountsBankRules = bankRulesByAccountId.get(`null`);
|
|
||||||
const accountBankRules = bankRulesByAccountId.get(
|
|
||||||
`${transaction.accountId}`
|
|
||||||
);
|
|
||||||
const recognizedBankRule = bankRulesMatchTransaction(
|
|
||||||
transaction,
|
|
||||||
accountBankRules
|
|
||||||
);
|
|
||||||
if (recognizedBankRule) {
|
|
||||||
await this.markBankRuleAsRecognized(
|
|
||||||
tenantId,
|
|
||||||
recognizedBankRule,
|
|
||||||
transaction,
|
|
||||||
trx
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
const result = await PromisePool.withConcurrency(MIGRATION_CONCURRENCY)
|
|
||||||
.for(uncategorizedTranasctions)
|
|
||||||
.process((transaction: UncategorizedCashflowTransaction, index, pool) => {
|
|
||||||
return regonizeTransaction(transaction);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param {number} uncategorizedTransaction
|
|
||||||
*/
|
|
||||||
public async regonizeTransaction(
|
|
||||||
uncategorizedTransaction: UncategorizedCashflowTransaction
|
|
||||||
) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
const MIGRATION_CONCURRENCY = 10;
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
import Container, { Service } from 'typedi';
|
|
||||||
import { RecognizeTranasctionsService } from './RecognizeTranasctionsService';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class RegonizeTransactionsJob {
|
|
||||||
/**
|
|
||||||
* Constructor method.
|
|
||||||
*/
|
|
||||||
constructor(agenda) {
|
|
||||||
agenda.define(
|
|
||||||
'recognize-uncategorized-transactions-job',
|
|
||||||
{ priority: 'high', concurrency: 2 },
|
|
||||||
this.handler
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Triggers sending invoice mail.
|
|
||||||
*/
|
|
||||||
private handler = async (job, done: Function) => {
|
|
||||||
const { tenantId } = job.attrs.data;
|
|
||||||
const regonizeTransactions = Container.get(RecognizeTranasctionsService);
|
|
||||||
|
|
||||||
try {
|
|
||||||
await regonizeTransactions.recognizeTransactions(tenantId);
|
|
||||||
done();
|
|
||||||
} catch (error) {
|
|
||||||
console.log(error);
|
|
||||||
done(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,104 +0,0 @@
|
|||||||
import UncategorizedCashflowTransaction from '@/models/UncategorizedCashflowTransaction';
|
|
||||||
import {
|
|
||||||
BankRuleApplyIfTransactionType,
|
|
||||||
BankRuleConditionComparator,
|
|
||||||
BankRuleConditionType,
|
|
||||||
IBankRule,
|
|
||||||
IBankRuleCondition,
|
|
||||||
} from '../Rules/types';
|
|
||||||
import { BankRule } from '@/models/BankRule';
|
|
||||||
|
|
||||||
const conditionsMatch = (
|
|
||||||
transaction: UncategorizedCashflowTransaction,
|
|
||||||
conditions: IBankRuleCondition[],
|
|
||||||
conditionsType: BankRuleConditionType = BankRuleConditionType.And
|
|
||||||
) => {
|
|
||||||
const method =
|
|
||||||
conditionsType === BankRuleConditionType.And ? 'every' : 'some';
|
|
||||||
|
|
||||||
return conditions[method]((condition) => {
|
|
||||||
switch (determineFieldType(condition.field)) {
|
|
||||||
case 'number':
|
|
||||||
return matchNumberCondition(transaction, condition);
|
|
||||||
case 'text':
|
|
||||||
return matchTextCondition(transaction, condition);
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const matchNumberCondition = (
|
|
||||||
transaction: UncategorizedCashflowTransaction,
|
|
||||||
condition: IBankRuleCondition
|
|
||||||
) => {
|
|
||||||
switch (condition.comparator) {
|
|
||||||
case BankRuleConditionComparator.Equals:
|
|
||||||
return transaction[condition.field] === condition.value;
|
|
||||||
case BankRuleConditionComparator.Contains:
|
|
||||||
return transaction[condition.field]
|
|
||||||
?.toString()
|
|
||||||
.includes(condition.value.toString());
|
|
||||||
case BankRuleConditionComparator.NotContain:
|
|
||||||
return !transaction[condition.field]
|
|
||||||
?.toString()
|
|
||||||
.includes(condition.value.toString());
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const matchTextCondition = (
|
|
||||||
transaction: UncategorizedCashflowTransaction,
|
|
||||||
condition: IBankRuleCondition
|
|
||||||
) => {
|
|
||||||
switch (condition.comparator) {
|
|
||||||
case BankRuleConditionComparator.Equals:
|
|
||||||
return transaction[condition.field] === condition.value;
|
|
||||||
case BankRuleConditionComparator.Contains:
|
|
||||||
return transaction[condition.field]?.includes(condition.value.toString());
|
|
||||||
case BankRuleConditionComparator.NotContain:
|
|
||||||
return !transaction[condition.field]?.includes(
|
|
||||||
condition.value.toString()
|
|
||||||
);
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const matchTransactionType = (
|
|
||||||
bankRule: BankRule,
|
|
||||||
transaction: UncategorizedCashflowTransaction
|
|
||||||
): boolean => {
|
|
||||||
return (
|
|
||||||
(transaction.isDepositTransaction &&
|
|
||||||
bankRule.applyIfTransactionType ===
|
|
||||||
BankRuleApplyIfTransactionType.Deposit) ||
|
|
||||||
(transaction.isWithdrawalTransaction &&
|
|
||||||
bankRule.applyIfTransactionType ===
|
|
||||||
BankRuleApplyIfTransactionType.Withdrawal)
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const bankRulesMatchTransaction = (
|
|
||||||
transaction: UncategorizedCashflowTransaction,
|
|
||||||
bankRules: IBankRule[]
|
|
||||||
) => {
|
|
||||||
return bankRules.find((rule) => {
|
|
||||||
return (
|
|
||||||
matchTransactionType(rule, transaction) &&
|
|
||||||
conditionsMatch(transaction, rule.conditions, rule.conditionsType)
|
|
||||||
);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const determineFieldType = (field: string): string => {
|
|
||||||
switch (field) {
|
|
||||||
case 'amount':
|
|
||||||
return 'number';
|
|
||||||
case 'description':
|
|
||||||
return 'text';
|
|
||||||
default:
|
|
||||||
return 'unknown';
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -1,76 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import events from '@/subscribers/events';
|
|
||||||
import {
|
|
||||||
IBankRuleEventCreatedPayload,
|
|
||||||
IBankRuleEventDeletedPayload,
|
|
||||||
IBankRuleEventEditedPayload,
|
|
||||||
} from '../../Rules/types';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class TriggerRecognizedTransactions {
|
|
||||||
@Inject('agenda')
|
|
||||||
private agenda: any;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor method.
|
|
||||||
*/
|
|
||||||
public attach(bus) {
|
|
||||||
bus.subscribe(
|
|
||||||
events.bankRules.onCreated,
|
|
||||||
this.recognizedTransactionsOnRuleCreated.bind(this)
|
|
||||||
);
|
|
||||||
bus.subscribe(
|
|
||||||
events.bankRules.onEdited,
|
|
||||||
this.recognizedTransactionsOnRuleEdited.bind(this)
|
|
||||||
);
|
|
||||||
bus.subscribe(
|
|
||||||
events.bankRules.onDeleted,
|
|
||||||
this.recognizedTransactionsOnRuleDeleted.bind(this)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Triggers the recognize uncategorized transactions job on rule created.
|
|
||||||
* @param {IBankRuleEventCreatedPayload} payload -
|
|
||||||
*/
|
|
||||||
private async recognizedTransactionsOnRuleCreated({
|
|
||||||
tenantId,
|
|
||||||
createRuleDTO,
|
|
||||||
}: IBankRuleEventCreatedPayload) {
|
|
||||||
const payload = { tenantId };
|
|
||||||
|
|
||||||
// Cannot run recognition if the option is not enabled.
|
|
||||||
if (createRuleDTO.recognition) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
await this.agenda.now('recognize-uncategorized-transactions-job', payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Triggers the recognize uncategorized transactions job on rule edited.
|
|
||||||
* @param {IBankRuleEventEditedPayload} payload -
|
|
||||||
*/
|
|
||||||
private async recognizedTransactionsOnRuleEdited({
|
|
||||||
tenantId,
|
|
||||||
editRuleDTO,
|
|
||||||
}: IBankRuleEventEditedPayload) {
|
|
||||||
const payload = { tenantId };
|
|
||||||
|
|
||||||
// Cannot run recognition if the option is not enabled.
|
|
||||||
if (!editRuleDTO.recognition) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
await this.agenda.now('recognize-uncategorized-transactions-job', payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Triggers the recognize uncategorized transactions job on rule deleted.
|
|
||||||
* @param {IBankRuleEventDeletedPayload} payload -
|
|
||||||
*/
|
|
||||||
private async recognizedTransactionsOnRuleDeleted({
|
|
||||||
tenantId,
|
|
||||||
}: IBankRuleEventDeletedPayload) {
|
|
||||||
const payload = { tenantId };
|
|
||||||
await this.agenda.now('recognize-uncategorized-transactions-job', payload);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { CreateBankRuleService } from './CreateBankRule';
|
|
||||||
import { DeleteBankRuleSerivce } from './DeleteBankRule';
|
|
||||||
import { EditBankRuleService } from './EditBankRule';
|
|
||||||
import { GetBankRuleService } from './GetBankRule';
|
|
||||||
import { GetBankRulesService } from './GetBankRules';
|
|
||||||
import { ICreateBankRuleDTO, IEditBankRuleDTO } from './types';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class BankRulesApplication {
|
|
||||||
@Inject()
|
|
||||||
private createBankRuleService: CreateBankRuleService;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private editBankRuleService: EditBankRuleService;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private deleteBankRuleService: DeleteBankRuleSerivce;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private getBankRuleService: GetBankRuleService;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private getBankRulesService: GetBankRulesService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates new bank rule.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {ICreateBankRuleDTO} createRuleDTO
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
public createBankRule(
|
|
||||||
tenantId: number,
|
|
||||||
createRuleDTO: ICreateBankRuleDTO
|
|
||||||
): Promise<void> {
|
|
||||||
return this.createBankRuleService.createBankRule(tenantId, createRuleDTO);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Edits the given bank rule.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {IEditBankRuleDTO} editRuleDTO
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
public editBankRule(
|
|
||||||
tenantId: number,
|
|
||||||
ruleId: number,
|
|
||||||
editRuleDTO: IEditBankRuleDTO
|
|
||||||
): Promise<void> {
|
|
||||||
return this.editBankRuleService.editBankRule(tenantId, ruleId, editRuleDTO);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Deletes the given bank rule.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} ruleId
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
public deleteBankRule(tenantId: number, ruleId: number): Promise<void> {
|
|
||||||
return this.deleteBankRuleService.deleteBankRule(tenantId, ruleId);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the given bank rule.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} ruleId
|
|
||||||
* @returns {Promise<any>}
|
|
||||||
*/
|
|
||||||
public getBankRule(tenantId: number, ruleId: number): Promise<any> {
|
|
||||||
return this.getBankRuleService.getBankRule(tenantId, ruleId);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the bank rules of the given account.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} accountId
|
|
||||||
* @returns {Promise<any>}
|
|
||||||
*/
|
|
||||||
public getBankRules(tenantId: number): Promise<any> {
|
|
||||||
return this.getBankRulesService.getBankRules(tenantId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,71 +0,0 @@
|
|||||||
import { Knex } from 'knex';
|
|
||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import {
|
|
||||||
IBankRuleEventCreatedPayload,
|
|
||||||
IBankRuleEventCreatingPayload,
|
|
||||||
ICreateBankRuleDTO,
|
|
||||||
} from './types';
|
|
||||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
|
||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
import UnitOfWork from '@/services/UnitOfWork';
|
|
||||||
import events from '@/subscribers/events';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class CreateBankRuleService {
|
|
||||||
@Inject()
|
|
||||||
private tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private uow: UnitOfWork;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private eventPublisher: EventPublisher;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Transformes the DTO to model.
|
|
||||||
* @param {ICreateBankRuleDTO} createDTO
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
private transformDTO(createDTO: ICreateBankRuleDTO) {
|
|
||||||
return {
|
|
||||||
...createDTO,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new bank rule.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {ICreateBankRuleDTO} createRuleDTO
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
public createBankRule(
|
|
||||||
tenantId: number,
|
|
||||||
createRuleDTO: ICreateBankRuleDTO
|
|
||||||
): Promise<void> {
|
|
||||||
const { BankRule } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const transformDTO = this.transformDTO(createRuleDTO);
|
|
||||||
|
|
||||||
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
|
|
||||||
// Triggers `onBankRuleCreating` event.
|
|
||||||
await this.eventPublisher.emitAsync(events.bankRules.onCreating, {
|
|
||||||
tenantId,
|
|
||||||
createRuleDTO,
|
|
||||||
trx,
|
|
||||||
} as IBankRuleEventCreatingPayload);
|
|
||||||
|
|
||||||
const bankRule = await BankRule.query(trx).upsertGraph({
|
|
||||||
...transformDTO,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Triggers `onBankRuleCreated` event.
|
|
||||||
await this.eventPublisher.emitAsync(events.bankRules.onCreated, {
|
|
||||||
tenantId,
|
|
||||||
createRuleDTO,
|
|
||||||
trx,
|
|
||||||
} as IBankRuleEventCreatedPayload);
|
|
||||||
|
|
||||||
return bankRule;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
import { Knex } from 'knex';
|
|
||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import UnitOfWork from '@/services/UnitOfWork';
|
|
||||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
|
||||||
import events from '@/subscribers/events';
|
|
||||||
import {
|
|
||||||
IBankRuleEventDeletedPayload,
|
|
||||||
IBankRuleEventDeletingPayload,
|
|
||||||
} from './types';
|
|
||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class DeleteBankRuleSerivce {
|
|
||||||
@Inject()
|
|
||||||
private uow: UnitOfWork;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private eventPublisher: EventPublisher;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Deletes the given bank rule.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} ruleId
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
public async deleteBankRule(tenantId: number, ruleId: number): Promise<void> {
|
|
||||||
const { BankRule, BankRuleCondition } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const oldBankRule = await BankRule.query()
|
|
||||||
.findById(ruleId)
|
|
||||||
.throwIfNotFound();
|
|
||||||
|
|
||||||
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
|
|
||||||
// Triggers `onBankRuleDeleting` event.
|
|
||||||
await this.eventPublisher.emitAsync(events.bankRules.onDeleting, {
|
|
||||||
tenantId,
|
|
||||||
oldBankRule,
|
|
||||||
ruleId,
|
|
||||||
trx,
|
|
||||||
} as IBankRuleEventDeletingPayload);
|
|
||||||
|
|
||||||
await BankRuleCondition.query(trx).where('ruleId', ruleId).delete();
|
|
||||||
await BankRule.query(trx).findById(ruleId).delete();
|
|
||||||
|
|
||||||
// Triggers `onBankRuleDeleted` event.
|
|
||||||
await await this.eventPublisher.emitAsync(events.bankRules.onDeleted, {
|
|
||||||
tenantId,
|
|
||||||
ruleId,
|
|
||||||
trx,
|
|
||||||
} as IBankRuleEventDeletedPayload);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
import { Knex } from 'knex';
|
|
||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
|
||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
import UnitOfWork from '@/services/UnitOfWork';
|
|
||||||
import events from '@/subscribers/events';
|
|
||||||
import {
|
|
||||||
IBankRuleEventEditedPayload,
|
|
||||||
IBankRuleEventEditingPayload,
|
|
||||||
IEditBankRuleDTO,
|
|
||||||
} from './types';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class EditBankRuleService {
|
|
||||||
@Inject()
|
|
||||||
private tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private uow: UnitOfWork;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private eventPublisher: EventPublisher;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param createDTO
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
private transformDTO(createDTO: IEditBankRuleDTO) {
|
|
||||||
return {
|
|
||||||
...createDTO,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Edits the given bank rule.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} ruleId -
|
|
||||||
* @param {IEditBankRuleDTO} editBankDTO
|
|
||||||
*/
|
|
||||||
public async editBankRule(
|
|
||||||
tenantId: number,
|
|
||||||
ruleId: number,
|
|
||||||
editRuleDTO: IEditBankRuleDTO
|
|
||||||
) {
|
|
||||||
const { BankRule } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const oldBankRule = await BankRule.query()
|
|
||||||
.findById(ruleId)
|
|
||||||
.throwIfNotFound();
|
|
||||||
|
|
||||||
const tranformDTO = this.transformDTO(editRuleDTO);
|
|
||||||
|
|
||||||
return this.uow.withTransaction(
|
|
||||||
tenantId,
|
|
||||||
async (trx?: Knex.Transaction) => {
|
|
||||||
// Triggers `onBankRuleEditing` event.
|
|
||||||
await this.eventPublisher.emitAsync(events.bankRules.onEditing, {
|
|
||||||
tenantId,
|
|
||||||
oldBankRule,
|
|
||||||
ruleId,
|
|
||||||
editRuleDTO,
|
|
||||||
trx,
|
|
||||||
} as IBankRuleEventEditingPayload);
|
|
||||||
|
|
||||||
// Updates the given bank rule.
|
|
||||||
await BankRule.query(trx)
|
|
||||||
.findById(ruleId)
|
|
||||||
.patch({ ...tranformDTO });
|
|
||||||
|
|
||||||
// Triggers `onBankRuleEdited` event.
|
|
||||||
await this.eventPublisher.emitAsync(events.bankRules.onEdited, {
|
|
||||||
tenantId,
|
|
||||||
oldBankRule,
|
|
||||||
ruleId,
|
|
||||||
editRuleDTO,
|
|
||||||
trx,
|
|
||||||
} as IBankRuleEventEditedPayload);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
|
||||||
import { BankRule } from '@/models/BankRule';
|
|
||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { GetBankRuleTransformer } from './GetBankRuleTransformer';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class GetBankRuleService {
|
|
||||||
@Inject()
|
|
||||||
private tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private transformer: TransformerInjectable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the bank rule.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} ruleId
|
|
||||||
* @returns {Promise<any>}
|
|
||||||
*/
|
|
||||||
async getBankRule(tenantId: number, ruleId: number): Promise<any> {
|
|
||||||
const { BankRule } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const bankRule = await BankRule.query()
|
|
||||||
.findById(ruleId)
|
|
||||||
.withGraphFetched('conditions');
|
|
||||||
|
|
||||||
return this.transformer.transform(
|
|
||||||
tenantId,
|
|
||||||
bankRule,
|
|
||||||
new GetBankRuleTransformer()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
import { Transformer } from '@/lib/Transformer/Transformer';
|
|
||||||
|
|
||||||
export class GetBankRuleTransformer extends Transformer {
|
|
||||||
/**
|
|
||||||
* Include these attributes to sale invoice object.
|
|
||||||
* @returns {Array}
|
|
||||||
*/
|
|
||||||
public includeAttributes = (): string[] => {
|
|
||||||
return [];
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
|
||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { GetBankRulesTransformer } from './GetBankRulesTransformer';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class GetBankRulesService {
|
|
||||||
@Inject()
|
|
||||||
private tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private transformer: TransformerInjectable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the bank rules of the given account.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} accountId
|
|
||||||
* @returns {Promise<any>}
|
|
||||||
*/
|
|
||||||
public async getBankRules(tenantId: number): Promise<any> {
|
|
||||||
const { BankRule } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const bankRule = await BankRule.query()
|
|
||||||
.withGraphFetched('conditions')
|
|
||||||
.withGraphFetched('assignAccount');
|
|
||||||
|
|
||||||
return this.transformer.transform(
|
|
||||||
tenantId,
|
|
||||||
bankRule,
|
|
||||||
new GetBankRulesTransformer()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
import { upperFirst, camelCase } from 'lodash';
|
|
||||||
import { Transformer } from '@/lib/Transformer/Transformer';
|
|
||||||
import { getTransactionTypeLabel } from '@/utils/transactions-types';
|
|
||||||
|
|
||||||
export class GetBankRulesTransformer extends Transformer {
|
|
||||||
/**
|
|
||||||
* Include these attributes to sale invoice object.
|
|
||||||
* @returns {Array}
|
|
||||||
*/
|
|
||||||
public includeAttributes = (): string[] => {
|
|
||||||
return [
|
|
||||||
'assignAccountName',
|
|
||||||
'assignCategoryFormatted',
|
|
||||||
'conditionsFormatted',
|
|
||||||
];
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the assign account name.
|
|
||||||
* @param bankRule
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected assignAccountName(bankRule: any) {
|
|
||||||
return bankRule.assignAccount.name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Assigned category formatted.
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected assignCategoryFormatted(bankRule: any) {
|
|
||||||
const assignCategory = upperFirst(camelCase(bankRule.assignCategory));
|
|
||||||
return getTransactionTypeLabel(assignCategory);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the bank rule formatted conditions.
|
|
||||||
* @param bankRule
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected conditionsFormatted(bankRule: any) {
|
|
||||||
return bankRule.conditions
|
|
||||||
.map((condition) => {
|
|
||||||
const field =
|
|
||||||
condition.field.charAt(0).toUpperCase() + condition.field.slice(1);
|
|
||||||
|
|
||||||
return `${field} ${condition.comparator} ${condition.value}`;
|
|
||||||
})
|
|
||||||
.join(bankRule.conditionsType === 'and' ? ' and ' : ' or ');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import { Knex } from 'knex';
|
|
||||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
|
||||||
import UnitOfWork from '@/services/UnitOfWork';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class UnlinkBankRuleRecognizedTransactions {
|
|
||||||
@Inject()
|
|
||||||
private tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private uow: UnitOfWork;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unlinks the given bank rule out of recognized transactions.
|
|
||||||
* @param {number} tenantId - Tenant id.
|
|
||||||
* @param {number} bankRuleId - Bank rule id.
|
|
||||||
* @param {Knex.Transaction} trx - Knex transaction.
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
public async unlinkBankRuleOutRecognizedTransactions(
|
|
||||||
tenantId: number,
|
|
||||||
bankRuleId: number,
|
|
||||||
trx?: Knex.Transaction
|
|
||||||
): Promise<void> {
|
|
||||||
const { UncategorizedCashflowTransaction, RecognizedBankTransaction } =
|
|
||||||
this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
return this.uow.withTransaction(
|
|
||||||
tenantId,
|
|
||||||
async (trx: Knex.Transaction) => {
|
|
||||||
// Retrieves all the recognized transactions of the banbk rule.
|
|
||||||
const recognizedTransactions = await RecognizedBankTransaction.query(
|
|
||||||
trx
|
|
||||||
).where('bankRuleId', bankRuleId);
|
|
||||||
|
|
||||||
const uncategorizedTransactionIds = recognizedTransactions.map(
|
|
||||||
(r) => r.uncategorizedTransactionId
|
|
||||||
);
|
|
||||||
// Unlink the recongized transactions out of uncategorized transactions.
|
|
||||||
await UncategorizedCashflowTransaction.query(trx)
|
|
||||||
.whereIn('id', uncategorizedTransactionIds)
|
|
||||||
.patch({
|
|
||||||
recognizedTransactionId: null,
|
|
||||||
});
|
|
||||||
// Delete the recognized bank transactions that assocaited to bank rule.
|
|
||||||
await RecognizedBankTransaction.query(trx)
|
|
||||||
.where({ bankRuleId })
|
|
||||||
.delete();
|
|
||||||
},
|
|
||||||
trx
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import events from '@/subscribers/events';
|
|
||||||
import { UnlinkBankRuleRecognizedTransactions } from '../UnlinkBankRuleRecognizedTransactions';
|
|
||||||
import { IBankRuleEventDeletingPayload } from '../types';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class UnlinkBankRuleOnDeleteBankRule {
|
|
||||||
@Inject()
|
|
||||||
private unlinkBankRule: UnlinkBankRuleRecognizedTransactions;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor method.
|
|
||||||
*/
|
|
||||||
public attach(bus) {
|
|
||||||
bus.subscribe(
|
|
||||||
events.bankRules.onDeleting,
|
|
||||||
this.unlinkBankRuleOutRecognizedTransactionsOnRuleDeleting.bind(this)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unlinks the bank rule out of recognized transactions.
|
|
||||||
* @param {IBankRuleEventDeletingPayload} payload -
|
|
||||||
*/
|
|
||||||
private async unlinkBankRuleOutRecognizedTransactionsOnRuleDeleting({
|
|
||||||
tenantId,
|
|
||||||
ruleId,
|
|
||||||
}: IBankRuleEventDeletingPayload) {
|
|
||||||
await this.unlinkBankRule.unlinkBankRuleOutRecognizedTransactions(
|
|
||||||
tenantId,
|
|
||||||
ruleId
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,116 +0,0 @@
|
|||||||
import { Knex } from 'knex';
|
|
||||||
|
|
||||||
export enum BankRuleConditionField {
|
|
||||||
Amount = 'Amount',
|
|
||||||
Description = 'Description',
|
|
||||||
Payee = 'Payee',
|
|
||||||
}
|
|
||||||
|
|
||||||
export enum BankRuleConditionComparator {
|
|
||||||
Contains = 'contains',
|
|
||||||
Equals = 'equals',
|
|
||||||
NotContain = 'not_contain',
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IBankRuleCondition {
|
|
||||||
id?: number;
|
|
||||||
field: BankRuleConditionField;
|
|
||||||
comparator: BankRuleConditionComparator;
|
|
||||||
value: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export enum BankRuleConditionType {
|
|
||||||
Or = 'or',
|
|
||||||
And = 'and',
|
|
||||||
}
|
|
||||||
|
|
||||||
export enum BankRuleApplyIfTransactionType {
|
|
||||||
Deposit = 'deposit',
|
|
||||||
Withdrawal = 'withdrawal',
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IBankRule {
|
|
||||||
name: string;
|
|
||||||
order?: number;
|
|
||||||
applyIfAccountId: number;
|
|
||||||
applyIfTransactionType: BankRuleApplyIfTransactionType;
|
|
||||||
|
|
||||||
conditionsType: BankRuleConditionType;
|
|
||||||
conditions: IBankRuleCondition[];
|
|
||||||
|
|
||||||
assignCategory: BankRuleAssignCategory;
|
|
||||||
assignAccountId: number;
|
|
||||||
assignPayee?: string;
|
|
||||||
assignMemo?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export enum BankRuleAssignCategory {
|
|
||||||
InterestIncome = 'InterestIncome',
|
|
||||||
OtherIncome = 'OtherIncome',
|
|
||||||
Deposit = 'Deposit',
|
|
||||||
Expense = 'Expense',
|
|
||||||
OwnerDrawings = 'OwnerDrawings',
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IBankRuleConditionDTO {
|
|
||||||
id?: number;
|
|
||||||
field: string;
|
|
||||||
comparator: string;
|
|
||||||
value: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IBankRuleCommonDTO {
|
|
||||||
name: string;
|
|
||||||
order?: number;
|
|
||||||
applyIfAccountId: number;
|
|
||||||
applyIfTransactionType: string;
|
|
||||||
|
|
||||||
conditions: IBankRuleConditionDTO[];
|
|
||||||
|
|
||||||
assignCategory: BankRuleAssignCategory;
|
|
||||||
assignAccountId: number;
|
|
||||||
assignPayee?: string;
|
|
||||||
assignMemo?: string;
|
|
||||||
|
|
||||||
recognition?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ICreateBankRuleDTO extends IBankRuleCommonDTO {}
|
|
||||||
export interface IEditBankRuleDTO extends IBankRuleCommonDTO {}
|
|
||||||
|
|
||||||
export interface IBankRuleEventCreatingPayload {
|
|
||||||
tenantId: number;
|
|
||||||
createRuleDTO: ICreateBankRuleDTO;
|
|
||||||
trx?: Knex.Transaction;
|
|
||||||
}
|
|
||||||
export interface IBankRuleEventCreatedPayload {
|
|
||||||
tenantId: number;
|
|
||||||
createRuleDTO: ICreateBankRuleDTO;
|
|
||||||
trx?: Knex.Transaction;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IBankRuleEventEditingPayload {
|
|
||||||
tenantId: number;
|
|
||||||
ruleId: number;
|
|
||||||
oldBankRule: any;
|
|
||||||
editRuleDTO: IEditBankRuleDTO;
|
|
||||||
trx?: Knex.Transaction;
|
|
||||||
}
|
|
||||||
export interface IBankRuleEventEditedPayload {
|
|
||||||
tenantId: number;
|
|
||||||
ruleId: number;
|
|
||||||
editRuleDTO: IEditBankRuleDTO;
|
|
||||||
trx?: Knex.Transaction;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IBankRuleEventDeletingPayload {
|
|
||||||
tenantId: number;
|
|
||||||
oldBankRule: any;
|
|
||||||
ruleId: number;
|
|
||||||
trx?: Knex.Transaction;
|
|
||||||
}
|
|
||||||
export interface IBankRuleEventDeletedPayload {
|
|
||||||
tenantId: number;
|
|
||||||
ruleId: number;
|
|
||||||
trx?: Knex.Transaction;
|
|
||||||
}
|
|
||||||
@@ -9,7 +9,6 @@ import {
|
|||||||
ICashflowAccountsFilter,
|
ICashflowAccountsFilter,
|
||||||
ICashflowNewCommandDTO,
|
ICashflowNewCommandDTO,
|
||||||
ICategorizeCashflowTransactioDTO,
|
ICategorizeCashflowTransactioDTO,
|
||||||
IGetRecognizedTransactionsQuery,
|
|
||||||
IGetUncategorizedTransactionsQuery,
|
IGetUncategorizedTransactionsQuery,
|
||||||
} from '@/interfaces';
|
} from '@/interfaces';
|
||||||
import { CategorizeTransactionAsExpense } from './CategorizeTransactionAsExpense';
|
import { CategorizeTransactionAsExpense } from './CategorizeTransactionAsExpense';
|
||||||
@@ -19,8 +18,6 @@ import { GetUncategorizedTransaction } from './GetUncategorizedTransaction';
|
|||||||
import NewCashflowTransactionService from './NewCashflowTransactionService';
|
import NewCashflowTransactionService from './NewCashflowTransactionService';
|
||||||
import GetCashflowAccountsService from './GetCashflowAccountsService';
|
import GetCashflowAccountsService from './GetCashflowAccountsService';
|
||||||
import { GetCashflowTransactionService } from './GetCashflowTransactionsService';
|
import { GetCashflowTransactionService } from './GetCashflowTransactionsService';
|
||||||
import { GetRecognizedTransactionsService } from './GetRecongizedTransactions';
|
|
||||||
import { GetRecognizedTransactionService } from './GetRecognizedTransaction';
|
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export class CashflowApplication {
|
export class CashflowApplication {
|
||||||
@@ -54,12 +51,6 @@ export class CashflowApplication {
|
|||||||
@Inject()
|
@Inject()
|
||||||
private createUncategorizedTransactionService: CreateUncategorizedTransaction;
|
private createUncategorizedTransactionService: CreateUncategorizedTransaction;
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private getRecognizedTranasctionsService: GetRecognizedTransactionsService;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private getRecognizedTransactionService: GetRecognizedTransactionService;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new cashflow transaction.
|
* Creates a new cashflow transaction.
|
||||||
* @param {number} tenantId
|
* @param {number} tenantId
|
||||||
@@ -222,36 +213,4 @@ export class CashflowApplication {
|
|||||||
uncategorizedTransactionId
|
uncategorizedTransactionId
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the recognized bank transactions.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} accountId
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
public getRecognizedTransactions(
|
|
||||||
tenantId: number,
|
|
||||||
filter?: IGetRecognizedTransactionsQuery
|
|
||||||
) {
|
|
||||||
return this.getRecognizedTranasctionsService.getRecognizedTranactions(
|
|
||||||
tenantId,
|
|
||||||
filter
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the recognized transaction of the given uncategorized transaction.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} uncategorizedTransactionId
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
public getRecognizedTransaction(
|
|
||||||
tenantId: number,
|
|
||||||
uncategorizedTransactionId: number
|
|
||||||
) {
|
|
||||||
return this.getRecognizedTransactionService.getRecognizedTransaction(
|
|
||||||
tenantId,
|
|
||||||
uncategorizedTransactionId
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ import { Knex } from 'knex';
|
|||||||
import { transformCategorizeTransToCashflow } from './utils';
|
import { transformCategorizeTransToCashflow } from './utils';
|
||||||
import { CommandCashflowValidator } from './CommandCasflowValidator';
|
import { CommandCashflowValidator } from './CommandCasflowValidator';
|
||||||
import NewCashflowTransactionService from './NewCashflowTransactionService';
|
import NewCashflowTransactionService from './NewCashflowTransactionService';
|
||||||
import { ServiceError } from '@/exceptions';
|
|
||||||
import { ERRORS } from './constants';
|
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export class CategorizeCashflowTransaction {
|
export class CategorizeCashflowTransaction {
|
||||||
@@ -49,10 +47,6 @@ export class CategorizeCashflowTransaction {
|
|||||||
.findById(uncategorizedTransactionId)
|
.findById(uncategorizedTransactionId)
|
||||||
.throwIfNotFound();
|
.throwIfNotFound();
|
||||||
|
|
||||||
// Validate cannot categorize excluded transaction.
|
|
||||||
if (transaction.excluded) {
|
|
||||||
throw new ServiceError(ERRORS.CANNOT_CATEGORIZE_EXCLUDED_TRANSACTION);
|
|
||||||
}
|
|
||||||
// Validates the transaction shouldn't be categorized before.
|
// Validates the transaction shouldn't be categorized before.
|
||||||
this.commandValidators.validateTransactionShouldNotCategorized(transaction);
|
this.commandValidators.validateTransactionShouldNotCategorized(transaction);
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
import { Service } from "typedi";
|
|
||||||
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class CategorizeRecognizedTransactionService {
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import HasTenancyService from '../Tenancy/TenancyService';
|
|
||||||
import { GetRecognizedTransactionTransformer } from './GetRecognizedTransactionTransformer';
|
|
||||||
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class GetRecognizedTransactionService {
|
|
||||||
@Inject()
|
|
||||||
private tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private transformer: TransformerInjectable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the recognized transaction of the given uncategorized transaction.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {number} uncategorizedTransactionId
|
|
||||||
*/
|
|
||||||
public async getRecognizedTransaction(
|
|
||||||
tenantId: number,
|
|
||||||
uncategorizedTransactionId: number
|
|
||||||
) {
|
|
||||||
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const uncategorizedTransaction =
|
|
||||||
await UncategorizedCashflowTransaction.query()
|
|
||||||
.findById(uncategorizedTransactionId)
|
|
||||||
.withGraphFetched('matchedBankTransactions')
|
|
||||||
.withGraphFetched('recognizedTransaction.assignAccount')
|
|
||||||
.withGraphFetched('recognizedTransaction.bankRule')
|
|
||||||
.withGraphFetched('account')
|
|
||||||
.throwIfNotFound();
|
|
||||||
|
|
||||||
return this.transformer.transform(
|
|
||||||
tenantId,
|
|
||||||
uncategorizedTransaction,
|
|
||||||
new GetRecognizedTransactionTransformer()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,262 +0,0 @@
|
|||||||
import { Transformer } from '@/lib/Transformer/Transformer';
|
|
||||||
import { formatNumber } from '@/utils';
|
|
||||||
|
|
||||||
export class GetRecognizedTransactionTransformer extends Transformer {
|
|
||||||
/**
|
|
||||||
* Include these attributes to sale credit note object.
|
|
||||||
* @returns {Array}
|
|
||||||
*/
|
|
||||||
public includeAttributes = (): string[] => {
|
|
||||||
return [
|
|
||||||
'uncategorizedTransactionId',
|
|
||||||
'referenceNo',
|
|
||||||
'description',
|
|
||||||
'payee',
|
|
||||||
'amount',
|
|
||||||
'formattedAmount',
|
|
||||||
'date',
|
|
||||||
'formattedDate',
|
|
||||||
'assignedAccountId',
|
|
||||||
'assignedAccountName',
|
|
||||||
'assignedAccountCode',
|
|
||||||
'assignedPayee',
|
|
||||||
'assignedMemo',
|
|
||||||
'assignedCategory',
|
|
||||||
'assignedCategoryFormatted',
|
|
||||||
'withdrawal',
|
|
||||||
'deposit',
|
|
||||||
'isDepositTransaction',
|
|
||||||
'isWithdrawalTransaction',
|
|
||||||
'formattedDepositAmount',
|
|
||||||
'formattedWithdrawalAmount',
|
|
||||||
'bankRuleId',
|
|
||||||
'bankRuleName',
|
|
||||||
];
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Exclude all attributes.
|
|
||||||
* @returns {Array<string>}
|
|
||||||
*/
|
|
||||||
public excludeAttributes = (): string[] => {
|
|
||||||
return ['*'];
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the uncategorized transaction id.
|
|
||||||
* @param transaction
|
|
||||||
* @returns {number}
|
|
||||||
*/
|
|
||||||
public uncategorizedTransactionId = (transaction): number => {
|
|
||||||
return transaction.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the reference number of the transaction.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
public referenceNo(transaction: any): string {
|
|
||||||
return transaction.referenceNo;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the description of the transaction.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
public description(transaction: any): string {
|
|
||||||
return transaction.description;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the payee of the transaction.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
public payee(transaction: any): string {
|
|
||||||
return transaction.payee;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the amount of the transaction.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {number}
|
|
||||||
*/
|
|
||||||
public amount(transaction: any): number {
|
|
||||||
return transaction.amount;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the formatted amount of the transaction.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
public formattedAmount(transaction: any): string {
|
|
||||||
return this.formatNumber(transaction.formattedAmount, {
|
|
||||||
money: true,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the date of the transaction.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
public date(transaction: any): string {
|
|
||||||
return transaction.date;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the formatted date of the transaction.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
public formattedDate(transaction: any): string {
|
|
||||||
return this.formatDate(transaction.date);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the assigned account ID of the transaction.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {number}
|
|
||||||
*/
|
|
||||||
public assignedAccountId(transaction: any): number {
|
|
||||||
return transaction.recognizedTransaction.assignedAccountId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the assigned account name of the transaction.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
public assignedAccountName(transaction: any): string {
|
|
||||||
return transaction.recognizedTransaction.assignAccount.name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the assigned account code of the transaction.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
public assignedAccountCode(transaction: any): string {
|
|
||||||
return transaction.recognizedTransaction.assignAccount.code;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the assigned payee of the transaction.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
public getAssignedPayee(transaction: any): string {
|
|
||||||
return transaction.recognizedTransaction.assignedPayee;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the assigned memo of the transaction.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
public assignedMemo(transaction: any): string {
|
|
||||||
return transaction.recognizedTransaction.assignedMemo;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the assigned category of the transaction.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
public assignedCategory(transaction: any): string {
|
|
||||||
return transaction.recognizedTransaction.assignedCategory;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
public assignedCategoryFormatted() {
|
|
||||||
return 'Other Income'
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if the transaction is a withdrawal.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
public isWithdrawal(transaction: any): boolean {
|
|
||||||
return transaction.withdrawal;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if the transaction is a deposit.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
public isDeposit(transaction: any): boolean {
|
|
||||||
return transaction.deposit;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if the transaction is a deposit transaction.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
public isDepositTransaction(transaction: any): boolean {
|
|
||||||
return transaction.isDepositTransaction;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if the transaction is a withdrawal transaction.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
public isWithdrawalTransaction(transaction: any): boolean {
|
|
||||||
return transaction.isWithdrawalTransaction;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get formatted deposit amount.
|
|
||||||
* @param {any} transaction
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected formattedDepositAmount(transaction) {
|
|
||||||
if (transaction.isDepositTransaction) {
|
|
||||||
return formatNumber(transaction.deposit, {
|
|
||||||
currencyCode: transaction.currencyCode,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get formatted withdrawal amount.
|
|
||||||
* @param transaction
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected formattedWithdrawalAmount(transaction) {
|
|
||||||
if (transaction.isWithdrawalTransaction) {
|
|
||||||
return formatNumber(transaction.withdrawal, {
|
|
||||||
currencyCode: transaction.currencyCode,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the transaction bank rule id.
|
|
||||||
* @param transaction
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected bankRuleId(transaction) {
|
|
||||||
return transaction.recognizedTransaction.bankRuleId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the transaction bank rule name.
|
|
||||||
* @param transaction
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
protected bankRuleName(transaction) {
|
|
||||||
return transaction.recognizedTransaction.bankRule.name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
|
||||||
import HasTenancyService from '../Tenancy/TenancyService';
|
|
||||||
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
|
||||||
import { GetRecognizedTransactionTransformer } from './GetRecognizedTransactionTransformer';
|
|
||||||
import { IGetRecognizedTransactionsQuery } from '@/interfaces';
|
|
||||||
|
|
||||||
@Service()
|
|
||||||
export class GetRecognizedTransactionsService {
|
|
||||||
@Inject()
|
|
||||||
private tenancy: HasTenancyService;
|
|
||||||
|
|
||||||
@Inject()
|
|
||||||
private transformer: TransformerInjectable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the recognized transactions of the given account.
|
|
||||||
* @param {number} tenantId
|
|
||||||
* @param {IGetRecognizedTransactionsQuery} filter -
|
|
||||||
*/
|
|
||||||
async getRecognizedTranactions(
|
|
||||||
tenantId: number,
|
|
||||||
filter?: IGetRecognizedTransactionsQuery
|
|
||||||
) {
|
|
||||||
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
|
|
||||||
|
|
||||||
const _filter = {
|
|
||||||
page: 1,
|
|
||||||
pageSize: 20,
|
|
||||||
...filter,
|
|
||||||
};
|
|
||||||
const { results, pagination } =
|
|
||||||
await UncategorizedCashflowTransaction.query()
|
|
||||||
.onBuild((q) => {
|
|
||||||
q.withGraphFetched('recognizedTransaction.assignAccount');
|
|
||||||
q.withGraphFetched('recognizedTransaction.bankRule');
|
|
||||||
q.whereNotNull('recognizedTransactionId');
|
|
||||||
|
|
||||||
if (_filter.accountId) {
|
|
||||||
q.where('accountId', _filter.accountId);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.pagination(_filter.page - 1, _filter.pageSize);
|
|
||||||
|
|
||||||
const data = await this.transformer.transform(
|
|
||||||
tenantId,
|
|
||||||
results,
|
|
||||||
new GetRecognizedTransactionTransformer()
|
|
||||||
);
|
|
||||||
return { data, pagination };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
import { Inject, Service } from 'typedi';
|
||||||
import { initialize } from 'objection';
|
|
||||||
import HasTenancyService from '../Tenancy/TenancyService';
|
import HasTenancyService from '../Tenancy/TenancyService';
|
||||||
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
||||||
import { UncategorizedTransactionTransformer } from './UncategorizedTransactionTransformer';
|
import { UncategorizedTransactionTransformer } from './UncategorizedTransactionTransformer';
|
||||||
@@ -23,13 +22,7 @@ export class GetUncategorizedTransactions {
|
|||||||
accountId: number,
|
accountId: number,
|
||||||
query: IGetUncategorizedTransactionsQuery
|
query: IGetUncategorizedTransactionsQuery
|
||||||
) {
|
) {
|
||||||
const {
|
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
|
||||||
UncategorizedCashflowTransaction,
|
|
||||||
RecognizedBankTransaction,
|
|
||||||
MatchedBankTransaction,
|
|
||||||
Account,
|
|
||||||
} = this.tenancy.models(tenantId);
|
|
||||||
const knex = this.tenancy.knex(tenantId);
|
|
||||||
|
|
||||||
// Parsed query with default values.
|
// Parsed query with default values.
|
||||||
const _query = {
|
const _query = {
|
||||||
@@ -37,30 +30,12 @@ export class GetUncategorizedTransactions {
|
|||||||
pageSize: 20,
|
pageSize: 20,
|
||||||
...query,
|
...query,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Initialize the ORM models metadata.
|
|
||||||
await initialize(knex, [
|
|
||||||
UncategorizedCashflowTransaction,
|
|
||||||
MatchedBankTransaction,
|
|
||||||
RecognizedBankTransaction,
|
|
||||||
Account,
|
|
||||||
]);
|
|
||||||
|
|
||||||
const { results, pagination } =
|
const { results, pagination } =
|
||||||
await UncategorizedCashflowTransaction.query()
|
await UncategorizedCashflowTransaction.query()
|
||||||
.onBuild((q) => {
|
.where('accountId', accountId)
|
||||||
q.where('accountId', accountId);
|
.where('categorized', false)
|
||||||
q.where('categorized', false);
|
.withGraphFetched('account')
|
||||||
q.modify('notExcluded');
|
.orderBy('date', 'DESC')
|
||||||
|
|
||||||
q.withGraphFetched('account');
|
|
||||||
q.withGraphFetched('recognizedTransaction.assignAccount');
|
|
||||||
|
|
||||||
q.withGraphJoined('matchedBankTransactions');
|
|
||||||
|
|
||||||
q.whereNull('matchedBankTransactions.id');
|
|
||||||
q.orderBy('date', 'DESC');
|
|
||||||
})
|
|
||||||
.pagination(_query.page - 1, _query.pageSize);
|
.pagination(_query.page - 1, _query.pageSize);
|
||||||
|
|
||||||
const data = await this.transformer.transform(
|
const data = await this.transformer.transform(
|
||||||
|
|||||||
@@ -10,27 +10,11 @@ export class UncategorizedTransactionTransformer extends Transformer {
|
|||||||
return [
|
return [
|
||||||
'formattedAmount',
|
'formattedAmount',
|
||||||
'formattedDate',
|
'formattedDate',
|
||||||
'formattedDepositAmount',
|
'formattetDepositAmount',
|
||||||
'formattedWithdrawalAmount',
|
'formattedWithdrawalAmount',
|
||||||
|
|
||||||
'assignedAccountId',
|
|
||||||
'assignedAccountName',
|
|
||||||
'assignedAccountCode',
|
|
||||||
'assignedPayee',
|
|
||||||
'assignedMemo',
|
|
||||||
'assignedCategory',
|
|
||||||
'assignedCategoryFormatted',
|
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Exclude all attributes.
|
|
||||||
* @returns {Array<string>}
|
|
||||||
*/
|
|
||||||
public excludeAttributes = (): string[] => {
|
|
||||||
return ['recognizedTransaction'];
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Formattes the transaction date.
|
* Formattes the transaction date.
|
||||||
* @param transaction
|
* @param transaction
|
||||||
@@ -42,7 +26,7 @@ export class UncategorizedTransactionTransformer extends Transformer {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Formatted amount.
|
* Formatted amount.
|
||||||
* @param transaction
|
* @param transaction
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
public formattedAmount(transaction) {
|
public formattedAmount(transaction) {
|
||||||
@@ -56,7 +40,7 @@ export class UncategorizedTransactionTransformer extends Transformer {
|
|||||||
* @param transaction
|
* @param transaction
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
protected formattedDepositAmount(transaction) {
|
protected formattetDepositAmount(transaction) {
|
||||||
if (transaction.isDepositTransaction) {
|
if (transaction.isDepositTransaction) {
|
||||||
return formatNumber(transaction.deposit, {
|
return formatNumber(transaction.deposit, {
|
||||||
currencyCode: transaction.currencyCode,
|
currencyCode: transaction.currencyCode,
|
||||||
@@ -78,69 +62,4 @@ export class UncategorizedTransactionTransformer extends Transformer {
|
|||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------
|
|
||||||
// # Recgonized transaction
|
|
||||||
// --------------------------------------------------------
|
|
||||||
/**
|
|
||||||
* Get the assigned account ID of the transaction.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {number}
|
|
||||||
*/
|
|
||||||
public assignedAccountId(transaction: any): number {
|
|
||||||
return transaction.recognizedTransaction?.assignedAccountId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the assigned account name of the transaction.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
public assignedAccountName(transaction: any): string {
|
|
||||||
return transaction.recognizedTransaction?.assignAccount?.name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the assigned account code of the transaction.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
public assignedAccountCode(transaction: any): string {
|
|
||||||
return transaction.recognizedTransaction?.assignAccount?.code;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the assigned payee of the transaction.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
public getAssignedPayee(transaction: any): string {
|
|
||||||
return transaction.recognizedTransaction?.assignedPayee;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the assigned memo of the transaction.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
public assignedMemo(transaction: any): string {
|
|
||||||
return transaction.recognizedTransaction?.assignedMemo;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the assigned category of the transaction.
|
|
||||||
* @param {object} transaction
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
public assignedCategory(transaction: any): string {
|
|
||||||
return transaction.recognizedTransaction?.assignedCategory;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the assigned formatted category.
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
public assignedCategoryFormatted() {
|
|
||||||
return 'Other Income';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,8 +15,6 @@ export const ERRORS = {
|
|||||||
'UNCATEGORIZED_TRANSACTION_TYPE_INVALID',
|
'UNCATEGORIZED_TRANSACTION_TYPE_INVALID',
|
||||||
CANNOT_DELETE_TRANSACTION_CONVERTED_FROM_UNCATEGORIZED:
|
CANNOT_DELETE_TRANSACTION_CONVERTED_FROM_UNCATEGORIZED:
|
||||||
'CANNOT_DELETE_TRANSACTION_CONVERTED_FROM_UNCATEGORIZED',
|
'CANNOT_DELETE_TRANSACTION_CONVERTED_FROM_UNCATEGORIZED',
|
||||||
|
|
||||||
CANNOT_CATEGORIZE_EXCLUDED_TRANSACTION: 'CANNOT_CATEGORIZE_EXCLUDED_TRANSACTION'
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export enum CASHFLOW_DIRECTION {
|
export enum CASHFLOW_DIRECTION {
|
||||||
|
|||||||
@@ -50,8 +50,8 @@ export class JournalSheetTable extends R.compose(
|
|||||||
{ key: 'description', accessor: 'entry.note' },
|
{ key: 'description', accessor: 'entry.note' },
|
||||||
{ key: 'account_code', accessor: 'entry.accountCode' },
|
{ key: 'account_code', accessor: 'entry.accountCode' },
|
||||||
{ key: 'account_name', accessor: 'entry.accountName' },
|
{ key: 'account_name', accessor: 'entry.accountName' },
|
||||||
{ key: 'debit', accessor: 'entry.formattedDebit' },
|
|
||||||
{ key: 'credit', accessor: 'entry.formattedCredit' },
|
{ key: 'credit', accessor: 'entry.formattedCredit' },
|
||||||
|
{ key: 'debit', accessor: 'entry.formattedDebit' },
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -67,8 +67,8 @@ export class JournalSheetTable extends R.compose(
|
|||||||
{ key: 'description', accessor: 'note' },
|
{ key: 'description', accessor: 'note' },
|
||||||
{ key: 'account_code', accessor: 'accountCode' },
|
{ key: 'account_code', accessor: 'accountCode' },
|
||||||
{ key: 'account_name', accessor: 'accountName' },
|
{ key: 'account_name', accessor: 'accountName' },
|
||||||
{ key: 'debit', accessor: 'formattedDebit' },
|
|
||||||
{ key: 'credit', accessor: 'formattedCredit' },
|
{ key: 'credit', accessor: 'formattedCredit' },
|
||||||
|
{ key: 'debit', accessor: 'formattedDebit' },
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -84,8 +84,8 @@ export class JournalSheetTable extends R.compose(
|
|||||||
{ key: 'description', accessor: '_empty_' },
|
{ key: 'description', accessor: '_empty_' },
|
||||||
{ key: 'account_code', accessor: '_empty_' },
|
{ key: 'account_code', accessor: '_empty_' },
|
||||||
{ key: 'account_name', accessor: '_empty_' },
|
{ key: 'account_name', accessor: '_empty_' },
|
||||||
{ key: 'debit', accessor: 'formattedDebit' },
|
|
||||||
{ key: 'credit', accessor: 'formattedCredit' },
|
{ key: 'credit', accessor: 'formattedCredit' },
|
||||||
|
{ key: 'debit', accessor: 'formattedDebit' },
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -101,8 +101,8 @@ export class JournalSheetTable extends R.compose(
|
|||||||
{ key: 'description', value: '' },
|
{ key: 'description', value: '' },
|
||||||
{ key: 'account_code', value: '' },
|
{ key: 'account_code', value: '' },
|
||||||
{ key: 'account_name', value: '' },
|
{ key: 'account_name', value: '' },
|
||||||
{ key: 'debit', value: '' },
|
|
||||||
{ key: 'credit', value: '' },
|
{ key: 'credit', value: '' },
|
||||||
|
{ key: 'debit', value: '' },
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -118,8 +118,8 @@ export class JournalSheetTable extends R.compose(
|
|||||||
{ key: 'description', label: 'Description' },
|
{ key: 'description', label: 'Description' },
|
||||||
{ key: 'account_code', label: 'Acc. Code' },
|
{ key: 'account_code', label: 'Acc. Code' },
|
||||||
{ key: 'account_name', label: 'Account' },
|
{ key: 'account_name', label: 'Account' },
|
||||||
{ key: 'debit', label: 'Debit' },
|
|
||||||
{ key: 'credit', label: 'Credit' },
|
{ key: 'credit', label: 'Credit' },
|
||||||
|
{ key: 'debit', label: 'Debit' },
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -205,7 +205,7 @@ export default {
|
|||||||
|
|
||||||
onPreMailSend: 'onSaleReceiptPreMailSend',
|
onPreMailSend: 'onSaleReceiptPreMailSend',
|
||||||
onMailSend: 'onSaleReceiptMailSend',
|
onMailSend: 'onSaleReceiptMailSend',
|
||||||
onMailSent: 'onSaleReceiptMailSent',
|
onMailSent: 'onSaleReceiptMailSent',
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -229,7 +229,7 @@ export default {
|
|||||||
|
|
||||||
onPreMailSend: 'onPaymentReceivePreMailSend',
|
onPreMailSend: 'onPaymentReceivePreMailSend',
|
||||||
onMailSend: 'onPaymentReceiveMailSend',
|
onMailSend: 'onPaymentReceiveMailSend',
|
||||||
onMailSent: 'onPaymentReceiveMailSent',
|
onMailSent: 'onPaymentReceiveMailSent',
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -616,27 +616,5 @@ export default {
|
|||||||
|
|
||||||
plaid: {
|
plaid: {
|
||||||
onItemCreated: 'onPlaidItemCreated',
|
onItemCreated: 'onPlaidItemCreated',
|
||||||
onTransactionsSynced: 'onPlaidTransactionsSynced',
|
|
||||||
},
|
|
||||||
|
|
||||||
// Bank rules.
|
|
||||||
bankRules: {
|
|
||||||
onCreating: 'onBankRuleCreating',
|
|
||||||
onCreated: 'onBankRuleCreated',
|
|
||||||
|
|
||||||
onEditing: 'onBankRuleEditing',
|
|
||||||
onEdited: 'onBankRuleEdited',
|
|
||||||
|
|
||||||
onDeleting: 'onBankRuleDeleting',
|
|
||||||
onDeleted: 'onBankRuleDeleted',
|
|
||||||
},
|
|
||||||
|
|
||||||
// Bank matching.
|
|
||||||
bankMatch: {
|
|
||||||
onMatching: 'onBankTransactionMatching',
|
|
||||||
onMatched: 'onBankTransactionMatched',
|
|
||||||
|
|
||||||
onUnmatching: 'onBankTransactionUnmathcing',
|
|
||||||
onUnmatched: 'onBankTransactionUnmathced',
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
:root {
|
|
||||||
--aside-topbar-offset: 60px;
|
|
||||||
--aside-width: 34%;
|
|
||||||
--aside-min-width: 400px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.main{
|
|
||||||
flex: 1 0;
|
|
||||||
height: inherit;
|
|
||||||
overflow: auto;
|
|
||||||
}
|
|
||||||
|
|
||||||
.aside{
|
|
||||||
width: var(--aside-width);
|
|
||||||
min-width: var(--aside-min-width);
|
|
||||||
height: 100dvh;
|
|
||||||
border-left: 1px solid rgba(17, 20, 24, 0.15);
|
|
||||||
height: inherit;
|
|
||||||
overflow: auto;
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
}
|
|
||||||
|
|
||||||
.root {
|
|
||||||
display: flex;
|
|
||||||
height: calc(100dvh - var(--aside-topbar-offset));
|
|
||||||
}
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
import React from 'react';
|
|
||||||
import { AppShellProvider, useAppShellContext } from './AppContentShellProvider';
|
|
||||||
import { Box, BoxProps } from '../../Layout';
|
|
||||||
import styles from './AppContentShell.module.scss';
|
|
||||||
|
|
||||||
interface AppContentShellProps {
|
|
||||||
topbarOffset?: number;
|
|
||||||
mainProps?: BoxProps;
|
|
||||||
asideProps?: BoxProps;
|
|
||||||
children: React.ReactNode;
|
|
||||||
hideAside?: boolean;
|
|
||||||
hideMain?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function AppContentShell({
|
|
||||||
asideProps,
|
|
||||||
mainProps,
|
|
||||||
topbarOffset = 0,
|
|
||||||
hideAside = false,
|
|
||||||
hideMain = false,
|
|
||||||
...restProps
|
|
||||||
}: AppContentShellProps) {
|
|
||||||
return (
|
|
||||||
<AppShellProvider
|
|
||||||
mainProps={mainProps}
|
|
||||||
asideProps={asideProps}
|
|
||||||
topbarOffset={topbarOffset}
|
|
||||||
hideAside={hideAside}
|
|
||||||
hideMain={hideMain}
|
|
||||||
>
|
|
||||||
<Box {...restProps} className={styles.root} />
|
|
||||||
</AppShellProvider>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
interface AppContentShellMainProps extends BoxProps {}
|
|
||||||
|
|
||||||
function AppContentShellMain({ ...props }: AppContentShellMainProps) {
|
|
||||||
const { hideMain } = useAppShellContext();
|
|
||||||
|
|
||||||
if (hideMain === true) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return <Box {...props} className={styles.main} />;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface AppContentShellAsideProps extends BoxProps {
|
|
||||||
children: React.ReactNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
function AppContentShellAside({ ...props }: AppContentShellAsideProps) {
|
|
||||||
const { hideAside } = useAppShellContext();
|
|
||||||
|
|
||||||
if (hideAside === true) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return <Box {...props} className={styles.aside} />;
|
|
||||||
}
|
|
||||||
|
|
||||||
AppContentShell.Main = AppContentShellMain;
|
|
||||||
AppContentShell.Aside = AppContentShellAside;
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
// @ts-nocheck
|
|
||||||
import React, { createContext } from 'react';
|
|
||||||
|
|
||||||
interface ContentShellCommonValue {
|
|
||||||
mainProps: any;
|
|
||||||
asideProps: any;
|
|
||||||
topbarOffset: number;
|
|
||||||
hideAside: boolean;
|
|
||||||
hideMain: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface AppShellContextValue extends ContentShellCommonValue {
|
|
||||||
topbarOffset: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
const AppShellContext = createContext<AppShellContextValue>(
|
|
||||||
{} as AppShellContextValue,
|
|
||||||
);
|
|
||||||
|
|
||||||
interface AppShellProviderProps extends ContentShellCommonValue {
|
|
||||||
children: React.ReactNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function AppShellProvider({
|
|
||||||
topbarOffset,
|
|
||||||
hideAside,
|
|
||||||
hideMain,
|
|
||||||
...props
|
|
||||||
}: AppShellProviderProps) {
|
|
||||||
const provider = {
|
|
||||||
topbarOffset,
|
|
||||||
hideAside,
|
|
||||||
hideMain,
|
|
||||||
} as AppShellContextValue;
|
|
||||||
|
|
||||||
return <AppShellContext.Provider value={provider} {...props} />;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const useAppShellContext = () =>
|
|
||||||
React.useContext<AppShellContextValue>(AppShellContext);
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
export * from './AppContentShell';
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
export * from './AppContentShell';
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
.root{
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
flex: 1 1 auto;
|
|
||||||
}
|
|
||||||
|
|
||||||
.title{
|
|
||||||
align-items: center;
|
|
||||||
background: #fff;
|
|
||||||
border-bottom: 1px solid #E1E2E9;
|
|
||||||
display: flex;
|
|
||||||
flex: 0 0 auto;
|
|
||||||
min-height: 40px;
|
|
||||||
padding: 5px 5px 5px 15px;
|
|
||||||
z-index: 0;
|
|
||||||
font-weight: 600;
|
|
||||||
}
|
|
||||||
|
|
||||||
.content{
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
flex: 1 1 auto;
|
|
||||||
background-color: #fff;
|
|
||||||
overflow-y: auto;
|
|
||||||
}
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
import { Button, Classes } from '@blueprintjs/core';
|
|
||||||
import clsx from 'classnames';
|
|
||||||
import { Box, BoxProps, Group } from '../Layout';
|
|
||||||
import { Icon } from '../Icon';
|
|
||||||
import styles from './Aside.module.scss';
|
|
||||||
|
|
||||||
interface AsideProps extends BoxProps {
|
|
||||||
title?: string;
|
|
||||||
onClose?: () => void;
|
|
||||||
children?: React.ReactNode;
|
|
||||||
hideCloseButton?: boolean;
|
|
||||||
classNames?: Record<string, string>;
|
|
||||||
className?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function Aside({
|
|
||||||
title,
|
|
||||||
onClose,
|
|
||||||
children,
|
|
||||||
hideCloseButton,
|
|
||||||
classNames,
|
|
||||||
className
|
|
||||||
}: AsideProps) {
|
|
||||||
const handleClose = () => {
|
|
||||||
onClose && onClose();
|
|
||||||
};
|
|
||||||
return (
|
|
||||||
<Box className={clsx(styles.root, className, classNames?.root)}>
|
|
||||||
<Group position="apart" className={clsx(styles.title, classNames?.title)}>
|
|
||||||
{title}
|
|
||||||
|
|
||||||
{hideCloseButton !== true && (
|
|
||||||
<Button
|
|
||||||
aria-label="Close"
|
|
||||||
className={Classes.DIALOG_CLOSE_BUTTON}
|
|
||||||
icon={<Icon icon={'smallCross'} color={'#000'} />}
|
|
||||||
minimal={true}
|
|
||||||
onClick={handleClose}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</Group>
|
|
||||||
|
|
||||||
{children}
|
|
||||||
</Box>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
interface AsideContentProps extends BoxProps {}
|
|
||||||
|
|
||||||
function AsideContent({ ...props }: AsideContentProps) {
|
|
||||||
return <Box {...props} className={clsx(styles.content, props?.className)} />;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface AsideFooterProps extends BoxProps {}
|
|
||||||
|
|
||||||
function AsideFooter({ ...props }: AsideFooterProps) {
|
|
||||||
return <Box {...props} />;
|
|
||||||
}
|
|
||||||
|
|
||||||
Aside.Body = AsideContent;
|
|
||||||
Aside.Footer = AsideFooter;
|
|
||||||
@@ -19,12 +19,6 @@ const ContentTabItemRoot = styled.button<ContentTabItemRootProps>`
|
|||||||
text-align: left;
|
text-align: left;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
|
|
||||||
${(props) =>
|
|
||||||
props.small &&
|
|
||||||
`
|
|
||||||
padding: 8px 10px;
|
|
||||||
`}
|
|
||||||
|
|
||||||
${(props) =>
|
${(props) =>
|
||||||
props.active &&
|
props.active &&
|
||||||
`
|
`
|
||||||
@@ -61,8 +55,6 @@ interface ContentTabsItemProps {
|
|||||||
title?: React.ReactNode;
|
title?: React.ReactNode;
|
||||||
description?: React.ReactNode;
|
description?: React.ReactNode;
|
||||||
active?: boolean;
|
active?: boolean;
|
||||||
className?: string;
|
|
||||||
small?: booean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const ContentTabsItem = ({
|
const ContentTabsItem = ({
|
||||||
@@ -70,18 +62,11 @@ const ContentTabsItem = ({
|
|||||||
description,
|
description,
|
||||||
active,
|
active,
|
||||||
onClick,
|
onClick,
|
||||||
small,
|
|
||||||
className,
|
|
||||||
}: ContentTabsItemProps) => {
|
}: ContentTabsItemProps) => {
|
||||||
return (
|
return (
|
||||||
<ContentTabItemRoot
|
<ContentTabItemRoot active={active} onClick={onClick}>
|
||||||
active={active}
|
|
||||||
onClick={onClick}
|
|
||||||
className={className}
|
|
||||||
small={small}
|
|
||||||
>
|
|
||||||
<ContentTabTitle>{title}</ContentTabTitle>
|
<ContentTabTitle>{title}</ContentTabTitle>
|
||||||
{description && <ContentTabDesc>{description}</ContentTabDesc>}
|
<ContentTabDesc>{description}</ContentTabDesc>
|
||||||
</ContentTabItemRoot>
|
</ContentTabItemRoot>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@@ -92,7 +77,6 @@ interface ContentTabsProps {
|
|||||||
onChange?: (value: string) => void;
|
onChange?: (value: string) => void;
|
||||||
children?: React.ReactNode;
|
children?: React.ReactNode;
|
||||||
className?: string;
|
className?: string;
|
||||||
small?: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ContentTabs({
|
export function ContentTabs({
|
||||||
@@ -101,7 +85,6 @@ export function ContentTabs({
|
|||||||
onChange,
|
onChange,
|
||||||
children,
|
children,
|
||||||
className,
|
className,
|
||||||
small,
|
|
||||||
}: ContentTabsProps) {
|
}: ContentTabsProps) {
|
||||||
const [localValue, handleItemChange] = useUncontrolled<string>({
|
const [localValue, handleItemChange] = useUncontrolled<string>({
|
||||||
initialValue,
|
initialValue,
|
||||||
@@ -119,7 +102,6 @@ export function ContentTabs({
|
|||||||
{...tab.props}
|
{...tab.props}
|
||||||
active={localValue === tab.props.id}
|
active={localValue === tab.props.id}
|
||||||
onClick={() => handleItemChange(tab.props?.id)}
|
onClick={() => handleItemChange(tab.props?.id)}
|
||||||
small={small}
|
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</ContentTabsRoot>
|
</ContentTabsRoot>
|
||||||
|
|||||||
@@ -51,7 +51,6 @@ import EstimateMailDialog from '@/containers/Sales/Estimates/EstimateMailDialog/
|
|||||||
import ReceiptMailDialog from '@/containers/Sales/Receipts/ReceiptMailDialog/ReceiptMailDialog';
|
import ReceiptMailDialog from '@/containers/Sales/Receipts/ReceiptMailDialog/ReceiptMailDialog';
|
||||||
import PaymentMailDialog from '@/containers/Sales/PaymentReceives/PaymentMailDialog/PaymentMailDialog';
|
import PaymentMailDialog from '@/containers/Sales/PaymentReceives/PaymentMailDialog/PaymentMailDialog';
|
||||||
import { ExportDialog } from '@/containers/Dialogs/ExportDialog';
|
import { ExportDialog } from '@/containers/Dialogs/ExportDialog';
|
||||||
import { RuleFormDialog } from '@/containers/Banking/Rules/RuleFormDialog/RuleFormDialog';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dialogs container.
|
* Dialogs container.
|
||||||
@@ -148,7 +147,6 @@ export default function DialogsContainer() {
|
|||||||
<ReceiptMailDialog dialogName={DialogsName.ReceiptMail} />
|
<ReceiptMailDialog dialogName={DialogsName.ReceiptMail} />
|
||||||
<PaymentMailDialog dialogName={DialogsName.PaymentMail} />
|
<PaymentMailDialog dialogName={DialogsName.PaymentMail} />
|
||||||
<ExportDialog dialogName={DialogsName.Export} />
|
<ExportDialog dialogName={DialogsName.Export} />
|
||||||
<RuleFormDialog dialogName={DialogsName.BankRuleForm} />
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user