Compare commits
86 Commits
migrate-se
...
users-modu
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
985e1dbc01 | ||
|
|
ce058b9416 | ||
|
|
4de1ef71ca | ||
|
|
ecb80b2cf2 | ||
|
|
aef208b9d8 | ||
|
|
c096135d9f | ||
|
|
0c9d961272 | ||
|
|
c10cad4256 | ||
|
|
9ebd967fe7 | ||
|
|
a42143a996 | ||
|
|
7506c2f37f | ||
|
|
3c8b7c92fe | ||
|
|
f78d6efe27 | ||
|
|
401b3dc111 | ||
|
|
c9d752d102 | ||
|
|
4f6ad2b293 | ||
|
|
1d53063e09 | ||
|
|
51de3631fc | ||
|
|
b9755ff01c | ||
|
|
5bfff51093 | ||
|
|
1bcee9293c | ||
|
|
c953c48c39 | ||
|
|
ab49113d5a | ||
|
|
d851e5b646 | ||
|
|
e8f1fedf35 | ||
|
|
04c25bd31a | ||
|
|
6287f8b6e3 | ||
|
|
4febc4e502 | ||
|
|
443fbdd89e | ||
|
|
55fcc908ef | ||
|
|
f068218a16 | ||
|
|
842a862b87 | ||
|
|
1ed77dd5ed | ||
|
|
e47ca98171 | ||
|
|
503d0016ea | ||
|
|
0a2ac4ee56 | ||
|
|
8eb23d3a6f | ||
|
|
18017d25d5 | ||
|
|
f11b09cd87 | ||
|
|
ed81d4c1e0 | ||
|
|
88f66f1c1c | ||
|
|
ab717b96ac | ||
|
|
caff6ce47c | ||
|
|
682be715ae | ||
|
|
85946d3161 | ||
|
|
173610d0fa | ||
|
|
f20f07a42f | ||
|
|
6251831741 | ||
|
|
1cfddf2b4d | ||
|
|
6461a2318f | ||
|
|
92d98ce1d3 | ||
|
|
ef22b9ddaf | ||
|
|
4c42515613 | ||
|
|
2eb56e5850 | ||
|
|
136cc907bb | ||
|
|
fd65ee9428 | ||
|
|
08de50e2b1 | ||
|
|
197d173db9 | ||
|
|
cf496909a5 | ||
|
|
67ae7ad037 | ||
|
|
40b7daa2e3 | ||
|
|
b7d0b6c24a | ||
|
|
95bb4fc8e3 | ||
|
|
5c0bb52b59 | ||
|
|
36851d3209 | ||
|
|
9eee0b384d | ||
|
|
9539003cac | ||
|
|
2017539032 | ||
|
|
c4692d1716 | ||
|
|
7b81d0c8e5 | ||
|
|
9a5110aa38 | ||
|
|
2e1c57438c | ||
|
|
b46f2a91c3 | ||
|
|
8e36aab529 | ||
|
|
9eec60ea22 | ||
|
|
6550e88af3 | ||
|
|
dfc5674088 | ||
|
|
6dd854178d | ||
|
|
520d053b36 | ||
|
|
108d286f62 | ||
|
|
271c46ea3b | ||
|
|
7bcd578c11 | ||
|
|
936800600b | ||
|
|
e7e7a95aa1 | ||
|
|
081fdebee0 | ||
|
|
4ab20ac76a |
@@ -1,3 +1,6 @@
|
||||
# App
|
||||
APP_JWT_SECRET=123123
|
||||
|
||||
# Mail
|
||||
MAIL_HOST=
|
||||
MAIL_USERNAME=
|
||||
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -6,4 +6,5 @@ node_modules/
|
||||
# Production env file
|
||||
.env
|
||||
|
||||
test-results/
|
||||
test-results/
|
||||
.qodo
|
||||
|
||||
@@ -34,6 +34,8 @@
|
||||
</p>
|
||||
</p>
|
||||
|
||||
> We are currently in the process of migrating all server-side API endpoints to NestJS to establish a more solid architecture. Some endpoints in development mode may be temporarily do not work during this stabilization phase. However, this migration doesn't affect the production Docker images, which remain on the latest stable version.
|
||||
|
||||
# What's Bigcapital?
|
||||
|
||||
Bigcapital is a smart and open-source accounting and inventory software, Bigcapital keeps all business finances in right place and automates accounting processes to give the business powerful and intelligent financial statements and reports to help in making decisions.
|
||||
|
||||
@@ -41,6 +41,8 @@ services:
|
||||
context: ./docker/redis
|
||||
expose:
|
||||
- "6379"
|
||||
ports:
|
||||
- "6379:6379"
|
||||
volumes:
|
||||
- redis:/data
|
||||
deploy:
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
FROM redis:4.0
|
||||
FROM redis:6.2.0
|
||||
|
||||
COPY redis.conf /usr/local/etc/redis/redis.conf
|
||||
|
||||
|
||||
24
launch.json
Normal file
24
launch.json
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "Debug Nest Framework",
|
||||
"runtimeExecutable": "npm",
|
||||
"runtimeArgs": [
|
||||
"run",
|
||||
"start:debug",
|
||||
"--",
|
||||
"--inspect-brk"
|
||||
],
|
||||
"autoAttachChildProcesses": true,
|
||||
"restart": true,
|
||||
"sourceMaps": true,
|
||||
"stopOnEntry": false,
|
||||
"console": "integratedTerminal"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
"server2:start": "lerna run start:dev --scope \"@bigcapital/server2\"",
|
||||
"test:watch": "lerna run test:watch",
|
||||
"test:e2e": "lerna run test:e2e",
|
||||
"start:debug": "lerna run start:debug",
|
||||
"prepare": "husky install"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
module.exports = {
|
||||
parser: '@typescript-eslint/parser',
|
||||
parserOptions: {
|
||||
project: 'tsconfig.json',
|
||||
tsconfigRootDir: __dirname,
|
||||
sourceType: 'module',
|
||||
},
|
||||
plugins: ['@typescript-eslint/eslint-plugin'],
|
||||
extends: [
|
||||
'plugin:@typescript-eslint/recommended',
|
||||
'plugin:prettier/recommended',
|
||||
],
|
||||
root: true,
|
||||
env: {
|
||||
node: true,
|
||||
jest: true,
|
||||
},
|
||||
ignorePatterns: ['.eslintrc.js'],
|
||||
rules: {
|
||||
'@typescript-eslint/interface-name-prefix': 'off',
|
||||
'@typescript-eslint/explicit-function-return-type': 'off',
|
||||
'@typescript-eslint/explicit-module-boundary-types': 'off',
|
||||
'@typescript-eslint/no-explicit-any': 'off',
|
||||
},
|
||||
};
|
||||
56
packages/server-nest/.gitignore
vendored
56
packages/server-nest/.gitignore
vendored
@@ -1,56 +0,0 @@
|
||||
# compiled output
|
||||
/dist
|
||||
/node_modules
|
||||
/build
|
||||
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
pnpm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
|
||||
# OS
|
||||
.DS_Store
|
||||
|
||||
# Tests
|
||||
/coverage
|
||||
/.nyc_output
|
||||
|
||||
# IDEs and editors
|
||||
/.idea
|
||||
.project
|
||||
.classpath
|
||||
.c9/
|
||||
*.launch
|
||||
.settings/
|
||||
*.sublime-workspace
|
||||
|
||||
# IDE - VSCode
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
|
||||
# dotenv environment variable files
|
||||
.env
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
.env.local
|
||||
|
||||
# temp directory
|
||||
.temp
|
||||
.tmp
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
*.pid.lock
|
||||
|
||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||
@@ -1 +0,0 @@
|
||||
## @bigcapitalhq/server
|
||||
@@ -1,125 +0,0 @@
|
||||
{
|
||||
"name": "@bigcapital/server2",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"author": "",
|
||||
"private": true,
|
||||
"license": "UNLICENSED",
|
||||
"scripts": {
|
||||
"build": "nest build",
|
||||
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
|
||||
"start": "nest start",
|
||||
"start:dev": "nest start --watch",
|
||||
"start:debug": "nest start --debug --watch",
|
||||
"start:prod": "node dist/main",
|
||||
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
|
||||
"test": "jest",
|
||||
"test:watch": "jest --watch",
|
||||
"test:cov": "jest --coverage",
|
||||
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
|
||||
"test:e2e": "jest --config ./test/jest-e2e.json --watchAll"
|
||||
},
|
||||
"dependencies": {
|
||||
"@bigcapital/email-components": "*",
|
||||
"@bigcapital/pdf-templates": "*",
|
||||
"@bigcapital/utils": "*",
|
||||
"@nestjs/bull": "^10.2.1",
|
||||
"@nestjs/bullmq": "^10.2.1",
|
||||
"@nestjs/cache-manager": "^2.2.2",
|
||||
"@nestjs/common": "^10.0.0",
|
||||
"@nestjs/config": "^3.2.3",
|
||||
"@nestjs/core": "^10.0.0",
|
||||
"@nestjs/event-emitter": "^2.0.4",
|
||||
"@nestjs/jwt": "^10.2.0",
|
||||
"@nestjs/passport": "^10.0.3",
|
||||
"@nestjs/platform-express": "^10.0.0",
|
||||
"@nestjs/swagger": "^7.4.2",
|
||||
"@nestjs/throttler": "^6.2.1",
|
||||
"@supercharge/promise-pool": "^3.2.0",
|
||||
"@types/passport-local": "^1.0.38",
|
||||
"@types/ramda": "^0.30.2",
|
||||
"accounting": "^0.4.1",
|
||||
"async": "^3.2.0",
|
||||
"axios": "^1.6.0",
|
||||
"bluebird": "^3.7.2",
|
||||
"bull": "^4.16.3",
|
||||
"bullmq": "^5.21.1",
|
||||
"cache-manager": "^6.1.1",
|
||||
"cache-manager-redis-store": "^3.0.1",
|
||||
"class-transformer": "^0.5.1",
|
||||
"class-validator": "^0.14.1",
|
||||
"express-validator": "^7.2.0",
|
||||
"form-data": "^4.0.0",
|
||||
"fp-ts": "^2.16.9",
|
||||
"js-money": "^0.6.3",
|
||||
"is-my-json-valid": "^2.20.5",
|
||||
"knex": "^3.1.0",
|
||||
"lamda": "^0.4.1",
|
||||
"lodash": "^4.17.21",
|
||||
"moment": "^2.30.1",
|
||||
"mysql": "^2.18.1",
|
||||
"mysql2": "^3.11.3",
|
||||
"nestjs-cls": "^4.4.1",
|
||||
"nestjs-i18n": "^10.4.9",
|
||||
"object-hash": "^2.0.3",
|
||||
"objection": "^3.1.5",
|
||||
"passport": "^0.7.0",
|
||||
"passport-jwt": "^4.0.1",
|
||||
"passport-local": "^1.0.0",
|
||||
"plaid": "^10.3.0",
|
||||
"pluralize": "^8.0.0",
|
||||
"posthog-node": "^4.3.2",
|
||||
"pug": "^3.0.2",
|
||||
"ramda": "^0.30.1",
|
||||
"redis": "^4.7.0",
|
||||
"reflect-metadata": "^0.2.0",
|
||||
"rxjs": "^7.8.1",
|
||||
"serialize-interceptor": "^1.1.7",
|
||||
"strategy": "^1.1.1",
|
||||
"uniqid": "^5.2.0",
|
||||
"uuid": "^10.0.0",
|
||||
"yup": "^0.28.1",
|
||||
"zod": "^3.23.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nestjs/cli": "^10.0.0",
|
||||
"@nestjs/schematics": "^10.0.0",
|
||||
"@nestjs/testing": "^10.0.0",
|
||||
"@types/express": "^5.0.0",
|
||||
"@types/jest": "^29.5.2",
|
||||
"@types/node": "^20.3.1",
|
||||
"@types/supertest": "^6.0.0",
|
||||
"@types/yup": "^0.29.13",
|
||||
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
||||
"@typescript-eslint/parser": "^8.0.0",
|
||||
"eslint": "^9.0.0",
|
||||
"eslint-config-prettier": "^9.0.0",
|
||||
"eslint-plugin-prettier": "^5.0.0",
|
||||
"jest": "^29.5.0",
|
||||
"prettier": "^3.0.0",
|
||||
"source-map-support": "^0.5.21",
|
||||
"supertest": "^7.0.0",
|
||||
"ts-jest": "^29.1.0",
|
||||
"ts-loader": "^9.4.3",
|
||||
"ts-node": "^10.9.1",
|
||||
"tsconfig-paths": "^4.2.0",
|
||||
"typescript": "^5.1.3"
|
||||
},
|
||||
"jest": {
|
||||
"moduleFileExtensions": [
|
||||
"js",
|
||||
"json",
|
||||
"ts"
|
||||
],
|
||||
"rootDir": "src",
|
||||
"testRegex": ".*\\.spec\\.ts$",
|
||||
"transform": {
|
||||
"^.+\\.(t|j)s$": "ts-jest"
|
||||
},
|
||||
"collectCoverageFrom": [
|
||||
"**/*.(t|j)s"
|
||||
],
|
||||
"coverageDirectory": "../coverage",
|
||||
"testEnvironment": "node"
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
import systemDatabase from './system-database';
|
||||
import tenantDatabase from './tenant-database';
|
||||
import signup from './signup';
|
||||
import gotenberg from './gotenberg';
|
||||
import plaid from './plaid';
|
||||
import lemonsqueezy from './lemonsqueezy';
|
||||
import s3 from './s3';
|
||||
import openExchange from './open-exchange';
|
||||
import posthog from './posthog';
|
||||
|
||||
export const config = [
|
||||
systemDatabase,
|
||||
tenantDatabase,
|
||||
signup,
|
||||
gotenberg,
|
||||
plaid,
|
||||
lemonsqueezy,
|
||||
s3,
|
||||
openExchange,
|
||||
posthog,
|
||||
];
|
||||
@@ -1,9 +0,0 @@
|
||||
import { registerAs } from '@nestjs/config';
|
||||
|
||||
export default registerAs('tenantDatabase', () => ({
|
||||
client: 'mysql',
|
||||
host: process.env.TENANT_DB_HOST || process.env.DB_HOST,
|
||||
port: process.env.TENANT_DB_PORT || process.env.DB_PORT || 5432,
|
||||
user: process.env.TENANT_DB_USER || process.env.DB_USER,
|
||||
password: process.env.TENANT_DB_PASSWORD || process.env.DB_PASSWORD,
|
||||
}));
|
||||
@@ -1,756 +0,0 @@
|
||||
export const events = {
|
||||
/**
|
||||
* Authentication service.
|
||||
*/
|
||||
auth: {
|
||||
signIn: 'onSignIn',
|
||||
signingIn: 'onSigningIn',
|
||||
|
||||
signUp: 'onSignUp',
|
||||
signingUp: 'onSigningUp',
|
||||
|
||||
signUpConfirming: 'signUpConfirming',
|
||||
signUpConfirmed: 'signUpConfirmed',
|
||||
|
||||
sendingResetPassword: 'onSendingResetPassword',
|
||||
sendResetPassword: 'onSendResetPassword',
|
||||
|
||||
resetPassword: 'onResetPassword',
|
||||
resetingPassword: 'onResetingPassword',
|
||||
},
|
||||
|
||||
/**
|
||||
* Invite users service.
|
||||
*/
|
||||
inviteUser: {
|
||||
acceptInvite: 'onUserAcceptInvite',
|
||||
sendInvite: 'onUserSendInvite',
|
||||
resendInvite: 'onUserInviteResend',
|
||||
checkInvite: 'onUserCheckInvite',
|
||||
sendInviteTenantSynced: 'onUserSendInviteTenantSynced',
|
||||
},
|
||||
|
||||
/**
|
||||
* Organization managment service.
|
||||
*/
|
||||
organization: {
|
||||
build: 'onOrganizationBuild',
|
||||
built: 'onOrganizationBuilt',
|
||||
|
||||
seeded: 'onOrganizationSeeded',
|
||||
|
||||
baseCurrencyUpdated: 'onOrganizationBaseCurrencyUpdated',
|
||||
},
|
||||
|
||||
/**
|
||||
* Organization subscription.
|
||||
*/
|
||||
subscription: {
|
||||
onSubscriptionCancel: 'onSubscriptionCancel',
|
||||
onSubscriptionCancelled: 'onSubscriptionCancelled',
|
||||
|
||||
onSubscriptionResume: 'onSubscriptionResume',
|
||||
onSubscriptionResumed: 'onSubscriptionResumed',
|
||||
|
||||
onSubscriptionPlanChange: 'onSubscriptionPlanChange',
|
||||
onSubscriptionPlanChanged: 'onSubscriptionPlanChanged',
|
||||
|
||||
onSubscriptionSubscribed: 'onSubscriptionSubscribed',
|
||||
|
||||
onSubscriptionPaymentSucceed: 'onSubscriptionPaymentSucceed',
|
||||
onSubscriptionPaymentFailed: 'onSubscriptionPaymentFailed',
|
||||
},
|
||||
|
||||
/**
|
||||
* Tenants managment service.
|
||||
*/
|
||||
tenantManager: {
|
||||
databaseCreated: 'onDatabaseCreated',
|
||||
tenantMigrated: 'onTenantMigrated',
|
||||
tenantSeeded: 'onTenantSeeded',
|
||||
},
|
||||
|
||||
/**
|
||||
* Accounts service.
|
||||
*/
|
||||
accounts: {
|
||||
onViewed: 'onAccountViewed',
|
||||
onListViewed: 'onAccountsListViewed',
|
||||
|
||||
onCreating: 'onAccountCreating',
|
||||
onCreated: 'onAccountCreated',
|
||||
|
||||
onEditing: 'onAccountEditing',
|
||||
onEdited: 'onAccountEdited',
|
||||
|
||||
onDelete: 'onAccountDelete',
|
||||
onDeleted: 'onAccountDeleted',
|
||||
|
||||
onBulkDeleted: 'onBulkDeleted',
|
||||
onBulkActivated: 'onAccountBulkActivated',
|
||||
|
||||
onActivated: 'onAccountActivated',
|
||||
},
|
||||
|
||||
/**
|
||||
* Manual journals service.
|
||||
*/
|
||||
manualJournals: {
|
||||
onCreating: 'onManualJournalCreating',
|
||||
onCreated: 'onManualJournalCreated',
|
||||
|
||||
onEditing: 'onManualJournalEditing',
|
||||
onEdited: 'onManualJournalEdited',
|
||||
|
||||
onDeleting: 'onManualJournalDeleting',
|
||||
onDeleted: 'onManualJournalDeleted',
|
||||
|
||||
onPublished: 'onManualJournalPublished',
|
||||
onPublishing: 'onManualJournalPublishing',
|
||||
},
|
||||
|
||||
/**
|
||||
* Expenses service.
|
||||
*/
|
||||
expenses: {
|
||||
onCreating: 'onExpenseCreating',
|
||||
onCreated: 'onExpenseCreated',
|
||||
|
||||
onEditing: 'onExpenseEditing',
|
||||
onEdited: 'onExpenseEdited',
|
||||
|
||||
onDeleting: 'onExpenseDeleting',
|
||||
onDeleted: 'onExpenseDeleted',
|
||||
|
||||
onPublishing: 'onExpensePublishing',
|
||||
onPublished: 'onExpensePublished',
|
||||
},
|
||||
|
||||
/**
|
||||
* Sales invoices service.
|
||||
*/
|
||||
saleInvoice: {
|
||||
onViewed: 'onSaleInvoiceItemViewed',
|
||||
onListViewed: 'onSaleInvoiceListViewed',
|
||||
|
||||
onPdfViewed: 'onSaleInvoicePdfViewed',
|
||||
|
||||
onCreate: 'onSaleInvoiceCreate',
|
||||
onCreating: 'onSaleInvoiceCreating',
|
||||
onCreated: 'onSaleInvoiceCreated',
|
||||
|
||||
onEdit: 'onSaleInvoiceEdit',
|
||||
onEditing: 'onSaleInvoiceEditing',
|
||||
onEdited: 'onSaleInvoiceEdited',
|
||||
|
||||
onDelete: 'onSaleInvoiceDelete',
|
||||
onDeleting: 'onSaleInvoiceDeleting',
|
||||
onDeleted: 'onSaleInvoiceDeleted',
|
||||
|
||||
onDelivering: 'onSaleInvoiceDelivering',
|
||||
onDeliver: 'onSaleInvoiceDeliver',
|
||||
onDelivered: 'onSaleInvoiceDelivered',
|
||||
|
||||
onPublish: 'onSaleInvoicePublish',
|
||||
onPublished: 'onSaleInvoicePublished',
|
||||
|
||||
onWriteoff: 'onSaleInvoiceWriteoff',
|
||||
onWrittenoff: 'onSaleInvoiceWrittenoff',
|
||||
onWrittenoffCancel: 'onSaleInvoiceWrittenoffCancel',
|
||||
onWrittenoffCanceled: 'onSaleInvoiceWrittenoffCanceled',
|
||||
|
||||
onNotifySms: 'onSaleInvoiceNotifySms',
|
||||
onNotifiedSms: 'onSaleInvoiceNotifiedSms',
|
||||
|
||||
onNotifyMail: 'onSaleInvoiceNotifyMail',
|
||||
onNotifyReminderMail: 'onSaleInvoiceNotifyReminderMail',
|
||||
|
||||
onPreMailSend: 'onSaleInvoicePreMailSend',
|
||||
onMailSend: 'onSaleInvoiceMailSend',
|
||||
onMailSent: 'onSaleInvoiceMailSent',
|
||||
|
||||
onMailReminderSend: 'onSaleInvoiceMailReminderSend',
|
||||
onMailReminderSent: 'onSaleInvoiceMailReminderSent',
|
||||
|
||||
onPublicLinkGenerating: 'onPublicSharableLinkGenerating',
|
||||
onPublicLinkGenerated: 'onPublicSharableLinkGenerated',
|
||||
},
|
||||
|
||||
/**
|
||||
* Sales estimates service.
|
||||
*/
|
||||
saleEstimate: {
|
||||
onPdfViewed: 'onSaleEstimatePdfViewed',
|
||||
|
||||
onCreating: 'onSaleEstimateCreating',
|
||||
onCreated: 'onSaleEstimateCreated',
|
||||
|
||||
onEditing: 'onSaleEstimateEditing',
|
||||
onEdited: 'onSaleEstimateEdited',
|
||||
|
||||
onDeleting: 'onSaleEstimatedDeleting',
|
||||
onDeleted: 'onSaleEstimatedDeleted',
|
||||
|
||||
onPublishing: 'onSaleEstimatedPublishing',
|
||||
onPublished: 'onSaleEstimatedPublished',
|
||||
|
||||
onNotifySms: 'onSaleEstimateNotifySms',
|
||||
onNotifiedSms: 'onSaleEstimateNotifiedSms',
|
||||
|
||||
onDelivering: 'onSaleEstimateDelivering',
|
||||
onDelivered: 'onSaleEstimateDelivered',
|
||||
|
||||
onConvertedToInvoice: 'onSaleEstimateConvertedToInvoice',
|
||||
|
||||
onApproving: 'onSaleEstimateApproving',
|
||||
onApproved: 'onSaleEstimateApproved',
|
||||
|
||||
onRejecting: 'onSaleEstimateRejecting',
|
||||
onRejected: 'onSaleEstimateRejected',
|
||||
|
||||
onNotifyMail: 'onSaleEstimateNotifyMail',
|
||||
|
||||
onPreMailSend: 'onSaleEstimatePreMailSend',
|
||||
onMailSend: 'onSaleEstimateMailSend',
|
||||
onMailSent: 'onSaleEstimateMailSend',
|
||||
|
||||
onViewed: 'onSaleEstimateViewed',
|
||||
},
|
||||
|
||||
/**
|
||||
* Sales receipts service.
|
||||
*/
|
||||
saleReceipt: {
|
||||
onPdfViewed: 'onSaleReceiptPdfViewed',
|
||||
|
||||
onCreating: 'onSaleReceiptsCreating',
|
||||
onCreated: 'onSaleReceiptsCreated',
|
||||
|
||||
onEditing: 'onSaleReceiptsEditing',
|
||||
onEdited: 'onSaleReceiptsEdited',
|
||||
|
||||
onDeleting: 'onSaleReceiptsDeleting',
|
||||
onDeleted: 'onSaleReceiptsDeleted',
|
||||
|
||||
onPublishing: 'onSaleReceiptPublishing',
|
||||
onPublished: 'onSaleReceiptPublished',
|
||||
|
||||
onClosed: 'onSaleReceiptClosed',
|
||||
onClosing: 'onSaleReceiptClosing',
|
||||
|
||||
onNotifySms: 'onSaleReceiptNotifySms',
|
||||
onNotifiedSms: 'onSaleReceiptNotifiedSms',
|
||||
|
||||
onPreMailSend: 'onSaleReceiptPreMailSend',
|
||||
onMailSend: 'onSaleReceiptMailSend',
|
||||
onMailSent: 'onSaleReceiptMailSent',
|
||||
},
|
||||
|
||||
/**
|
||||
* Payment receipts service.
|
||||
*/
|
||||
paymentReceive: {
|
||||
onPdfViewed: 'onPaymentReceivedPdfViewed',
|
||||
|
||||
onCreated: 'onPaymentReceiveCreated',
|
||||
onCreating: 'onPaymentReceiveCreating',
|
||||
|
||||
onEditing: 'onPaymentReceiveEditing',
|
||||
onEdited: 'onPaymentReceiveEdited',
|
||||
|
||||
onDeleting: 'onPaymentReceiveDeleting',
|
||||
onDeleted: 'onPaymentReceiveDeleted',
|
||||
|
||||
onPublishing: 'onPaymentReceivePublishing',
|
||||
onPublished: 'onPaymentReceivePublished',
|
||||
|
||||
onNotifySms: 'onPaymentReceiveNotifySms',
|
||||
onNotifiedSms: 'onPaymentReceiveNotifiedSms',
|
||||
|
||||
onPreMailSend: 'onPaymentReceivePreMailSend',
|
||||
onMailSend: 'onPaymentReceiveMailSend',
|
||||
onMailSent: 'onPaymentReceiveMailSent',
|
||||
},
|
||||
|
||||
/**
|
||||
* Bills service.
|
||||
*/
|
||||
bill: {
|
||||
onCreating: 'onBillCreating',
|
||||
onCreated: 'onBillCreated',
|
||||
|
||||
onEditing: 'onBillEditing',
|
||||
onEdited: 'onBillEdited',
|
||||
|
||||
onDeleting: 'onBillDeleting',
|
||||
onDeleted: 'onBillDeleted',
|
||||
|
||||
onPublishing: 'onBillPublishing',
|
||||
onPublished: 'onBillPublished',
|
||||
|
||||
onOpening: 'onBillOpening',
|
||||
onOpened: 'onBillOpened',
|
||||
},
|
||||
|
||||
/**
|
||||
* Bill payments service.
|
||||
*/
|
||||
billPayment: {
|
||||
onCreating: 'onBillPaymentCreating',
|
||||
onCreated: 'onBillPaymentCreated',
|
||||
|
||||
onEditing: 'onBillPaymentEditing',
|
||||
onEdited: 'onBillPaymentEdited',
|
||||
|
||||
onDeleted: 'onBillPaymentDeleted',
|
||||
onDeleting: 'onBillPaymentDeleting',
|
||||
|
||||
onPublishing: 'onBillPaymentPublishing',
|
||||
onPublished: 'onBillPaymentPublished',
|
||||
},
|
||||
|
||||
/**
|
||||
* Customers services.
|
||||
*/
|
||||
customers: {
|
||||
onCreating: 'onCustomerCreating',
|
||||
onCreated: 'onCustomerCreated',
|
||||
|
||||
onEdited: 'onCustomerEdited',
|
||||
onEditing: 'onCustomerEditing',
|
||||
|
||||
onDeleted: 'onCustomerDeleted',
|
||||
onDeleting: 'onCustomerDeleting',
|
||||
onBulkDeleted: 'onBulkDeleted',
|
||||
|
||||
onOpeningBalanceChanging: 'onCustomerOpeningBalanceChanging',
|
||||
onOpeningBalanceChanged: 'onCustomerOpeingBalanceChanged',
|
||||
|
||||
onActivating: 'onCustomerActivating',
|
||||
onActivated: 'onCustomerActivated',
|
||||
},
|
||||
|
||||
/**
|
||||
* Vendors services.
|
||||
*/
|
||||
vendors: {
|
||||
onCreated: 'onVendorCreated',
|
||||
onCreating: 'onVendorCreating',
|
||||
|
||||
onEdited: 'onVendorEdited',
|
||||
onEditing: 'onVendorEditing',
|
||||
|
||||
onDeleted: 'onVendorDeleted',
|
||||
onDeleting: 'onVendorDeleting',
|
||||
|
||||
onOpeningBalanceChanging: 'onVendorOpeingBalanceChanging',
|
||||
onOpeningBalanceChanged: 'onVendorOpeingBalanceChanged',
|
||||
|
||||
onActivating: 'onVendorActivating',
|
||||
onActivated: 'onVendorActivated',
|
||||
},
|
||||
|
||||
/**
|
||||
* Items service.
|
||||
*/
|
||||
item: {
|
||||
onViewed: 'onItemViewed',
|
||||
|
||||
onCreated: 'onItemCreated',
|
||||
onCreating: 'onItemCreating',
|
||||
|
||||
onEditing: 'onItemEditing',
|
||||
onEdited: 'onItemEdited',
|
||||
|
||||
onDeleted: 'onItemDeleted',
|
||||
onDeleting: 'onItemDeleting',
|
||||
|
||||
onActivating: 'onItemActivating',
|
||||
onActivated: 'onItemActivated',
|
||||
|
||||
onInactivating: 'onInactivating',
|
||||
onInactivated: 'onItemInactivated',
|
||||
},
|
||||
|
||||
/**
|
||||
* Item category service.
|
||||
*/
|
||||
itemCategory: {
|
||||
onCreated: 'onItemCategoryCreated',
|
||||
onEdited: 'onItemCategoryEdited',
|
||||
onDeleted: 'onItemCategoryDeleted',
|
||||
onBulkDeleted: 'onItemCategoryBulkDeleted',
|
||||
},
|
||||
|
||||
/**
|
||||
* Inventory service.
|
||||
*/
|
||||
inventory: {
|
||||
onInventoryTransactionsCreated: 'onInventoryTransactionsCreated',
|
||||
onInventoryTransactionsDeleted: 'onInventoryTransactionsDeleted',
|
||||
|
||||
onComputeItemCostJobScheduled: 'onComputeItemCostJobScheduled',
|
||||
onComputeItemCostJobStarted: 'onComputeItemCostJobStarted',
|
||||
onComputeItemCostJobCompleted: 'onComputeItemCostJobCompleted',
|
||||
|
||||
onInventoryCostEntriesWritten: 'onInventoryCostEntriesWritten',
|
||||
|
||||
onCostLotsGLEntriesBeforeWrite: 'onInventoryCostLotsGLEntriesBeforeWrite',
|
||||
onCostLotsGLEntriesWrite: 'onInventoryCostLotsGLEntriesWrite',
|
||||
},
|
||||
|
||||
/**
|
||||
* Inventory adjustment service.
|
||||
*/
|
||||
inventoryAdjustment: {
|
||||
onQuickCreating: 'onInventoryAdjustmentCreating',
|
||||
onQuickCreated: 'onInventoryAdjustmentQuickCreated',
|
||||
|
||||
onCreated: 'onInventoryAdjustmentCreated',
|
||||
|
||||
onDeleting: 'onInventoryAdjustmentDeleting',
|
||||
onDeleted: 'onInventoryAdjustmentDeleted',
|
||||
|
||||
onPublishing: 'onInventoryAdjustmentPublishing',
|
||||
onPublished: 'onInventoryAdjustmentPublished',
|
||||
},
|
||||
|
||||
/**
|
||||
* Bill landed cost.
|
||||
*/
|
||||
billLandedCost: {
|
||||
onCreate: 'onBillLandedCostCreate',
|
||||
onCreated: 'onBillLandedCostCreated',
|
||||
onDelete: 'onBillLandedCostDelete',
|
||||
onDeleted: 'onBillLandedCostDeleted',
|
||||
},
|
||||
|
||||
cashflow: {
|
||||
onOwnerContributionCreate: 'onCashflowOwnerContributionCreate',
|
||||
onOwnerContributionCreated: 'onCashflowOwnerContributionCreated',
|
||||
|
||||
onOtherIncomeCreate: 'onCashflowOtherIncomeCreate',
|
||||
onOtherIncomeCreated: 'onCashflowOtherIncomeCreated',
|
||||
|
||||
onTransactionCreating: 'onCashflowTransactionCreating',
|
||||
onTransactionCreated: 'onCashflowTransactionCreated',
|
||||
|
||||
onTransactionDeleting: 'onCashflowTransactionDeleting',
|
||||
onTransactionDeleted: 'onCashflowTransactionDeleted',
|
||||
|
||||
onTransactionCategorizing: 'onTransactionCategorizing',
|
||||
onTransactionCategorized: 'onCashflowTransactionCategorized',
|
||||
|
||||
onTransactionUncategorizedCreating: 'onTransactionUncategorizedCreating',
|
||||
onTransactionUncategorizedCreated: 'onTransactionUncategorizedCreated',
|
||||
|
||||
onTransactionUncategorizing: 'onTransactionUncategorizing',
|
||||
onTransactionUncategorized: 'onTransactionUncategorized',
|
||||
|
||||
onTransactionCategorizingAsExpense: 'onTransactionCategorizingAsExpense',
|
||||
onTransactionCategorizedAsExpense: 'onTransactionCategorizedAsExpense',
|
||||
},
|
||||
|
||||
/**
|
||||
* Roles service events.
|
||||
*/
|
||||
roles: {
|
||||
onCreate: 'onRoleCreate',
|
||||
onCreated: 'onRoleCreated',
|
||||
onEdit: 'onRoleEdit',
|
||||
onEdited: 'onRoleEdited',
|
||||
onDelete: 'onRoleDelete',
|
||||
onDeleted: 'onRoleDeleted',
|
||||
},
|
||||
|
||||
tenantUser: {
|
||||
onEdited: 'onTenantUserEdited',
|
||||
onDeleted: 'onTenantUserDeleted',
|
||||
onActivated: 'onTenantUserActivated',
|
||||
onInactivated: 'onTenantUserInactivated',
|
||||
},
|
||||
|
||||
/**
|
||||
* Credit note service.
|
||||
*/
|
||||
creditNote: {
|
||||
onPdfViewed: 'onCreditNotePdfViewed',
|
||||
|
||||
onCreate: 'onCreditNoteCreate',
|
||||
onCreating: 'onCreditNoteCreating',
|
||||
onCreated: 'onCreditNoteCreated',
|
||||
|
||||
onEditing: 'onCreditNoteEditing',
|
||||
onEdit: 'onCreditNoteEdit',
|
||||
onEdited: 'onCreditNoteEdited',
|
||||
|
||||
onDelete: 'onCreditNoteDelete',
|
||||
onDeleting: 'onCreditNoteDeleting',
|
||||
onDeleted: 'onCreditNoteDeleted',
|
||||
|
||||
onOpen: 'onCreditNoteOpen',
|
||||
onOpening: 'onCreditNoteOpening',
|
||||
onOpened: 'onCreditNoteOpened',
|
||||
|
||||
onRefundCreate: 'onCreditNoteRefundCreate',
|
||||
onRefundCreating: 'onCreditNoteRefundCreating',
|
||||
onRefundCreated: 'onCreditNoteRefundCreated',
|
||||
|
||||
onRefundDelete: 'onCreditNoteRefundDelete',
|
||||
onRefundDeleting: 'onCreditNoteRefundDeleting',
|
||||
onRefundDeleted: 'onCreditNoteRefundDeleted',
|
||||
|
||||
onApplyToInvoicesCreated: 'onCreditNoteApplyToInvoiceCreated',
|
||||
onApplyToInvoicesCreate: 'onCreditNoteApplyToInvoiceCreate',
|
||||
onApplyToInvoicesDeleted: 'onCreditNoteApplyToInvoiceDeleted',
|
||||
},
|
||||
|
||||
/**
|
||||
* Vendor credit service.
|
||||
*/
|
||||
vendorCredit: {
|
||||
onCreate: 'onVendorCreditCreate',
|
||||
onCreating: 'onVendorCreditCreating',
|
||||
onCreated: 'onVendorCreditCreated',
|
||||
|
||||
onEdit: 'onVendorCreditEdit',
|
||||
onEditing: 'onVendorCreditEditing',
|
||||
onEdited: 'onVendorCreditEdited',
|
||||
|
||||
onDelete: 'onVendorCreditDelete',
|
||||
onDeleting: 'onVendorCreditDeleting',
|
||||
onDeleted: 'onVendorCreditDeleted',
|
||||
|
||||
onOpen: 'onVendorCreditOpen',
|
||||
onOpened: 'onVendorCreditOpened',
|
||||
|
||||
onRefundCreating: 'onVendorCreditRefundCreating',
|
||||
onRefundCreate: 'onVendorCreditRefundCreate',
|
||||
onRefundCreated: 'onVendorCreditRefundCreated',
|
||||
|
||||
onRefundDelete: 'onVendorCreditRefundDelete',
|
||||
onRefundDeleting: 'onVendorCreditRefundDeleting',
|
||||
onRefundDeleted: 'onVendorCreditRefundDeleted',
|
||||
|
||||
onApplyToInvoicesCreated: 'onVendorCreditApplyToInvoiceCreated',
|
||||
onApplyToInvoicesCreate: 'onVendorCreditApplyToInvoiceCreate',
|
||||
onApplyToInvoicesDeleted: 'onVendorCreditApplyToInvoiceDeleted',
|
||||
},
|
||||
|
||||
transactionsLocking: {
|
||||
locked: 'onTransactionLockingLocked',
|
||||
lockCanceled: 'onTransactionLockingLockCanceled',
|
||||
partialUnlocked: 'onTransactionLockingPartialUnlocked',
|
||||
partialUnlockCanceled: 'onTransactionLockingPartialUnlockCanceled',
|
||||
},
|
||||
|
||||
warehouse: {
|
||||
onCreate: 'onWarehouseCreate',
|
||||
onCreated: 'onWarehouseCreated',
|
||||
|
||||
onEdit: 'onWarehouseEdit',
|
||||
onEdited: 'onWarehouseEdited',
|
||||
|
||||
onDelete: 'onWarehouseDelete',
|
||||
onDeleted: 'onWarehouseDeleted',
|
||||
|
||||
onActivate: 'onWarehouseActivate',
|
||||
onActivated: 'onWarehouseActivated',
|
||||
|
||||
onMarkPrimary: 'onWarehouseMarkPrimary',
|
||||
onMarkedPrimary: 'onWarehouseMarkedPrimary',
|
||||
},
|
||||
|
||||
warehouseTransfer: {
|
||||
onCreate: 'onWarehouseTransferCreate',
|
||||
onCreated: 'onWarehouseTransferCreated',
|
||||
|
||||
onEdit: 'onWarehouseTransferEdit',
|
||||
onEdited: 'onWarehouseTransferEdited',
|
||||
|
||||
onDelete: 'onWarehouseTransferDelete',
|
||||
onDeleted: 'onWarehouseTransferDeleted',
|
||||
|
||||
onInitiate: 'onWarehouseTransferInitiate',
|
||||
onInitiated: 'onWarehouseTransferInitated',
|
||||
|
||||
onTransfer: 'onWarehouseTransferInitiate',
|
||||
onTransferred: 'onWarehouseTransferTransferred',
|
||||
},
|
||||
|
||||
/**
|
||||
* Branches.
|
||||
*/
|
||||
branch: {
|
||||
onActivate: 'onBranchActivate',
|
||||
onActivated: 'onBranchActivated',
|
||||
|
||||
onMarkPrimary: 'onBranchMarkPrimary',
|
||||
onMarkedPrimary: 'onBranchMarkedPrimary',
|
||||
},
|
||||
|
||||
/**
|
||||
* Projects.
|
||||
*/
|
||||
project: {
|
||||
onCreate: 'onProjectCreate',
|
||||
onCreating: 'onProjectCreating',
|
||||
onCreated: 'onProjectCreated',
|
||||
|
||||
onEdit: 'onEditProject',
|
||||
onEditing: 'onEditingProject',
|
||||
onEdited: 'onEditedProject',
|
||||
|
||||
onEditStatus: 'onEditStatusProject',
|
||||
onEditingStatus: 'onEditingStatusProject',
|
||||
onEditedStatus: 'onEditedStatusProject',
|
||||
|
||||
onDelete: 'onDeleteProject',
|
||||
onDeleting: 'onDeletingProject',
|
||||
onDeleted: 'onDeletedProject',
|
||||
},
|
||||
|
||||
/**
|
||||
* Project Tasks.
|
||||
*/
|
||||
projectTask: {
|
||||
onCreate: 'onProjectTaskCreate',
|
||||
onCreating: 'onProjectTaskCreating',
|
||||
onCreated: 'onProjectTaskCreated',
|
||||
|
||||
onEdit: 'onProjectTaskEdit',
|
||||
onEditing: 'onProjectTaskEditing',
|
||||
onEdited: 'onProjectTaskEdited',
|
||||
|
||||
onDelete: 'onProjectTaskDelete',
|
||||
onDeleting: 'onProjectTaskDeleting',
|
||||
onDeleted: 'onProjectTaskDeleted',
|
||||
},
|
||||
|
||||
/**
|
||||
* Project Times.
|
||||
*/
|
||||
projectTime: {
|
||||
onCreate: 'onProjectTimeCreate',
|
||||
onCreating: 'onProjectTimeCreating',
|
||||
onCreated: 'onProjectTimeCreated',
|
||||
|
||||
onEdit: 'onProjectTimeEdit',
|
||||
onEditing: 'onProjectTimeEditing',
|
||||
onEdited: 'onProjectTimeEdited',
|
||||
|
||||
onDelete: 'onProjectTimeDelete',
|
||||
onDeleting: 'onProjectTimeDeleting',
|
||||
onDeleted: 'onProjectTimeDeleted',
|
||||
},
|
||||
|
||||
taxRates: {
|
||||
onCreating: 'onTaxRateCreating',
|
||||
onCreated: 'onTaxRateCreated',
|
||||
|
||||
onEditing: 'onTaxRateEditing',
|
||||
onEdited: 'onTaxRateEdited',
|
||||
|
||||
onDeleting: 'onTaxRateDeleting',
|
||||
onDeleted: 'onTaxRateDeleted',
|
||||
|
||||
onActivating: 'onTaxRateActivating',
|
||||
onActivated: 'onTaxRateActivated',
|
||||
|
||||
onInactivating: 'onTaxRateInactivating',
|
||||
onInactivated: 'onTaxRateInactivated',
|
||||
},
|
||||
|
||||
plaid: {
|
||||
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',
|
||||
},
|
||||
|
||||
bankTransactions: {
|
||||
onExcluding: 'onBankTransactionExclude',
|
||||
onExcluded: 'onBankTransactionExcluded',
|
||||
|
||||
onUnexcluding: 'onBankTransactionUnexcluding',
|
||||
onUnexcluded: 'onBankTransactionUnexcluded',
|
||||
|
||||
onPendingRemoving: 'onBankTransactionPendingRemoving',
|
||||
onPendingRemoved: 'onBankTransactionPendingRemoved',
|
||||
},
|
||||
|
||||
bankAccount: {
|
||||
onDisconnecting: 'onBankAccountDisconnecting',
|
||||
onDisconnected: 'onBankAccountDisconnected',
|
||||
},
|
||||
|
||||
// Import files.
|
||||
import: {
|
||||
onImportCommitted: 'onImportFileCommitted',
|
||||
},
|
||||
|
||||
// Branding templates
|
||||
pdfTemplate: {
|
||||
onCreating: 'onPdfTemplateCreating',
|
||||
onCreated: 'onPdfTemplateCreated',
|
||||
|
||||
onEditing: 'onPdfTemplateEditing',
|
||||
onEdited: 'onPdfTemplatedEdited',
|
||||
|
||||
onDeleting: 'onPdfTemplateDeleting',
|
||||
onDeleted: 'onPdfTemplateDeleted',
|
||||
|
||||
onAssignedDefault: 'onPdfTemplateAssignedDefault',
|
||||
onAssigningDefault: 'onPdfTemplateAssigningDefault',
|
||||
},
|
||||
|
||||
// Payment method.
|
||||
paymentMethod: {
|
||||
onEditing: 'onPaymentMethodEditing',
|
||||
onEdited: 'onPaymentMethodEdited',
|
||||
|
||||
onDeleted: 'onPaymentMethodDeleted',
|
||||
},
|
||||
|
||||
// Payment methods integrations
|
||||
paymentIntegrationLink: {
|
||||
onPaymentIntegrationLink: 'onPaymentIntegrationLink',
|
||||
onPaymentIntegrationDeleteLink: 'onPaymentIntegrationDeleteLink',
|
||||
},
|
||||
|
||||
// Stripe Payment Integration
|
||||
stripeIntegration: {
|
||||
onAccountCreated: 'onStripeIntegrationAccountCreated',
|
||||
onAccountDeleted: 'onStripeIntegrationAccountDeleted',
|
||||
|
||||
onPaymentLinkCreated: 'onStripePaymentLinkCreated',
|
||||
onPaymentLinkInactivated: 'onStripePaymentLinkInactivated',
|
||||
|
||||
onOAuthCodeGranted: 'onStripeOAuthCodeGranted',
|
||||
},
|
||||
|
||||
// Stripe Payment Webhooks
|
||||
stripeWebhooks: {
|
||||
onCheckoutSessionCompleted: 'onStripeCheckoutSessionCompleted',
|
||||
onAccountUpdated: 'onStripeAccountUpdated',
|
||||
},
|
||||
};
|
||||
@@ -1,24 +0,0 @@
|
||||
import {
|
||||
ExceptionFilter,
|
||||
Catch,
|
||||
ArgumentsHost,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
import { Response } from 'express';
|
||||
import { ServiceError } from '@/modules/Items/ServiceError';
|
||||
|
||||
@Catch(ServiceError)
|
||||
export class ServiceErrorFilter implements ExceptionFilter {
|
||||
catch(exception: ServiceError, host: ArgumentsHost) {
|
||||
const ctx = host.switchToHttp();
|
||||
const response = ctx.getResponse<Response>();
|
||||
const status = exception.getStatus();
|
||||
|
||||
response.status(status).json({
|
||||
statusCode: status,
|
||||
errorType: exception.errorType,
|
||||
message: exception.message,
|
||||
payload: exception.payload,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
{
|
||||
"HELLO": "Hello",
|
||||
"PRODUCT": {
|
||||
"NEW": "New Product: {name}"
|
||||
},
|
||||
"ENGLISH": "English",
|
||||
"ARRAY": ["ONE", "TWO", "THREE"],
|
||||
"cat": "Cat",
|
||||
"cat_name": "Cat: {name}",
|
||||
"set-up-password": {
|
||||
"heading": "Hello, {username}",
|
||||
"title": "Forgot password",
|
||||
"followLink": "Please follow the link to set up your password"
|
||||
},
|
||||
"day_interval": {
|
||||
"one": "Every day",
|
||||
"other": "Every {count} days",
|
||||
"zero": "Never"
|
||||
},
|
||||
"nested": "We go shopping: $t(test.day_interval, {{\"count\": {count} }})"
|
||||
}
|
||||
@@ -1,174 +0,0 @@
|
||||
import { Knex } from 'knex';
|
||||
|
||||
export interface IAccountDTO {
|
||||
name: string;
|
||||
code: string;
|
||||
description: string;
|
||||
accountType: string;
|
||||
parentAccountId?: number;
|
||||
active: boolean;
|
||||
bankBalance?: number;
|
||||
accountMask?: string;
|
||||
}
|
||||
|
||||
export interface IAccountCreateDTO extends IAccountDTO {
|
||||
currencyCode?: string;
|
||||
plaidAccountId?: string;
|
||||
plaidItemId?: string;
|
||||
}
|
||||
|
||||
export interface IAccountEditDTO extends IAccountDTO {}
|
||||
|
||||
export interface IAccount {
|
||||
id: number;
|
||||
name: string;
|
||||
slug: string;
|
||||
code: string;
|
||||
index: number;
|
||||
description: string;
|
||||
accountType: string;
|
||||
parentAccountId: number;
|
||||
active: boolean;
|
||||
predefined: boolean;
|
||||
amount: number;
|
||||
currencyCode: string;
|
||||
transactions?: any[];
|
||||
type?: any[];
|
||||
accountNormal: string;
|
||||
accountParentType: string;
|
||||
bankBalance: string;
|
||||
plaidItemId: number | null;
|
||||
lastFeedsUpdatedAt: Date;
|
||||
}
|
||||
|
||||
export enum AccountNormal {
|
||||
DEBIT = 'debit',
|
||||
CREDIT = 'credit',
|
||||
}
|
||||
|
||||
export interface IAccountsTransactionsFilter {
|
||||
accountId?: number;
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
export interface IAccountTransaction {
|
||||
id?: number;
|
||||
|
||||
credit: number;
|
||||
debit: number;
|
||||
currencyCode: string;
|
||||
exchangeRate: number;
|
||||
|
||||
accountId: number;
|
||||
contactId?: number | null;
|
||||
date: string | Date;
|
||||
|
||||
referenceType: string;
|
||||
referenceTypeFormatted: string;
|
||||
referenceId: number;
|
||||
|
||||
referenceNumber?: string;
|
||||
|
||||
transactionNumber?: string;
|
||||
transactionType?: string;
|
||||
|
||||
note?: string;
|
||||
|
||||
index: number;
|
||||
indexGroup?: number;
|
||||
|
||||
costable?: boolean;
|
||||
|
||||
userId?: number;
|
||||
itemId?: number;
|
||||
branchId?: number;
|
||||
projectId?: number;
|
||||
|
||||
account?: IAccount;
|
||||
|
||||
taxRateId?: number;
|
||||
taxRate?: number;
|
||||
}
|
||||
export interface IAccountResponse extends IAccount {}
|
||||
|
||||
export enum IAccountsStructureType {
|
||||
Tree = 'tree',
|
||||
Flat = 'flat',
|
||||
}
|
||||
|
||||
export interface IAccountsFilter {
|
||||
stringifiedFilterRoles?: string;
|
||||
onlyInactive: boolean;
|
||||
structure?: IAccountsStructureType;
|
||||
}
|
||||
|
||||
export interface IAccountType {
|
||||
label: string;
|
||||
key: string;
|
||||
normal: string;
|
||||
rootType: string;
|
||||
childType: string;
|
||||
balanceSheet: boolean;
|
||||
incomeSheet: boolean;
|
||||
}
|
||||
|
||||
export interface IAccountsTypesService {
|
||||
getAccountsTypes(tenantId: number): Promise<IAccountType>;
|
||||
}
|
||||
|
||||
export interface IAccountEventCreatingPayload {
|
||||
tenantId: number;
|
||||
accountDTO: any;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
export interface IAccountEventCreatedPayload {
|
||||
tenantId: number;
|
||||
account: IAccount;
|
||||
accountId: number;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IAccountEventEditedPayload {
|
||||
tenantId: number;
|
||||
account: IAccount;
|
||||
oldAccount: IAccount;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IAccountEventDeletedPayload {
|
||||
tenantId: number;
|
||||
accountId: number;
|
||||
oldAccount: IAccount;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IAccountEventDeletePayload {
|
||||
trx: Knex.Transaction;
|
||||
oldAccount: IAccount;
|
||||
tenantId: number;
|
||||
}
|
||||
|
||||
export interface IAccountEventActivatedPayload {
|
||||
tenantId: number;
|
||||
accountId: number;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export enum AccountAction {
|
||||
CREATE = 'Create',
|
||||
EDIT = 'Edit',
|
||||
DELETE = 'Delete',
|
||||
VIEW = 'View',
|
||||
TransactionsLocking = 'TransactionsLocking',
|
||||
}
|
||||
|
||||
export enum TaxRateAction {
|
||||
CREATE = 'Create',
|
||||
EDIT = 'Edit',
|
||||
DELETE = 'Delete',
|
||||
VIEW = 'View',
|
||||
}
|
||||
|
||||
export interface CreateAccountParams {
|
||||
ignoreUniqueName: boolean;
|
||||
}
|
||||
@@ -1,165 +0,0 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { Knex } from 'knex';
|
||||
import { Item } from '@/modules/Items/models/Item';
|
||||
// import { AbilitySubject } from '@/interfaces';
|
||||
// import { IFilterRole } from '@/interfaces/DynamicFilter';
|
||||
|
||||
export interface IItem {
|
||||
id: number;
|
||||
name: string;
|
||||
type: string;
|
||||
code: string;
|
||||
|
||||
sellable: boolean;
|
||||
purchasable: boolean;
|
||||
|
||||
costPrice: number;
|
||||
sellPrice: number;
|
||||
currencyCode: string;
|
||||
|
||||
costAccountId: number;
|
||||
sellAccountId: number;
|
||||
inventoryAccountId: number;
|
||||
|
||||
sellDescription: string;
|
||||
purchaseDescription: string;
|
||||
|
||||
sellTaxRateId: number;
|
||||
purchaseTaxRateId: number;
|
||||
|
||||
quantityOnHand: number;
|
||||
|
||||
note: string;
|
||||
active: boolean;
|
||||
|
||||
categoryId: number;
|
||||
userId: number;
|
||||
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
export class IItemDTO {
|
||||
@ApiProperty()
|
||||
name: string;
|
||||
|
||||
@ApiProperty()
|
||||
type: string;
|
||||
|
||||
@ApiProperty()
|
||||
code: string;
|
||||
|
||||
@ApiProperty()
|
||||
sellable: boolean;
|
||||
|
||||
@ApiProperty()
|
||||
purchasable: boolean;
|
||||
|
||||
@ApiProperty()
|
||||
costPrice: number;
|
||||
|
||||
@ApiProperty()
|
||||
sellPrice: number;
|
||||
|
||||
@ApiProperty()
|
||||
currencyCode: string;
|
||||
|
||||
@ApiProperty()
|
||||
costAccountId: number;
|
||||
|
||||
@ApiProperty()
|
||||
sellAccountId: number;
|
||||
|
||||
@ApiProperty()
|
||||
inventoryAccountId: number;
|
||||
|
||||
@ApiProperty()
|
||||
sellDescription: string;
|
||||
|
||||
@ApiProperty()
|
||||
purchaseDescription: string;
|
||||
|
||||
@ApiProperty()
|
||||
sellTaxRateId: number;
|
||||
|
||||
@ApiProperty()
|
||||
purchaseTaxRateId: number;
|
||||
|
||||
@ApiProperty()
|
||||
quantityOnHand: number;
|
||||
|
||||
@ApiProperty()
|
||||
note: string;
|
||||
|
||||
@ApiProperty()
|
||||
active: boolean;
|
||||
|
||||
@ApiProperty()
|
||||
categoryId: number;
|
||||
}
|
||||
|
||||
export interface IItemCreateDTO extends IItemDTO {}
|
||||
export interface IItemEditDTO extends IItemDTO {}
|
||||
|
||||
// export interface IItemsService {
|
||||
// getItem(tenantId: number, itemId: number): Promise<IItem>;
|
||||
// deleteItem(tenantId: number, itemId: number): Promise<void>;
|
||||
// editItem(tenantId: number, itemId: number, itemDTO: IItemDTO): Promise<IItem>;
|
||||
// newItem(tenantId: number, itemDTO: IItemDTO): Promise<IItem>;
|
||||
// itemsList(
|
||||
// tenantId: number,
|
||||
// itemsFilter: IItemsFilter,
|
||||
// ): Promise<{ items: IItem[] }>;
|
||||
// }
|
||||
|
||||
// export interface IItemsFilter extends IDynamicListFilterDTO {
|
||||
// stringifiedFilterRoles?: string;
|
||||
// page: number;
|
||||
// pageSize: number;
|
||||
// inactiveMode: boolean;
|
||||
// viewSlug?: string;
|
||||
// }
|
||||
|
||||
// export interface IItemsAutoCompleteFilter {
|
||||
// limit: number;
|
||||
// keyword: string;
|
||||
// filterRoles?: IFilterRole[];
|
||||
// columnSortBy: string;
|
||||
// sortOrder: string;
|
||||
// }
|
||||
|
||||
export interface IItemEventCreatedPayload {
|
||||
// tenantId: number;
|
||||
item: Item;
|
||||
itemId: number;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IItemEventEditedPayload {
|
||||
item: Item;
|
||||
oldItem: Item;
|
||||
itemId: number;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IItemEventDeletingPayload {
|
||||
// tenantId: number;
|
||||
trx: Knex.Transaction;
|
||||
oldItem: Item;
|
||||
}
|
||||
|
||||
export interface IItemEventDeletedPayload {
|
||||
// tenantId: number;
|
||||
itemId: number;
|
||||
oldItem: Item;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export enum ItemAction {
|
||||
CREATE = 'Create',
|
||||
EDIT = 'Edit',
|
||||
DELETE = 'Delete',
|
||||
VIEW = 'View',
|
||||
}
|
||||
|
||||
// export type ItemAbility = [ItemAction, AbilitySubject.Item];
|
||||
@@ -1,193 +0,0 @@
|
||||
export interface IModel {
|
||||
name: string;
|
||||
tableName: string;
|
||||
fields: { [key: string]: any };
|
||||
}
|
||||
|
||||
export interface IFilterMeta {
|
||||
sortOrder: string;
|
||||
sortBy: string;
|
||||
}
|
||||
|
||||
export interface IPaginationMeta {
|
||||
pageSize: number;
|
||||
page: number;
|
||||
}
|
||||
|
||||
export interface IModelMetaDefaultSort {
|
||||
sortOrder: ISortOrder;
|
||||
sortField: string;
|
||||
}
|
||||
|
||||
export type IModelColumnType =
|
||||
| 'text'
|
||||
| 'number'
|
||||
| 'enumeration'
|
||||
| 'boolean'
|
||||
| 'relation';
|
||||
|
||||
export type ISortOrder = 'DESC' | 'ASC';
|
||||
|
||||
export interface IModelMetaFieldCommon {
|
||||
name: string;
|
||||
column: string;
|
||||
columnable?: boolean;
|
||||
customQuery?: Function;
|
||||
required?: boolean;
|
||||
importHint?: string;
|
||||
importableRelationLabel?: string;
|
||||
order?: number;
|
||||
unique?: number;
|
||||
dataTransferObjectKey?: string;
|
||||
}
|
||||
|
||||
export interface IModelMetaFieldText {
|
||||
fieldType: 'text';
|
||||
minLength?: number;
|
||||
maxLength?: number;
|
||||
}
|
||||
export interface IModelMetaFieldBoolean {
|
||||
fieldType: 'boolean';
|
||||
}
|
||||
export interface IModelMetaFieldNumber {
|
||||
fieldType: 'number';
|
||||
min?: number;
|
||||
max?: number;
|
||||
}
|
||||
export interface IModelMetaFieldDate {
|
||||
fieldType: 'date';
|
||||
}
|
||||
export interface IModelMetaFieldUrl {
|
||||
fieldType: 'url';
|
||||
}
|
||||
export type IModelMetaField = IModelMetaFieldCommon &
|
||||
(
|
||||
| IModelMetaFieldText
|
||||
| IModelMetaFieldNumber
|
||||
| IModelMetaFieldBoolean
|
||||
| IModelMetaFieldDate
|
||||
| IModelMetaFieldUrl
|
||||
| IModelMetaEnumerationField
|
||||
| IModelMetaRelationField
|
||||
| IModelMetaCollectionField
|
||||
);
|
||||
|
||||
export interface IModelMetaEnumerationOption {
|
||||
key: string;
|
||||
label: string;
|
||||
}
|
||||
|
||||
export interface IModelMetaEnumerationField {
|
||||
fieldType: 'enumeration';
|
||||
options: IModelMetaEnumerationOption[];
|
||||
}
|
||||
|
||||
export interface IModelMetaRelationFieldCommon {
|
||||
fieldType: 'relation';
|
||||
}
|
||||
|
||||
export interface IModelMetaRelationEnumerationField {
|
||||
relationType: 'enumeration';
|
||||
relationKey: string;
|
||||
relationEntityLabel: string;
|
||||
relationEntityKey: string;
|
||||
}
|
||||
|
||||
export interface IModelMetaFieldWithFields {
|
||||
fields: IModelMetaFieldCommon2 &
|
||||
(
|
||||
| IModelMetaFieldText
|
||||
| IModelMetaFieldNumber
|
||||
| IModelMetaFieldBoolean
|
||||
| IModelMetaFieldDate
|
||||
| IModelMetaFieldUrl
|
||||
| IModelMetaEnumerationField
|
||||
| IModelMetaRelationField
|
||||
);
|
||||
}
|
||||
|
||||
interface IModelMetaCollectionObjectField extends IModelMetaFieldWithFields {
|
||||
collectionOf: 'object';
|
||||
}
|
||||
|
||||
export interface IModelMetaCollectionFieldCommon {
|
||||
fieldType: 'collection';
|
||||
collectionMinLength?: number;
|
||||
collectionMaxLength?: number;
|
||||
}
|
||||
|
||||
export type IModelMetaCollectionField = IModelMetaCollectionFieldCommon &
|
||||
IModelMetaCollectionObjectField;
|
||||
|
||||
export type IModelMetaRelationField = IModelMetaRelationFieldCommon &
|
||||
IModelMetaRelationEnumerationField;
|
||||
|
||||
interface IModelPrintMeta {
|
||||
pageTitle: string;
|
||||
}
|
||||
|
||||
export interface IModelMeta {
|
||||
defaultFilterField: string;
|
||||
defaultSort: IModelMetaDefaultSort;
|
||||
|
||||
exportable?: boolean;
|
||||
exportFlattenOn?: string;
|
||||
|
||||
importable?: boolean;
|
||||
importAggregator?: string;
|
||||
importAggregateOn?: string;
|
||||
importAggregateBy?: string;
|
||||
|
||||
print?: IModelPrintMeta;
|
||||
|
||||
fields: Record<string, IModelMetaField>;
|
||||
fields2: Record<string, IModelMetaField2>;
|
||||
columns: Record<string, IModelMetaColumn>;
|
||||
}
|
||||
|
||||
// ----
|
||||
export interface IModelMetaFieldCommon2 {
|
||||
name: string;
|
||||
required?: boolean;
|
||||
importHint?: string;
|
||||
order?: number;
|
||||
unique?: number;
|
||||
features?: Array<any>;
|
||||
}
|
||||
|
||||
export interface IModelMetaRelationField2 {
|
||||
fieldType: 'relation';
|
||||
relationModel: string;
|
||||
importableRelationLabel: string | string[];
|
||||
}
|
||||
|
||||
export type IModelMetaField2 = IModelMetaFieldCommon2 &
|
||||
(
|
||||
| IModelMetaFieldText
|
||||
| IModelMetaFieldNumber
|
||||
| IModelMetaFieldBoolean
|
||||
| IModelMetaFieldDate
|
||||
| IModelMetaFieldUrl
|
||||
| IModelMetaEnumerationField
|
||||
| IModelMetaRelationField2
|
||||
| IModelMetaCollectionField
|
||||
);
|
||||
|
||||
export interface ImodelMetaColumnMeta {
|
||||
name: string;
|
||||
accessor?: string;
|
||||
exportable?: boolean;
|
||||
}
|
||||
|
||||
interface IModelMetaColumnText {
|
||||
type: 'text;';
|
||||
}
|
||||
|
||||
interface IModelMetaColumnCollection {
|
||||
type: 'collection';
|
||||
collectionOf: 'object';
|
||||
columns: { [key: string]: ImodelMetaColumnMeta & IModelMetaColumnText };
|
||||
}
|
||||
|
||||
export type IModelMetaColumn = ImodelMetaColumnMeta &
|
||||
(IModelMetaColumnText | IModelMetaColumnCollection);
|
||||
@@ -1,172 +0,0 @@
|
||||
|
||||
const OperationType = {
|
||||
LOGIC: 'LOGIC',
|
||||
STRING: 'STRING',
|
||||
COMPARISON: 'COMPARISON',
|
||||
MATH: 'MATH',
|
||||
};
|
||||
|
||||
export class Lexer {
|
||||
// operation table
|
||||
static get optable() {
|
||||
return {
|
||||
'=': OperationType.LOGIC,
|
||||
'&': OperationType.LOGIC,
|
||||
'|': OperationType.LOGIC,
|
||||
'?': OperationType.LOGIC,
|
||||
':': OperationType.LOGIC,
|
||||
|
||||
'\'': OperationType.STRING,
|
||||
'"': OperationType.STRING,
|
||||
|
||||
'!': OperationType.COMPARISON,
|
||||
'>': OperationType.COMPARISON,
|
||||
'<': OperationType.COMPARISON,
|
||||
|
||||
'(': OperationType.MATH,
|
||||
')': OperationType.MATH,
|
||||
'+': OperationType.MATH,
|
||||
'-': OperationType.MATH,
|
||||
'*': OperationType.MATH,
|
||||
'/': OperationType.MATH,
|
||||
'%': OperationType.MATH,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param {*} expression -
|
||||
*/
|
||||
constructor(expression) {
|
||||
this.currentIndex = 0;
|
||||
this.input = expression;
|
||||
this.tokenList = [];
|
||||
}
|
||||
|
||||
getTokens() {
|
||||
let tok;
|
||||
do {
|
||||
// read current token, so step should be -1
|
||||
tok = this.pickNext(-1);
|
||||
const pos = this.currentIndex;
|
||||
switch (Lexer.optable[tok]) {
|
||||
case OperationType.LOGIC:
|
||||
// == && || ===
|
||||
this.readLogicOpt(tok);
|
||||
break;
|
||||
|
||||
case OperationType.STRING:
|
||||
this.readString(tok);
|
||||
break;
|
||||
|
||||
case OperationType.COMPARISON:
|
||||
this.readCompare(tok);
|
||||
break;
|
||||
|
||||
case OperationType.MATH:
|
||||
this.receiveToken();
|
||||
break;
|
||||
|
||||
default:
|
||||
this.readValue(tok);
|
||||
}
|
||||
|
||||
// if the pos not changed, this loop will go into a infinite loop, every step of while loop,
|
||||
// we must move the pos forward
|
||||
// so here we should throw error, for example `1 & 2`
|
||||
if (pos === this.currentIndex && tok !== undefined) {
|
||||
const err = new Error(`unkonw token ${tok} from input string ${this.input}`);
|
||||
err.name = 'UnknowToken';
|
||||
throw err;
|
||||
}
|
||||
} while (tok !== undefined)
|
||||
|
||||
return this.tokenList;
|
||||
}
|
||||
|
||||
/**
|
||||
* read next token, the index param can set next step, default go foward 1 step
|
||||
*
|
||||
* @param index next postion
|
||||
*/
|
||||
pickNext(index = 0) {
|
||||
return this.input[index + this.currentIndex + 1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Store token into result tokenList, and move the pos index
|
||||
*
|
||||
* @param index
|
||||
*/
|
||||
receiveToken(index = 1) {
|
||||
const tok = this.input.slice(this.currentIndex, this.currentIndex + index).trim();
|
||||
// skip empty string
|
||||
if (tok) {
|
||||
this.tokenList.push(tok);
|
||||
}
|
||||
|
||||
this.currentIndex += index;
|
||||
}
|
||||
|
||||
// ' or "
|
||||
readString(tok) {
|
||||
let next;
|
||||
let index = 0;
|
||||
do {
|
||||
next = this.pickNext(index);
|
||||
index += 1;
|
||||
} while (next !== tok && next !== undefined);
|
||||
this.receiveToken(index + 1);
|
||||
}
|
||||
|
||||
// > or < or >= or <= or !==
|
||||
// tok in (>, <, !)
|
||||
readCompare(tok) {
|
||||
if (this.pickNext() !== '=') {
|
||||
this.receiveToken(1);
|
||||
return;
|
||||
}
|
||||
// !==
|
||||
if (tok === '!' && this.pickNext(1) === '=') {
|
||||
this.receiveToken(3);
|
||||
return;
|
||||
}
|
||||
this.receiveToken(2);
|
||||
}
|
||||
|
||||
// === or ==
|
||||
// && ||
|
||||
readLogicOpt(tok) {
|
||||
if (this.pickNext() === tok) {
|
||||
// ===
|
||||
if (tok === '=' && this.pickNext(1) === tok) {
|
||||
return this.receiveToken(3);
|
||||
}
|
||||
// == && ||
|
||||
return this.receiveToken(2);
|
||||
}
|
||||
// handle as &&
|
||||
// a ? b : c is equal to a && b || c
|
||||
if (tok === '?' || tok === ':') {
|
||||
return this.receiveToken(1);
|
||||
}
|
||||
}
|
||||
|
||||
readValue(tok) {
|
||||
if (!tok) {
|
||||
return;
|
||||
}
|
||||
|
||||
let index = 0;
|
||||
while (!Lexer.optable[tok] && tok !== undefined) {
|
||||
tok = this.pickNext(index);
|
||||
index += 1;
|
||||
}
|
||||
this.receiveToken(index);
|
||||
}
|
||||
}
|
||||
|
||||
export default function token(expression) {
|
||||
const lexer = new Lexer(expression);
|
||||
return lexer.getTokens();
|
||||
}
|
||||
@@ -1,159 +0,0 @@
|
||||
export const OPERATION = {
|
||||
'!': 5,
|
||||
'*': 4,
|
||||
'/': 4,
|
||||
'%': 4,
|
||||
'+': 3,
|
||||
'-': 3,
|
||||
'>': 2,
|
||||
'<': 2,
|
||||
'>=': 2,
|
||||
'<=': 2,
|
||||
'===': 2,
|
||||
'!==': 2,
|
||||
'==': 2,
|
||||
'!=': 2,
|
||||
'&&': 1,
|
||||
'||': 1,
|
||||
'?': 1,
|
||||
':': 1,
|
||||
};
|
||||
|
||||
// export interface Node {
|
||||
// left: Node | string | null;
|
||||
// right: Node | string | null;
|
||||
// operation: string;
|
||||
// grouped?: boolean;
|
||||
// };
|
||||
|
||||
export default class Parser {
|
||||
|
||||
constructor(token) {
|
||||
this.index = -1;
|
||||
this.blockLevel = 0;
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return {Node | string} =-
|
||||
*/
|
||||
parse() {
|
||||
let tok;
|
||||
let root = {
|
||||
left: null,
|
||||
right: null,
|
||||
operation: null,
|
||||
};
|
||||
|
||||
do {
|
||||
tok = this.parseStatement();
|
||||
|
||||
if (tok === null || tok === undefined) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (root.left === null) {
|
||||
root.left = tok;
|
||||
root.operation = this.nextToken();
|
||||
|
||||
if (!root.operation) {
|
||||
return tok;
|
||||
}
|
||||
|
||||
root.right = this.parseStatement();
|
||||
} else {
|
||||
if (typeof tok !== 'string') {
|
||||
throw new Error('operation must be string, but get ' + JSON.stringify(tok));
|
||||
}
|
||||
root = this.addNode(tok, this.parseStatement(), root);
|
||||
}
|
||||
} while (tok);
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
nextToken() {
|
||||
this.index += 1;
|
||||
return this.token[this.index];
|
||||
}
|
||||
|
||||
prevToken() {
|
||||
return this.token[this.index - 1];
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} operation
|
||||
* @param {Node|String|null} right
|
||||
* @param {Node} root
|
||||
*/
|
||||
addNode(operation, right, root) {
|
||||
let pre = root;
|
||||
|
||||
if (this.compare(pre.operation, operation) < 0 && !pre.grouped) {
|
||||
|
||||
while (pre.right !== null &&
|
||||
typeof pre.right !== 'string' &&
|
||||
this.compare(pre.right.operation, operation) < 0 && !pre.right.grouped) {
|
||||
pre = pre.right;
|
||||
}
|
||||
|
||||
pre.right = {
|
||||
operation,
|
||||
left: pre.right,
|
||||
right,
|
||||
};
|
||||
return root;
|
||||
}
|
||||
return {
|
||||
left: pre,
|
||||
right,
|
||||
operation,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {String} a
|
||||
* @param {String} b
|
||||
*/
|
||||
compare(a, b) {
|
||||
if (!OPERATION.hasOwnProperty(a) || !OPERATION.hasOwnProperty(b)) {
|
||||
throw new Error(`unknow operation ${a} or ${b}`);
|
||||
}
|
||||
return OPERATION[a] - OPERATION[b];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string | Node | null
|
||||
*/
|
||||
parseStatement() {
|
||||
const token = this.nextToken();
|
||||
if (token === '(') {
|
||||
this.blockLevel += 1;
|
||||
const node = this.parse();
|
||||
this.blockLevel -= 1;
|
||||
|
||||
if (typeof node !== 'string') {
|
||||
node.grouped = true;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
if (token === ')') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (token === '!') {
|
||||
return { left: null, operation: token, right: this.parseStatement() }
|
||||
}
|
||||
|
||||
// 3 > -12 or -12 + 10
|
||||
if (token === '-' && (OPERATION[this.prevToken()] > 0 || this.prevToken() === undefined)) {
|
||||
return { left: '0', operation: token, right: this.parseStatement(), grouped: true };
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
import { OPERATION } from './Parser';
|
||||
|
||||
export default class QueryParser {
|
||||
|
||||
constructor(tree, queries) {
|
||||
this.tree = tree;
|
||||
this.queries = queries;
|
||||
this.query = null;
|
||||
}
|
||||
|
||||
setQuery(query) {
|
||||
this.query = query.clone();
|
||||
}
|
||||
|
||||
parse() {
|
||||
return this.parseNode(this.tree);
|
||||
}
|
||||
|
||||
parseNode(node) {
|
||||
if (typeof node === 'string') {
|
||||
const nodeQuery = this.getQuery(node);
|
||||
return (query) => { nodeQuery(query); };
|
||||
}
|
||||
if (OPERATION[node.operation] === undefined) {
|
||||
throw new Error(`unknow expression ${node.operation}`);
|
||||
}
|
||||
const leftQuery = this.getQuery(node.left);
|
||||
const rightQuery = this.getQuery(node.right);
|
||||
|
||||
switch (node.operation) {
|
||||
case '&&':
|
||||
case 'AND':
|
||||
default:
|
||||
return (nodeQuery) => nodeQuery.where((query) => {
|
||||
query.where((q) => { leftQuery(q); });
|
||||
query.andWhere((q) => { rightQuery(q); });
|
||||
});
|
||||
case '||':
|
||||
case 'OR':
|
||||
return (nodeQuery) => nodeQuery.where((query) => {
|
||||
query.where((q) => { leftQuery(q); });
|
||||
query.orWhere((q) => { rightQuery(q); });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
getQuery(node) {
|
||||
if (typeof node !== 'string' && node !== null) {
|
||||
return this.parseNode(node);
|
||||
}
|
||||
const value = parseFloat(node);
|
||||
|
||||
if (!isNaN(value)) {
|
||||
if (typeof this.queries[node] === 'undefined') {
|
||||
throw new Error(`unknow query under index ${node}`);
|
||||
}
|
||||
return this.queries[node];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
||||
import { ClsMiddleware } from 'nestjs-cls';
|
||||
import './utils/moment-mysql';
|
||||
import { AppModule } from './modules/App/App.module';
|
||||
import { ServiceErrorFilter } from './common/filters/service-error.filter';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
app.setGlobalPrefix('/api');
|
||||
|
||||
// create and mount the middleware manually here
|
||||
app.use(new ClsMiddleware({}).use);
|
||||
|
||||
const config = new DocumentBuilder()
|
||||
.setTitle('Bigcapital')
|
||||
.setDescription('Financial accounting software')
|
||||
.setVersion('1.0')
|
||||
.addTag('cats')
|
||||
.build();
|
||||
|
||||
const documentFactory = () => SwaggerModule.createDocument(app, config);
|
||||
SwaggerModule.setup('swagger', app, documentFactory);
|
||||
|
||||
app.useGlobalFilters(new ServiceErrorFilter());
|
||||
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
@@ -1,43 +0,0 @@
|
||||
import { QueryBuilder, Model } from 'objection';
|
||||
|
||||
interface PaginationResult<M extends Model> {
|
||||
results: M[];
|
||||
pagination: {
|
||||
total: number;
|
||||
page: number;
|
||||
pageSize: number;
|
||||
};
|
||||
}
|
||||
|
||||
export type PaginationQueryBuilderType<M extends Model> = QueryBuilder<
|
||||
M,
|
||||
PaginationResult<M>
|
||||
>;
|
||||
|
||||
class PaginationQueryBuilder<M extends Model, R = M[]> extends QueryBuilder<
|
||||
M,
|
||||
R
|
||||
> {
|
||||
pagination(page: number, pageSize: number): PaginationQueryBuilderType<M> {
|
||||
const query = super.page(page, pageSize);
|
||||
|
||||
return query.runAfter(({ results, total }) => {
|
||||
return {
|
||||
results,
|
||||
pagination: {
|
||||
total,
|
||||
page: page + 1,
|
||||
pageSize,
|
||||
},
|
||||
};
|
||||
}) as unknown as PaginationQueryBuilderType<M>;
|
||||
}
|
||||
}
|
||||
|
||||
export class BaseModel extends Model {
|
||||
public readonly id: number;
|
||||
public readonly tableName: string;
|
||||
|
||||
QueryBuilderType!: PaginationQueryBuilder<this>;
|
||||
static QueryBuilder = PaginationQueryBuilder;
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
import {
|
||||
Controller,
|
||||
Post,
|
||||
Body,
|
||||
Param,
|
||||
Delete,
|
||||
Get,
|
||||
Query,
|
||||
ParseIntPipe,
|
||||
} from '@nestjs/common';
|
||||
import { AccountsApplication } from './AccountsApplication.service';
|
||||
import { CreateAccountDTO } from './CreateAccount.dto';
|
||||
import { EditAccountDTO } from './EditAccount.dto';
|
||||
import { PublicRoute } from '../Auth/Jwt.guard';
|
||||
import { IAccountsFilter, IAccountsTransactionsFilter } from './Accounts.types';
|
||||
// import { IAccountsFilter, IAccountsTransactionsFilter } from './Accounts.types';
|
||||
// import { ZodValidationPipe } from '@/common/pipes/ZodValidation.pipe';
|
||||
|
||||
@Controller('accounts')
|
||||
@PublicRoute()
|
||||
export class AccountsController {
|
||||
constructor(private readonly accountsApplication: AccountsApplication) {}
|
||||
|
||||
@Post()
|
||||
async createAccount(@Body() accountDTO: CreateAccountDTO) {
|
||||
return this.accountsApplication.createAccount(accountDTO);
|
||||
}
|
||||
|
||||
@Post(':id')
|
||||
async editAccount(
|
||||
@Param('id', ParseIntPipe) id: number,
|
||||
@Body() accountDTO: EditAccountDTO,
|
||||
) {
|
||||
return this.accountsApplication.editAccount(id, accountDTO);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async deleteAccount(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.accountsApplication.deleteAccount(id);
|
||||
}
|
||||
|
||||
@Post(':id/activate')
|
||||
async activateAccount(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.accountsApplication.activateAccount(id);
|
||||
}
|
||||
|
||||
@Post(':id/inactivate')
|
||||
async inactivateAccount(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.accountsApplication.inactivateAccount(id);
|
||||
}
|
||||
|
||||
@Get('types')
|
||||
async getAccountTypes() {
|
||||
return this.accountsApplication.getAccountTypes();
|
||||
}
|
||||
|
||||
@Get('transactions')
|
||||
async getAccountTransactions(@Query() filter: IAccountsTransactionsFilter) {
|
||||
return this.accountsApplication.getAccountsTransactions(filter);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async getAccount(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.accountsApplication.getAccount(id);
|
||||
}
|
||||
|
||||
@Get()
|
||||
async getAccounts(@Query() filter: IAccountsFilter) {
|
||||
return this.accountsApplication.getAccounts(filter);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
// import { Inject, Service } from 'typedi';
|
||||
// import { AccountsApplication } from './AccountsApplication.service';
|
||||
// import { Exportable } from '../Export/Exportable';
|
||||
// import { IAccountsFilter, IAccountsStructureType } from '@/interfaces';
|
||||
// import { EXPORT_SIZE_LIMIT } from '../Export/constants';
|
||||
|
||||
// @Service()
|
||||
// export class AccountsExportable extends Exportable {
|
||||
// @Inject()
|
||||
// private accountsApplication: AccountsApplication;
|
||||
|
||||
// /**
|
||||
// * Retrieves the accounts data to exportable sheet.
|
||||
// * @param {number} tenantId
|
||||
// * @returns
|
||||
// */
|
||||
// public exportable(tenantId: number, query: IAccountsFilter) {
|
||||
// const parsedQuery = {
|
||||
// sortOrder: 'desc',
|
||||
// columnSortBy: 'created_at',
|
||||
// inactiveMode: false,
|
||||
// ...query,
|
||||
// structure: IAccountsStructureType.Flat,
|
||||
// pageSize: EXPORT_SIZE_LIMIT,
|
||||
// page: 1,
|
||||
// } as IAccountsFilter;
|
||||
|
||||
// return this.accountsApplication
|
||||
// .getAccounts(tenantId, parsedQuery)
|
||||
// .then((output) => output.accounts);
|
||||
// }
|
||||
// }
|
||||
@@ -1,45 +0,0 @@
|
||||
// import { Inject, Service } from 'typedi';
|
||||
// import { Knex } from 'knex';
|
||||
// import { IAccountCreateDTO } from '@/interfaces';
|
||||
// import { CreateAccount } from './CreateAccount.service';
|
||||
// import { Importable } from '../Import/Importable';
|
||||
// import { AccountsSampleData } from './AccountsImportable.SampleData';
|
||||
|
||||
// @Service()
|
||||
// export class AccountsImportable extends Importable {
|
||||
// @Inject()
|
||||
// private createAccountService: CreateAccount;
|
||||
|
||||
// /**
|
||||
// * Importing to account service.
|
||||
// * @param {number} tenantId
|
||||
// * @param {IAccountCreateDTO} createAccountDTO
|
||||
// * @returns
|
||||
// */
|
||||
// public importable(
|
||||
// tenantId: number,
|
||||
// createAccountDTO: IAccountCreateDTO,
|
||||
// trx?: Knex.Transaction
|
||||
// ) {
|
||||
// return this.createAccountService.createAccount(
|
||||
// tenantId,
|
||||
// createAccountDTO,
|
||||
// trx
|
||||
// );
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Concurrrency controlling of the importing process.
|
||||
// * @returns {number}
|
||||
// */
|
||||
// public get concurrency() {
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Retrieves the sample data that used to download accounts sample sheet.
|
||||
// */
|
||||
// public sampleData(): any[] {
|
||||
// return AccountsSampleData;
|
||||
// }
|
||||
// }
|
||||
@@ -1,51 +0,0 @@
|
||||
import {
|
||||
IsString,
|
||||
IsOptional,
|
||||
IsInt,
|
||||
MinLength,
|
||||
MaxLength,
|
||||
IsBoolean,
|
||||
} from 'class-validator';
|
||||
|
||||
export class CreateAccountDTO {
|
||||
@IsString()
|
||||
@MinLength(3)
|
||||
@MaxLength(255) // Assuming DATATYPES_LENGTH.STRING is 255
|
||||
name: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MinLength(3)
|
||||
@MaxLength(6)
|
||||
code?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
currencyCode?: string;
|
||||
|
||||
@IsString()
|
||||
@MinLength(3)
|
||||
@MaxLength(255) // Assuming DATATYPES_LENGTH.STRING is 255
|
||||
accountType: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MaxLength(65535) // Assuming DATATYPES_LENGTH.TEXT is 65535
|
||||
description?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
parentAccountId?: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
active?: boolean;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
plaidAccountId?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
plaidItemId?: string;
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
import {
|
||||
IsString,
|
||||
IsOptional,
|
||||
IsInt,
|
||||
MinLength,
|
||||
MaxLength,
|
||||
} from 'class-validator';
|
||||
|
||||
export class EditAccountDTO {
|
||||
@IsString()
|
||||
@MinLength(3)
|
||||
@MaxLength(255) // Assuming DATATYPES_LENGTH.STRING is 255
|
||||
name: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MinLength(3)
|
||||
@MaxLength(6)
|
||||
code?: string;
|
||||
|
||||
@IsString()
|
||||
@MinLength(3)
|
||||
@MaxLength(255) // Assuming DATATYPES_LENGTH.STRING is 255
|
||||
accountType: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MaxLength(65535) // Assuming DATATYPES_LENGTH.TEXT is 65535
|
||||
description?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
parentAccountId?: number;
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import * as R from 'ramda';
|
||||
import { IAccountsFilter } from './Accounts.types';
|
||||
import { DynamicListService } from '../DynamicListing/DynamicList.service';
|
||||
import { AccountTransformer } from './Account.transformer';
|
||||
import { TransformerInjectable } from '../Transformer/TransformerInjectable.service';
|
||||
import { Account } from './models/Account.model';
|
||||
import { AccountRepository } from './repositories/Account.repository';
|
||||
import { IFilterMeta } from '@/interfaces/Model';
|
||||
|
||||
@Injectable()
|
||||
export class GetAccountsService {
|
||||
constructor(
|
||||
private readonly dynamicListService: DynamicListService,
|
||||
private readonly transformerService: TransformerInjectable,
|
||||
|
||||
@Inject(Account.name)
|
||||
private readonly accountModel: typeof Account,
|
||||
private readonly accountRepository: AccountRepository,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieve accounts datatable list.
|
||||
* @param {IAccountsFilter} accountsFilter
|
||||
* @returns {Promise<{ accounts: IAccountResponse[]; filterMeta: IFilterMeta }>}
|
||||
*/
|
||||
public async getAccountsList(
|
||||
filterDTO: IAccountsFilter,
|
||||
): Promise<{ accounts: Account[]; filterMeta: IFilterMeta }> {
|
||||
// Parses the stringified filter roles.
|
||||
const filter = this.parseListFilterDTO(filterDTO);
|
||||
|
||||
// Dynamic list service.
|
||||
const dynamicList = await this.dynamicListService.dynamicList(
|
||||
this.accountModel,
|
||||
filter,
|
||||
);
|
||||
// Retrieve accounts model based on the given query.
|
||||
const accounts = await this.accountModel.query().onBuild((builder) => {
|
||||
dynamicList.buildQuery()(builder);
|
||||
builder.modify('inactiveMode', filter.inactiveMode);
|
||||
});
|
||||
const accountsGraph = await this.accountRepository.getDependencyGraph();
|
||||
|
||||
// Retrieves the transformed accounts collection.
|
||||
const transformedAccounts = await this.transformerService.transform(
|
||||
accounts,
|
||||
new AccountTransformer(),
|
||||
{ accountsGraph, structure: filterDTO.structure },
|
||||
);
|
||||
|
||||
return {
|
||||
accounts: transformedAccounts,
|
||||
filterMeta: dynamicList.getResponseMeta(),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Parsees accounts list filter DTO.
|
||||
* @param filterDTO
|
||||
* @returns
|
||||
*/
|
||||
private parseListFilterDTO(filterDTO) {
|
||||
return R.compose(this.dynamicListService.parseStringifiedFilter)(filterDTO);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
// import { Inject, Service } from 'typedi';
|
||||
// import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
|
||||
// @Service()
|
||||
// export class MutateBaseCurrencyAccounts {
|
||||
// @Inject()
|
||||
// tenancy: HasTenancyService;
|
||||
|
||||
// /**
|
||||
// * Mutates the all accounts or the organziation.
|
||||
// * @param {number} tenantId
|
||||
// * @param {string} currencyCode
|
||||
// */
|
||||
// public mutateAllAccountsCurrency = async (
|
||||
// tenantId: number,
|
||||
// currencyCode: string
|
||||
// ) => {
|
||||
// const { Account } = this.tenancy.models(tenantId);
|
||||
|
||||
// await Account.query().update({ currencyCode });
|
||||
// };
|
||||
// }
|
||||
@@ -1,34 +0,0 @@
|
||||
// import { Service, Inject } from 'typedi';
|
||||
// import events from '@/subscribers/events';
|
||||
// import { MutateBaseCurrencyAccounts } from '../MutateBaseCurrencyAccounts';
|
||||
|
||||
// @Service()
|
||||
// export class MutateBaseCurrencyAccountsSubscriber {
|
||||
// @Inject()
|
||||
// public mutateBaseCurrencyAccounts: MutateBaseCurrencyAccounts;
|
||||
|
||||
// /**
|
||||
// * Attaches the events with handles.
|
||||
// * @param bus
|
||||
// */
|
||||
// attach(bus) {
|
||||
// bus.subscribe(
|
||||
// events.organization.baseCurrencyUpdated,
|
||||
// this.updateAccountsCurrencyOnBaseCurrencyMutated
|
||||
// );
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Updates the all accounts currency once the base currency
|
||||
// * of the organization is mutated.
|
||||
// */
|
||||
// private updateAccountsCurrencyOnBaseCurrencyMutated = async ({
|
||||
// tenantId,
|
||||
// organizationDTO,
|
||||
// }) => {
|
||||
// await this.mutateBaseCurrencyAccounts.mutateAllAccountsCurrency(
|
||||
// tenantId,
|
||||
// organizationDTO.baseCurrency
|
||||
// );
|
||||
// };
|
||||
// }
|
||||
@@ -1,190 +0,0 @@
|
||||
import { MiddlewareConsumer, Module, RequestMethod } from '@nestjs/common';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { EventEmitterModule } from '@nestjs/event-emitter';
|
||||
import { join } from 'path';
|
||||
import {
|
||||
AcceptLanguageResolver,
|
||||
CookieResolver,
|
||||
HeaderResolver,
|
||||
I18nModule,
|
||||
QueryResolver,
|
||||
} from 'nestjs-i18n';
|
||||
import { BullModule } from '@nestjs/bullmq';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { PassportModule } from '@nestjs/passport';
|
||||
import { ClsModule } from 'nestjs-cls';
|
||||
import { AppController } from './App.controller';
|
||||
import { AppService } from './App.service';
|
||||
import { ItemsModule } from '../Items/items.module';
|
||||
import { config } from '../../common/config';
|
||||
import { SystemDatabaseModule } from '../System/SystemDB/SystemDB.module';
|
||||
import { SystemModelsModule } from '../System/SystemModels/SystemModels.module';
|
||||
import { JwtStrategy } from '../Auth/Jwt.strategy';
|
||||
import { jwtConstants } from '../Auth/Auth.constants';
|
||||
import { TenancyDatabaseModule } from '../Tenancy/TenancyDB/TenancyDB.module';
|
||||
import { TenancyModelsModule } from '../Tenancy/TenancyModels/Tenancy.module';
|
||||
import { LoggerMiddleware } from '@/middleware/logger.middleware';
|
||||
import { ExcludeNullInterceptor } from '@/interceptors/ExcludeNull.interceptor';
|
||||
import { APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';
|
||||
import { JwtAuthGuard } from '../Auth/Jwt.guard';
|
||||
import { UserIpInterceptor } from '@/interceptors/user-ip.interceptor';
|
||||
import { TenancyGlobalMiddleware } from '../Tenancy/TenancyGlobal.middleware';
|
||||
import { TransformerModule } from '../Transformer/Transformer.module';
|
||||
import { AccountsModule } from '../Accounts/Accounts.module';
|
||||
import { ExpensesModule } from '../Expenses/Expenses.module';
|
||||
import { ItemCategoryModule } from '../ItemCategories/ItemCategory.module';
|
||||
import { TaxRatesModule } from '../TaxRates/TaxRate.module';
|
||||
import { PdfTemplatesModule } from '../PdfTemplate/PdfTemplates.module';
|
||||
import { BranchesModule } from '../Branches/Branches.module';
|
||||
import { WarehousesModule } from '../Warehouses/Warehouses.module';
|
||||
import { SerializeInterceptor } from '@/common/interceptors/serialize.interceptor';
|
||||
import { ChromiumlyTenancyModule } from '../ChromiumlyTenancy/ChromiumlyTenancy.module';
|
||||
import { CustomersModule } from '../Customers/Customers.module';
|
||||
import { VendorsModule } from '../Vendors/Vendors.module';
|
||||
import { SaleEstimatesModule } from '../SaleEstimates/SaleEstimates.module';
|
||||
import { BillsModule } from '../Bills/Bills.module';
|
||||
import { SaleInvoicesModule } from '../SaleInvoices/SaleInvoices.module';
|
||||
import { SaleReceiptsModule } from '../SaleReceipts/SaleReceipts.module';
|
||||
import { ManualJournalsModule } from '../ManualJournals/ManualJournals.module';
|
||||
import { CreditNotesModule } from '../CreditNotes/CreditNotes.module';
|
||||
import { VendorCreditsModule } from '../VendorCredit/VendorCredits.module';
|
||||
import { VendorCreditApplyBillsModule } from '../VendorCreditsApplyBills/VendorCreditApplyBills.module';
|
||||
import { VendorCreditsRefundModule } from '../VendorCreditsRefund/VendorCreditsRefund.module';
|
||||
import { CreditNoteRefundsModule } from '../CreditNoteRefunds/CreditNoteRefunds.module';
|
||||
import { BillPaymentsModule } from '../BillPayments/BillPayments.module';
|
||||
import { PaymentsReceivedModule } from '../PaymentReceived/PaymentsReceived.module';
|
||||
import { LedgerModule } from '../Ledger/Ledger.module';
|
||||
import { BankRulesModule } from '../BankRules/BankRules.module';
|
||||
import { BankAccountsModule } from '../BankingAccounts/BankAccounts.module';
|
||||
import { BankingTransactionsExcludeModule } from '../BankingTransactionsExclude/BankingTransactionsExclude.module';
|
||||
import { BankingTransactionsRegonizeModule } from '../BankingTranasctionsRegonize/BankingTransactionsRegonize.module';
|
||||
import { BankingMatchingModule } from '../BankingMatching/BankingMatching.module';
|
||||
import { BankingTransactionsModule } from '../BankingTransactions/BankingTransactions.module';
|
||||
import { TransactionsLockingModule } from '../TransactionsLocking/TransactionsLocking.module';
|
||||
import { SettingsModule } from '../Settings/Settings.module';
|
||||
import { InventoryAdjustmentsModule } from '../InventoryAdjutments/InventoryAdjustments.module';
|
||||
import { PostHogModule } from '../EventsTracker/postHog.module';
|
||||
import { EventTrackerModule } from '../EventsTracker/EventTracker.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
envFilePath: '.env',
|
||||
load: config,
|
||||
isGlobal: true,
|
||||
}),
|
||||
SystemDatabaseModule,
|
||||
SystemModelsModule,
|
||||
EventEmitterModule.forRoot(),
|
||||
I18nModule.forRootAsync({
|
||||
useFactory: () => ({
|
||||
fallbackLanguage: 'en',
|
||||
loaderOptions: {
|
||||
path: join(__dirname, '/../../i18n/'),
|
||||
watch: true,
|
||||
},
|
||||
}),
|
||||
resolvers: [
|
||||
new QueryResolver(),
|
||||
new HeaderResolver(),
|
||||
new CookieResolver(),
|
||||
AcceptLanguageResolver,
|
||||
],
|
||||
}),
|
||||
PassportModule,
|
||||
JwtModule.register({
|
||||
secret: jwtConstants.secret,
|
||||
signOptions: { expiresIn: '60s' },
|
||||
}),
|
||||
BullModule.forRootAsync({
|
||||
imports: [ConfigModule],
|
||||
useFactory: async (configService: ConfigService) => ({
|
||||
connection: {
|
||||
host: configService.get('QUEUE_HOST'),
|
||||
port: configService.get('QUEUE_PORT'),
|
||||
},
|
||||
}),
|
||||
inject: [ConfigService],
|
||||
}),
|
||||
ClsModule.forRoot({
|
||||
global: true,
|
||||
middleware: {
|
||||
mount: true,
|
||||
setup: (cls, req: Request, res: Response) => {
|
||||
cls.set('organizationId', req.headers['organization-id']);
|
||||
cls.set('userId', 1);
|
||||
},
|
||||
},
|
||||
}),
|
||||
TenancyDatabaseModule,
|
||||
TenancyModelsModule,
|
||||
ChromiumlyTenancyModule,
|
||||
TransformerModule,
|
||||
ItemsModule,
|
||||
ItemCategoryModule,
|
||||
AccountsModule,
|
||||
ExpensesModule,
|
||||
TaxRatesModule,
|
||||
PdfTemplatesModule,
|
||||
BranchesModule,
|
||||
WarehousesModule,
|
||||
CustomersModule,
|
||||
VendorsModule,
|
||||
SaleInvoicesModule,
|
||||
SaleEstimatesModule,
|
||||
SaleReceiptsModule,
|
||||
BillsModule,
|
||||
ManualJournalsModule,
|
||||
CreditNotesModule,
|
||||
VendorCreditsModule,
|
||||
VendorCreditApplyBillsModule,
|
||||
VendorCreditsRefundModule,
|
||||
CreditNoteRefundsModule,
|
||||
BillPaymentsModule,
|
||||
PaymentsReceivedModule,
|
||||
LedgerModule,
|
||||
BankAccountsModule,
|
||||
BankRulesModule,
|
||||
BankingTransactionsModule,
|
||||
BankingTransactionsExcludeModule,
|
||||
BankingTransactionsRegonizeModule,
|
||||
BankingMatchingModule,
|
||||
TransactionsLockingModule,
|
||||
SettingsModule,
|
||||
InventoryAdjustmentsModule,
|
||||
PostHogModule,
|
||||
EventTrackerModule,
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [
|
||||
{
|
||||
provide: APP_INTERCEPTOR,
|
||||
useClass: SerializeInterceptor,
|
||||
},
|
||||
{
|
||||
provide: APP_GUARD,
|
||||
useClass: JwtAuthGuard,
|
||||
},
|
||||
{
|
||||
provide: APP_INTERCEPTOR,
|
||||
useClass: UserIpInterceptor,
|
||||
},
|
||||
{
|
||||
provide: APP_INTERCEPTOR,
|
||||
useClass: ExcludeNullInterceptor,
|
||||
},
|
||||
AppService,
|
||||
JwtStrategy,
|
||||
],
|
||||
})
|
||||
export class AppModule {
|
||||
configure(consumer: MiddlewareConsumer) {
|
||||
consumer
|
||||
.apply(LoggerMiddleware)
|
||||
.forRoutes({ path: '*', method: RequestMethod.ALL });
|
||||
|
||||
consumer
|
||||
.apply(TenancyGlobalMiddleware)
|
||||
.forRoutes({ path: '*', method: RequestMethod.ALL });
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
// configService: ConfigService;
|
||||
|
||||
constructor(
|
||||
private configService: ConfigService,
|
||||
private jwtService: JwtService,
|
||||
) {}
|
||||
|
||||
getHello(): string {
|
||||
console.log(this.configService.get('DATABASE_PORT'));
|
||||
const payload = {};
|
||||
|
||||
const accessToken = this.jwtService.sign(payload);
|
||||
|
||||
console.log(accessToken);
|
||||
|
||||
return accessToken;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
export const jwtConstants = {
|
||||
secret:
|
||||
'DO NOT USE THIS VALUE. INSTEAD, CREATE A COMPLEX SECRET AND KEEP IT SAFE OUTSIDE OF THE SOURCE CODE.',
|
||||
};
|
||||
@@ -1,5 +0,0 @@
|
||||
export interface IAuthSignedInEventPayload {}
|
||||
|
||||
export interface IAuthSigningInEventPayload {}
|
||||
|
||||
export interface IAuthSignInPOJO {}
|
||||
@@ -1,4 +0,0 @@
|
||||
|
||||
export class AuthApplication {
|
||||
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
import {
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
Scope,
|
||||
SetMetadata,
|
||||
} from '@nestjs/common';
|
||||
import { Reflector } from '@nestjs/core';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { ClsService } from 'nestjs-cls';
|
||||
|
||||
export const IS_PUBLIC_KEY = 'isPublic';
|
||||
export const PublicRoute = () => SetMetadata(IS_PUBLIC_KEY, true);
|
||||
|
||||
@Injectable()
|
||||
export class JwtAuthGuard extends AuthGuard('jwt') {
|
||||
constructor(
|
||||
private reflector: Reflector,
|
||||
private readonly cls: ClsService,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
canActivate(context: ExecutionContext) {
|
||||
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
|
||||
context.getHandler(),
|
||||
context.getClass(),
|
||||
]);
|
||||
if (isPublic) {
|
||||
return true;
|
||||
}
|
||||
return super.canActivate(context);
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
import { ExtractJwt, Strategy } from 'passport-jwt';
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { jwtConstants } from './Auth.constants';
|
||||
|
||||
@Injectable()
|
||||
export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
constructor() {
|
||||
super({
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
ignoreExpiration: false,
|
||||
secretOrKey: jwtConstants.secret,
|
||||
});
|
||||
}
|
||||
|
||||
async validate(payload: any) {
|
||||
return { userId: payload.sub, username: payload.username };
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
/**
|
||||
* Auto increment orders service.
|
||||
*/
|
||||
@Injectable()
|
||||
export class AutoIncrementOrdersService {
|
||||
/**
|
||||
* Check if the auto increment is enabled for the given settings group.
|
||||
* @param {string} settingsGroup - Settings group.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public autoIncrementEnabled = (settingsGroup: string): boolean => {
|
||||
// const settings = this.tenancy.settings(tenantId);
|
||||
// const group = settingsGroup;
|
||||
|
||||
// // Settings service transaction number and prefix.
|
||||
// return settings.get({ group, key: 'auto_increment' }, false);
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve the next service transaction number.
|
||||
* @param {string} settingsGroup
|
||||
* @param {Function} getMaxTransactionNo
|
||||
* @return {Promise<string>}
|
||||
*/
|
||||
getNextTransactionNumber(group: string): string {
|
||||
// const settings = this.tenancy.settings(tenantId);
|
||||
|
||||
// // Settings service transaction number and prefix.
|
||||
// const autoIncrement = this.autoIncrementEnabled(tenantId, group);
|
||||
|
||||
// const settingNo = settings.get({ group, key: 'next_number' }, '');
|
||||
// const settingPrefix = settings.get({ group, key: 'number_prefix' }, '');
|
||||
|
||||
// return autoIncrement ? `${settingPrefix}${settingNo}` : '';
|
||||
|
||||
return '1';
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment setting next number.
|
||||
* @param {string} orderGroup - Order group.
|
||||
* @param {string} orderNumber -Order number.
|
||||
*/
|
||||
async incrementSettingsNextNumber(group: string) {
|
||||
// const settings = this.tenancy.settings(tenantId);
|
||||
// const settingNo = settings.get({ group, key: 'next_number' });
|
||||
// const autoIncrement = settings.get({ group, key: 'auto_increment' });
|
||||
// // Can't continue if the auto-increment of the service was disabled.
|
||||
// if (!autoIncrement) {
|
||||
// return;
|
||||
// }
|
||||
// settings.set(
|
||||
// { group, key: 'next_number' },
|
||||
// transactionIncrement(settingNo)
|
||||
// );
|
||||
// await settings.save();
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Param,
|
||||
Post,
|
||||
Put,
|
||||
} from '@nestjs/common';
|
||||
import { BankRulesApplication } from './BankRulesApplication';
|
||||
import { ICreateBankRuleDTO, IEditBankRuleDTO } from './types';
|
||||
import { PublicRoute } from '../Auth/Jwt.guard';
|
||||
import { BankRule } from './models/BankRule';
|
||||
|
||||
@Controller('banking/rules')
|
||||
@PublicRoute()
|
||||
export class BankRulesController {
|
||||
constructor(private readonly bankRulesApplication: BankRulesApplication) {}
|
||||
|
||||
@Post()
|
||||
async createBankRule(
|
||||
@Body() createRuleDTO: ICreateBankRuleDTO,
|
||||
): Promise<BankRule> {
|
||||
return this.bankRulesApplication.createBankRule(createRuleDTO);
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
async editBankRule(
|
||||
@Param('id') ruleId: number,
|
||||
@Body() editRuleDTO: IEditBankRuleDTO,
|
||||
): Promise<void> {
|
||||
return this.bankRulesApplication.editBankRule(ruleId, editRuleDTO);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async deleteBankRule(@Param('id') ruleId: number): Promise<void> {
|
||||
return this.bankRulesApplication.deleteBankRule(ruleId);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async getBankRule(@Param('id') ruleId: number): Promise<any> {
|
||||
return this.bankRulesApplication.getBankRule(ruleId);
|
||||
}
|
||||
|
||||
@Get()
|
||||
async getBankRules(): Promise<any> {
|
||||
return this.bankRulesApplication.getBankRules();
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { CreateBankRuleService } from './commands/CreateBankRule.service';
|
||||
import { DeleteBankRuleService } from './commands/DeleteBankRule.service';
|
||||
import { EditBankRuleService } from './commands/EditBankRule.service';
|
||||
import { GetBankRuleService } from './queries/GetBankRule.service';
|
||||
import { GetBankRulesService } from './queries/GetBankRules.service';
|
||||
import { ICreateBankRuleDTO, IEditBankRuleDTO } from './types';
|
||||
import { BankRule } from './models/BankRule';
|
||||
|
||||
@Injectable()
|
||||
export class BankRulesApplication {
|
||||
constructor(
|
||||
private readonly createBankRuleService: CreateBankRuleService,
|
||||
private readonly editBankRuleService: EditBankRuleService,
|
||||
private readonly deleteBankRuleService: DeleteBankRuleService,
|
||||
private readonly getBankRuleService: GetBankRuleService,
|
||||
private readonly getBankRulesService: GetBankRulesService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Creates new bank rule.
|
||||
* @param {ICreateBankRuleDTO} createRuleDTO - Bank rule data.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public createBankRule(
|
||||
createRuleDTO: ICreateBankRuleDTO,
|
||||
): Promise<BankRule> {
|
||||
return this.createBankRuleService.createBankRule(createRuleDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits the given bank rule.
|
||||
* @param {number} ruleId - Bank rule identifier.
|
||||
* @param {IEditBankRuleDTO} editRuleDTO - Bank rule data.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public editBankRule(
|
||||
ruleId: number,
|
||||
editRuleDTO: IEditBankRuleDTO,
|
||||
): Promise<void> {
|
||||
return this.editBankRuleService.editBankRule(ruleId, editRuleDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the given bank rule.
|
||||
* @param {number} ruleId - Bank rule identifier.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public deleteBankRule(ruleId: number): Promise<void> {
|
||||
return this.deleteBankRuleService.deleteBankRule(ruleId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the given bank rule.
|
||||
* @param {number} ruleId - Bank rule identifier.
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
public getBankRule(ruleId: number): Promise<any> {
|
||||
return this.getBankRuleService.getBankRule(ruleId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the bank rules of the given account.
|
||||
* @param {number} accountId - Bank account identifier.
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
public getBankRules(): Promise<any> {
|
||||
return this.getBankRulesService.getBankRules();
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
import { Knex } from 'knex';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import {
|
||||
IBankRuleEventCreatedPayload,
|
||||
IBankRuleEventCreatingPayload,
|
||||
ICreateBankRuleDTO,
|
||||
} from '../types';
|
||||
import { UnitOfWork } from '../../Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { events } from '@/common/events/events';
|
||||
import { BankRule } from '../models/BankRule';
|
||||
|
||||
@Injectable()
|
||||
export class CreateBankRuleService {
|
||||
constructor(
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
|
||||
@Inject(BankRule.name) private readonly bankRuleModel: typeof BankRule,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Transforms the DTO to model.
|
||||
* @param {ICreateBankRuleDTO} createDTO
|
||||
*/
|
||||
private transformDTO(createDTO: ICreateBankRuleDTO) {
|
||||
return {
|
||||
...createDTO,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new bank rule.
|
||||
* @param {ICreateBankRuleDTO} createRuleDTO
|
||||
* @returns {Promise<BankRule>}
|
||||
*/
|
||||
public async createBankRule(
|
||||
createRuleDTO: ICreateBankRuleDTO,
|
||||
): Promise<BankRule> {
|
||||
const transformDTO = this.transformDTO(createRuleDTO);
|
||||
|
||||
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
|
||||
// Triggers `onBankRuleCreating` event.
|
||||
await this.eventPublisher.emitAsync(events.bankRules.onCreating, {
|
||||
createRuleDTO,
|
||||
trx,
|
||||
} as IBankRuleEventCreatingPayload);
|
||||
|
||||
const bankRule = await this.bankRuleModel.query(trx).upsertGraphAndFetch({
|
||||
...transformDTO,
|
||||
});
|
||||
// Triggers `onBankRuleCreated` event.
|
||||
await this.eventPublisher.emitAsync(events.bankRules.onCreated, {
|
||||
createRuleDTO,
|
||||
bankRule,
|
||||
trx,
|
||||
} as IBankRuleEventCreatedPayload);
|
||||
|
||||
return bankRule;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import {
|
||||
IBankRuleEventEditedPayload,
|
||||
IBankRuleEventEditingPayload,
|
||||
IEditBankRuleDTO,
|
||||
} from '../types';
|
||||
import { BankRule } from '../models/BankRule';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { events } from '@/common/events/events';
|
||||
|
||||
@Injectable()
|
||||
export class EditBankRuleService {
|
||||
constructor(
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
|
||||
@Inject(BankRule.name) private bankRuleModel: typeof BankRule,
|
||||
) {}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param createDTO
|
||||
* @returns
|
||||
*/
|
||||
private transformDTO(createDTO: IEditBankRuleDTO) {
|
||||
return {
|
||||
...createDTO,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits the given bank rule.
|
||||
* @param {number} ruleId -
|
||||
* @param {IEditBankRuleDTO} editBankDTO
|
||||
*/
|
||||
public async editBankRule(
|
||||
ruleId: number,
|
||||
editRuleDTO: IEditBankRuleDTO
|
||||
) {
|
||||
const oldBankRule = await this.bankRuleModel.query()
|
||||
.findById(ruleId)
|
||||
.withGraphFetched('conditions')
|
||||
.throwIfNotFound();
|
||||
|
||||
const tranformDTO = this.transformDTO(editRuleDTO);
|
||||
|
||||
return this.uow.withTransaction(async (trx) => {
|
||||
// Triggers `onBankRuleEditing` event.
|
||||
await this.eventPublisher.emitAsync(events.bankRules.onEditing, {
|
||||
oldBankRule,
|
||||
ruleId,
|
||||
editRuleDTO,
|
||||
trx,
|
||||
} as IBankRuleEventEditingPayload);
|
||||
|
||||
// Updates the given bank rule.
|
||||
const bankRule = await this.bankRuleModel.query(trx).upsertGraphAndFetch({
|
||||
...tranformDTO,
|
||||
id: ruleId,
|
||||
});
|
||||
// Triggers `onBankRuleEdited` event.
|
||||
await this.eventPublisher.emitAsync(events.bankRules.onEdited, {
|
||||
oldBankRule,
|
||||
bankRule,
|
||||
editRuleDTO,
|
||||
trx,
|
||||
} as IBankRuleEventEditedPayload);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
import { BaseModel } from '@/models/Model';
|
||||
import { Model } from 'objection';
|
||||
import { BankRuleCondition } from './BankRuleCondition';
|
||||
import { BankRuleAssignCategory, BankRuleConditionType } from '../types';
|
||||
|
||||
export class BankRule extends BaseModel {
|
||||
public id!: number;
|
||||
public name!: string;
|
||||
public order!: number;
|
||||
public applyIfAccountId!: number;
|
||||
public applyIfTransactionType!: string;
|
||||
public assignCategory!: BankRuleAssignCategory;
|
||||
public assignAccountId!: number;
|
||||
public assignPayee!: string;
|
||||
public assignMemo!: string;
|
||||
public conditionsType!: BankRuleConditionType;
|
||||
|
||||
conditions!: BankRuleCondition[];
|
||||
|
||||
/**
|
||||
* 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('./BankRuleCondition');
|
||||
const { Account } = require('../../Accounts/models/Account.model');
|
||||
|
||||
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,
|
||||
join: {
|
||||
from: 'bank_rules.assignAccountId',
|
||||
to: 'accounts.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { GetBankRuleTransformer } from './GetBankRuleTransformer';
|
||||
import { TransformerInjectable } from '../../Transformer/TransformerInjectable.service';
|
||||
import { BankRule } from '../models/BankRule';
|
||||
import { GetBankRulesTransformer } from './GetBankRulesTransformer';
|
||||
|
||||
@Injectable()
|
||||
export class GetBankRuleService {
|
||||
constructor(
|
||||
@Inject(BankRule.name) private bankRuleModel: typeof BankRule,
|
||||
private transformer: TransformerInjectable,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieves the bank rule.
|
||||
* @param {number} ruleId
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
async getBankRule(ruleId: number): Promise<any> {
|
||||
const bankRule = await this.bankRuleModel
|
||||
.query()
|
||||
.findById(ruleId)
|
||||
.withGraphFetched('conditions')
|
||||
.withGraphFetched('assignAccount');
|
||||
|
||||
return this.transformer.transform(
|
||||
bankRule,
|
||||
new GetBankRulesTransformer()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { GetBankRulesTransformer } from './GetBankRulesTransformer';
|
||||
import { BankRule } from '../models/BankRule';
|
||||
import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
|
||||
|
||||
@Injectable()
|
||||
export class GetBankRulesService {
|
||||
constructor(
|
||||
private transformer: TransformerInjectable,
|
||||
|
||||
@Inject(BankRule.name)
|
||||
private bankRuleModel: typeof BankRule,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieves the bank rules of the given account.
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
public async getBankRules(): Promise<any> {
|
||||
const bankRule = await this.bankRuleModel
|
||||
.query()
|
||||
.withGraphFetched('conditions')
|
||||
.withGraphFetched('assignAccount');
|
||||
|
||||
return this.transformer.transform(
|
||||
bankRule,
|
||||
new GetBankRulesTransformer()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
import { Knex } from 'knex';
|
||||
import { BankRule } from './models/BankRule';
|
||||
|
||||
export enum BankRuleConditionField {
|
||||
Amount = 'amount',
|
||||
Description = 'description',
|
||||
Payee = 'payee',
|
||||
}
|
||||
|
||||
export enum BankRuleConditionComparator {
|
||||
Contains = 'contains',
|
||||
Equals = 'equals',
|
||||
Equal = 'equal',
|
||||
NotContain = 'not_contains',
|
||||
Bigger = 'bigger',
|
||||
BiggerOrEqual = 'bigger_or_equal',
|
||||
Smaller = 'smaller',
|
||||
SmallerOrEqual = 'smaller_or_equal',
|
||||
}
|
||||
|
||||
export interface IBankRuleCondition {
|
||||
id?: number;
|
||||
field: BankRuleConditionField;
|
||||
comparator: BankRuleConditionComparator;
|
||||
value: string;
|
||||
}
|
||||
|
||||
export enum BankRuleConditionType {
|
||||
Or = 'or',
|
||||
And = 'and',
|
||||
}
|
||||
|
||||
export enum BankRuleApplyIfTransactionType {
|
||||
Deposit = 'deposit',
|
||||
Withdrawal = 'withdrawal',
|
||||
}
|
||||
|
||||
// export interface BankRule {
|
||||
// id?: number;
|
||||
// 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 type BankRuleComparator =
|
||||
| 'contains'
|
||||
| 'equals'
|
||||
| 'not_contains'
|
||||
| 'equal'
|
||||
| 'bigger'
|
||||
| 'bigger_or_equal'
|
||||
| 'smaller'
|
||||
| 'smaller_or_equal';
|
||||
|
||||
export interface IBankRuleConditionDTO {
|
||||
id?: number;
|
||||
field: string;
|
||||
comparator: BankRuleComparator;
|
||||
value: string;
|
||||
}
|
||||
|
||||
export interface IBankRuleCommonDTO {
|
||||
name: string;
|
||||
order?: number;
|
||||
applyIfAccountId: number;
|
||||
applyIfTransactionType: string;
|
||||
|
||||
conditions: IBankRuleConditionDTO[];
|
||||
|
||||
assignCategory: BankRuleAssignCategory;
|
||||
assignAccountId: number;
|
||||
assignPayee?: string;
|
||||
assignMemo?: string;
|
||||
}
|
||||
|
||||
export interface ICreateBankRuleDTO extends IBankRuleCommonDTO {}
|
||||
export interface IEditBankRuleDTO extends IBankRuleCommonDTO {}
|
||||
|
||||
export interface IBankRuleEventCreatingPayload {
|
||||
createRuleDTO: ICreateBankRuleDTO;
|
||||
trx?: Knex.Transaction;
|
||||
}
|
||||
export interface IBankRuleEventCreatedPayload {
|
||||
createRuleDTO: ICreateBankRuleDTO;
|
||||
bankRule: BankRule;
|
||||
trx?: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IBankRuleEventEditingPayload {
|
||||
ruleId: number;
|
||||
oldBankRule: any;
|
||||
editRuleDTO: IEditBankRuleDTO;
|
||||
trx?: Knex.Transaction;
|
||||
}
|
||||
export interface IBankRuleEventEditedPayload {
|
||||
oldBankRule: BankRule;
|
||||
bankRule: BankRule;
|
||||
editRuleDTO: IEditBankRuleDTO;
|
||||
trx?: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IBankRuleEventDeletingPayload {
|
||||
oldBankRule: any;
|
||||
trx?: Knex.Transaction;
|
||||
}
|
||||
export interface IBankRuleEventDeletedPayload {
|
||||
ruleId: number;
|
||||
trx?: Knex.Transaction;
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
import { Controller, Param, Post } from '@nestjs/common';
|
||||
import { BankAccountsApplication } from './BankAccountsApplication.service';
|
||||
|
||||
@Controller('banking/accounts')
|
||||
export class BankAccountsController {
|
||||
constructor(private bankAccountsApplication: BankAccountsApplication) {}
|
||||
|
||||
@Post(':id/disconnect')
|
||||
async disconnectBankAccount(@Param('id') bankAccountId: number) {
|
||||
return this.bankAccountsApplication.disconnectBankAccount(bankAccountId);
|
||||
}
|
||||
|
||||
@Post(':id/refresh')
|
||||
async refreshBankAccount(@Param('id') bankAccountId: number) {
|
||||
return this.bankAccountsApplication.refreshBankAccount(bankAccountId);
|
||||
}
|
||||
|
||||
@Post(':id/pause')
|
||||
async pauseBankAccount(@Param('id') bankAccountId: number) {
|
||||
return this.bankAccountsApplication.pauseBankAccount(bankAccountId);
|
||||
}
|
||||
|
||||
@Post(':id/resume')
|
||||
async resumeBankAccount(@Param('id') bankAccountId: number) {
|
||||
return this.bankAccountsApplication.resumeBankAccount(bankAccountId);
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { BankAccountsApplication } from './BankAccountsApplication.service';
|
||||
import { DisconnectBankAccountService } from './commands/DisconnectBankAccount.service';
|
||||
import { RefreshBankAccountService } from './commands/RefreshBankAccount.service';
|
||||
import { ResumeBankAccountFeedsService } from './commands/ResumeBankAccountFeeds.service';
|
||||
import { PauseBankAccountFeeds } from './commands/PauseBankAccountFeeds.service';
|
||||
import { DeleteUncategorizedTransactionsOnAccountDeleting } from './subscribers/DeleteUncategorizedTransactionsOnAccountDeleting';
|
||||
import { DisconnectPlaidItemOnAccountDeleted } from './subscribers/DisconnectPlaidItemOnAccountDeleted';
|
||||
import { BankAccountsController } from './BankAccounts.controller';
|
||||
import { BankingPlaidModule } from '../BankingPlaid/BankingPlaid.module';
|
||||
import { PlaidModule } from '../Plaid/Plaid.module';
|
||||
import { BankRulesModule } from '../BankRules/BankRules.module';
|
||||
|
||||
@Module({
|
||||
imports: [BankingPlaidModule, PlaidModule, BankRulesModule],
|
||||
providers: [
|
||||
DisconnectBankAccountService,
|
||||
RefreshBankAccountService,
|
||||
ResumeBankAccountFeedsService,
|
||||
PauseBankAccountFeeds,
|
||||
// DeleteUncategorizedTransactionsOnAccountDeleting,
|
||||
DisconnectPlaidItemOnAccountDeleted,
|
||||
BankAccountsApplication
|
||||
],
|
||||
exports: [BankAccountsApplication],
|
||||
controllers: [BankAccountsController],
|
||||
})
|
||||
export class BankAccountsModule {}
|
||||
@@ -1,57 +0,0 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { DisconnectBankAccountService } from './commands/DisconnectBankAccount.service';
|
||||
import { RefreshBankAccountService } from './commands/RefreshBankAccount.service';
|
||||
import { ResumeBankAccountFeedsService } from './commands/ResumeBankAccountFeeds.service';
|
||||
import { PauseBankAccountFeeds } from './commands/PauseBankAccountFeeds.service';
|
||||
|
||||
@Injectable()
|
||||
export class BankAccountsApplication {
|
||||
constructor(
|
||||
private disconnectBankAccountService: DisconnectBankAccountService,
|
||||
private readonly refreshBankAccountService: RefreshBankAccountService,
|
||||
private readonly resumeBankAccountFeedsService: ResumeBankAccountFeedsService,
|
||||
private readonly pauseBankAccountFeedsService: PauseBankAccountFeeds,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Disconnects the given bank account.
|
||||
* @param {number} bankAccountId - Bank account identifier.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async disconnectBankAccount(bankAccountId: number) {
|
||||
return this.disconnectBankAccountService.disconnectBankAccount(
|
||||
bankAccountId,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the bank transactions of the given bank account.
|
||||
* @param {number} bankAccountId - Bank account identifier.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async refreshBankAccount(bankAccountId: number) {
|
||||
return this.refreshBankAccountService.refreshBankAccount(bankAccountId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pauses the feeds sync of the given bank account.
|
||||
* @param {number} bankAccountId - Bank account identifier.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async pauseBankAccount(bankAccountId: number) {
|
||||
return this.pauseBankAccountFeedsService.pauseBankAccountFeeds(
|
||||
bankAccountId,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resumes the feeds sync of the given bank account.
|
||||
* @param {number} bankAccountId - Bank account identifier.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async resumeBankAccount(bankAccountId: number) {
|
||||
return this.resumeBankAccountFeedsService.resumeBankAccountFeeds(
|
||||
bankAccountId,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,117 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Account } from '@/modules/Accounts/models/Account.model';
|
||||
import { UncategorizedBankTransaction } from '@/modules/BankingTransactions/models/UncategorizedBankTransaction';
|
||||
import { BaseModel } from '@/models/Model';
|
||||
|
||||
@Injectable()
|
||||
export class GetBankAccountSummary {
|
||||
constructor(
|
||||
@Inject(Account.name)
|
||||
private readonly accountModel: typeof Account,
|
||||
|
||||
@Inject(UncategorizedBankTransaction.name)
|
||||
private readonly uncategorizedBankTransactionModel: typeof UncategorizedBankTransaction,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieves the bank account meta summary
|
||||
* @param {number} bankAccountId - The bank account id.
|
||||
* @returns {Promise<IBankAccountSummary>}
|
||||
*/
|
||||
public async getBankAccountSummary(bankAccountId: number) {
|
||||
const bankAccount = await this.accountModel
|
||||
.query()
|
||||
.findById(bankAccountId)
|
||||
.throwIfNotFound();
|
||||
|
||||
const commonQuery = (q) => {
|
||||
// Include just the given account.
|
||||
q.where('accountId', bankAccountId);
|
||||
|
||||
// Only the not excluded.
|
||||
q.modify('notExcluded');
|
||||
|
||||
// Only the not categorized.
|
||||
q.modify('notCategorized');
|
||||
};
|
||||
|
||||
interface UncategorizedTransactionsCount {
|
||||
total: number;
|
||||
}
|
||||
|
||||
// Retrieves the uncategorized transactions count of the given bank account.
|
||||
const uncategorizedTranasctionsCount =
|
||||
await this.uncategorizedBankTransactionModel.query().onBuild((q) => {
|
||||
commonQuery(q);
|
||||
|
||||
// Only the not matched bank transactions.
|
||||
q.withGraphJoined('matchedBankTransactions');
|
||||
q.whereNull('matchedBankTransactions.id');
|
||||
|
||||
// Exclude the pending transactions.
|
||||
q.modify('notPending');
|
||||
|
||||
// Count the results.
|
||||
q.count('uncategorized_cashflow_transactions.id as total');
|
||||
q.first();
|
||||
});
|
||||
|
||||
// Retrives the recognized transactions count.
|
||||
const recognizedTransactionsCount =
|
||||
await this.uncategorizedBankTransactionModel.query().onBuild((q) => {
|
||||
commonQuery(q);
|
||||
|
||||
q.withGraphJoined('recognizedTransaction');
|
||||
q.whereNotNull('recognizedTransaction.id');
|
||||
|
||||
// Exclude the pending transactions.
|
||||
q.modify('notPending');
|
||||
|
||||
// Count the results.
|
||||
q.count('uncategorized_cashflow_transactions.id as total');
|
||||
q.first();
|
||||
});
|
||||
// Retrieves excluded transactions count.
|
||||
const excludedTransactionsCount =
|
||||
await this.uncategorizedBankTransactionModel.query().onBuild((q) => {
|
||||
q.where('accountId', bankAccountId);
|
||||
q.modify('excluded');
|
||||
|
||||
// Exclude the pending transactions.
|
||||
q.modify('notPending');
|
||||
|
||||
// Count the results.
|
||||
q.count('uncategorized_cashflow_transactions.id as total');
|
||||
q.first();
|
||||
});
|
||||
|
||||
// Retrieves the pending transactions count.
|
||||
const pendingTransactionsCount =
|
||||
await this.uncategorizedBankTransactionModel.query().onBuild((q) => {
|
||||
q.where('accountId', bankAccountId);
|
||||
q.modify('pending');
|
||||
|
||||
// Count the results.
|
||||
q.count('uncategorized_cashflow_transactions.id as total');
|
||||
q.first();
|
||||
});
|
||||
|
||||
const totalUncategorizedTransactions =
|
||||
// @ts-ignore
|
||||
uncategorizedTranasctionsCount?.total || 0;
|
||||
// @ts-ignore
|
||||
const totalRecognizedTransactions = recognizedTransactionsCount?.total || 0;
|
||||
// @ts-ignore
|
||||
const totalExcludedTransactions = excludedTransactionsCount?.total || 0;
|
||||
// @ts-ignore
|
||||
const totalPendingTransactions = pendingTransactionsCount?.total || 0;
|
||||
|
||||
return {
|
||||
name: bankAccount.name,
|
||||
totalUncategorizedTransactions,
|
||||
totalRecognizedTransactions,
|
||||
totalExcludedTransactions,
|
||||
totalPendingTransactions,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
import { events } from '@/common/events/events';
|
||||
import { IAccountEventDeletePayload } from '@/interfaces/Account';
|
||||
import { RevertRecognizedTransactionsService } from '@/modules/BankingTranasctionsRegonize/commands/RevertRecognizedTransactions.service';
|
||||
import { UncategorizedBankTransaction } from '@/modules/BankingTransactions/models/UncategorizedBankTransaction';
|
||||
import { DeleteBankRulesService } from '@/modules/BankRules/commands/DeleteBankRules.service';
|
||||
import { BankRule } from '@/modules/BankRules/models/BankRule';
|
||||
|
||||
@Injectable()
|
||||
export class DeleteUncategorizedTransactionsOnAccountDeleting {
|
||||
constructor(
|
||||
private readonly deleteBankRules: DeleteBankRulesService,
|
||||
private readonly revertRecognizedTransactins: RevertRecognizedTransactionsService,
|
||||
|
||||
@Inject(BankRule.name) private bankRuleModel: typeof BankRule,
|
||||
@Inject(UncategorizedBankTransaction.name)
|
||||
private uncategorizedCashflowTransactionModel: typeof UncategorizedBankTransaction,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Handles revert the recognized transactions and delete all the bank rules
|
||||
* associated to the deleted bank account.
|
||||
* @param {IAccountEventDeletePayload}
|
||||
*/
|
||||
@OnEvent(events.accounts.onDelete)
|
||||
public async handleDeleteBankRulesOnAccountDeleting({
|
||||
oldAccount,
|
||||
trx,
|
||||
}: IAccountEventDeletePayload) {
|
||||
const foundAssociatedRules = await this.bankRuleModel.query(trx).where(
|
||||
'applyIfAccountId',
|
||||
oldAccount.id,
|
||||
);
|
||||
const foundAssociatedRulesIds = foundAssociatedRules.map((rule) => rule.id);
|
||||
|
||||
// Revert the recognized transactions of the given bank rules.
|
||||
await this.revertRecognizedTransactins.revertRecognizedTransactions(
|
||||
foundAssociatedRulesIds,
|
||||
null,
|
||||
trx,
|
||||
);
|
||||
// Delete the associated uncategorized transactions.
|
||||
await this.uncategorizedCashflowTransactionModel
|
||||
.query(trx)
|
||||
.where('accountId', oldAccount.id)
|
||||
.delete();
|
||||
|
||||
// Delete the given bank rules.
|
||||
await this.deleteBankRules.deleteBankRules(
|
||||
foundAssociatedRulesIds,
|
||||
trx,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { IAccountEventDeletedPayload } from '@/interfaces/Account';
|
||||
import { events } from '@/common/events/events';
|
||||
import { PlaidItem } from '@/modules/BankingPlaid/models/PlaidItem';
|
||||
import { Account } from '@/modules/Accounts/models/Account.model';
|
||||
import { PlaidApi } from 'plaid';
|
||||
import { PLAID_CLIENT } from '@/modules/Plaid/Plaid.module';
|
||||
|
||||
@Injectable()
|
||||
export class DisconnectPlaidItemOnAccountDeleted {
|
||||
constructor(
|
||||
@Inject(PLAID_CLIENT) private plaidClient: PlaidApi,
|
||||
@Inject(PlaidItem.name) private plaidItemModel: typeof PlaidItem,
|
||||
@Inject(Account.name) private accountModel: typeof Account,
|
||||
) {}
|
||||
/**
|
||||
* Deletes Plaid item from the system and Plaid once the account deleted.
|
||||
* @param {IAccountEventDeletedPayload} payload
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
@OnEvent(events.accounts.onDeleted)
|
||||
public async handleDisconnectPlaidItemOnAccountDelete({
|
||||
tenantId,
|
||||
oldAccount,
|
||||
trx,
|
||||
}: IAccountEventDeletedPayload) {
|
||||
// Can't continue if the deleted account is not linked to Plaid item.
|
||||
if (!oldAccount.plaidItemId) return;
|
||||
|
||||
// Retrieves the Plaid item that associated to the deleted account.
|
||||
const oldPlaidItem = await this.plaidItemModel
|
||||
.query(trx)
|
||||
.findOne('plaidItemId', oldAccount.plaidItemId);
|
||||
// Unlink the Plaid item from all account before deleting it.
|
||||
await this.accountModel
|
||||
.query(trx)
|
||||
.where('plaidItemId', oldAccount.plaidItemId)
|
||||
.patch({
|
||||
plaidAccountId: null,
|
||||
plaidItemId: null,
|
||||
});
|
||||
// Remove the Plaid item from the system.
|
||||
await this.plaidItemModel
|
||||
.query(trx)
|
||||
.findOne('plaidItemId', oldAccount.plaidItemId)
|
||||
.delete();
|
||||
|
||||
// Remove Plaid item once the transaction resolve.
|
||||
if (oldPlaidItem) {
|
||||
// Remove the Plaid item.
|
||||
await this.plaidClient.itemRemove({
|
||||
access_token: oldPlaidItem.plaidAccessToken,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
import { Knex } from 'knex';
|
||||
|
||||
export interface IBankAccountDisconnectingEventPayload {
|
||||
bankAccountId: number;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IBankAccountDisconnectedEventPayload {
|
||||
bankAccountId: number;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export const ERRORS = {
|
||||
BANK_ACCOUNT_NOT_CONNECTED: 'BANK_ACCOUNT_NOT_CONNECTED',
|
||||
BANK_ACCOUNT_FEEDS_ALREADY_PAUSED: 'BANK_ACCOUNT_FEEDS_ALREADY_PAUSED',
|
||||
BANK_ACCOUNT_FEEDS_ALREADY_RESUMED: 'BANK_ACCOUNT_FEEDS_ALREADY_RESUMED',
|
||||
};
|
||||
@@ -1,110 +0,0 @@
|
||||
import { castArray } from 'lodash';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Knex } from 'knex';
|
||||
import {
|
||||
ICashflowTransactionCategorizedPayload,
|
||||
ICashflowTransactionUncategorizingPayload,
|
||||
ICategorizeCashflowTransactioDTO,
|
||||
} from '../types/BankingCategorize.types';
|
||||
import {
|
||||
transformCategorizeTransToCashflow,
|
||||
validateUncategorizedTransactionsNotExcluded,
|
||||
} from '../../BankingTransactions/utils';
|
||||
import { CommandBankTransactionValidator } from '../../BankingTransactions/commands/CommandCasflowValidator.service';
|
||||
import { CreateBankTransactionService } from '../../BankingTransactions/commands/CreateBankTransaction.service';
|
||||
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { UncategorizedBankTransaction } from '@/modules/BankingTransactions/models/UncategorizedBankTransaction';
|
||||
import { events } from '@/common/events/events';
|
||||
|
||||
@Injectable()
|
||||
export class CategorizeCashflowTransaction {
|
||||
constructor(
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly commandValidators: CommandBankTransactionValidator,
|
||||
private readonly createBankTransaction: CreateBankTransactionService,
|
||||
|
||||
@Inject(UncategorizedBankTransaction.name)
|
||||
private readonly uncategorizedBankTransactionModel: typeof UncategorizedBankTransaction,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Categorize the given cashflow transaction.
|
||||
* @param {ICategorizeCashflowTransactioDTO} categorizeDTO - Categorize DTO.
|
||||
*/
|
||||
public async categorize(
|
||||
uncategorizedTransactionId: number | Array<number>,
|
||||
categorizeDTO: ICategorizeCashflowTransactioDTO,
|
||||
) {
|
||||
const uncategorizedTransactionIds = castArray(uncategorizedTransactionId);
|
||||
|
||||
// Retrieves the uncategorized transaction or throw an error.
|
||||
const oldUncategorizedTransactions =
|
||||
await this.uncategorizedBankTransactionModel.query()
|
||||
.whereIn('id', uncategorizedTransactionIds)
|
||||
.throwIfNotFound();
|
||||
|
||||
// Validate cannot categorize excluded transaction.
|
||||
validateUncategorizedTransactionsNotExcluded(oldUncategorizedTransactions);
|
||||
|
||||
// Validates the transaction shouldn't be categorized before.
|
||||
this.commandValidators.validateTransactionsShouldNotCategorized(
|
||||
oldUncategorizedTransactions,
|
||||
);
|
||||
// Validate the uncateogirzed transaction if it's deposit the transaction direction
|
||||
// should `IN` and the same thing if it's withdrawal the direction should be OUT.
|
||||
this.commandValidators.validateUncategorizeTransactionType(
|
||||
oldUncategorizedTransactions,
|
||||
categorizeDTO.transactionType,
|
||||
);
|
||||
// Edits the cashflow transaction under UOW env.
|
||||
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
|
||||
// Triggers `onTransactionCategorizing` event.
|
||||
await this.eventPublisher.emitAsync(
|
||||
events.cashflow.onTransactionCategorizing,
|
||||
{
|
||||
// tenantId,
|
||||
oldUncategorizedTransactions,
|
||||
trx,
|
||||
} as ICashflowTransactionUncategorizingPayload,
|
||||
);
|
||||
// Transformes the categorize DTO to the cashflow transaction.
|
||||
const cashflowTransactionDTO = transformCategorizeTransToCashflow(
|
||||
oldUncategorizedTransactions,
|
||||
categorizeDTO,
|
||||
);
|
||||
// Creates a new cashflow transaction.
|
||||
const cashflowTransaction =
|
||||
await this.createBankTransaction.newCashflowTransaction(
|
||||
cashflowTransactionDTO,
|
||||
);
|
||||
|
||||
// Updates the uncategorized transaction as categorized.
|
||||
await this.uncategorizedBankTransactionModel.query(trx)
|
||||
.whereIn('id', uncategorizedTransactionIds)
|
||||
.patch({
|
||||
categorized: true,
|
||||
categorizeRefType: 'CashflowTransaction',
|
||||
categorizeRefId: cashflowTransaction.id,
|
||||
});
|
||||
// Fetch the new updated uncategorized transactions.
|
||||
const uncategorizedTransactions =
|
||||
await this.uncategorizedBankTransactionModel.query(trx).whereIn(
|
||||
'id',
|
||||
uncategorizedTransactionIds,
|
||||
);
|
||||
// Triggers `onCashflowTransactionCategorized` event.
|
||||
await this.eventPublisher.emitAsync(
|
||||
events.cashflow.onTransactionCategorized,
|
||||
{
|
||||
cashflowTransaction,
|
||||
uncategorizedTransactions,
|
||||
oldUncategorizedTransactions,
|
||||
categorizeDTO,
|
||||
trx,
|
||||
} as ICashflowTransactionCategorizedPayload,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
import { Knex } from 'knex';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { BankTransaction } from '@/modules/BankingTransactions/models/BankTransaction';
|
||||
import { CreateExpense } from '@/modules/Expenses/commands/CreateExpense.service';
|
||||
import { Inject } from '@nestjs/common';
|
||||
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { events } from '@/common/events/events';
|
||||
import {
|
||||
ICashflowTransactionCategorizedPayload,
|
||||
ICategorizeCashflowTransactioDTO,
|
||||
} from '../types/BankingCategorize.types';
|
||||
|
||||
@Injectable()
|
||||
export class CategorizeTransactionAsExpense {
|
||||
constructor(
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
private readonly createExpenseService: CreateExpense,
|
||||
|
||||
@Inject(BankTransaction.name)
|
||||
private readonly bankTransactionModel: typeof BankTransaction,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Categorize the transaction as expense transaction.
|
||||
* @param {number} cashflowTransactionId
|
||||
* @param {CategorizeTransactionAsExpenseDTO} transactionDTO
|
||||
*/
|
||||
public async categorize(
|
||||
cashflowTransactionId: number,
|
||||
transactionDTO: ICategorizeCashflowTransactioDTO,
|
||||
) {
|
||||
const transaction = await this.bankTransactionModel
|
||||
.query()
|
||||
.findById(cashflowTransactionId)
|
||||
.throwIfNotFound();
|
||||
|
||||
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
|
||||
// Triggers `onTransactionUncategorizing` event.
|
||||
await this.eventPublisher.emitAsync(
|
||||
events.cashflow.onTransactionCategorizingAsExpense,
|
||||
{
|
||||
trx,
|
||||
} as ICashflowTransactionCategorizedPayload,
|
||||
);
|
||||
// Creates a new expense transaction.
|
||||
// TODO: the DTO is not complete, we need to add the missing properties.
|
||||
// @ts-ignore
|
||||
const expenseTransaction = await this.createExpenseService.newExpense({
|
||||
// ...transactionDTO,
|
||||
// publishedAt: transaction.publishedAt,
|
||||
});
|
||||
|
||||
// Updates the item on the storage and fetches the updated once.
|
||||
const cashflowTransaction = await this.bankTransactionModel
|
||||
.query(trx)
|
||||
.patchAndFetchById(cashflowTransactionId, {
|
||||
categorizeRefType: 'Expense',
|
||||
categorizeRefId: expenseTransaction.id,
|
||||
uncategorized: true,
|
||||
});
|
||||
// Triggers `onTransactionUncategorized` event.
|
||||
await this.eventPublisher.emitAsync(
|
||||
events.cashflow.onTransactionCategorizedAsExpense,
|
||||
{
|
||||
cashflowTransaction,
|
||||
trx,
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
import { Knex } from 'knex';
|
||||
import {
|
||||
CreateUncategorizedTransactionDTO,
|
||||
IUncategorizedTransactionCreatedEventPayload,
|
||||
IUncategorizedTransactionCreatingEventPayload,
|
||||
} from '../types/BankingCategorize.types';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { UncategorizedBankTransaction } from '../../BankingTransactions/models/UncategorizedBankTransaction';
|
||||
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { events } from '@/common/events/events';
|
||||
|
||||
@Injectable()
|
||||
export class CreateUncategorizedTransactionService {
|
||||
constructor(
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
|
||||
@Inject(UncategorizedBankTransaction.name)
|
||||
private readonly uncategorizedBankTransaction: typeof UncategorizedBankTransaction,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Creates an uncategorized cashflow transaction.
|
||||
* @param {CreateUncategorizedTransactionDTO} createDTO - Create uncategorized transaction DTO.
|
||||
* @param {Knex.Transaction} trx - Knex transaction.
|
||||
* @returns {Promise<UncategorizedBankTransaction>}
|
||||
*/
|
||||
public create(
|
||||
createUncategorizedTransactionDTO: CreateUncategorizedTransactionDTO,
|
||||
trx?: Knex.Transaction
|
||||
) {
|
||||
return this.uow.withTransaction(
|
||||
async (trx: Knex.Transaction) => {
|
||||
await this.eventPublisher.emitAsync(
|
||||
events.cashflow.onTransactionUncategorizedCreating,
|
||||
{
|
||||
createUncategorizedTransactionDTO,
|
||||
trx,
|
||||
} as IUncategorizedTransactionCreatingEventPayload
|
||||
);
|
||||
|
||||
const uncategorizedTransaction =
|
||||
await this.uncategorizedBankTransaction.query(trx).insertAndFetch({
|
||||
...createUncategorizedTransactionDTO,
|
||||
});
|
||||
|
||||
await this.eventPublisher.emitAsync(
|
||||
events.cashflow.onTransactionUncategorizedCreated,
|
||||
{
|
||||
uncategorizedTransaction,
|
||||
createUncategorizedTransactionDTO,
|
||||
trx,
|
||||
} as IUncategorizedTransactionCreatedEventPayload
|
||||
);
|
||||
return uncategorizedTransaction;
|
||||
},
|
||||
trx
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Knex } from 'knex';
|
||||
import * as yup from 'yup';
|
||||
import uniqid from 'uniqid';
|
||||
import { Importable } from '../../Import/Importable';
|
||||
import { CreateUncategorizedTransactionService } from './CreateUncategorizedTransaction.service';
|
||||
import { ImportableContext } from '../../Import/interfaces';
|
||||
import { BankTransactionsSampleData } from '../../BankingTransactions/constants';
|
||||
import { Account } from '@/modules/Accounts/models/Account.model';
|
||||
import { CreateUncategorizedTransactionDTO } from '../types/BankingCategorize.types';
|
||||
|
||||
@Injectable()
|
||||
export class UncategorizedTransactionsImportable extends Importable {
|
||||
constructor(
|
||||
private readonly createUncategorizedTransaction: CreateUncategorizedTransactionService,
|
||||
|
||||
@Inject(Account.name)
|
||||
private readonly accountModel: typeof Account,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Passing the sheet DTO to create uncategorized transaction.
|
||||
* @param {CreateUncategorizedTransactionDTO,} createDTO
|
||||
* @param {Knex.Transaction} trx
|
||||
*/
|
||||
public async importable(
|
||||
createDTO: CreateUncategorizedTransactionDTO,
|
||||
trx?: Knex.Transaction,
|
||||
) {
|
||||
return this.createUncategorizedTransaction.create(createDTO, trx);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transformes the DTO before validating and importing.
|
||||
* @param {CreateUncategorizedTransactionDTO} createDTO
|
||||
* @param {ImportableContext} context
|
||||
* @returns {CreateUncategorizedTransactionDTO}
|
||||
*/
|
||||
public transform(
|
||||
createDTO: CreateUncategorizedTransactionDTO,
|
||||
context?: ImportableContext,
|
||||
): CreateUncategorizedTransactionDTO {
|
||||
return {
|
||||
...createDTO,
|
||||
accountId: context.import.paramsParsed.accountId,
|
||||
batch: context.import.paramsParsed.batch,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Sample data used to download sample sheet.
|
||||
* @returns {Record<string, any>[]}
|
||||
*/
|
||||
public sampleData(): Record<string, any>[] {
|
||||
return BankTransactionsSampleData;
|
||||
}
|
||||
|
||||
// ------------------
|
||||
// # Params
|
||||
// ------------------
|
||||
/**
|
||||
* Params validation schema.
|
||||
* @returns {ValidationSchema[]}
|
||||
*/
|
||||
public paramsValidationSchema() {
|
||||
return yup.object().shape({
|
||||
accountId: yup.number().required(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the params existance asyncly.
|
||||
* @param {number} tenantId -
|
||||
* @param {Record<string, any>} params -
|
||||
*/
|
||||
public async validateParams(params: Record<string, any>): Promise<void> {
|
||||
if (params.accountId) {
|
||||
await this.accountModel
|
||||
.query()
|
||||
.findById(params.accountId)
|
||||
.throwIfNotFound({});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms the import params before storing them.
|
||||
* @param {Record<string, any>} parmas
|
||||
*/
|
||||
public transformParams(parmas: Record<string, any>) {
|
||||
const batch = uniqid();
|
||||
|
||||
return {
|
||||
...parmas,
|
||||
batch,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common';
|
||||
import { BankingMatchingApplication } from './BankingMatchingApplication';
|
||||
import { GetMatchedTransactionsFilter, IMatchTransactionDTO } from './types';
|
||||
|
||||
@Controller('banking/matching')
|
||||
export class BankingMatchingController {
|
||||
constructor(
|
||||
private readonly bankingMatchingApplication: BankingMatchingApplication
|
||||
) {}
|
||||
|
||||
@Get('matched/transactions')
|
||||
async getMatchedTransactions(
|
||||
@Query('uncategorizedTransactionIds') uncategorizedTransactionIds: number[],
|
||||
@Query() filter: GetMatchedTransactionsFilter
|
||||
) {
|
||||
return this.bankingMatchingApplication.getMatchedTransactions(
|
||||
uncategorizedTransactionIds,
|
||||
filter
|
||||
);
|
||||
}
|
||||
|
||||
@Post('/match/:uncategorizedTransactionId')
|
||||
async matchTransaction(
|
||||
@Param('uncategorizedTransactionId') uncategorizedTransactionId: number | number[],
|
||||
@Body() matchedTransactions: IMatchTransactionDTO[]
|
||||
) {
|
||||
return this.bankingMatchingApplication.matchTransaction(
|
||||
uncategorizedTransactionId,
|
||||
matchedTransactions
|
||||
);
|
||||
}
|
||||
|
||||
@Post('/unmatch/:uncategorizedTransactionId')
|
||||
async unmatchMatchedTransaction(
|
||||
@Param('uncategorizedTransactionId') uncategorizedTransactionId: number
|
||||
) {
|
||||
return this.bankingMatchingApplication.unmatchMatchedTransaction(
|
||||
uncategorizedTransactionId
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
import moment from 'moment';
|
||||
import * as R from 'ramda';
|
||||
import { isEmpty, sumBy } from 'lodash';
|
||||
import { ERRORS, MatchedTransactionPOJO } from './types';
|
||||
import { ServiceError } from '../Items/ServiceError';
|
||||
|
||||
export const sortClosestMatchTransactions = (
|
||||
amount: number,
|
||||
date: Date,
|
||||
matches: MatchedTransactionPOJO[]
|
||||
) => {
|
||||
return R.sortWith([
|
||||
// Sort by amount difference (closest to uncategorized transaction amount first)
|
||||
R.ascend((match: MatchedTransactionPOJO) =>
|
||||
Math.abs(match.amount - amount)
|
||||
),
|
||||
// Sort by date difference (closest to uncategorized transaction date first)
|
||||
R.ascend((match: MatchedTransactionPOJO) =>
|
||||
Math.abs(moment(match.date).diff(moment(date), 'days'))
|
||||
),
|
||||
])(matches);
|
||||
};
|
||||
|
||||
export const sumMatchTranasctions = (transactions: Array<any>) => {
|
||||
return transactions.reduce(
|
||||
(total, item) =>
|
||||
total +
|
||||
(item.transactionNormal === 'debit' ? 1 : -1) * parseFloat(item.amount),
|
||||
0
|
||||
);
|
||||
};
|
||||
|
||||
export const sumUncategorizedTransactions = (
|
||||
uncategorizedTransactions: Array<any>
|
||||
) => {
|
||||
return sumBy(uncategorizedTransactions, 'amount');
|
||||
};
|
||||
|
||||
export const validateUncategorizedTransactionsNotMatched = (
|
||||
uncategorizedTransactions: any
|
||||
) => {
|
||||
const matchedTransactions = uncategorizedTransactions.filter(
|
||||
(trans) => !isEmpty(trans.matchedBankTransactions)
|
||||
);
|
||||
//
|
||||
if (matchedTransactions.length > 0) {
|
||||
throw new ServiceError(ERRORS.TRANSACTION_ALREADY_MATCHED, '', {
|
||||
matchedTransactionsIds: matchedTransactions?.map((m) => m.id),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const validateUncategorizedTransactionsExcluded = (
|
||||
uncategorizedTransactions: any
|
||||
) => {
|
||||
const excludedTransactions = uncategorizedTransactions.filter(
|
||||
(trans) => trans.excluded
|
||||
);
|
||||
if (excludedTransactions.length > 0) {
|
||||
throw new ServiceError(ERRORS.CANNOT_MATCH_EXCLUDED_TRANSACTION, '', {
|
||||
excludedTransactionsIds: excludedTransactions.map((e) => e.id),
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -1,148 +0,0 @@
|
||||
import { castArray } from 'lodash';
|
||||
import { Knex } from 'knex';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { PromisePool } from '@supercharge/promise-pool';
|
||||
import {
|
||||
ERRORS,
|
||||
IBankTransactionMatchedEventPayload,
|
||||
IBankTransactionMatchingEventPayload,
|
||||
IMatchTransactionDTO,
|
||||
} from '../types';
|
||||
import { MatchTransactionsTypes } from './MatchTransactionsTypes';
|
||||
import {
|
||||
sumMatchTranasctions,
|
||||
sumUncategorizedTransactions,
|
||||
validateUncategorizedTransactionsExcluded,
|
||||
validateUncategorizedTransactionsNotMatched,
|
||||
} from '../_utils';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { ServiceError } from '@/modules/Items/ServiceError';
|
||||
import { UncategorizedBankTransaction } from '@/modules/BankingTransactions/models/UncategorizedBankTransaction';
|
||||
import { events } from '@/common/events/events';
|
||||
|
||||
@Injectable()
|
||||
export class MatchBankTransactions {
|
||||
constructor(
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
private readonly matchedBankTransactions: MatchTransactionsTypes,
|
||||
|
||||
@Inject(UncategorizedBankTransaction.name)
|
||||
private readonly uncategorizedBankTransactionModel: typeof UncategorizedBankTransaction,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Validates the match bank transactions DTO.
|
||||
* @param {number} uncategorizedTransactionId - Uncategorized transaction id.
|
||||
* @param {IMatchTransactionsDTO} matchTransactionsDTO - Match transactions DTO.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async validate(
|
||||
uncategorizedTransactionId: number | Array<number>,
|
||||
matchedTransactions: Array<IMatchTransactionDTO>,
|
||||
) {
|
||||
const uncategorizedTransactionIds = castArray(uncategorizedTransactionId);
|
||||
|
||||
// Validates the uncategorized transaction existance.
|
||||
const uncategorizedTransactions =
|
||||
await this.uncategorizedBankTransactionModel
|
||||
.query()
|
||||
.whereIn('id', uncategorizedTransactionIds)
|
||||
.withGraphFetched('matchedBankTransactions')
|
||||
.throwIfNotFound();
|
||||
|
||||
// Validates the uncategorized transaction is not already matched.
|
||||
validateUncategorizedTransactionsNotMatched(uncategorizedTransactions);
|
||||
|
||||
// Validate the uncategorized transaction is not excluded.
|
||||
validateUncategorizedTransactionsExcluded(uncategorizedTransactions);
|
||||
|
||||
// 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(
|
||||
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 = sumMatchTranasctions(
|
||||
validatationResult.results,
|
||||
);
|
||||
const totalUncategorizedTransactions = sumUncategorizedTransactions(
|
||||
uncategorizedTransactions,
|
||||
);
|
||||
// Validates the total given matching transcations whether is not equal
|
||||
// uncategorized transaction amount.
|
||||
if (totalUncategorizedTransactions !== totalMatchedTranasctions) {
|
||||
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(
|
||||
uncategorizedTransactionId: number | Array<number>,
|
||||
matchedTransactions: Array<IMatchTransactionDTO>,
|
||||
): Promise<void> {
|
||||
const uncategorizedTransactionIds = castArray(uncategorizedTransactionId);
|
||||
|
||||
// Validates the given matching transactions DTO.
|
||||
await this.validate(uncategorizedTransactionIds, matchedTransactions);
|
||||
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
|
||||
// Triggers the event `onBankTransactionMatching`.
|
||||
await this.eventPublisher.emitAsync(events.bankMatch.onMatching, {
|
||||
uncategorizedTransactionIds,
|
||||
matchedTransactions,
|
||||
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(
|
||||
uncategorizedTransactionIds,
|
||||
matchedTransaction,
|
||||
trx,
|
||||
);
|
||||
});
|
||||
// Triggers the event `onBankTransactionMatched`.
|
||||
await this.eventPublisher.emitAsync(events.bankMatch.onMatched, {
|
||||
uncategorizedTransactionIds,
|
||||
matchedTransactions,
|
||||
trx,
|
||||
} as IBankTransactionMatchedEventPayload);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
import { Knex } from 'knex';
|
||||
import { ERRORS } from '../types';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { ServiceError } from '@/modules/Items/ServiceError';
|
||||
import { MatchedBankTransaction } from '../models/MatchedBankTransaction';
|
||||
|
||||
@Injectable()
|
||||
export class ValidateTransactionMatched {
|
||||
constructor(
|
||||
@Inject(MatchedBankTransaction.name)
|
||||
private readonly matchedBankTransactionModel: typeof MatchedBankTransaction,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Validate the given transaction whether is matched with bank transactions.
|
||||
* @param {string} referenceType - Transaction reference type.
|
||||
* @param {number} referenceId - Transaction reference id.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public async validateTransactionNoMatchLinking(
|
||||
referenceType: string,
|
||||
referenceId: number,
|
||||
trx?: Knex.Transaction
|
||||
) {
|
||||
const foundMatchedTransaction =
|
||||
await this.matchedBankTransactionModel.query(trx).findOne({
|
||||
referenceType,
|
||||
referenceId,
|
||||
});
|
||||
if (foundMatchedTransaction) {
|
||||
throw new ServiceError(ERRORS.CANNOT_DELETE_TRANSACTION_MATCHED);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
import {
|
||||
IBankTransactionMatchedEventPayload,
|
||||
IBankTransactionUnmatchedEventPayload,
|
||||
} from '../types';
|
||||
import PromisePool from '@supercharge/promise-pool';
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
import { Account } from '@/modules/Accounts/models/Account.model';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { UncategorizedBankTransaction } from '@/modules/BankingTransactions/models/UncategorizedBankTransaction';
|
||||
import { events } from '@/common/events/events';
|
||||
|
||||
@Injectable()
|
||||
export class DecrementUncategorizedTransactionOnMatchingSubscriber {
|
||||
constructor(
|
||||
@Inject(Account.name)
|
||||
private readonly accountModel: typeof Account,
|
||||
|
||||
@Inject(UncategorizedBankTransaction.name)
|
||||
private readonly uncategorizedBankTransactionModel: typeof UncategorizedBankTransaction,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Validates the cashflow transaction whether matched with bank transaction on deleting.
|
||||
* @param {IManualJournalDeletingPayload}
|
||||
*/
|
||||
@OnEvent(events.bankMatch.onMatched)
|
||||
public async decrementUnCategorizedTransactionsOnMatching({
|
||||
uncategorizedTransactionIds,
|
||||
trx,
|
||||
}: IBankTransactionMatchedEventPayload) {
|
||||
const uncategorizedTransactions =
|
||||
await this.uncategorizedBankTransactionModel.query().whereIn(
|
||||
'id',
|
||||
uncategorizedTransactionIds
|
||||
);
|
||||
await PromisePool.withConcurrency(1)
|
||||
.for(uncategorizedTransactions)
|
||||
.process(async (transaction) => {
|
||||
await this.accountModel
|
||||
.query(trx)
|
||||
.findById(transaction.accountId)
|
||||
.decrement('uncategorizedTransactions', 1);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the cashflow transaction whether matched with bank transaction on deleting.
|
||||
* @param {IManualJournalDeletingPayload}
|
||||
*/
|
||||
@OnEvent(events.bankMatch.onUnmatched)
|
||||
public async incrementUnCategorizedTransactionsOnUnmatching({
|
||||
uncategorizedTransactionId,
|
||||
trx,
|
||||
}: IBankTransactionUnmatchedEventPayload) {
|
||||
const transaction =
|
||||
await this.uncategorizedBankTransactionModel.query().findById(
|
||||
uncategorizedTransactionId
|
||||
);
|
||||
await this.accountModel
|
||||
.query(trx)
|
||||
.findById(transaction.accountId)
|
||||
.increment('uncategorizedTransactions', 1);
|
||||
}
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
import { GetMatchedTransactionsByType } from './GetMatchedTransactionsByType';
|
||||
import { GetMatchedTransactionCashflowTransformer } from './GetMatchedTransactionCashflowTransformer';
|
||||
import { GetMatchedTransactionsFilter } from '../types';
|
||||
import { BankTransaction } from '@/modules/BankingTransactions/models/BankTransaction';
|
||||
import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class GetMatchedTransactionsByCashflow extends GetMatchedTransactionsByType {
|
||||
constructor(
|
||||
private readonly transformer: TransformerInjectable,
|
||||
|
||||
@Inject(BankTransaction.name)
|
||||
private readonly bankTransactionModel: typeof BankTransaction,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the matched transactions of cash flow.
|
||||
* @param {number} tenantId
|
||||
* @param {GetMatchedTransactionsFilter} filter
|
||||
* @returns
|
||||
*/
|
||||
async getMatchedTransactions(
|
||||
filter: Omit<GetMatchedTransactionsFilter, 'transactionType'>,
|
||||
) {
|
||||
const transactions = await this.bankTransactionModel
|
||||
.query()
|
||||
.onBuild((q) => {
|
||||
// Not matched to bank transaction.
|
||||
q.withGraphJoined('matchedBankTransaction');
|
||||
q.whereNull('matchedBankTransaction.id');
|
||||
|
||||
// Not categorized.
|
||||
q.modify('notCategorized');
|
||||
|
||||
// Published.
|
||||
q.modify('published');
|
||||
|
||||
if (filter.fromDate) {
|
||||
q.where('date', '>=', filter.fromDate);
|
||||
}
|
||||
if (filter.toDate) {
|
||||
q.where('date', '<=', filter.toDate);
|
||||
}
|
||||
q.orderBy('date', 'DESC');
|
||||
});
|
||||
|
||||
return this.transformer.transform(
|
||||
transactions,
|
||||
new GetMatchedTransactionCashflowTransformer(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the matched transaction of cash flow.
|
||||
* @param {number} tenantId
|
||||
* @param {number} transactionId
|
||||
* @returns
|
||||
*/
|
||||
async getMatchedTransaction(transactionId: number) {
|
||||
const transactions = await this.bankTransactionModel
|
||||
.query()
|
||||
.findById(transactionId)
|
||||
.withGraphJoined('matchedBankTransaction')
|
||||
.whereNull('matchedBankTransaction.id')
|
||||
.modify('notCategorized')
|
||||
.modify('published')
|
||||
.throwIfNotFound();
|
||||
|
||||
return this.transformer.transform(
|
||||
transactions,
|
||||
new GetMatchedTransactionCashflowTransformer(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { GetMatchedTransactionsFilter, MatchedTransactionPOJO } from '../types';
|
||||
import { GetMatchedTransactionsByType } from './GetMatchedTransactionsByType';
|
||||
import { GetMatchedTransactionExpensesTransformer } from './GetMatchedTransactionExpensesTransformer';
|
||||
import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
|
||||
import { Expense } from '@/modules/Expenses/models/Expense.model';
|
||||
|
||||
@Injectable()
|
||||
export class GetMatchedTransactionsByExpenses extends GetMatchedTransactionsByType {
|
||||
constructor(
|
||||
protected readonly transformer: TransformerInjectable,
|
||||
|
||||
@Inject(Expense.name)
|
||||
protected readonly expenseModel: typeof Expense,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the matched transactions of expenses.
|
||||
* @param {number} tenantId
|
||||
* @param {GetMatchedTransactionsFilter} filter
|
||||
* @returns
|
||||
*/
|
||||
async getMatchedTransactions(filter: GetMatchedTransactionsFilter) {
|
||||
// Retrieve the expense matches.
|
||||
const expenses = await this.expenseModel.query().onBuild((query) => {
|
||||
// Filter out the not matched to bank transactions.
|
||||
query.withGraphJoined('matchedBankTransaction');
|
||||
query.whereNull('matchedBankTransaction.id');
|
||||
|
||||
// Filter the published onyl
|
||||
query.modify('filterByPublished');
|
||||
|
||||
if (filter.fromDate) {
|
||||
query.where('paymentDate', '>=', filter.fromDate);
|
||||
}
|
||||
if (filter.toDate) {
|
||||
query.where('paymentDate', '<=', filter.toDate);
|
||||
}
|
||||
if (filter.minAmount) {
|
||||
query.where('totalAmount', '>=', filter.minAmount);
|
||||
}
|
||||
if (filter.maxAmount) {
|
||||
query.where('totalAmount', '<=', filter.maxAmount);
|
||||
}
|
||||
query.orderBy('paymentDate', 'DESC');
|
||||
});
|
||||
return this.transformer.transform(
|
||||
expenses,
|
||||
new GetMatchedTransactionExpensesTransformer(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the given matched expense transaction.
|
||||
* @param {number} tenantId
|
||||
* @param {number} transactionId
|
||||
* @returns {GetMatchedTransactionExpensesTransformer-}
|
||||
*/
|
||||
public async getMatchedTransaction(
|
||||
transactionId: number,
|
||||
): Promise<MatchedTransactionPOJO> {
|
||||
const expense = await this.expenseModel
|
||||
.query()
|
||||
.findById(transactionId)
|
||||
.throwIfNotFound();
|
||||
|
||||
return this.transformer.transform(
|
||||
expense,
|
||||
new GetMatchedTransactionExpensesTransformer(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,132 +0,0 @@
|
||||
import { Knex } from 'knex';
|
||||
import { first } from 'lodash';
|
||||
import { GetMatchedTransactionInvoicesTransformer } from './GetMatchedTransactionInvoicesTransformer';
|
||||
import {
|
||||
GetMatchedTransactionsFilter,
|
||||
IMatchTransactionDTO,
|
||||
MatchedTransactionPOJO,
|
||||
MatchedTransactionsPOJO,
|
||||
} from '../types';
|
||||
import { GetMatchedTransactionsByType } from './GetMatchedTransactionsByType';
|
||||
import { CreatePaymentReceivedService } from '@/modules/PaymentReceived/commands/CreatePaymentReceived.serivce';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { SaleInvoice } from '@/modules/SaleInvoices/models/SaleInvoice';
|
||||
import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
|
||||
import { UncategorizedBankTransaction } from '@/modules/BankingTransactions/models/UncategorizedBankTransaction';
|
||||
import { IPaymentReceivedCreateDTO } from '@/modules/PaymentReceived/types/PaymentReceived.types';
|
||||
|
||||
@Injectable()
|
||||
export class GetMatchedTransactionsByInvoices extends GetMatchedTransactionsByType {
|
||||
constructor(
|
||||
private readonly transformer: TransformerInjectable,
|
||||
private readonly createPaymentReceivedService: CreatePaymentReceivedService,
|
||||
|
||||
@Inject(SaleInvoice.name)
|
||||
private readonly saleInvoiceModel: typeof SaleInvoice,
|
||||
|
||||
@Inject(UncategorizedBankTransaction.name)
|
||||
private readonly uncategorizedBankTransactionModel: typeof UncategorizedBankTransaction,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the matched transactions.
|
||||
* @param {GetMatchedTransactionsFilter} filter -
|
||||
* @returns {Promise<MatchedTransactionsPOJO>}
|
||||
*/
|
||||
public async getMatchedTransactions(
|
||||
filter: GetMatchedTransactionsFilter
|
||||
): Promise<MatchedTransactionsPOJO> {
|
||||
// Retrieve the invoices that not matched, unpaid.
|
||||
const invoices = await this.saleInvoiceModel.query().onBuild((q) => {
|
||||
q.withGraphJoined('matchedBankTransaction');
|
||||
q.whereNull('matchedBankTransaction.id');
|
||||
q.modify('unpaid');
|
||||
q.modify('published');
|
||||
|
||||
if (filter.fromDate) {
|
||||
q.where('invoiceDate', '>=', filter.fromDate);
|
||||
}
|
||||
if (filter.toDate) {
|
||||
q.where('invoiceDate', '<=', filter.toDate);
|
||||
}
|
||||
q.orderBy('invoiceDate', 'DESC');
|
||||
});
|
||||
|
||||
return this.transformer.transform(
|
||||
invoices,
|
||||
new GetMatchedTransactionInvoicesTransformer()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the matched transaction.
|
||||
* @param {number} tenantId
|
||||
* @param {number} transactionId
|
||||
* @returns {Promise<MatchedTransactionPOJO>}
|
||||
*/
|
||||
public async getMatchedTransaction(
|
||||
transactionId: number
|
||||
): Promise<MatchedTransactionPOJO> {
|
||||
const invoice = await this.saleInvoiceModel.query().findById(transactionId);
|
||||
|
||||
return this.transformer.transform(
|
||||
invoice,
|
||||
new GetMatchedTransactionInvoicesTransformer()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the common matched transaction.
|
||||
* @param {number} tenantId
|
||||
* @param {Array<number>} uncategorizedTransactionIds
|
||||
* @param {IMatchTransactionDTO} matchTransactionDTO
|
||||
* @param {Knex.Transaction} trx
|
||||
*/
|
||||
public async createMatchedTransaction(
|
||||
uncategorizedTransactionIds: Array<number>,
|
||||
matchTransactionDTO: IMatchTransactionDTO,
|
||||
trx?: Knex.Transaction
|
||||
) {
|
||||
await super.createMatchedTransaction(
|
||||
uncategorizedTransactionIds,
|
||||
matchTransactionDTO,
|
||||
trx
|
||||
);
|
||||
const uncategorizedTransactionId = first(uncategorizedTransactionIds);
|
||||
const uncategorizedTransaction =
|
||||
await this.uncategorizedBankTransactionModel.query(trx)
|
||||
.findById(uncategorizedTransactionId)
|
||||
.throwIfNotFound();
|
||||
|
||||
const invoice = await SaleInvoice.query(trx)
|
||||
.findById(matchTransactionDTO.referenceId)
|
||||
.throwIfNotFound();
|
||||
|
||||
const createPaymentReceivedDTO: IPaymentReceivedCreateDTO = {
|
||||
customerId: invoice.customerId,
|
||||
paymentDate: uncategorizedTransaction.date,
|
||||
amount: invoice.dueAmount,
|
||||
depositAccountId: uncategorizedTransaction.accountId,
|
||||
entries: [
|
||||
{
|
||||
index: 1,
|
||||
invoiceId: invoice.id,
|
||||
paymentAmount: invoice.dueAmount,
|
||||
},
|
||||
],
|
||||
branchId: invoice.branchId,
|
||||
};
|
||||
// Create a payment received associated to the matched invoice.
|
||||
const paymentReceived = await this.createPaymentReceivedService.createPaymentReceived(
|
||||
createPaymentReceivedDTO,
|
||||
trx
|
||||
);
|
||||
// Link the create payment received with matched invoice transaction.
|
||||
await super.createMatchedTransaction(uncategorizedTransactionIds, {
|
||||
referenceType: 'PaymentReceive',
|
||||
referenceId: paymentReceived.id,
|
||||
}, trx)
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { GetMatchedTransactionManualJournalsTransformer } from './GetMatchedTransactionManualJournalsTransformer';
|
||||
import { GetMatchedTransactionsByType } from './GetMatchedTransactionsByType';
|
||||
import { GetMatchedTransactionsFilter } from '../types';
|
||||
import { ManualJournal } from '@/modules/ManualJournals/models/ManualJournal';
|
||||
import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
|
||||
|
||||
@Injectable()
|
||||
export class GetMatchedTransactionsByManualJournals extends GetMatchedTransactionsByType {
|
||||
constructor(
|
||||
private readonly transformer: TransformerInjectable,
|
||||
|
||||
@Inject(ManualJournal.name)
|
||||
private readonly manualJournalModel: typeof ManualJournal,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the matched transactions of manual journals.
|
||||
* @param {number} tenantId
|
||||
* @param {GetMatchedTransactionsFilter} filter
|
||||
* @returns
|
||||
*/
|
||||
async getMatchedTransactions(
|
||||
filter: Omit<GetMatchedTransactionsFilter, 'transactionType'>,
|
||||
) {
|
||||
// @todo: get the account id from the filter
|
||||
const accountId = 1000;
|
||||
|
||||
const manualJournals = await this.manualJournalModel.query().onBuild((query) => {
|
||||
query.withGraphJoined('matchedBankTransaction');
|
||||
query.whereNull('matchedBankTransaction.id');
|
||||
|
||||
query.withGraphJoined('entries');
|
||||
query.where('entries.accountId', accountId);
|
||||
query.modify('filterByPublished');
|
||||
|
||||
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(
|
||||
manualJournals,
|
||||
new GetMatchedTransactionManualJournalsTransformer(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the matched transaction of manual journals.
|
||||
* @param {number} tenantId
|
||||
* @param {number} transactionId
|
||||
* @returns
|
||||
*/
|
||||
public async getMatchedTransaction(transactionId: number) {
|
||||
const manualJournal = await this.manualJournalModel.query()
|
||||
.findById(transactionId)
|
||||
.whereNotExists(ManualJournal.relatedQuery('matchedBankTransaction'))
|
||||
.throwIfNotFound();
|
||||
|
||||
return this.transformer.transform(
|
||||
manualJournal,
|
||||
new GetMatchedTransactionManualJournalsTransformer(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
import { Knex } from 'knex';
|
||||
import {
|
||||
GetMatchedTransactionsFilter,
|
||||
IMatchTransactionDTO,
|
||||
MatchedTransactionPOJO,
|
||||
MatchedTransactionsPOJO,
|
||||
} from '../types';
|
||||
import PromisePool from '@supercharge/promise-pool';
|
||||
import { MatchedBankTransaction } from '../models/MatchedBankTransaction';
|
||||
import { Inject } from '@nestjs/common';
|
||||
|
||||
export abstract class GetMatchedTransactionsByType {
|
||||
@Inject(MatchedBankTransaction.name)
|
||||
private readonly matchedBankTransactionModel: typeof MatchedBankTransaction;
|
||||
|
||||
/**
|
||||
* Retrieves the matched transactions.
|
||||
* @param {number} tenantId -
|
||||
* @param {GetMatchedTransactionsFilter} filter -
|
||||
* @returns {Promise<MatchedTransactionsPOJO>}
|
||||
*/
|
||||
public async getMatchedTransactions(
|
||||
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(
|
||||
transactionId: number
|
||||
): Promise<MatchedTransactionPOJO> {
|
||||
throw new Error(
|
||||
'The `getMatchedTransaction` method is not defined for the transaction type.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the common matched transaction.
|
||||
* @param {number} tenantId
|
||||
* @param {Array<number>} uncategorizedTransactionIds
|
||||
* @param {IMatchTransactionDTO} matchTransactionDTO
|
||||
* @param {Knex.Transaction} trx
|
||||
*/
|
||||
public async createMatchedTransaction(
|
||||
uncategorizedTransactionIds: Array<number>,
|
||||
matchTransactionDTO: IMatchTransactionDTO,
|
||||
trx?: Knex.Transaction
|
||||
) {
|
||||
await PromisePool.withConcurrency(2)
|
||||
.for(uncategorizedTransactionIds)
|
||||
.process(async (uncategorizedTransactionId) => {
|
||||
await this.matchedBankTransactionModel.query(trx).insert({
|
||||
uncategorizedTransactionId,
|
||||
referenceType: matchTransactionDTO.referenceType,
|
||||
referenceId: matchTransactionDTO.referenceId,
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { PlaidItem } from '../models/PlaidItem';
|
||||
import { PlaidApi } from 'plaid';
|
||||
import { PLAID_CLIENT } from '../../Plaid/Plaid.module';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { events } from '@/common/events/events';
|
||||
import { SystemPlaidItem } from '../models/SystemPlaidItem';
|
||||
import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service';
|
||||
import { IPlaidItemCreatedEventPayload, PlaidItemDTO } from '../types/BankingPlaid.types';
|
||||
|
||||
@Injectable()
|
||||
export class PlaidItemService {
|
||||
constructor(
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
private readonly tenancyContext: TenancyContext,
|
||||
|
||||
@Inject(SystemPlaidItem.name)
|
||||
private readonly systemPlaidItemModel: typeof SystemPlaidItem,
|
||||
|
||||
@Inject(PlaidItem.name)
|
||||
private readonly plaidItemModel: typeof PlaidItem,
|
||||
|
||||
@Inject(PLAID_CLIENT)
|
||||
private readonly plaidClient: PlaidApi,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Exchanges the public token to get access token and item id and then creates
|
||||
* a new Plaid item.
|
||||
* @param {PlaidItemDTO} itemDTO - Plaid item data transfer object.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public async item(itemDTO: PlaidItemDTO): Promise<void> {
|
||||
const { publicToken, institutionId } = itemDTO;
|
||||
|
||||
const tenant = await this.tenancyContext.getTenant();
|
||||
const tenantId = tenant.id;
|
||||
|
||||
// Exchange the public token for a private access token and store with the item.
|
||||
const response = await this.plaidClient.itemPublicTokenExchange({
|
||||
public_token: publicToken,
|
||||
});
|
||||
const plaidAccessToken = response.data.access_token;
|
||||
const plaidItemId = response.data.item_id;
|
||||
|
||||
// Store the Plaid item metadata on tenant scope.
|
||||
const plaidItem = await this.plaidItemModel.query().insertAndFetch({
|
||||
tenantId,
|
||||
plaidAccessToken,
|
||||
plaidItemId,
|
||||
plaidInstitutionId: institutionId,
|
||||
});
|
||||
// Stores the Plaid item id on system scope.
|
||||
await this.systemPlaidItemModel.query().insert({ tenantId, plaidItemId });
|
||||
|
||||
// Triggers `onPlaidItemCreated` event.
|
||||
await this.eventEmitter.emitAsync(events.plaid.onItemCreated, {
|
||||
plaidAccessToken,
|
||||
plaidItemId,
|
||||
plaidInstitutionId: institutionId,
|
||||
} as IPlaidItemCreatedEventPayload);
|
||||
}
|
||||
}
|
||||
@@ -1,244 +0,0 @@
|
||||
import * as R from 'ramda';
|
||||
import bluebird from 'bluebird';
|
||||
import { entries, groupBy } from 'lodash';
|
||||
import {
|
||||
AccountBase as PlaidAccountBase,
|
||||
Item as PlaidItem,
|
||||
Institution as PlaidInstitution,
|
||||
Transaction as PlaidTransaction,
|
||||
} from 'plaid';
|
||||
import {
|
||||
transformPlaidAccountToCreateAccount,
|
||||
transformPlaidTrxsToCashflowCreate,
|
||||
} from '../utils';
|
||||
import { Knex } from 'knex';
|
||||
import uniqid from 'uniqid';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { RemovePendingUncategorizedTransaction } from '../../BankingTransactions/commands/RemovePendingUncategorizedTransaction.service';
|
||||
import { CreateAccountService } from '../../Accounts/CreateAccount.service';
|
||||
import { Account } from '../../Accounts/models/Account.model';
|
||||
import { events } from '@/common/events/events';
|
||||
import { PlaidItem as PlaidItemModel } from '../models/PlaidItem';
|
||||
import { IAccountCreateDTO } from '@/interfaces/Account';
|
||||
import { IPlaidTransactionsSyncedEventPayload } from '../types/BankingPlaid.types';
|
||||
import { UncategorizedBankTransaction } from '../../BankingTransactions/models/UncategorizedBankTransaction';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { CreateUncategorizedTransactionService } from '@/modules/BankingCategorize/commands/CreateUncategorizedTransaction.service';
|
||||
|
||||
const CONCURRENCY_ASYNC = 10;
|
||||
|
||||
@Injectable()
|
||||
export class PlaidSyncDb {
|
||||
constructor(
|
||||
private readonly createAccountService: CreateAccountService,
|
||||
private readonly createUncategorizedTransaction: CreateUncategorizedTransactionService,
|
||||
private readonly removePendingTransaction: RemovePendingUncategorizedTransaction,
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
|
||||
@Inject(Account.name)
|
||||
private readonly accountModel: typeof Account,
|
||||
|
||||
@Inject(PlaidItemModel.name)
|
||||
private readonly plaidItemModel: typeof PlaidItemModel,
|
||||
|
||||
@Inject(UncategorizedBankTransaction.name)
|
||||
private readonly uncategorizedBankTransactionModel: typeof UncategorizedBankTransaction,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Syncs the Plaid bank account.
|
||||
* @param {number} tenantId
|
||||
* @param {IAccountCreateDTO} createBankAccountDTO
|
||||
* @param {Knex.Transaction} trx
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public async syncBankAccount(
|
||||
createBankAccountDTO: IAccountCreateDTO,
|
||||
trx?: Knex.Transaction,
|
||||
) {
|
||||
const plaidAccount = await this.accountModel
|
||||
.query(trx)
|
||||
.findOne('plaidAccountId', createBankAccountDTO.plaidAccountId);
|
||||
// Can't continue if the Plaid account is already created.
|
||||
if (plaidAccount) {
|
||||
return;
|
||||
}
|
||||
await this.createAccountService.createAccount(createBankAccountDTO, trx, {
|
||||
ignoreUniqueName: true,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Syncs the plaid accounts to the system accounts.
|
||||
* @param {number} tenantId Tenant ID.
|
||||
* @param {PlaidAccount[]} plaidAccounts
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public async syncBankAccounts(
|
||||
plaidAccounts: PlaidAccountBase[],
|
||||
institution: PlaidInstitution,
|
||||
item: PlaidItem,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> {
|
||||
const transformToPlaidAccounts = R.curry(
|
||||
transformPlaidAccountToCreateAccount,
|
||||
)(item, institution);
|
||||
const accountCreateDTOs = R.map(transformToPlaidAccounts)(plaidAccounts);
|
||||
|
||||
await bluebird.map(
|
||||
accountCreateDTOs,
|
||||
(createAccountDTO: any) => this.syncBankAccount(createAccountDTO, trx),
|
||||
{ concurrency: CONCURRENCY_ASYNC },
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Synsc the Plaid transactions to the system GL entries.
|
||||
* @param {number} tenantId - Tenant ID.
|
||||
* @param {number} plaidAccountId - Plaid account ID.
|
||||
* @param {PlaidTransaction[]} plaidTranasctions - Plaid transactions
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async syncAccountTranactions(
|
||||
plaidAccountId: number,
|
||||
plaidTranasctions: PlaidTransaction[],
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> {
|
||||
const batch = uniqid();
|
||||
const cashflowAccount = await this.accountModel
|
||||
.query(trx)
|
||||
.findOne({ plaidAccountId })
|
||||
.throwIfNotFound();
|
||||
|
||||
// Transformes the Plaid transactions to cashflow create DTOs.
|
||||
const transformTransaction = R.curry(transformPlaidTrxsToCashflowCreate)(
|
||||
cashflowAccount.id,
|
||||
);
|
||||
const uncategorizedTransDTOs =
|
||||
R.map(transformTransaction)(plaidTranasctions);
|
||||
|
||||
// Creating account transaction queue.
|
||||
await bluebird.map(
|
||||
uncategorizedTransDTOs,
|
||||
(uncategoriedDTO) =>
|
||||
this.createUncategorizedTransaction.create(
|
||||
{ ...uncategoriedDTO, batch },
|
||||
trx,
|
||||
),
|
||||
{ concurrency: 1 },
|
||||
);
|
||||
// Triggers `onPlaidTransactionsSynced` event.
|
||||
await this.eventPublisher.emitAsync(events.plaid.onTransactionsSynced, {
|
||||
plaidAccountId,
|
||||
batch,
|
||||
} as IPlaidTransactionsSyncedEventPayload);
|
||||
}
|
||||
|
||||
/**
|
||||
* Syncs the accounts transactions in paraller under controlled concurrency.
|
||||
* @param {number} tenantId
|
||||
* @param {PlaidTransaction[]} plaidTransactions
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async syncAccountsTransactions(
|
||||
plaidAccountsTransactions: PlaidTransaction[],
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> {
|
||||
const groupedTrnsxByAccountId = entries(
|
||||
groupBy(plaidAccountsTransactions, 'account_id'),
|
||||
);
|
||||
await bluebird.map(
|
||||
groupedTrnsxByAccountId,
|
||||
([plaidAccountId, plaidTransactions]: [number, PlaidTransaction[]]) => {
|
||||
return this.syncAccountTranactions(
|
||||
plaidAccountId,
|
||||
plaidTransactions,
|
||||
trx,
|
||||
);
|
||||
},
|
||||
{ concurrency: CONCURRENCY_ASYNC },
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Syncs the removed Plaid transactions ids from the cashflow system transactions.
|
||||
* @param {string[]} plaidTransactionsIds - Plaid Transactions IDs.
|
||||
*/
|
||||
public async syncRemoveTransactions(
|
||||
plaidTransactionsIds: string[],
|
||||
trx?: Knex.Transaction,
|
||||
) {
|
||||
const uncategorizedTransactions =
|
||||
await this.uncategorizedBankTransactionModel
|
||||
.query(trx)
|
||||
.whereIn('plaidTransactionId', plaidTransactionsIds);
|
||||
const uncategorizedTransactionsIds = uncategorizedTransactions.map(
|
||||
(trans) => trans.id,
|
||||
);
|
||||
await bluebird.map(
|
||||
uncategorizedTransactionsIds,
|
||||
(uncategorizedTransactionId: number) =>
|
||||
this.removePendingTransaction.removePendingTransaction(
|
||||
uncategorizedTransactionId,
|
||||
trx,
|
||||
),
|
||||
{ concurrency: CONCURRENCY_ASYNC },
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Syncs the Plaid item last transaction cursor.
|
||||
* @param {number} tenantId - Tenant ID.
|
||||
* @param {string} itemId - Plaid item ID.
|
||||
* @param {string} lastCursor - Last transaction cursor.
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async syncTransactionsCursor(
|
||||
plaidItemId: string,
|
||||
lastCursor: string,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> {
|
||||
await this.plaidItemModel
|
||||
.query(trx)
|
||||
.findOne({ plaidItemId })
|
||||
.patch({ lastCursor });
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the last feeds updated at of the given Plaid accounts ids.
|
||||
* @param {number} tenantId
|
||||
* @param {string[]} plaidAccountIds
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async updateLastFeedsUpdatedAt(
|
||||
plaidAccountIds: string[],
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> {
|
||||
await this.accountModel
|
||||
.query(trx)
|
||||
.whereIn('plaid_account_id', plaidAccountIds)
|
||||
.patch({
|
||||
lastFeedsUpdatedAt: new Date(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the accounts feed active status of the given Plaid accounts ids.
|
||||
* @param {number} tenantId
|
||||
* @param {number[]} plaidAccountIds
|
||||
* @param {boolean} isFeedsActive
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public async updateAccountsFeedsActive(
|
||||
plaidAccountIds: string[],
|
||||
isFeedsActive: boolean = true,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> {
|
||||
await this.accountModel
|
||||
.query(trx)
|
||||
.whereIn('plaid_account_id', plaidAccountIds)
|
||||
.patch({
|
||||
isFeedsActive,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,150 +0,0 @@
|
||||
import { Knex } from 'knex';
|
||||
import { PlaidSyncDb } from './PlaidSyncDB';
|
||||
import { PlaidFetchedTransactionsUpdates } from '../types/BankingPlaid.types';
|
||||
import { PlaidItem } from '../models/PlaidItem';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import {
|
||||
CountryCode,
|
||||
PlaidApi,
|
||||
Transaction as PlaidTransaction,
|
||||
RemovedTransaction,
|
||||
} from 'plaid';
|
||||
import { PLAID_CLIENT } from '@/modules/Plaid/Plaid.module';
|
||||
|
||||
@Injectable()
|
||||
export class PlaidUpdateTransactions {
|
||||
constructor(
|
||||
private readonly plaidSync: PlaidSyncDb,
|
||||
private readonly uow: UnitOfWork,
|
||||
|
||||
@Inject(PlaidItem.name)
|
||||
private readonly plaidItemModel: typeof PlaidItem,
|
||||
|
||||
@Inject(PLAID_CLIENT)
|
||||
private readonly plaidClient: PlaidApi,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Handles sync the Plaid item to Bigcaptial under UOW.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {number} plaidItemId - Plaid item id.
|
||||
* @returns {Promise<{ addedCount: number; modifiedCount: number; removedCount: number; }>}
|
||||
*/
|
||||
public async updateTransactions(plaidItemId: string) {
|
||||
return this.uow.withTransaction((trx: Knex.Transaction) => {
|
||||
return this.updateTransactionsWork(plaidItemId, trx);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the fetching and storing the following:
|
||||
* - New, modified, or removed transactions.
|
||||
* - New bank accounts.
|
||||
* - Last accounts feeds updated at.
|
||||
* - Turn on the accounts feed flag.
|
||||
* @param {number} tenantId - Tenant ID.
|
||||
* @param {string} plaidItemId - The Plaid ID for the item.
|
||||
* @returns {Promise<{ addedCount: number; modifiedCount: number; removedCount: number; }>}
|
||||
*/
|
||||
public async updateTransactionsWork(
|
||||
plaidItemId: string,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<{
|
||||
addedCount: number;
|
||||
modifiedCount: number;
|
||||
removedCount: number;
|
||||
}> {
|
||||
// Fetch new transactions from plaid api.
|
||||
const { added, modified, removed, cursor, accessToken } =
|
||||
await this.fetchTransactionUpdates(plaidItemId);
|
||||
|
||||
const request = { access_token: accessToken };
|
||||
const {
|
||||
data: { accounts, item },
|
||||
} = await this.plaidClient.accountsGet(request);
|
||||
|
||||
const plaidAccountsIds = accounts.map((a) => a.account_id);
|
||||
const {
|
||||
data: { institution },
|
||||
} = await this.plaidClient.institutionsGetById({
|
||||
institution_id: item.institution_id,
|
||||
country_codes: [CountryCode.Us, CountryCode.Gb],
|
||||
});
|
||||
// Sync bank accounts.
|
||||
await this.plaidSync.syncBankAccounts(accounts, institution, item, trx);
|
||||
// Sync removed transactions.
|
||||
await this.plaidSync.syncRemoveTransactions(
|
||||
removed?.map((r) => r.transaction_id),
|
||||
trx,
|
||||
);
|
||||
// Sync bank account transactions.
|
||||
await this.plaidSync.syncAccountsTransactions(added.concat(modified), trx);
|
||||
// Sync transactions cursor.
|
||||
await this.plaidSync.syncTransactionsCursor(plaidItemId, cursor, trx);
|
||||
// Update the last feeds updated at of the updated accounts.
|
||||
await this.plaidSync.updateLastFeedsUpdatedAt(plaidAccountsIds, trx);
|
||||
// Turn on the accounts feeds flag.
|
||||
await this.plaidSync.updateAccountsFeedsActive(plaidAccountsIds, true, trx);
|
||||
|
||||
return {
|
||||
addedCount: added.length,
|
||||
modifiedCount: modified.length,
|
||||
removedCount: removed.length,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches transactions from the `Plaid API` for a given item.
|
||||
* @param {number} tenantId - Tenant ID.
|
||||
* @param {string} plaidItemId - The Plaid ID for the item.
|
||||
* @returns {Promise<PlaidFetchedTransactionsUpdates>}
|
||||
*/
|
||||
private async fetchTransactionUpdates(
|
||||
plaidItemId: string,
|
||||
): Promise<PlaidFetchedTransactionsUpdates> {
|
||||
// the transactions endpoint is paginated, so we may need to hit it multiple times to
|
||||
// retrieve all available transactions.
|
||||
const plaidItem = await this.plaidItemModel
|
||||
.query()
|
||||
.findOne('plaidItemId', plaidItemId);
|
||||
if (!plaidItem) {
|
||||
throw new Error('The given Plaid item id is not found.');
|
||||
}
|
||||
const { plaidAccessToken, lastCursor } = plaidItem;
|
||||
let cursor = lastCursor;
|
||||
|
||||
// New transaction updates since "cursor"
|
||||
let added: PlaidTransaction[] = [];
|
||||
let modified: PlaidTransaction[] = [];
|
||||
// Removed transaction ids
|
||||
let removed: RemovedTransaction[] = [];
|
||||
let hasMore = true;
|
||||
|
||||
const batchSize = 100;
|
||||
try {
|
||||
// Iterate through each page of new transaction updates for item
|
||||
/* eslint-disable no-await-in-loop */
|
||||
while (hasMore) {
|
||||
const request = {
|
||||
access_token: plaidAccessToken,
|
||||
cursor: cursor,
|
||||
count: batchSize,
|
||||
};
|
||||
const response = await this.plaidClient.transactionsSync(request);
|
||||
const data = response.data;
|
||||
// Add this page of results
|
||||
added = added.concat(data.added);
|
||||
modified = modified.concat(data.modified);
|
||||
removed = removed.concat(data.removed);
|
||||
hasMore = data.has_more;
|
||||
// Update cursor to the next cursor
|
||||
cursor = data.next_cursor;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`Error fetching transactions: ${err.message}`);
|
||||
cursor = lastCursor;
|
||||
}
|
||||
return { added, modified, removed, cursor, accessToken: plaidAccessToken };
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
import { castArray } from 'lodash';
|
||||
import { Knex } from 'knex';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { RevertRecognizedTransactionsCriteria } from '../_types';
|
||||
import { RecognizedBankTransaction } from '../models/RecognizedBankTransaction';
|
||||
import { UncategorizedBankTransaction } from '@/modules/BankingTransactions/models/UncategorizedBankTransaction';
|
||||
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
|
||||
|
||||
@Injectable()
|
||||
export class RevertRecognizedTransactionsService {
|
||||
constructor(
|
||||
private readonly uow: UnitOfWork,
|
||||
|
||||
@Inject(RecognizedBankTransaction.name)
|
||||
private readonly recognizedBankTransactionModel: typeof RecognizedBankTransaction,
|
||||
|
||||
@Inject(UncategorizedBankTransaction.name)
|
||||
private readonly uncategorizedBankTransactionModel: typeof UncategorizedBankTransaction,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Revert and unlinks the recognized transactions based on the given bank rule
|
||||
* and transactions criteria..
|
||||
* @param {number|Array<number>} bankRuleId - Bank rule id.
|
||||
* @param {RevertRecognizedTransactionsCriteria} transactionsCriteria -
|
||||
* @param {Knex.Transaction} trx - Knex transaction.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public async revertRecognizedTransactions(
|
||||
ruleId?: number | Array<number>,
|
||||
transactionsCriteria?: RevertRecognizedTransactionsCriteria,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> {
|
||||
const rulesIds = castArray(ruleId);
|
||||
|
||||
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
|
||||
// Retrieves all the recognized transactions of the banbk rule.
|
||||
const uncategorizedTransactions =
|
||||
await this.uncategorizedBankTransactionModel.query(trx).onBuild((q) => {
|
||||
q.withGraphJoined('recognizedTransaction');
|
||||
q.whereNotNull('recognizedTransaction.id');
|
||||
|
||||
if (rulesIds.length > 0) {
|
||||
q.whereIn('recognizedTransaction.bankRuleId', rulesIds);
|
||||
}
|
||||
if (transactionsCriteria?.accountId) {
|
||||
q.where('accountId', transactionsCriteria.accountId);
|
||||
}
|
||||
if (transactionsCriteria?.batch) {
|
||||
q.where('batch', transactionsCriteria.batch);
|
||||
}
|
||||
});
|
||||
const uncategorizedTransactionIds = uncategorizedTransactions.map(
|
||||
(r) => r.id,
|
||||
);
|
||||
// Unlink the recongized transactions out of uncategorized transactions.
|
||||
await this.uncategorizedBankTransactionModel
|
||||
.query(trx)
|
||||
.whereIn('id', uncategorizedTransactionIds)
|
||||
.patch({
|
||||
recognizedTransactionId: null,
|
||||
});
|
||||
// Delete the recognized bank transactions that assocaited to bank rule.
|
||||
await this.recognizedBankTransactionModel
|
||||
.query(trx)
|
||||
.whereIn('uncategorizedTransactionId', uncategorizedTransactionIds)
|
||||
.delete();
|
||||
}, trx);
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Param,
|
||||
Post,
|
||||
Query,
|
||||
} from '@nestjs/common';
|
||||
import { BankingTransactionsApplication } from './BankingTransactionsApplication.service';
|
||||
import { ICashflowNewCommandDTO } from './types/BankingTransactions.types';
|
||||
import { PublicRoute } from '../Auth/Jwt.guard';
|
||||
|
||||
@Controller('banking/transactions')
|
||||
@PublicRoute()
|
||||
export class BankingTransactionsController {
|
||||
constructor(
|
||||
private readonly bankingTransactionsApplication: BankingTransactionsApplication,
|
||||
) {}
|
||||
|
||||
@Get('')
|
||||
async getBankAccounts(@Query() filterDTO: ICashflowAccountsFilter) {
|
||||
return this.bankingTransactionsApplication.getBankAccounts(filterDTO);
|
||||
}
|
||||
|
||||
@Post()
|
||||
async createTransaction(@Body() transactionDTO: ICashflowNewCommandDTO) {
|
||||
return this.bankingTransactionsApplication.createTransaction(
|
||||
transactionDTO,
|
||||
);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async deleteTransaction(@Param('id') transactionId: string) {
|
||||
return this.bankingTransactionsApplication.deleteTransaction(
|
||||
Number(transactionId),
|
||||
);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async getTransaction(@Param('id') transactionId: string) {
|
||||
return this.bankingTransactionsApplication.getTransaction(
|
||||
Number(transactionId),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
import { Knex } from 'knex';
|
||||
import { DeleteCashflowTransaction } from './commands/DeleteCashflowTransaction.service';
|
||||
import { CreateBankTransactionService } from './commands/CreateBankTransaction.service';
|
||||
import { GetBankTransactionService } from './queries/GetBankTransaction.service';
|
||||
import { ICashflowNewCommandDTO } from './types/BankingTransactions.types';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { GetBankAccountsService } from './queries/GetBankAccounts.service';
|
||||
|
||||
@Injectable()
|
||||
export class BankingTransactionsApplication {
|
||||
constructor(
|
||||
private readonly createTransactionService: CreateBankTransactionService,
|
||||
private readonly deleteTransactionService: DeleteCashflowTransaction,
|
||||
private readonly getCashflowTransactionService: GetBankTransactionService,
|
||||
private readonly getBankAccountsService: GetBankAccountsService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Creates a new cashflow transaction.
|
||||
* @param {ICashflowNewCommandDTO} transactionDTO
|
||||
* @returns
|
||||
*/
|
||||
public createTransaction(transactionDTO: ICashflowNewCommandDTO) {
|
||||
return this.createTransactionService.newCashflowTransaction(transactionDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the given cashflow transaction.
|
||||
* @param {number} cashflowTransactionId - Cashflow transaction id.
|
||||
* @returns {Promise<{ oldCashflowTransaction: ICashflowTransaction }>}
|
||||
*/
|
||||
public deleteTransaction(cashflowTransactionId: number) {
|
||||
return this.deleteTransactionService.deleteCashflowTransaction(
|
||||
cashflowTransactionId,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves specific cashflow transaction.
|
||||
* @param {number} cashflowTransactionId
|
||||
* @returns
|
||||
*/
|
||||
public getTransaction(cashflowTransactionId: number) {
|
||||
return this.getCashflowTransactionService.getBankTransaction(
|
||||
cashflowTransactionId,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the cashflow accounts.
|
||||
* @param {ICashflowAccountsFilter} filterDTO
|
||||
*/
|
||||
public getBankAccounts(filterDTO: ICashflowAccountsFilter) {
|
||||
return this.getBankAccountsService.getBankAccounts(filterDTO);
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { GetRecognizedTransactionTransformer } from './GetRecognizedTransactionTransformer';
|
||||
import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
|
||||
import { UncategorizedBankTransaction } from '../models/UncategorizedBankTransaction';
|
||||
|
||||
@Injectable()
|
||||
export class GetRecognizedTransactionService {
|
||||
constructor(
|
||||
private readonly transformer: TransformerInjectable,
|
||||
|
||||
@Inject(UncategorizedBankTransaction.name)
|
||||
private readonly uncategorizedBankTransactionModel: typeof UncategorizedBankTransaction
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieves the recognized transaction of the given uncategorized transaction.
|
||||
* @param {number} tenantId
|
||||
* @param {number} uncategorizedTransactionId
|
||||
*/
|
||||
public async getRecognizedTransaction(
|
||||
uncategorizedTransactionId: number
|
||||
) {
|
||||
const uncategorizedTransaction =
|
||||
await this.uncategorizedBankTransactionModel.query()
|
||||
.findById(uncategorizedTransactionId)
|
||||
.withGraphFetched('matchedBankTransactions')
|
||||
.withGraphFetched('recognizedTransaction.assignAccount')
|
||||
.withGraphFetched('recognizedTransaction.bankRule')
|
||||
.withGraphFetched('account')
|
||||
.throwIfNotFound();
|
||||
|
||||
return this.transformer.transform(
|
||||
uncategorizedTransaction,
|
||||
new GetRecognizedTransactionTransformer()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
import { BankTransactionAutoIncrement } from '../commands/BankTransactionAutoIncrement.service';
|
||||
import { BankTransactionGLEntriesService } from '../commands/BankTransactionGLEntries';
|
||||
import { events } from '@/common/events/events';
|
||||
import { ICommandCashflowCreatedPayload, ICommandCashflowDeletedPayload } from '../types/BankingTransactions.types';
|
||||
|
||||
@Injectable()
|
||||
export default class BankingTransactionGLEntriesSubscriber {
|
||||
/**
|
||||
* @param {BankTransactionGLEntriesService} bankTransactionGLEntries - Bank transaction GL entries service.
|
||||
* @param {BankTransactionAutoIncrement} cashflowTransactionAutoIncrement - Cashflow transaction auto increment service.
|
||||
*/
|
||||
constructor(
|
||||
private readonly bankTransactionGLEntries: BankTransactionGLEntriesService,
|
||||
private readonly cashflowTransactionAutoIncrement: BankTransactionAutoIncrement,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Writes the journal entries once the cashflow transaction create.
|
||||
* @param {ICommandCashflowCreatedPayload} payload -
|
||||
*/
|
||||
@OnEvent(events.cashflow.onTransactionCreated)
|
||||
public async writeJournalEntriesOnceTransactionCreated({
|
||||
cashflowTransaction,
|
||||
trx,
|
||||
}: ICommandCashflowCreatedPayload) {
|
||||
// Can't write GL entries if the transaction not published yet.
|
||||
if (!cashflowTransaction.isPublished) return;
|
||||
|
||||
await this.bankTransactionGLEntries.writeJournalEntries(
|
||||
cashflowTransaction.id,
|
||||
trx,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment the cashflow transaction number once the transaction created.
|
||||
* @param {ICommandCashflowCreatedPayload} payload -
|
||||
*/
|
||||
@OnEvent(events.cashflow.onTransactionCreated)
|
||||
public async incrementTransactionNumberOnceTransactionCreated({}: ICommandCashflowCreatedPayload) {
|
||||
this.cashflowTransactionAutoIncrement.incrementNextTransactionNumber();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the GL entries once the cashflow transaction deleted.
|
||||
* @param {ICommandCashflowDeletedPayload} payload -
|
||||
*/
|
||||
@OnEvent(events.cashflow.onTransactionDeleted)
|
||||
public async revertGLEntriesOnceTransactionDeleted({
|
||||
cashflowTransactionId,
|
||||
trx,
|
||||
}: ICommandCashflowDeletedPayload) {
|
||||
await this.bankTransactionGLEntries.revertJournalEntries(
|
||||
cashflowTransactionId,
|
||||
trx,
|
||||
);
|
||||
};
|
||||
}
|
||||
@@ -1,122 +0,0 @@
|
||||
import { Knex } from "knex";
|
||||
import { UncategorizedBankTransaction } from "../models/UncategorizedBankTransaction";
|
||||
import { BankTransaction } from "../models/BankTransaction";
|
||||
|
||||
export interface IPendingTransactionRemovingEventPayload {
|
||||
uncategorizedTransactionId: number;
|
||||
pendingTransaction: UncategorizedBankTransaction;
|
||||
trx?: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IPendingTransactionRemovedEventPayload {
|
||||
uncategorizedTransactionId: number;
|
||||
pendingTransaction: UncategorizedBankTransaction;
|
||||
trx?: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IGetRecognizedTransactionsQuery {
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
accountId?: number;
|
||||
minDate?: Date;
|
||||
maxDate?: Date;
|
||||
minAmount?: number;
|
||||
maxAmount?: number;
|
||||
}
|
||||
|
||||
export interface ICashflowCommandDTO {
|
||||
date: Date;
|
||||
|
||||
transactionNumber: string;
|
||||
referenceNo: string;
|
||||
transactionType: string;
|
||||
description: string;
|
||||
|
||||
amount: number;
|
||||
exchangeRate: number;
|
||||
currencyCode: string;
|
||||
|
||||
creditAccountId: number;
|
||||
cashflowAccountId: number;
|
||||
|
||||
publish: boolean;
|
||||
branchId?: number;
|
||||
plaidTransactionId?: string;
|
||||
}
|
||||
|
||||
export interface ICashflowNewCommandDTO extends ICashflowCommandDTO {
|
||||
plaidAccountId?: string;
|
||||
uncategorizedTransactionId?: number;
|
||||
}
|
||||
|
||||
export enum CashflowDirection {
|
||||
IN = 'in',
|
||||
OUT = 'out',
|
||||
}
|
||||
|
||||
export interface ICommandCashflowCreatingPayload {
|
||||
trx: Knex.Transaction;
|
||||
newTransactionDTO: ICashflowNewCommandDTO;
|
||||
}
|
||||
|
||||
export interface ICommandCashflowCreatedPayload {
|
||||
newTransactionDTO: ICashflowNewCommandDTO;
|
||||
cashflowTransaction: BankTransaction;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface ICommandCashflowDeletingPayload {
|
||||
oldCashflowTransaction: BankTransaction;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface ICommandCashflowDeletedPayload {
|
||||
cashflowTransactionId: number;
|
||||
oldCashflowTransaction: BankTransaction;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface ICashflowTransactionCategorizedPayload {
|
||||
uncategorizedTransactions: Array<UncategorizedBankTransaction>;
|
||||
cashflowTransaction: BankTransaction;
|
||||
oldUncategorizedTransactions: Array<UncategorizedBankTransaction>;
|
||||
categorizeDTO: any;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface ICashflowTransactionUncategorizingPayload {
|
||||
uncategorizedTransactionId: number;
|
||||
oldUncategorizedTransactions: Array<UncategorizedBankTransaction>;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface ICashflowTransactionUncategorizedPayload {
|
||||
uncategorizedTransactionId: number;
|
||||
uncategorizedTransactions: Array<UncategorizedBankTransaction>;
|
||||
oldMainUncategorizedTransaction: UncategorizedBankTransaction;
|
||||
oldUncategorizedTransactions: Array<UncategorizedBankTransaction>;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export enum CashflowAction {
|
||||
Create = 'Create',
|
||||
Delete = 'Delete',
|
||||
View = 'View',
|
||||
}
|
||||
|
||||
export interface CategorizeTransactionAsExpenseDTO {
|
||||
expenseAccountId: number;
|
||||
exchangeRate: number;
|
||||
referenceNo: string;
|
||||
description: string;
|
||||
branchId?: number;
|
||||
}
|
||||
|
||||
export interface IGetUncategorizedTransactionsQuery {
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
minDate?: Date;
|
||||
maxDate?: Date;
|
||||
minAmount?: number;
|
||||
maxAmount?: number;
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
|
||||
import { ExcludedBankTransactionsQuery } from '../types/BankTransactionsExclude.types';
|
||||
import { UncategorizedTransactionTransformer } from '@/modules/BankingCategorize/commands/UncategorizedTransaction.transformer';
|
||||
import { UncategorizedBankTransaction } from '@/modules/BankingTransactions/models/UncategorizedBankTransaction';
|
||||
|
||||
@Injectable()
|
||||
export class GetExcludedBankTransactionsService {
|
||||
constructor(
|
||||
private readonly transformer: TransformerInjectable,
|
||||
|
||||
@Inject(UncategorizedBankTransaction.name)
|
||||
private readonly uncategorizedBankTransaction: typeof UncategorizedBankTransaction,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieves the excluded uncategorized bank transactions.
|
||||
* @param {ExcludedBankTransactionsQuery} filter
|
||||
* @returns
|
||||
*/
|
||||
public async getExcludedBankTransactions(
|
||||
filter: ExcludedBankTransactionsQuery,
|
||||
) {
|
||||
// Parsed query with default values.
|
||||
const _query = {
|
||||
page: 1,
|
||||
pageSize: 20,
|
||||
...filter,
|
||||
};
|
||||
const { results, pagination } = await this.uncategorizedBankTransaction
|
||||
.query()
|
||||
.onBuild((q) => {
|
||||
q.modify('excluded');
|
||||
q.orderBy('date', 'DESC');
|
||||
|
||||
if (_query.accountId) {
|
||||
q.where('account_id', _query.accountId);
|
||||
}
|
||||
if (_query.minDate) {
|
||||
q.modify('fromDate', _query.minDate);
|
||||
}
|
||||
if (_query.maxDate) {
|
||||
q.modify('toDate', _query.maxDate);
|
||||
}
|
||||
if (_query.minAmount) {
|
||||
q.modify('minAmount', _query.minAmount);
|
||||
}
|
||||
if (_query.maxAmount) {
|
||||
q.modify('maxAmount', _query.maxAmount);
|
||||
}
|
||||
})
|
||||
.pagination(_query.page - 1, _query.pageSize);
|
||||
|
||||
const data = await this.transformer.transform(
|
||||
results,
|
||||
new UncategorizedTransactionTransformer(),
|
||||
);
|
||||
return { data, pagination };
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import {
|
||||
IBankTransactionExcludedEventPayload,
|
||||
IBankTransactionUnexcludedEventPayload,
|
||||
} from '../types/BankTransactionsExclude.types';
|
||||
import { Account } from '@/modules/Accounts/models/Account.model';
|
||||
import { UncategorizedBankTransaction } from '@/modules/BankingTransactions/models/UncategorizedBankTransaction';
|
||||
import { events } from '@/common/events/events';
|
||||
|
||||
@Injectable()
|
||||
export class DecrementUncategorizedTransactionOnExclude {
|
||||
constructor(
|
||||
@Inject(Account.name)
|
||||
private readonly account: typeof Account,
|
||||
|
||||
@Inject(UncategorizedBankTransaction.name)
|
||||
private readonly uncategorizedBankTransaction: typeof UncategorizedBankTransaction,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Validates the cashflow transaction whether matched with bank transaction on deleting.
|
||||
* @param {IManualJournalDeletingPayload}
|
||||
*/
|
||||
@OnEvent(events.bankTransactions.onExcluded)
|
||||
public async decrementUnCategorizedTransactionsOnExclude({
|
||||
uncategorizedTransactionId,
|
||||
trx,
|
||||
}: IBankTransactionExcludedEventPayload) {
|
||||
const transaction = await this.uncategorizedBankTransaction.query(
|
||||
trx
|
||||
).findById(uncategorizedTransactionId);
|
||||
|
||||
await this.account.query(trx)
|
||||
.findById(transaction.accountId)
|
||||
.decrement('uncategorizedTransactions', 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the cashflow transaction whether matched with bank transaction on deleting.
|
||||
* @param {IManualJournalDeletingPayload}
|
||||
*/
|
||||
@OnEvent(events.bankTransactions.onUnexcluded)
|
||||
public async incrementUnCategorizedTransactionsOnUnexclude({
|
||||
uncategorizedTransactionId,
|
||||
trx,
|
||||
}: IBankTransactionUnexcludedEventPayload) {
|
||||
const transaction = await this.uncategorizedBankTransaction.query().findById(
|
||||
uncategorizedTransactionId
|
||||
);
|
||||
//
|
||||
await this.account.query(trx)
|
||||
.findById(transaction.accountId)
|
||||
.increment('uncategorizedTransactions', 1);
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Param,
|
||||
Post,
|
||||
Put,
|
||||
} from '@nestjs/common';
|
||||
import { BillPaymentsApplication } from './BillPaymentsApplication.service';
|
||||
import { IBillPaymentDTO } from './types/BillPayments.types';
|
||||
|
||||
@Controller('bill-payments')
|
||||
export class BillPaymentsController {
|
||||
constructor(private billPaymentsApplication: BillPaymentsApplication) {}
|
||||
|
||||
@Post()
|
||||
public createBillPayment(@Body() billPaymentDTO: IBillPaymentDTO) {
|
||||
return this.billPaymentsApplication.createBillPayment(billPaymentDTO);
|
||||
}
|
||||
|
||||
@Delete(':billPaymentId')
|
||||
public deleteBillPayment(@Param('billPaymentId') billPaymentId: string) {
|
||||
return this.billPaymentsApplication.deleteBillPayment(
|
||||
Number(billPaymentId),
|
||||
);
|
||||
}
|
||||
|
||||
@Put(':billPaymentId')
|
||||
public editBillPayment(
|
||||
@Param('billPaymentId') billPaymentId: string,
|
||||
@Body() billPaymentDTO: IBillPaymentDTO,
|
||||
) {
|
||||
return this.billPaymentsApplication.editBillPayment(
|
||||
Number(billPaymentId),
|
||||
billPaymentDTO,
|
||||
);
|
||||
}
|
||||
|
||||
@Get(':billPaymentId')
|
||||
public getBillPayment(@Param('billPaymentId') billPaymentId: string) {
|
||||
return this.billPaymentsApplication.getBillPayment(Number(billPaymentId));
|
||||
}
|
||||
|
||||
@Get(':billPaymentId/bills')
|
||||
public getPaymentBills(@Param('billPaymentId') billPaymentId: string) {
|
||||
return this.billPaymentsApplication.getPaymentBills(Number(billPaymentId));
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Knex } from 'knex';
|
||||
import { Bill } from '../../Bills/models/Bill';
|
||||
import { IBillPaymentEntryDTO } from '../types/BillPayments.types';
|
||||
import { entriesAmountDiff } from '@/utils/entries-amount-diff';
|
||||
import Objection from 'objection';
|
||||
|
||||
@Injectable()
|
||||
export class BillPaymentBillSync {
|
||||
constructor(
|
||||
@Inject(Bill.name)
|
||||
private readonly bill: typeof Bill
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Saves bills payment amount changes different.
|
||||
* @param {number} tenantId -
|
||||
* @param {IBillPaymentEntryDTO[]} paymentMadeEntries -
|
||||
* @param {IBillPaymentEntryDTO[]} oldPaymentMadeEntries -
|
||||
*/
|
||||
public async saveChangeBillsPaymentAmount(
|
||||
paymentMadeEntries: IBillPaymentEntryDTO[],
|
||||
oldPaymentMadeEntries?: IBillPaymentEntryDTO[],
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> {
|
||||
const opers: Objection.QueryBuilder<Bill, Bill[]>[] = [];
|
||||
|
||||
const diffEntries = entriesAmountDiff(
|
||||
paymentMadeEntries,
|
||||
oldPaymentMadeEntries,
|
||||
'paymentAmount',
|
||||
'billId',
|
||||
);
|
||||
diffEntries.forEach(
|
||||
(diffEntry: { paymentAmount: number; billId: number }) => {
|
||||
if (diffEntry.paymentAmount === 0) {
|
||||
return;
|
||||
}
|
||||
const oper = this.bill.changePaymentAmount(
|
||||
diffEntry.billId,
|
||||
diffEntry.paymentAmount,
|
||||
trx,
|
||||
);
|
||||
opers.push(oper);
|
||||
},
|
||||
);
|
||||
await Promise.all(opers);
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
// import { Inject, Service } from 'typedi';
|
||||
// import { Exportable } from '@/services/Export/Exportable';
|
||||
// import { BillPaymentsApplication } from './BillPaymentsApplication';
|
||||
// import { EXPORT_SIZE_LIMIT } from '@/services/Export/constants';
|
||||
|
||||
// @Service()
|
||||
// export class BillPaymentExportable extends Exportable {
|
||||
// @Inject()
|
||||
// private billPaymentsApplication: BillPaymentsApplication;
|
||||
|
||||
// /**
|
||||
// * Retrieves the accounts data to exportable sheet.
|
||||
// * @param {number} tenantId
|
||||
// * @returns
|
||||
// */
|
||||
// public exportable(tenantId: number, query: any) {
|
||||
// const filterQuery = (builder) => {
|
||||
// builder.withGraphFetched('entries.bill');
|
||||
// builder.withGraphFetched('branch');
|
||||
// };
|
||||
// const parsedQuery = {
|
||||
// sortOrder: 'desc',
|
||||
// columnSortBy: 'created_at',
|
||||
// ...query,
|
||||
// page: 1,
|
||||
// pageSize: EXPORT_SIZE_LIMIT,
|
||||
// filterQuery
|
||||
// } as any;
|
||||
|
||||
// return this.billPaymentsApplication
|
||||
// .getBillPayments(tenantId, parsedQuery)
|
||||
// .then((output) => output.billPayments);
|
||||
// }
|
||||
// }
|
||||
@@ -1,99 +0,0 @@
|
||||
import { Knex } from 'knex';
|
||||
import { BillPaymentGL } from './BillPaymentGL';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { LedgerStorageService } from '@/modules/Ledger/LedgerStorage.service';
|
||||
import { Account } from '@/modules/Accounts/models/Account.model';
|
||||
import { BillPayment } from '../models/BillPayment';
|
||||
import { AccountRepository } from '@/modules/Accounts/repositories/Account.repository';
|
||||
import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service';
|
||||
|
||||
@Injectable()
|
||||
export class BillPaymentGLEntries {
|
||||
constructor(
|
||||
private readonly ledgerStorage: LedgerStorageService,
|
||||
private readonly accountRepository: AccountRepository,
|
||||
private readonly tenancyContext: TenancyContext,
|
||||
|
||||
@Inject(BillPayment.name)
|
||||
private readonly billPaymentModel: typeof BillPayment,
|
||||
|
||||
@Inject(Account.name)
|
||||
private readonly accountModel: typeof Account,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Creates a bill payment GL entries.
|
||||
* @param {number} tenantId
|
||||
* @param {number} billPaymentId
|
||||
* @param {Knex.Transaction} trx
|
||||
*/
|
||||
public writePaymentGLEntries = async (
|
||||
billPaymentId: number,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> => {
|
||||
// Retrieves the bill payment details with associated entries.
|
||||
const payment = await this.billPaymentModel
|
||||
.query(trx)
|
||||
.findById(billPaymentId)
|
||||
.withGraphFetched('entries.bill');
|
||||
|
||||
// Retrieves the given tenant metadata.
|
||||
const tenantMeta = await this.tenancyContext.getTenantMetadata();
|
||||
|
||||
// Finds or creates a new A/P account of the given currency.
|
||||
const APAccount = await this.accountRepository.findOrCreateAccountsPayable(
|
||||
payment.currencyCode,
|
||||
{},
|
||||
trx,
|
||||
);
|
||||
// Exchange gain or loss account.
|
||||
const EXGainLossAccount = await this.accountModel
|
||||
.query(trx)
|
||||
.modify('findBySlug', 'exchange-grain-loss')
|
||||
.first();
|
||||
|
||||
// Retrieves the bill payment ledger.
|
||||
const ledger = new BillPaymentGL(payment)
|
||||
.setAPAccountId(APAccount.id)
|
||||
.setGainLossAccountId(EXGainLossAccount.id)
|
||||
.setBaseCurrency(tenantMeta.baseCurrency)
|
||||
.getBillPaymentLedger();
|
||||
|
||||
// Commits the ledger on the storage.
|
||||
await this.ledgerStorage.commit(ledger, trx);
|
||||
};
|
||||
|
||||
/**
|
||||
* Rewrites the bill payment GL entries.
|
||||
* @param {number} tenantId
|
||||
* @param {number} billPaymentId
|
||||
* @param {Knex.Transaction} trx
|
||||
*/
|
||||
public rewritePaymentGLEntries = async (
|
||||
billPaymentId: number,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> => {
|
||||
// Revert payment GL entries.
|
||||
await this.revertPaymentGLEntries(billPaymentId, trx);
|
||||
|
||||
// Write payment GL entries.
|
||||
await this.writePaymentGLEntries(billPaymentId, trx);
|
||||
};
|
||||
|
||||
/**
|
||||
* Reverts the bill payment GL entries.
|
||||
* @param {number} tenantId
|
||||
* @param {number} billPaymentId
|
||||
* @param {Knex.Transaction} trx
|
||||
*/
|
||||
public revertPaymentGLEntries = async (
|
||||
billPaymentId: number,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> => {
|
||||
await this.ledgerStorage.deleteByReference(
|
||||
billPaymentId,
|
||||
'BillPayment',
|
||||
trx,
|
||||
);
|
||||
};
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
// import { Inject, Service } from 'typedi';
|
||||
// import { Knex } from 'knex';
|
||||
// import { IBillPaymentDTO } from '@/interfaces';
|
||||
// import { CreateBillPayment } from './CreateBillPayment';
|
||||
// import { Importable } from '@/services/Import/Importable';
|
||||
// import { BillsPaymentsSampleData } from './constants';
|
||||
|
||||
// @Service()
|
||||
// export class BillPaymentsImportable extends Importable {
|
||||
// @Inject()
|
||||
// private createBillPaymentService: CreateBillPayment;
|
||||
|
||||
// /**
|
||||
// * Importing to account service.
|
||||
// * @param {number} tenantId
|
||||
// * @param {IAccountCreateDTO} createAccountDTO
|
||||
// * @returns
|
||||
// */
|
||||
// public importable(
|
||||
// tenantId: number,
|
||||
// billPaymentDTO: IBillPaymentDTO,
|
||||
// trx?: Knex.Transaction
|
||||
// ) {
|
||||
// return this.createBillPaymentService.createBillPayment(
|
||||
// tenantId,
|
||||
// billPaymentDTO,
|
||||
// trx
|
||||
// );
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Concurrrency controlling of the importing process.
|
||||
// * @returns {number}
|
||||
// */
|
||||
// public get concurrency() {
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Retrieves the sample data that used to download accounts sample sheet.
|
||||
// */
|
||||
// public sampleData(): any[] {
|
||||
// return BillsPaymentsSampleData;
|
||||
// }
|
||||
// }
|
||||
@@ -1,185 +0,0 @@
|
||||
import { Model, mixin } from 'objection';
|
||||
// import TenantModel from 'models/TenantModel';
|
||||
// import ModelSetting from './ModelSetting';
|
||||
// import BillPaymentSettings from './BillPayment.Settings';
|
||||
// import CustomViewBaseModel from './CustomViewBaseModel';
|
||||
// import { DEFAULT_VIEWS } from '@/services/Sales/PaymentReceived/constants';
|
||||
// import ModelSearchable from './ModelSearchable';
|
||||
import { BaseModel } from '@/models/Model';
|
||||
import { BillPaymentEntry } from './BillPaymentEntry';
|
||||
import { Vendor } from '@/modules/Vendors/models/Vendor';
|
||||
import { Document } from '@/modules/ChromiumlyTenancy/models/Document';
|
||||
|
||||
export class BillPayment extends BaseModel{
|
||||
vendorId: number;
|
||||
amount: number;
|
||||
currencyCode: string;
|
||||
paymentAccountId: number;
|
||||
paymentNumber: string;
|
||||
paymentDate: string;
|
||||
paymentMethod: string;
|
||||
reference: string;
|
||||
userId: number;
|
||||
statement: string;
|
||||
exchangeRate: number;
|
||||
|
||||
createdAt?: Date;
|
||||
updatedAt?: Date;
|
||||
|
||||
branchId?: number;
|
||||
|
||||
entries?: BillPaymentEntry[];
|
||||
vendor?: Vendor;
|
||||
attachments?: Document[];
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'bills_payments';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamps columns.
|
||||
*/
|
||||
get timestamps() {
|
||||
return ['createdAt', 'updatedAt'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Virtual attributes.
|
||||
*/
|
||||
static get virtualAttributes() {
|
||||
return ['localAmount'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Payment amount in local currency.
|
||||
* @returns {number}
|
||||
*/
|
||||
get localAmount() {
|
||||
return this.amount * this.exchangeRate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Model settings.
|
||||
*/
|
||||
// static get meta() {
|
||||
// return BillPaymentSettings;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
// static get relationMappings() {
|
||||
// const BillPaymentEntry = require('models/BillPaymentEntry');
|
||||
// const AccountTransaction = require('models/AccountTransaction');
|
||||
// const Vendor = require('models/Vendor');
|
||||
// const Account = require('models/Account');
|
||||
// const Branch = require('models/Branch');
|
||||
// const Document = require('models/Document');
|
||||
|
||||
// return {
|
||||
// entries: {
|
||||
// relation: Model.HasManyRelation,
|
||||
// modelClass: BillPaymentEntry.default,
|
||||
// join: {
|
||||
// from: 'bills_payments.id',
|
||||
// to: 'bills_payments_entries.billPaymentId',
|
||||
// },
|
||||
// filter: (query) => {
|
||||
// query.orderBy('index', 'ASC');
|
||||
// },
|
||||
// },
|
||||
|
||||
// vendor: {
|
||||
// relation: Model.BelongsToOneRelation,
|
||||
// modelClass: Vendor.default,
|
||||
// join: {
|
||||
// from: 'bills_payments.vendorId',
|
||||
// to: 'contacts.id',
|
||||
// },
|
||||
// filter(query) {
|
||||
// query.where('contact_service', 'vendor');
|
||||
// },
|
||||
// },
|
||||
|
||||
// paymentAccount: {
|
||||
// relation: Model.BelongsToOneRelation,
|
||||
// modelClass: Account.default,
|
||||
// join: {
|
||||
// from: 'bills_payments.paymentAccountId',
|
||||
// to: 'accounts.id',
|
||||
// },
|
||||
// },
|
||||
|
||||
// transactions: {
|
||||
// relation: Model.HasManyRelation,
|
||||
// modelClass: AccountTransaction.default,
|
||||
// join: {
|
||||
// from: 'bills_payments.id',
|
||||
// to: 'accounts_transactions.referenceId',
|
||||
// },
|
||||
// filter(builder) {
|
||||
// builder.where('reference_type', 'BillPayment');
|
||||
// },
|
||||
// },
|
||||
|
||||
// /**
|
||||
// * Bill payment may belongs to branch.
|
||||
// */
|
||||
// branch: {
|
||||
// relation: Model.BelongsToOneRelation,
|
||||
// modelClass: Branch.default,
|
||||
// join: {
|
||||
// from: 'bills_payments.branchId',
|
||||
// to: 'branches.id',
|
||||
// },
|
||||
// },
|
||||
|
||||
// /**
|
||||
// * Bill payment may has many attached attachments.
|
||||
// */
|
||||
// attachments: {
|
||||
// relation: Model.ManyToManyRelation,
|
||||
// modelClass: Document.default,
|
||||
// join: {
|
||||
// from: 'bills_payments.id',
|
||||
// through: {
|
||||
// from: 'document_links.modelId',
|
||||
// to: 'document_links.documentId',
|
||||
// },
|
||||
// to: 'documents.id',
|
||||
// },
|
||||
// filter(query) {
|
||||
// query.where('model_ref', 'BillPayment');
|
||||
// },
|
||||
// },
|
||||
// };
|
||||
// }
|
||||
|
||||
/**
|
||||
* Retrieve the default custom views, roles and columns.
|
||||
*/
|
||||
// static get defaultViews() {
|
||||
// return DEFAULT_VIEWS;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Model search attributes.
|
||||
*/
|
||||
static get searchRoles() {
|
||||
return [
|
||||
{ fieldKey: 'payment_number', comparator: 'contains' },
|
||||
{ condition: 'or', fieldKey: 'reference_no', comparator: 'contains' },
|
||||
{ condition: 'or', fieldKey: 'amount', comparator: 'equals' },
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Prevents mutate base currency since the model is not empty.
|
||||
*/
|
||||
static get preventMutateBaseCurrency() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Bill } from '@/modules/Bills/models/Bill';
|
||||
import { BillPayment } from '../models/BillPayment';
|
||||
|
||||
@Injectable()
|
||||
export class GetPaymentBills {
|
||||
/**
|
||||
* @param {typeof Bill} billModel
|
||||
* @param {typeof BillPayment} billPaymentModel
|
||||
*/
|
||||
constructor(
|
||||
@Inject(Bill.name)
|
||||
private readonly billModel: typeof Bill,
|
||||
|
||||
@Inject(BillPayment.name)
|
||||
private readonly billPaymentModel: typeof BillPayment,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieve payment made associated bills.
|
||||
* @param {number} billPaymentId - Bill payment id.
|
||||
*/
|
||||
public async getPaymentBills(billPaymentId: number) {
|
||||
const billPayment = await this.billPaymentModel
|
||||
.query()
|
||||
.findById(billPaymentId)
|
||||
.throwIfNotFound();
|
||||
|
||||
const paymentBillsIds = billPayment.entries.map((entry) => entry.id);
|
||||
|
||||
const bills = await this.billModel.query().whereIn('id', paymentBillsIds);
|
||||
|
||||
return bills;
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
import { Knex } from 'knex';
|
||||
import { BillPayment } from '../models/BillPayment';
|
||||
import { AttachmentLinkDTO } from '@/modules/Attachments/Attachments.types';
|
||||
|
||||
export interface IBillPaymentEntryDTO {
|
||||
billId: number;
|
||||
paymentAmount: number;
|
||||
}
|
||||
|
||||
export interface IBillPaymentDTO {
|
||||
vendorId: number;
|
||||
amount?: number;
|
||||
paymentAccountId: number;
|
||||
paymentNumber?: string;
|
||||
paymentDate: Date | string;
|
||||
exchangeRate?: number;
|
||||
statement?: string;
|
||||
reference?: string;
|
||||
entries: IBillPaymentEntryDTO[];
|
||||
branchId?: number;
|
||||
attachments?: AttachmentLinkDTO[];
|
||||
}
|
||||
|
||||
export interface IBillReceivePageEntry {
|
||||
billId: number;
|
||||
entryType: string;
|
||||
billNo: string;
|
||||
dueAmount: number;
|
||||
amount: number;
|
||||
totalPaymentAmount: number;
|
||||
paymentAmount: number;
|
||||
currencyCode: string;
|
||||
date: Date | string;
|
||||
}
|
||||
|
||||
export interface IBillPaymentEventCreatedPayload {
|
||||
billPayment: BillPayment;
|
||||
billPaymentDTO: IBillPaymentDTO;
|
||||
billPaymentId: number;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IBillPaymentCreatingPayload {
|
||||
billPaymentDTO: IBillPaymentDTO;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IBillPaymentEditingPayload {
|
||||
billPaymentDTO: IBillPaymentDTO;
|
||||
oldBillPayment: BillPayment;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
export interface IBillPaymentEventEditedPayload {
|
||||
billPaymentId: number;
|
||||
billPayment: BillPayment;
|
||||
oldBillPayment: BillPayment;
|
||||
billPaymentDTO: IBillPaymentDTO;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IBillPaymentEventDeletedPayload {
|
||||
billPaymentId: number;
|
||||
oldBillPayment: BillPayment;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IBillPaymentDeletingPayload {
|
||||
oldBillPayment: BillPayment;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IBillPaymentPublishingPayload {
|
||||
oldBillPayment: BillPayment;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export enum IPaymentMadeAction {
|
||||
Create = 'Create',
|
||||
Edit = 'Edit',
|
||||
Delete = 'Delete',
|
||||
View = 'View',
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
import {
|
||||
Controller,
|
||||
Post,
|
||||
Body,
|
||||
Put,
|
||||
Param,
|
||||
Delete,
|
||||
Get,
|
||||
Query,
|
||||
} from '@nestjs/common';
|
||||
import { BillsApplication } from './Bills.application';
|
||||
import { IBillDTO, IBillEditDTO } from './Bills.types';
|
||||
import { PublicRoute } from '../Auth/Jwt.guard';
|
||||
|
||||
@Controller('bills')
|
||||
@PublicRoute()
|
||||
export class BillsController {
|
||||
constructor(private billsApplication: BillsApplication) {}
|
||||
|
||||
@Post()
|
||||
createBill(@Body() billDTO: IBillDTO) {
|
||||
return this.billsApplication.createBill(billDTO);
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
editBill(@Param('id') billId: number, @Body() billDTO: IBillEditDTO) {
|
||||
return this.billsApplication.editBill(billId, billDTO);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
deleteBill(@Param('id') billId: number) {
|
||||
return this.billsApplication.deleteBill(billId);
|
||||
}
|
||||
|
||||
@Get()
|
||||
getBills(@Query() filterDTO: IBillsFilter) {
|
||||
return this.billsApplication.getBills(filterDTO);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
getBill(@Param('id') billId: number) {
|
||||
return this.billsApplication.getBill(billId);
|
||||
}
|
||||
|
||||
@Post(':id/open')
|
||||
openBill(@Param('id') billId: number) {
|
||||
return this.billsApplication.openBill(billId);
|
||||
}
|
||||
|
||||
@Get('due')
|
||||
getDueBills(@Body('vendorId') vendorId?: number) {
|
||||
return this.billsApplication.getDueBills(vendorId);
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
import { Knex } from 'knex';
|
||||
import { Bill } from '../models/Bill';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ItemsEntriesService } from '@/modules/Items/ItemsEntries.service';
|
||||
import { InventoryService } from '@/modules/InventoryCost/Inventory';
|
||||
|
||||
@Injectable()
|
||||
export class BillInventoryTransactions {
|
||||
constructor(
|
||||
private readonly itemsEntriesService: ItemsEntriesService,
|
||||
private readonly inventoryService: InventoryService,
|
||||
|
||||
private readonly bill: typeof Bill
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Records the inventory transactions from the given bill input.
|
||||
* @param {number} billId - Bill id.
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async recordInventoryTransactions(
|
||||
billId: number,
|
||||
override?: boolean,
|
||||
trx?: Knex.Transaction
|
||||
): Promise<void> {
|
||||
// Retireve bill with assocaited entries and allocated cost entries.
|
||||
|
||||
const bill = await this.bill.query(trx)
|
||||
.findById(billId)
|
||||
.withGraphFetched('entries.allocatedCostEntries');
|
||||
|
||||
// Loads the inventory items entries of the given sale invoice.
|
||||
const inventoryEntries =
|
||||
await this.itemsEntriesService.filterInventoryEntries(
|
||||
bill.entries
|
||||
);
|
||||
const transaction = {
|
||||
transactionId: bill.id,
|
||||
transactionType: 'Bill',
|
||||
exchangeRate: bill.exchangeRate,
|
||||
|
||||
date: bill.billDate,
|
||||
direction: 'IN',
|
||||
entries: inventoryEntries,
|
||||
createdAt: bill.createdAt,
|
||||
|
||||
warehouseId: bill.warehouseId,
|
||||
};
|
||||
await this.inventoryService.recordInventoryTransactionsFromItemsEntries(
|
||||
transaction,
|
||||
override,
|
||||
trx
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverts the inventory transactions of the given bill id.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {number} billId - Bill id.
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async revertInventoryTransactions(
|
||||
billId: number,
|
||||
trx?: Knex.Transaction
|
||||
) {
|
||||
// Deletes the inventory transactions by the given reference id and type.
|
||||
await this.inventoryService.deleteInventoryTransactions(
|
||||
billId,
|
||||
'Bill',
|
||||
trx
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
// import { Inject, Service } from 'typedi';
|
||||
// import { Knex } from 'knex';
|
||||
// import { IBillsFilter } from '@/interfaces';
|
||||
// import { Exportable } from '@/services/Export/Exportable';
|
||||
// import { BillsApplication } from '../Bills.application';
|
||||
// import { EXPORT_SIZE_LIMIT } from '@/services/Export/constants';
|
||||
// import Objection from 'objection';
|
||||
|
||||
// @Service()
|
||||
// export class BillsExportable extends Exportable {
|
||||
// @Inject()
|
||||
// private billsApplication: BillsApplication;
|
||||
|
||||
// /**
|
||||
// * Retrieves the accounts data to exportable sheet.
|
||||
// * @param {number} tenantId
|
||||
// * @returns
|
||||
// */
|
||||
// public exportable(tenantId: number, query: IBillsFilter) {
|
||||
// const filterQuery = (query) => {
|
||||
// query.withGraphFetched('branch');
|
||||
// query.withGraphFetched('warehouse');
|
||||
// };
|
||||
// const parsedQuery = {
|
||||
// sortOrder: 'desc',
|
||||
// columnSortBy: 'created_at',
|
||||
// ...query,
|
||||
// page: 1,
|
||||
// pageSize: EXPORT_SIZE_LIMIT,
|
||||
// filterQuery,
|
||||
// } as IBillsFilter;
|
||||
|
||||
// return this.billsApplication
|
||||
// .getBills(tenantId, parsedQuery)
|
||||
// .then((output) => output.bills);
|
||||
// }
|
||||
// }
|
||||
@@ -1,46 +0,0 @@
|
||||
// import { Inject, Service } from 'typedi';
|
||||
// import { Knex } from 'knex';
|
||||
// import { Importable } from '@/services/Import/Importable';
|
||||
// import { CreateBill } from './CreateBill.service';
|
||||
// import { IBillDTO } from '@/interfaces';
|
||||
// import { BillsSampleData } from '../Bills.constants';
|
||||
|
||||
// @Service()
|
||||
// export class BillsImportable extends Importable {
|
||||
// @Inject()
|
||||
// private createBillService: CreateBill;
|
||||
|
||||
// /**
|
||||
// * Importing to account service.
|
||||
// * @param {number} tenantId
|
||||
// * @param {IAccountCreateDTO} createAccountDTO
|
||||
// * @returns
|
||||
// */
|
||||
// public importable(
|
||||
// tenantId: number,
|
||||
// createAccountDTO: IBillDTO,
|
||||
// trx?: Knex.Transaction
|
||||
// ) {
|
||||
// return this.createBillService.createBill(
|
||||
// tenantId,
|
||||
// createAccountDTO,
|
||||
// {},
|
||||
// trx
|
||||
// );
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Concurrrency controlling of the importing process.
|
||||
// * @returns {number}
|
||||
// */
|
||||
// public get concurrency() {
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Retrieves the sample data that used to download accounts sample sheet.
|
||||
// */
|
||||
// public sampleData(): any[] {
|
||||
// return BillsSampleData;
|
||||
// }
|
||||
// }
|
||||
@@ -1,656 +0,0 @@
|
||||
import { Model, raw, mixin } from 'objection';
|
||||
import { castArray, difference, defaultTo } from 'lodash';
|
||||
import * as moment from 'moment';
|
||||
import * as R from 'ramda';
|
||||
// import TenantModel from 'models/TenantModel';
|
||||
// import BillSettings from './Bill.Settings';
|
||||
// import ModelSetting from './ModelSetting';
|
||||
// import CustomViewBaseModel from './CustomViewBaseModel';
|
||||
// import { DEFAULT_VIEWS } from '@/services/Purchases/Bills/constants';
|
||||
// import ModelSearchable from './ModelSearchable';
|
||||
import { BaseModel, PaginationQueryBuilderType } from '@/models/Model';
|
||||
import { ItemEntry } from '@/modules/TransactionItemEntry/models/ItemEntry';
|
||||
import { BillLandedCost } from '@/modules/BillLandedCosts/models/BillLandedCost';
|
||||
import { DiscountType } from '@/common/types/Discount';
|
||||
import type { Knex, QueryBuilder } from 'knex';
|
||||
|
||||
export class Bill extends BaseModel {
|
||||
public amount: number;
|
||||
public paymentAmount: number;
|
||||
public landedCostAmount: number;
|
||||
public allocatedCostAmount: number;
|
||||
public isInclusiveTax: boolean;
|
||||
public taxAmountWithheld: number;
|
||||
public exchangeRate: number;
|
||||
public vendorId: number;
|
||||
public billNumber: string;
|
||||
public billDate: Date;
|
||||
public dueDate: Date;
|
||||
public referenceNo: string;
|
||||
public status: string;
|
||||
public note: string;
|
||||
public currencyCode: string;
|
||||
public creditedAmount: number;
|
||||
public invLotNumber: string;
|
||||
public invoicedAmount: number;
|
||||
public openedAt: Date | string;
|
||||
public userId: number;
|
||||
|
||||
public discountType: DiscountType;
|
||||
public discount: number;
|
||||
public adjustment: number;
|
||||
|
||||
public branchId: number;
|
||||
public warehouseId: number;
|
||||
public projectId: number;
|
||||
|
||||
public createdAt: Date;
|
||||
public updatedAt: Date | null;
|
||||
|
||||
public entries?: ItemEntry[];
|
||||
public locatedLandedCosts?: BillLandedCost[];
|
||||
/**
|
||||
* Timestamps columns.
|
||||
*/
|
||||
get timestamps() {
|
||||
return ['createdAt', 'updatedAt'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Virtual attributes.
|
||||
*/
|
||||
static get virtualAttributes() {
|
||||
return [
|
||||
'balance',
|
||||
'dueAmount',
|
||||
'isOpen',
|
||||
'isPartiallyPaid',
|
||||
'isFullyPaid',
|
||||
'isPaid',
|
||||
'remainingDays',
|
||||
'overdueDays',
|
||||
'isOverdue',
|
||||
'unallocatedCostAmount',
|
||||
'localAmount',
|
||||
'localAllocatedCostAmount',
|
||||
'billableAmount',
|
||||
'amountLocal',
|
||||
|
||||
'discountAmount',
|
||||
'discountAmountLocal',
|
||||
'discountPercentage',
|
||||
|
||||
'adjustmentLocal',
|
||||
|
||||
'subtotal',
|
||||
'subtotalLocal',
|
||||
'subtotalExludingTax',
|
||||
'taxAmountWithheldLocal',
|
||||
'total',
|
||||
'totalLocal',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoice amount in base currency.
|
||||
* @returns {number}
|
||||
*/
|
||||
get amountLocal() {
|
||||
return this.amount * this.exchangeRate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtotal. (Tax inclusive) if the tax inclusive is enabled.
|
||||
* @returns {number}
|
||||
*/
|
||||
get subtotal() {
|
||||
return this.amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtotal in base currency. (Tax inclusive) if the tax inclusive is enabled.
|
||||
* @returns {number}
|
||||
*/
|
||||
get subtotalLocal() {
|
||||
return this.amountLocal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sale invoice amount excluding tax.
|
||||
* @returns {number}
|
||||
*/
|
||||
get subtotalExcludingTax() {
|
||||
return this.isInclusiveTax
|
||||
? this.subtotal - this.taxAmountWithheld
|
||||
: this.subtotal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tax amount withheld in base currency.
|
||||
* @returns {number}
|
||||
*/
|
||||
get taxAmountWithheldLocal() {
|
||||
return this.taxAmountWithheld * this.exchangeRate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Discount amount.
|
||||
* @returns {number}
|
||||
*/
|
||||
get discountAmount() {
|
||||
return this.discountType === DiscountType.Amount
|
||||
? this.discount
|
||||
: this.subtotal * (this.discount / 100);
|
||||
}
|
||||
|
||||
/**
|
||||
* Discount amount in local currency.
|
||||
* @returns {number | null}
|
||||
*/
|
||||
get discountAmountLocal() {
|
||||
return this.discountAmount ? this.discountAmount * this.exchangeRate : null;
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Discount percentage.
|
||||
* @returns {number | null}
|
||||
*/
|
||||
get discountPercentage(): number | null {
|
||||
return this.discountType === DiscountType.Percentage ? this.discount : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjustment amount in local currency.
|
||||
* @returns {number | null}
|
||||
*/
|
||||
get adjustmentLocal() {
|
||||
return this.adjustment ? this.adjustment * this.exchangeRate : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoice total. (Tax included)
|
||||
* @returns {number}
|
||||
*/
|
||||
get total() {
|
||||
const adjustmentAmount = defaultTo(this.adjustment, 0);
|
||||
|
||||
return R.compose(
|
||||
R.add(adjustmentAmount),
|
||||
R.subtract(R.__, this.discountAmount),
|
||||
R.when(R.always(this.isInclusiveTax), R.add(this.taxAmountWithheld)),
|
||||
)(this.subtotal);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoice total in local currency. (Tax included)
|
||||
* @returns {number}
|
||||
*/
|
||||
get totalLocal() {
|
||||
return this.total * this.exchangeRate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'bills';
|
||||
}
|
||||
|
||||
/**
|
||||
* Model modifiers.
|
||||
*/
|
||||
static get modifiers() {
|
||||
return {
|
||||
/**
|
||||
* Filters the bills in draft status.
|
||||
*/
|
||||
draft(query) {
|
||||
query.where('opened_at', null);
|
||||
},
|
||||
|
||||
/**
|
||||
* Filters the opened bills.
|
||||
*/
|
||||
published(query) {
|
||||
query.whereNot('openedAt', null);
|
||||
},
|
||||
|
||||
/**
|
||||
* Filters the opened bills.
|
||||
*/
|
||||
opened(query) {
|
||||
query.whereNot('opened_at', null);
|
||||
},
|
||||
/**
|
||||
* Filters the unpaid bills.
|
||||
*/
|
||||
unpaid(query) {
|
||||
query.where('payment_amount', 0);
|
||||
},
|
||||
/**
|
||||
* Filters the due bills.
|
||||
*/
|
||||
dueBills(query) {
|
||||
query.where(
|
||||
raw(`COALESCE(AMOUNT, 0) -
|
||||
COALESCE(PAYMENT_AMOUNT, 0) -
|
||||
COALESCE(CREDITED_AMOUNT, 0) > 0
|
||||
`),
|
||||
);
|
||||
},
|
||||
/**
|
||||
* Filters the overdue bills.
|
||||
*/
|
||||
overdue(query) {
|
||||
query.where('due_date', '<', moment().format('YYYY-MM-DD'));
|
||||
},
|
||||
/**
|
||||
* Filters the not overdue invoices.
|
||||
*/
|
||||
notOverdue(query, asDate = moment().format('YYYY-MM-DD')) {
|
||||
query.where('due_date', '>=', asDate);
|
||||
},
|
||||
/**
|
||||
* Filters the partially paid bills.
|
||||
*/
|
||||
partiallyPaid(query) {
|
||||
query.whereNot('payment_amount', 0);
|
||||
query.whereNot(raw('`PAYMENT_AMOUNT` = `AMOUNT`'));
|
||||
},
|
||||
/**
|
||||
* Filters the paid bills.
|
||||
*/
|
||||
paid(query) {
|
||||
query.where(raw('`PAYMENT_AMOUNT` = `AMOUNT`'));
|
||||
},
|
||||
/**
|
||||
* Filters the bills from the given date.
|
||||
*/
|
||||
fromDate(query, fromDate) {
|
||||
query.where('bill_date', '<=', fromDate);
|
||||
},
|
||||
|
||||
/**
|
||||
* Sort the bills by full-payment bills.
|
||||
*/
|
||||
sortByStatus(query, order) {
|
||||
query.orderByRaw(`PAYMENT_AMOUNT = AMOUNT ${order}`);
|
||||
},
|
||||
|
||||
/**
|
||||
* Status filter.
|
||||
*/
|
||||
statusFilter(query, filterType) {
|
||||
switch (filterType) {
|
||||
case 'draft':
|
||||
query.modify('draft');
|
||||
break;
|
||||
case 'delivered':
|
||||
query.modify('delivered');
|
||||
break;
|
||||
case 'unpaid':
|
||||
query.modify('unpaid');
|
||||
break;
|
||||
case 'overdue':
|
||||
default:
|
||||
query.modify('overdue');
|
||||
break;
|
||||
case 'partially-paid':
|
||||
query.modify('partiallyPaid');
|
||||
break;
|
||||
case 'paid':
|
||||
query.modify('paid');
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Filters by branches.
|
||||
*/
|
||||
filterByBranches(query, branchesIds) {
|
||||
const formattedBranchesIds = castArray(branchesIds);
|
||||
|
||||
query.whereIn('branchId', formattedBranchesIds);
|
||||
},
|
||||
|
||||
dueBillsFromDate(query, asDate = moment().format('YYYY-MM-DD')) {
|
||||
query.modify('dueBills');
|
||||
query.modify('notOverdue');
|
||||
query.modify('fromDate', asDate);
|
||||
},
|
||||
|
||||
overdueBillsFromDate(query, asDate = moment().format('YYYY-MM-DD')) {
|
||||
query.modify('dueBills');
|
||||
query.modify('overdue', asDate);
|
||||
query.modify('fromDate', asDate);
|
||||
},
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
billable(query) {
|
||||
query.where(raw('AMOUNT > INVOICED_AMOUNT'));
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoice amount in organization base currency.
|
||||
* @deprecated
|
||||
* @returns {number}
|
||||
*/
|
||||
get localAmount() {
|
||||
return this.amountLocal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the local allocated cost amount.
|
||||
* @returns {number}
|
||||
*/
|
||||
get localAllocatedCostAmount() {
|
||||
return this.allocatedCostAmount * this.exchangeRate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the local landed cost amount.
|
||||
* @returns {number}
|
||||
*/
|
||||
get localLandedCostAmount() {
|
||||
return this.landedCostAmount * this.exchangeRate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the local unallocated cost amount.
|
||||
* @returns {number}
|
||||
*/
|
||||
get localUnallocatedCostAmount() {
|
||||
return this.unallocatedCostAmount * this.exchangeRate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the balance of bill.
|
||||
* @return {number}
|
||||
*/
|
||||
get balance() {
|
||||
return this.paymentAmount + this.creditedAmount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Due amount of the given.
|
||||
* @return {number}
|
||||
*/
|
||||
get dueAmount() {
|
||||
return Math.max(this.total - this.balance, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Detarmine whether the bill is open.
|
||||
* @return {boolean}
|
||||
*/
|
||||
get isOpen() {
|
||||
return !!this.openedAt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deetarmine whether the bill paid partially.
|
||||
* @return {boolean}
|
||||
*/
|
||||
get isPartiallyPaid() {
|
||||
return this.dueAmount !== this.total && this.dueAmount > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deetarmine whether the bill paid fully.
|
||||
* @return {boolean}
|
||||
*/
|
||||
get isFullyPaid() {
|
||||
return this.dueAmount === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detarmines whether the bill paid fully or partially.
|
||||
* @return {boolean}
|
||||
*/
|
||||
get isPaid() {
|
||||
return this.isPartiallyPaid || this.isFullyPaid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the remaining days in number
|
||||
* @return {number|null}
|
||||
*/
|
||||
get remainingDays() {
|
||||
const currentMoment = moment();
|
||||
const dueDateMoment = moment(this.dueDate);
|
||||
|
||||
return Math.max(dueDateMoment.diff(currentMoment, 'days'), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the overdue days in number.
|
||||
* @return {number|null}
|
||||
*/
|
||||
get overdueDays() {
|
||||
const currentMoment = moment();
|
||||
const dueDateMoment = moment(this.dueDate);
|
||||
|
||||
return Math.max(currentMoment.diff(dueDateMoment, 'days'), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Detarmines the due date is over.
|
||||
* @return {boolean}
|
||||
*/
|
||||
get isOverdue() {
|
||||
return this.overdueDays > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the unallocated cost amount.
|
||||
* @return {number}
|
||||
*/
|
||||
get unallocatedCostAmount() {
|
||||
return Math.max(this.landedCostAmount - this.allocatedCostAmount, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the calculated amount which have not been invoiced.
|
||||
*/
|
||||
get billableAmount() {
|
||||
return Math.max(this.total - this.invoicedAmount, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bill model settings.
|
||||
*/
|
||||
// static get meta() {
|
||||
// return BillSettings;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const { Vendor } = require('../../Vendors/models/Vendor');
|
||||
const {
|
||||
ItemEntry,
|
||||
} = require('../../TransactionItemEntry/models/ItemEntry');
|
||||
const {
|
||||
BillLandedCost,
|
||||
} = require('../../BillLandedCosts/models/BillLandedCost');
|
||||
const { Branch } = require('../../Branches/models/Branch.model');
|
||||
const { Warehouse } = require('../../Warehouses/models/Warehouse.model');
|
||||
const { TaxRateModel } = require('../../TaxRates/models/TaxRate.model');
|
||||
const { TaxRateTransaction } = require('../../TaxRates/models/TaxRateTransaction.model');
|
||||
const { Document } = require('../../ChromiumlyTenancy/models/Document');
|
||||
// const { MatchedBankTransaction } = require('models/MatchedBankTransaction');
|
||||
|
||||
return {
|
||||
vendor: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: Vendor,
|
||||
join: {
|
||||
from: 'bills.vendorId',
|
||||
to: 'contacts.id',
|
||||
},
|
||||
filter(query) {
|
||||
query.where('contact_service', 'vendor');
|
||||
},
|
||||
},
|
||||
|
||||
entries: {
|
||||
relation: Model.HasManyRelation,
|
||||
modelClass: ItemEntry,
|
||||
join: {
|
||||
from: 'bills.id',
|
||||
to: 'items_entries.referenceId',
|
||||
},
|
||||
filter(builder) {
|
||||
builder.where('reference_type', 'Bill');
|
||||
builder.orderBy('index', 'ASC');
|
||||
},
|
||||
},
|
||||
|
||||
locatedLandedCosts: {
|
||||
relation: Model.HasManyRelation,
|
||||
modelClass: BillLandedCost,
|
||||
join: {
|
||||
from: 'bills.id',
|
||||
to: 'bill_located_costs.billId',
|
||||
},
|
||||
},
|
||||
|
||||
/**
|
||||
* Bill may belongs to associated branch.
|
||||
*/
|
||||
branch: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: Branch,
|
||||
join: {
|
||||
from: 'bills.branchId',
|
||||
to: 'branches.id',
|
||||
},
|
||||
},
|
||||
|
||||
/**
|
||||
* Bill may has associated warehouse.
|
||||
*/
|
||||
warehouse: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: Warehouse,
|
||||
join: {
|
||||
from: 'bills.warehouseId',
|
||||
to: 'warehouses.id',
|
||||
},
|
||||
},
|
||||
|
||||
/**
|
||||
* Bill may has associated tax rate transactions.
|
||||
*/
|
||||
taxes: {
|
||||
relation: Model.HasManyRelation,
|
||||
modelClass: TaxRateTransaction,
|
||||
join: {
|
||||
from: 'bills.id',
|
||||
to: 'tax_rate_transactions.referenceId',
|
||||
},
|
||||
filter(builder) {
|
||||
builder.where('reference_type', 'Bill');
|
||||
},
|
||||
},
|
||||
|
||||
/**
|
||||
* Bill may has many attached attachments.
|
||||
*/
|
||||
attachments: {
|
||||
relation: Model.ManyToManyRelation,
|
||||
modelClass: Document,
|
||||
join: {
|
||||
from: 'bills.id',
|
||||
through: {
|
||||
from: 'document_links.modelId',
|
||||
to: 'document_links.documentId',
|
||||
},
|
||||
to: 'documents.id',
|
||||
},
|
||||
filter(query) {
|
||||
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');
|
||||
// },
|
||||
// },
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the not found bills ids as array that associated to the given vendor.
|
||||
* @param {Array} billsIds
|
||||
* @param {number} vendorId -
|
||||
* @return {Array}
|
||||
*/
|
||||
static async getNotFoundBills(billsIds, vendorId) {
|
||||
const storedBills = await this.query().onBuild((builder) => {
|
||||
builder.whereIn('id', billsIds);
|
||||
|
||||
if (vendorId) {
|
||||
builder.where('vendor_id', vendorId);
|
||||
}
|
||||
});
|
||||
|
||||
const storedBillsIds = storedBills.map((t) => t.id);
|
||||
|
||||
const notFoundBillsIds = difference(billsIds, storedBillsIds);
|
||||
return notFoundBillsIds;
|
||||
}
|
||||
|
||||
static changePaymentAmount(
|
||||
billId: number,
|
||||
amount: number,
|
||||
trx: Knex.Transaction,
|
||||
) {
|
||||
const changeMethod = amount > 0 ? 'increment' : 'decrement';
|
||||
|
||||
return this.query(trx)
|
||||
.where('id', billId)
|
||||
[changeMethod]('payment_amount', Math.abs(amount));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the default custom views, roles and columns.
|
||||
*/
|
||||
// static get defaultViews() {
|
||||
// return DEFAULT_VIEWS;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Model search attributes.
|
||||
*/
|
||||
static get searchRoles() {
|
||||
return [
|
||||
{ fieldKey: 'bill_number', comparator: 'contains' },
|
||||
{ condition: 'or', fieldKey: 'reference_no', comparator: 'contains' },
|
||||
{ condition: 'or', fieldKey: 'amount', comparator: 'equals' },
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Prevents mutate base currency since the model is not empty.
|
||||
*/
|
||||
static get preventMutateBaseCurrency() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { BillsValidators } from '../commands/BillsValidators.service';
|
||||
import { BillTransformer } from './Bill.transformer';
|
||||
import { Bill } from '../models/Bill';
|
||||
import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
|
||||
|
||||
@Injectable()
|
||||
export class GetBill {
|
||||
constructor(
|
||||
@Inject(Bill.name) private billModel: typeof Bill,
|
||||
|
||||
private transformer: TransformerInjectable,
|
||||
private validators: BillsValidators,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieve the given bill details with associated items entries.
|
||||
* @param {Integer} billId - Specific bill.
|
||||
* @returns {Promise<IBill>}
|
||||
*/
|
||||
public async getBill(billId: number): Promise<Bill> {
|
||||
const bill = await this.billModel
|
||||
.query()
|
||||
.findById(billId)
|
||||
.withGraphFetched('vendor')
|
||||
.withGraphFetched('entries.item')
|
||||
.withGraphFetched('branch')
|
||||
.withGraphFetched('taxes.taxRate')
|
||||
.withGraphFetched('attachments');
|
||||
|
||||
// Validates the bill existence.
|
||||
this.validators.validateBillExistance(bill);
|
||||
|
||||
return this.transformer.transform(
|
||||
bill,
|
||||
new BillTransformer(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { BillPaymentEntry } from '@/modules/BillPayments/models/BillPaymentEntry';
|
||||
import { BillPaymentTransactionTransformer } from '@/modules/BillPayments/queries/BillPaymentTransactionTransformer';
|
||||
import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
|
||||
|
||||
@Injectable()
|
||||
export class GetBillPayments {
|
||||
constructor(
|
||||
@Inject(BillPaymentEntry.name)
|
||||
private billPaymentEntryModel: typeof BillPaymentEntry,
|
||||
private transformer: TransformerInjectable,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieve the specific bill associated payment transactions.
|
||||
* @param {number} billId
|
||||
* @returns {}
|
||||
*/
|
||||
public getBillPayments = async (billId: number) => {
|
||||
const billsEntries = await this.billPaymentEntryModel
|
||||
.query()
|
||||
.where('billId', billId)
|
||||
.withGraphJoined('payment.paymentAccount')
|
||||
.withGraphJoined('bill')
|
||||
.orderBy('payment:paymentDate', 'ASC');
|
||||
|
||||
return this.transformer.transform(
|
||||
billsEntries,
|
||||
new BillPaymentTransactionTransformer(),
|
||||
);
|
||||
};
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import * as R from 'ramda';
|
||||
import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
|
||||
import { DynamicListService } from '@/modules/DynamicListing/DynamicList.service';
|
||||
import { Bill } from '../models/Bill';
|
||||
import { IFilterMeta, IPaginationMeta } from '@/interfaces/Model';
|
||||
import { BillTransformer } from './Bill.transformer';
|
||||
import { IBillsFilter } from '../Bills.types';
|
||||
|
||||
@Injectable()
|
||||
export class GetBillsService {
|
||||
constructor(
|
||||
private transformer: TransformerInjectable,
|
||||
private dynamicListService: DynamicListService,
|
||||
|
||||
@Inject(Bill.name) private billModel: typeof Bill,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieve bills data table list.
|
||||
* @param {IBillsFilter} billsFilter -
|
||||
*/
|
||||
public async getBills(filterDTO: IBillsFilter): Promise<{
|
||||
bills: Bill;
|
||||
pagination: IPaginationMeta;
|
||||
filterMeta: IFilterMeta;
|
||||
}> {
|
||||
// Parses bills list filter DTO.
|
||||
const filter = this.parseListFilterDTO(filterDTO);
|
||||
|
||||
// Dynamic list service.
|
||||
const dynamicFilter = await this.dynamicListService.dynamicList(
|
||||
Bill,
|
||||
filter,
|
||||
);
|
||||
const { results, pagination } = await this.billModel
|
||||
.query()
|
||||
.onBuild((builder) => {
|
||||
builder.withGraphFetched('vendor');
|
||||
builder.withGraphFetched('entries.item');
|
||||
dynamicFilter.buildQuery()(builder);
|
||||
|
||||
// Filter query.
|
||||
filterDTO?.filterQuery && filterDTO?.filterQuery(builder);
|
||||
})
|
||||
.pagination(filter.page - 1, filter.pageSize);
|
||||
|
||||
// Tranform the bills to POJO.
|
||||
const bills = await this.transformer.transform(
|
||||
results,
|
||||
new BillTransformer(),
|
||||
);
|
||||
return {
|
||||
bills,
|
||||
pagination,
|
||||
filterMeta: dynamicFilter.getResponseMeta(),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses bills list filter DTO.
|
||||
* @param filterDTO -
|
||||
*/
|
||||
private parseListFilterDTO(filterDTO) {
|
||||
return R.compose(this.dynamicListService.parseStringifiedFilter)(filterDTO);
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Bill } from '../models/Bill';
|
||||
|
||||
@Injectable()
|
||||
export class GetDueBills {
|
||||
constructor(
|
||||
@Inject(Bill.name)
|
||||
private billModel: typeof Bill,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieve all due bills or for specific given vendor id.
|
||||
* @param {number} vendorId -
|
||||
*/
|
||||
public async getDueBills(vendorId?: number): Promise<Bill[]> {
|
||||
const dueBills = await this.billModel.query().onBuild((query) => {
|
||||
query.orderBy('bill_date', 'DESC');
|
||||
query.modify('dueBills');
|
||||
|
||||
if (vendorId) {
|
||||
query.where('vendor_id', vendorId);
|
||||
}
|
||||
});
|
||||
return dueBills;
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Put,
|
||||
Delete,
|
||||
Body,
|
||||
Param,
|
||||
} from '@nestjs/common';
|
||||
import { BranchesApplication } from './BranchesApplication.service';
|
||||
import { ICreateBranchDTO, IEditBranchDTO } from './Branches.types';
|
||||
import { PublicRoute } from '../Auth/Jwt.guard';
|
||||
|
||||
@Controller('branches')
|
||||
@PublicRoute()
|
||||
export class BranchesController {
|
||||
constructor(private readonly branchesApplication: BranchesApplication) {}
|
||||
|
||||
@Get()
|
||||
getBranches() {
|
||||
// return this.branchesApplication.getBranches();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
getBranch(@Param('id') id: string) {
|
||||
return this.branchesApplication.getBranch(Number(id));
|
||||
}
|
||||
|
||||
@Post()
|
||||
createBranch(@Body() createBranchDTO: ICreateBranchDTO) {
|
||||
return this.branchesApplication.createBranch(createBranchDTO);
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
editBranch(@Param('id') id: string, @Body() editBranchDTO: IEditBranchDTO) {
|
||||
return this.branchesApplication.editBranch(Number(id), editBranchDTO);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
deleteBranch(@Param('id') id: string) {
|
||||
return this.branchesApplication.deleteBranch(Number(id));
|
||||
}
|
||||
|
||||
@Post('activate')
|
||||
activateBranches() {
|
||||
return this.branchesApplication.activateBranches();
|
||||
}
|
||||
|
||||
@Put(':id/mark-as-primary')
|
||||
markBranchAsPrimary(@Param('id') id: string) {
|
||||
return this.branchesApplication.markBranchAsPrimary(Number(id));
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TenancyContext } from '../Tenancy/TenancyContext.service';
|
||||
import { TenancyDatabaseModule } from '../Tenancy/TenancyDB/TenancyDB.module';
|
||||
import { TransformerInjectable } from '../Transformer/TransformerInjectable.service';
|
||||
import { BranchesController } from './Branches.controller';
|
||||
import { CreateBranchService } from './commands/CreateBranch.service';
|
||||
import { DeleteBranchService } from './commands/DeleteBranch.service';
|
||||
import { EditBranchService } from './commands/EditBranch.service';
|
||||
import { MarkBranchAsPrimaryService } from './commands/MarkBranchAsPrimary.service';
|
||||
import { GetBranchService } from './queries/GetBranch.service';
|
||||
import { GetBranchesService } from './queries/GetBranches.service';
|
||||
import { ActivateBranches } from './commands/ActivateBranchesFeature.service';
|
||||
import { BranchesApplication } from './BranchesApplication.service';
|
||||
import { BranchesSettingsService } from './BranchesSettings';
|
||||
import { BranchCommandValidator } from './commands/BranchCommandValidator.service';
|
||||
import { BranchTransactionDTOTransformer } from './integrations/BranchTransactionDTOTransform';
|
||||
import { ManualJournalBranchesDTOTransformer } from './integrations/ManualJournals/ManualJournalDTOTransformer.service';
|
||||
|
||||
@Module({
|
||||
imports: [TenancyDatabaseModule],
|
||||
controllers: [BranchesController],
|
||||
providers: [
|
||||
CreateBranchService,
|
||||
EditBranchService,
|
||||
DeleteBranchService,
|
||||
GetBranchService,
|
||||
GetBranchesService,
|
||||
MarkBranchAsPrimaryService,
|
||||
ActivateBranches,
|
||||
BranchesApplication,
|
||||
BranchesSettingsService,
|
||||
TenancyContext,
|
||||
TransformerInjectable,
|
||||
BranchCommandValidator,
|
||||
BranchTransactionDTOTransformer,
|
||||
ManualJournalBranchesDTOTransformer,
|
||||
],
|
||||
exports: [
|
||||
BranchesSettingsService,
|
||||
BranchTransactionDTOTransformer,
|
||||
ManualJournalBranchesDTOTransformer,
|
||||
],
|
||||
})
|
||||
export class BranchesModule {}
|
||||
@@ -1,23 +0,0 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class BranchesSettingsService {
|
||||
/**
|
||||
* Marks multi-branches as activated.
|
||||
*/
|
||||
public markMultiBranchesAsActivated = () => {
|
||||
// const settings = this.tenancy.settings(tenantId);
|
||||
|
||||
// settings.set({ group: 'features', key: Features.BRANCHES, value: 1 });
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves whether multi-branches is active.
|
||||
*/
|
||||
public isMultiBranchesActive = () => {
|
||||
// const settings = this.tenancy.settings(tenantId);
|
||||
|
||||
// return settings.get({ group: 'features', key: Features.BRANCHES });
|
||||
return false;
|
||||
};
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user