mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-11 02:10:30 +00:00
Compare commits
47 Commits
lerna
...
docker-dev
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2d9859cde0 | ||
|
|
8c3d6b61d6 | ||
|
|
0ce9c93077 | ||
|
|
c3a2ea5064 | ||
|
|
28de827a99 | ||
|
|
b4559703f9 | ||
|
|
7532b44a57 | ||
|
|
a142b734d3 | ||
|
|
f26ced97fe | ||
|
|
25fb280e29 | ||
|
|
0c1bf302e5 | ||
|
|
57e3f68219 | ||
|
|
3b79ac66ae | ||
|
|
44fc26b156 | ||
|
|
d46f8faf26 | ||
|
|
2263cf5657 | ||
|
|
058d525afc | ||
|
|
490b8e09f2 | ||
|
|
e488c0eea9 | ||
|
|
f093239a15 | ||
|
|
5c537e094d | ||
|
|
a371fd44f7 | ||
|
|
59cb168331 | ||
|
|
8a5fbfc041 | ||
|
|
e3a072e267 | ||
|
|
b03606406e | ||
|
|
a1a7ee2b5b | ||
|
|
228ae71a1c | ||
|
|
71a8d3e77f | ||
|
|
4ddeb927cc | ||
|
|
72c1685fa6 | ||
|
|
7e7ee24109 | ||
|
|
708d971717 | ||
|
|
7781d092ca | ||
|
|
d0e84fb51a | ||
|
|
0e673ffa7c | ||
|
|
4c4c73db2d | ||
|
|
0086ee5186 | ||
|
|
bb49fcb42b | ||
|
|
d47b1165c4 | ||
|
|
f2e1efcb45 | ||
|
|
2e3b2cbf92 | ||
|
|
68e61429aa | ||
|
|
646be4bb20 | ||
|
|
dfd8b0ca4e | ||
|
|
2ab1a5606a | ||
|
|
c33370d4d2 |
5
.github/workflows/docker-build.yml
vendored
5
.github/workflows/docker-build.yml
vendored
@@ -19,7 +19,7 @@ on:
|
||||
|
||||
env:
|
||||
REGISTRY: ghcr.io
|
||||
IMAGE_NAME: abouhuolia/bigcapital-client
|
||||
IMAGE_NAME: abouhuolia/bigcapital-webapp
|
||||
|
||||
jobs:
|
||||
setup-build-publish-deploy:
|
||||
@@ -50,8 +50,9 @@ jobs:
|
||||
uses: docker/build-push-action@v2
|
||||
with:
|
||||
context: .
|
||||
file: ./packages/webapp/Dockerfile
|
||||
push: true
|
||||
tags: ghcr.io/bigcapitalhq/client:latest
|
||||
tags: ghcr.io/bigcapitalhq/webapp:latest
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
||||
# Send notification to Slack channel.
|
||||
- name: Slack Notification built and published successfully.
|
||||
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
node_modules/
|
||||
node_modules/
|
||||
data
|
||||
4
.husky/commit-msg
Normal file
4
.husky/commit-msg
Normal file
@@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env sh
|
||||
. "$(dirname -- "$0")/_/husky.sh"
|
||||
|
||||
yarn commitlint --edit
|
||||
2
.husky/pre-commit
Normal file
2
.husky/pre-commit
Normal file
@@ -0,0 +1,2 @@
|
||||
#!/usr/bin/env sh
|
||||
. "$(dirname -- "$0")/_/husky.sh"
|
||||
5
.vercelignore
Normal file
5
.vercelignore
Normal file
@@ -0,0 +1,5 @@
|
||||
/*
|
||||
!package.json
|
||||
!package-lock.json
|
||||
!yarn.lock
|
||||
!packages/webapp
|
||||
0
bin/.gitkeep
Normal file
0
bin/.gitkeep
Normal file
1
commitlint.config.js
Normal file
1
commitlint.config.js
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = { extends: ['@commitlint/config-lerna-scopes'] };
|
||||
34
docker-compose.yml
Normal file
34
docker-compose.yml
Normal file
@@ -0,0 +1,34 @@
|
||||
version: '3.3'
|
||||
|
||||
services:
|
||||
mysql:
|
||||
build:
|
||||
context: ./docker/mysql
|
||||
args:
|
||||
- MYSQL_DATABASE=bigcapital_system
|
||||
- MYSQL_USER=default_user
|
||||
- MYSQL_PASSWORD=secret
|
||||
- MYSQL_ROOT_PASSWORD=root
|
||||
volumes:
|
||||
- ./data/mysql/:/var/lib/mysql
|
||||
expose:
|
||||
- '3306'
|
||||
ports:
|
||||
- '3306:3306'
|
||||
|
||||
mongo:
|
||||
build: ./docker/mongo
|
||||
expose:
|
||||
- '27017'
|
||||
volumes:
|
||||
- ./data/mongo/:/var/lib/mongodb
|
||||
ports:
|
||||
- '27017:27017'
|
||||
|
||||
redis:
|
||||
build:
|
||||
context: ./docker/redis
|
||||
expose:
|
||||
- "6379"
|
||||
volumes:
|
||||
- ./data/redis:/data
|
||||
1
docker/mongo/Dockerfile
Normal file
1
docker/mongo/Dockerfile
Normal file
@@ -0,0 +1 @@
|
||||
FROM mongo:5.0
|
||||
18
docker/mysql/Dockerfile
Normal file
18
docker/mysql/Dockerfile
Normal file
@@ -0,0 +1,18 @@
|
||||
FROM mysql:5.7
|
||||
|
||||
ADD my.cnf /etc/mysql/conf.d/my.cnf
|
||||
|
||||
RUN chown -R mysql:root /var/lib/mysql/
|
||||
|
||||
ARG MYSQL_DATABASE=default_database
|
||||
ARG MYSQL_USER=default_user
|
||||
ARG MYSQL_PASSWORD=secret
|
||||
ARG MYSQL_ROOT_PASSWORD=root
|
||||
|
||||
ENV MYSQL_DATABASE=$MYSQL_DATABASE
|
||||
ENV MYSQL_USER=$MYSQL_USER
|
||||
ENV MYSQL_PASSWORD=$MYSQL_PASSWORD
|
||||
ENV MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD
|
||||
|
||||
CMD ["mysqld"]
|
||||
EXPOSE 3306
|
||||
2
docker/mysql/my.cnf
Normal file
2
docker/mysql/my.cnf
Normal file
@@ -0,0 +1,2 @@
|
||||
[mysqld]
|
||||
bind-address = 0.0.0.0
|
||||
5
docker/redis/Dockerfile
Normal file
5
docker/redis/Dockerfile
Normal file
@@ -0,0 +1,5 @@
|
||||
FROM redis:4.0
|
||||
|
||||
COPY redis.conf /usr/local/etc/redis/redis.conf
|
||||
|
||||
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
|
||||
48
docker/redis/redis.conf
Normal file
48
docker/redis/redis.conf
Normal file
@@ -0,0 +1,48 @@
|
||||
daemonize no
|
||||
pidfile /var/run/redis.pid
|
||||
port 6379
|
||||
tcp-backlog 511
|
||||
timeout 0
|
||||
tcp-keepalive 0
|
||||
loglevel notice
|
||||
logfile ""
|
||||
databases 16
|
||||
save 900 1
|
||||
save 300 10
|
||||
save 60 10000
|
||||
stop-writes-on-bgsave-error yes
|
||||
rdbcompression yes
|
||||
rdbchecksum yes
|
||||
dbfilename dump.rdb
|
||||
slave-serve-stale-data yes
|
||||
slave-read-only yes
|
||||
repl-diskless-sync no
|
||||
repl-diskless-sync-delay 5
|
||||
repl-disable-tcp-nodelay no
|
||||
slave-priority 100
|
||||
appendonly no
|
||||
appendfilename "appendonly.aof"
|
||||
appendfsync everysec
|
||||
no-appendfsync-on-rewrite no
|
||||
auto-aof-rewrite-percentage 100
|
||||
auto-aof-rewrite-min-size 64mb
|
||||
aof-load-truncated yes
|
||||
lua-time-limit 5000
|
||||
slowlog-log-slower-than 10000
|
||||
slowlog-max-len 128
|
||||
latency-monitor-threshold 0
|
||||
notify-keyspace-events ""
|
||||
hash-max-ziplist-entries 512
|
||||
hash-max-ziplist-value 64
|
||||
list-max-ziplist-entries 512
|
||||
list-max-ziplist-value 64
|
||||
set-max-intset-entries 512
|
||||
zset-max-ziplist-entries 128
|
||||
zset-max-ziplist-value 64
|
||||
hll-sparse-max-bytes 3000
|
||||
activerehashing yes
|
||||
client-output-buffer-limit normal 0 0 0
|
||||
client-output-buffer-limit slave 256mb 64mb 60
|
||||
client-output-buffer-limit pubsub 32mb 8mb 60
|
||||
hz 10
|
||||
aof-rewrite-incremental-fsync yes
|
||||
@@ -1,5 +1,6 @@
|
||||
{
|
||||
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
|
||||
"useWorkspaces": true,
|
||||
"version": "0.0.0"
|
||||
"version": "0.0.0",
|
||||
"npmClient": "npm"
|
||||
}
|
||||
|
||||
5697
package-lock.json
generated
Normal file
5697
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
26
package.json
26
package.json
@@ -6,17 +6,29 @@
|
||||
"dev": "lerna run dev",
|
||||
"build": "lerna run build",
|
||||
"dev:webapp": "lerna run dev --scope \"@bigcapital/webapp\"",
|
||||
"build:webapp": "lerna run dev --scope \"@bigcapital/webapp\"",
|
||||
"build:webapp": "lerna run build --scope \"@bigcapital/webapp\"",
|
||||
"dev:server": "lerna run dev --scope \"@bigcapital/server\"",
|
||||
"build:server": "lerna run dev --scope \"@bigcapital/server\""
|
||||
"build:server": "lerna run build --scope \"@bigcapital/server\"",
|
||||
"prepare": "husky install"
|
||||
},
|
||||
"workspaces": [
|
||||
"packages/*"
|
||||
"packages/*",
|
||||
"shared/*"
|
||||
],
|
||||
"devDependencies": {
|
||||
"lerna": "^6.4.1"
|
||||
"@commitlint/config-conventional": "^17.4.2",
|
||||
"@commitlint/config-lerna-scopes": "^17.4.2",
|
||||
"husky": "^8.0.3",
|
||||
"lerna": "^6.4.1",
|
||||
"@commitlint/cli": "^17.4.2"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14.20"
|
||||
}
|
||||
}
|
||||
"node": "14.x"
|
||||
},
|
||||
"husky": {
|
||||
"hooks": {
|
||||
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
|
||||
}
|
||||
},
|
||||
"dependencies": {}
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
# @bigcapital/server
|
||||
@@ -1,94 +0,0 @@
|
||||
import Knex from 'knex';
|
||||
import { knexSnakeCaseMappers } from 'objection';
|
||||
import color from 'colorette';
|
||||
import config from '../src/config';
|
||||
// import { systemKnexConfig } from '../src/config/knexConfig';
|
||||
|
||||
function initSystemKnex() {
|
||||
return Knex({
|
||||
client: config.system.db_client,
|
||||
connection: {
|
||||
host: config.system.db_host,
|
||||
user: config.system.db_user,
|
||||
password: config.system.db_password,
|
||||
database: config.system.db_name,
|
||||
charset: 'utf8',
|
||||
},
|
||||
migrations: {
|
||||
directory: config.system.migrations_dir,
|
||||
},
|
||||
seeds: {
|
||||
directory: config.system.seeds_dir,
|
||||
},
|
||||
pool: { min: 0, max: 7 },
|
||||
...knexSnakeCaseMappers({ upperCase: true }),
|
||||
});
|
||||
}
|
||||
|
||||
function getAllSystemTenants(knex) {
|
||||
return knex('tenants');
|
||||
}
|
||||
|
||||
function initTenantKnex(organizationId) {
|
||||
return Knex({
|
||||
client: config.tenant.db_client,
|
||||
connection: {
|
||||
host: config.tenant.db_host,
|
||||
user: config.tenant.db_user,
|
||||
password: config.tenant.db_password,
|
||||
database: `${config.tenant.db_name_prefix}${organizationId}`,
|
||||
charset: config.tenant.charset,
|
||||
},
|
||||
migrations: {
|
||||
directory: config.tenant.migrations_dir,
|
||||
},
|
||||
seeds: {
|
||||
directory: config.tenant.seeds_dir,
|
||||
},
|
||||
pool: { min: 0, max: 5 },
|
||||
...knexSnakeCaseMappers({ upperCase: true }),
|
||||
})
|
||||
}
|
||||
|
||||
function exit(text) {
|
||||
if (text instanceof Error) {
|
||||
console.error(
|
||||
color.red(`${text.detail ? `${text.detail}\n` : ''}${text.stack}`)
|
||||
);
|
||||
} else {
|
||||
console.error(color.red(text));
|
||||
}
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
function success(text) {
|
||||
console.log(text);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
function log(text) {
|
||||
console.log(text);
|
||||
}
|
||||
|
||||
function getDeepValue(prop, obj) {
|
||||
if (!Object.keys(obj).length) { return []; }
|
||||
|
||||
return Object.entries(obj).reduce((acc, [key, val]) => {
|
||||
if (key === prop) {
|
||||
acc.push(val);
|
||||
} else {
|
||||
acc.push(Array.isArray(val) ? val.map(getIds).flat() : getIds(val));
|
||||
}
|
||||
return acc.flat();
|
||||
}, []);
|
||||
}
|
||||
|
||||
export {
|
||||
initTenantKnex,
|
||||
initSystemKnex,
|
||||
getAllSystemTenants,
|
||||
exit,
|
||||
success,
|
||||
log,
|
||||
getDeepValue,
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "bigcapital-server",
|
||||
"name": "@bigcapital/server",
|
||||
"version": "1.7.1",
|
||||
"description": "",
|
||||
"main": "src/server.ts",
|
||||
@@ -8,7 +8,9 @@
|
||||
"clear": "rimraf build",
|
||||
"dev": "cross-env NODE_ENV=development webpack --config scripts/webpack.config.js",
|
||||
"build:resources": "gulp --gulpfile=scripts/gulpfile.js styles styles-rtl",
|
||||
"build": "cross-env NODE_ENV=production webpack --config scripts/webpack.config.js",
|
||||
"build:app": "cross-env NODE_ENV=production webpack --config scripts/webpack.config.js",
|
||||
"build:commands": "cross-env NODE_ENV=production webpack --config scripts/webpack.cli.js",
|
||||
"build": "npm-run-all build:*",
|
||||
"lint:fix": "eslint --fix ./**/*.ts"
|
||||
},
|
||||
"author": "Ahmed Bouhuolia, <a.bouhuolia@gmail.com>",
|
||||
|
||||
11
packages/server/scripts/webpack.cli.js
Normal file
11
packages/server/scripts/webpack.cli.js
Normal file
@@ -0,0 +1,11 @@
|
||||
const { getCommonWebpackOptions } = require('./webpack.common');
|
||||
|
||||
const inputEntry = './src/commands/index.ts';
|
||||
const outputDir = '../build';
|
||||
const outputFilename = 'commands.js';
|
||||
|
||||
module.exports = getCommonWebpackOptions({
|
||||
inputEntry,
|
||||
outputDir,
|
||||
outputFilename,
|
||||
});
|
||||
76
packages/server/scripts/webpack.common.js
Normal file
76
packages/server/scripts/webpack.common.js
Normal file
@@ -0,0 +1,76 @@
|
||||
const path = require('path');
|
||||
const { NormalModuleReplacementPlugin } = require('webpack');
|
||||
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
|
||||
const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin');
|
||||
const nodeExternals = require('webpack-node-externals');
|
||||
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
|
||||
|
||||
const isDev = process.env.NODE_ENV === 'development';
|
||||
|
||||
exports.getCommonWebpackOptions = ({
|
||||
inputEntry,
|
||||
outputDir,
|
||||
outputFilename,
|
||||
}) => {
|
||||
const webpackOptions = {
|
||||
entry: ['regenerator-runtime/runtime', inputEntry],
|
||||
target: 'node',
|
||||
mode: isDev ? 'development' : 'production',
|
||||
watch: isDev,
|
||||
watchOptions: {
|
||||
aggregateTimeout: 200,
|
||||
poll: 1000,
|
||||
},
|
||||
output: {
|
||||
path: path.resolve(__dirname, outputDir),
|
||||
filename: outputFilename,
|
||||
},
|
||||
resolve: {
|
||||
extensions: ['.ts', '.tsx', '.js'],
|
||||
extensionAlias: {
|
||||
'.ts': ['.js', '.ts'],
|
||||
'.cts': ['.cjs', '.cts'],
|
||||
'.mts': ['.mjs', '.mts'],
|
||||
},
|
||||
plugins: [
|
||||
new TsconfigPathsPlugin({
|
||||
configFile: './tsconfig.json',
|
||||
extensions: ['.ts', '.tsx', '.js'],
|
||||
}),
|
||||
],
|
||||
},
|
||||
plugins: [
|
||||
// Ignore knex dynamic required dialects that we don't use
|
||||
new NormalModuleReplacementPlugin(
|
||||
/m[sy]sql2?|oracle(db)?|sqlite3|pg-(native|query)/,
|
||||
'noop2'
|
||||
),
|
||||
new ProgressBarPlugin(),
|
||||
],
|
||||
externals: [nodeExternals(), 'aws-sdk', 'prettier'],
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.([cm]?ts|tsx|js)$/,
|
||||
use: [
|
||||
{
|
||||
loader: 'ts-loader',
|
||||
options: {
|
||||
transpileOnly: true,
|
||||
configFile: 'tsconfig.json',
|
||||
},
|
||||
},
|
||||
],
|
||||
exclude: /(node_modules)/,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
if (isDev) {
|
||||
webpackOptions.plugins.push(
|
||||
new RunScriptWebpackPlugin({ name: outputFilename })
|
||||
);
|
||||
}
|
||||
return webpackOptions;
|
||||
};
|
||||
@@ -1,74 +1,11 @@
|
||||
const path = require('path');
|
||||
const { NormalModuleReplacementPlugin } = require('webpack');
|
||||
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
|
||||
const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin');
|
||||
const nodeExternals = require('webpack-node-externals');
|
||||
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
|
||||
const { getCommonWebpackOptions } = require('./webpack.common');
|
||||
|
||||
const isDev = process.env.NODE_ENV === 'development';
|
||||
const inputEntry = './src/server.ts';
|
||||
const outputDir = '../build';
|
||||
const outputFilename = 'index.js';
|
||||
const inputEntry = './src/server.ts';
|
||||
|
||||
const webpackOptions = {
|
||||
entry: ['regenerator-runtime/runtime', inputEntry],
|
||||
target: 'node',
|
||||
mode: isDev ? 'development' : 'production',
|
||||
watch: isDev,
|
||||
watchOptions: {
|
||||
aggregateTimeout: 200,
|
||||
poll: 1000,
|
||||
},
|
||||
output: {
|
||||
path: path.resolve(__dirname, outputDir),
|
||||
filename: outputFilename,
|
||||
},
|
||||
resolve: {
|
||||
extensions: ['.ts', '.tsx', '.js'],
|
||||
extensionAlias: {
|
||||
'.ts': ['.js', '.ts'],
|
||||
'.cts': ['.cjs', '.cts'],
|
||||
'.mts': ['.mjs', '.mts'],
|
||||
},
|
||||
plugins: [
|
||||
new TsconfigPathsPlugin({
|
||||
configFile: './tsconfig.json',
|
||||
extensions: ['.ts', '.tsx', '.js'],
|
||||
}),
|
||||
],
|
||||
},
|
||||
plugins: [
|
||||
// Ignore knex dynamic required dialects that we don't use
|
||||
new NormalModuleReplacementPlugin(
|
||||
/m[sy]sql2?|oracle(db)?|sqlite3|pg-(native|query)/,
|
||||
'noop2'
|
||||
),
|
||||
new ProgressBarPlugin(),
|
||||
],
|
||||
externals: [nodeExternals(), 'aws-sdk', 'prettier'],
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.([cm]?ts|tsx|js)$/,
|
||||
use: [
|
||||
{
|
||||
loader: 'ts-loader',
|
||||
options: {
|
||||
transpileOnly: true,
|
||||
configFile: 'tsconfig.json',
|
||||
},
|
||||
},
|
||||
],
|
||||
exclude: /(node_modules)/,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
if (isDev) {
|
||||
webpackOptions.plugins.push(
|
||||
new RunScriptWebpackPlugin({ name: outputFilename })
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = webpackOptions;
|
||||
module.exports = getCommonWebpackOptions({
|
||||
inputEntry,
|
||||
outputDir,
|
||||
outputFilename,
|
||||
});
|
||||
|
||||
@@ -9,6 +9,7 @@ import DynamicListingService from '@/services/DynamicListing/DynamicListService'
|
||||
import { DATATYPES_LENGTH } from '@/data/DataTypes';
|
||||
import CheckPolicies from '@/api/middleware/CheckPolicies';
|
||||
import { AccountsApplication } from '@/services/Accounts/AccountsApplication';
|
||||
import { MAX_ACCOUNTS_CHART_DEPTH } from 'services/Accounts/constants';
|
||||
|
||||
@Service()
|
||||
export default class AccountsController extends BaseController {
|
||||
@@ -494,6 +495,22 @@ export default class AccountsController extends BaseController {
|
||||
}
|
||||
);
|
||||
}
|
||||
if (error.errorType === 'PARENT_ACCOUNT_EXCEEDED_THE_DEPTH_LEVEL') {
|
||||
return res.boom.badRequest(
|
||||
'The parent account exceeded the depth level of accounts chart.',
|
||||
{
|
||||
errors: [
|
||||
{
|
||||
type: 'PARENT_ACCOUNT_EXCEEDED_THE_DEPTH_LEVEL',
|
||||
code: 1500,
|
||||
data: {
|
||||
maxDepth: MAX_ACCOUNTS_CHART_DEPTH,
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import { check, ValidationChain } from 'express-validator';
|
||||
import asyncMiddleware from '@/api/middleware/asyncMiddleware';
|
||||
import JWTAuth from '@/api/middleware/jwtAuth';
|
||||
import TenancyMiddleware from '@/api/middleware/TenancyMiddleware';
|
||||
import SubscriptionMiddleware from '@/api/middleware/SubscriptionMiddleware';
|
||||
import AttachCurrentTenantUser from '@/api/middleware/AttachCurrentTenantUser';
|
||||
import OrganizationService from '@/services/Organization/OrganizationService';
|
||||
import {
|
||||
@@ -24,7 +23,7 @@ const ACCEPTED_LOCATIONS = ['libya'];
|
||||
@Service()
|
||||
export default class OrganizationController extends BaseController {
|
||||
@Inject()
|
||||
organizationService: OrganizationService;
|
||||
private organizationService: OrganizationService;
|
||||
|
||||
/**
|
||||
* Router constructor.
|
||||
@@ -32,13 +31,10 @@ export default class OrganizationController extends BaseController {
|
||||
router() {
|
||||
const router = Router();
|
||||
|
||||
// Should before build tenant database the user be authorized and
|
||||
// most important than that, should be subscribed to any plan.
|
||||
router.use(JWTAuth);
|
||||
router.use(AttachCurrentTenantUser);
|
||||
router.use(TenancyMiddleware);
|
||||
|
||||
router.use('/build', SubscriptionMiddleware('main'));
|
||||
router.post(
|
||||
'/build',
|
||||
this.organizationValidationSchema,
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
import { Router, Request, Response, NextFunction } from 'express';
|
||||
import { check, ValidationChain } from 'express-validator';
|
||||
import BaseController from './BaseController';
|
||||
import SetupService from '@/services/Setup/SetupService';
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { IOrganizationSetupDTO } from '@/interfaces';
|
||||
import { ServiceError } from '@/exceptions';
|
||||
// Middlewares
|
||||
import JWTAuth from '@/api/middleware/jwtAuth';
|
||||
import AttachCurrentTenantUser from '@/api/middleware/AttachCurrentTenantUser';
|
||||
import SubscriptionMiddleware from '@/api/middleware/SubscriptionMiddleware';
|
||||
import TenancyMiddleware from '@/api/middleware/TenancyMiddleware';
|
||||
import EnsureTenantIsInitialized from '@/api/middleware/EnsureTenantIsInitialized';
|
||||
import SettingsMiddleware from '@/api/middleware/SettingsMiddleware';
|
||||
|
||||
@Service()
|
||||
export default class SetupController extends BaseController {
|
||||
@Inject()
|
||||
setupService: SetupService;
|
||||
|
||||
router() {
|
||||
const router = Router('/setup');
|
||||
|
||||
router.use(JWTAuth);
|
||||
router.use(AttachCurrentTenantUser);
|
||||
router.use(TenancyMiddleware);
|
||||
router.use(SubscriptionMiddleware('main'));
|
||||
router.use(EnsureTenantIsInitialized);
|
||||
router.use(SettingsMiddleware);
|
||||
router.post(
|
||||
'/organization',
|
||||
this.organizationSetupSchema,
|
||||
this.validationResult,
|
||||
this.asyncMiddleware(this.organizationSetup.bind(this)),
|
||||
this.handleServiceErrors
|
||||
);
|
||||
return router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Organization setup schema.
|
||||
*/
|
||||
private get organizationSetupSchema(): ValidationChain[] {
|
||||
return [
|
||||
check('organization_name').exists().trim(),
|
||||
check('base_currency').exists(),
|
||||
check('time_zone').exists(),
|
||||
check('fiscal_year').exists(),
|
||||
check('industry').optional(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Organization setup.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
* @returns
|
||||
*/
|
||||
async organizationSetup(req: Request, res: Response, next: NextFunction) {
|
||||
const { tenantId } = req;
|
||||
const setupDTO: IOrganizationSetupDTO = this.matchedBodyData(req);
|
||||
|
||||
try {
|
||||
await this.setupService.organizationSetup(tenantId, setupDTO);
|
||||
|
||||
return res.status(200).send({
|
||||
message: 'The setup settings set successfully.',
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles service errors.
|
||||
* @param {Error} error
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
handleServiceErrors(
|
||||
error: Error,
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) {
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'TENANT_IS_ALREADY_SETUPED') {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'TENANT_IS_ALREADY_SETUPED', code: 1000 }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'BASE_CURRENCY_INVALID') {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'BASE_CURRENCY_INVALID', code: 110 }],
|
||||
});
|
||||
}
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
@@ -1,250 +0,0 @@
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { Router, Request, Response, NextFunction } from 'express';
|
||||
import { check, oneOf, ValidationChain } from 'express-validator';
|
||||
import basicAuth from 'express-basic-auth';
|
||||
import config from '@/config';
|
||||
import { License } from '@/system/models';
|
||||
import { ServiceError } from '@/exceptions';
|
||||
import BaseController from '@/api/controllers/BaseController';
|
||||
import LicenseService from '@/services/Payment/License';
|
||||
import asyncMiddleware from '@/api/middleware/asyncMiddleware';
|
||||
import { ILicensesFilter, ISendLicenseDTO } from '@/interfaces';
|
||||
|
||||
@Service()
|
||||
export default class LicensesController extends BaseController {
|
||||
@Inject()
|
||||
licenseService: LicenseService;
|
||||
|
||||
/**
|
||||
* Router constructor.
|
||||
*/
|
||||
router() {
|
||||
const router = Router();
|
||||
|
||||
router.use(
|
||||
basicAuth({
|
||||
users: {
|
||||
[config.licensesAuth.user]: config.licensesAuth.password,
|
||||
},
|
||||
challenge: true,
|
||||
})
|
||||
);
|
||||
router.post(
|
||||
'/generate',
|
||||
this.generateLicenseSchema,
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.generateLicense.bind(this)),
|
||||
this.catchServiceErrors,
|
||||
);
|
||||
router.post(
|
||||
'/disable/:licenseId',
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.disableLicense.bind(this)),
|
||||
this.catchServiceErrors,
|
||||
);
|
||||
router.post(
|
||||
'/send',
|
||||
this.sendLicenseSchemaValidation,
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.sendLicense.bind(this)),
|
||||
this.catchServiceErrors,
|
||||
);
|
||||
router.delete(
|
||||
'/:licenseId',
|
||||
asyncMiddleware(this.deleteLicense.bind(this)),
|
||||
this.catchServiceErrors,
|
||||
);
|
||||
router.get('/', asyncMiddleware(this.listLicenses.bind(this)));
|
||||
return router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate license validation schema.
|
||||
*/
|
||||
get generateLicenseSchema(): ValidationChain[] {
|
||||
return [
|
||||
check('loop').exists().isNumeric().toInt(),
|
||||
check('period').exists().isNumeric().toInt(),
|
||||
check('period_interval')
|
||||
.exists()
|
||||
.isIn(['month', 'months', 'year', 'years', 'day', 'days']),
|
||||
check('plan_slug').exists().trim().escape(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Specific license validation schema.
|
||||
*/
|
||||
get specificLicenseSchema(): ValidationChain[] {
|
||||
return [
|
||||
oneOf(
|
||||
[check('license_id').exists().isNumeric().toInt()],
|
||||
[check('license_code').exists().isNumeric().toInt()]
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Send license validation schema.
|
||||
*/
|
||||
get sendLicenseSchemaValidation(): ValidationChain[] {
|
||||
return [
|
||||
check('period').exists().isNumeric(),
|
||||
check('period_interval').exists().trim().escape(),
|
||||
check('plan_slug').exists().trim().escape(),
|
||||
oneOf([
|
||||
check('phone_number').exists().trim().escape(),
|
||||
check('email').exists().trim().escape(),
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate licenses codes with given period in bulk.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @return {Response}
|
||||
*/
|
||||
async generateLicense(req: Request, res: Response, next: Function) {
|
||||
const { loop = 10, period, periodInterval, planSlug } = this.matchedBodyData(
|
||||
req
|
||||
);
|
||||
|
||||
try {
|
||||
await this.licenseService.generateLicenses(
|
||||
loop,
|
||||
period,
|
||||
periodInterval,
|
||||
planSlug
|
||||
);
|
||||
return res.status(200).send({
|
||||
code: 100,
|
||||
type: 'LICENSEES.GENERATED.SUCCESSFULLY',
|
||||
message: 'The licenses have been generated successfully.',
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable the given license on the storage.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @return {Response}
|
||||
*/
|
||||
async disableLicense(req: Request, res: Response, next: Function) {
|
||||
const { licenseId } = req.params;
|
||||
|
||||
try {
|
||||
await this.licenseService.disableLicense(licenseId);
|
||||
|
||||
return res.status(200).send({ license_id: licenseId });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the given license code on the storage.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @return {Response}
|
||||
*/
|
||||
async deleteLicense(req: Request, res: Response, next: Function) {
|
||||
const { licenseId } = req.params;
|
||||
|
||||
try {
|
||||
await this.licenseService.deleteLicense(licenseId);
|
||||
|
||||
return res.status(200).send({ license_id: licenseId });
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send license code in the given period to the customer via email or phone number
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @return {Response}
|
||||
*/
|
||||
async sendLicense(req: Request, res: Response, next: Function) {
|
||||
const sendLicenseDTO: ISendLicenseDTO = this.matchedBodyData(req);
|
||||
|
||||
try {
|
||||
await this.licenseService.sendLicenseToCustomer(sendLicenseDTO);
|
||||
|
||||
return res.status(200).send({
|
||||
status: 100,
|
||||
code: 'LICENSE.CODE.SENT',
|
||||
message: 'The license has been sent to the given customer.',
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Listing licenses.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
*/
|
||||
async listLicenses(req: Request, res: Response) {
|
||||
const filter: ILicensesFilter = {
|
||||
disabled: false,
|
||||
used: false,
|
||||
sent: false,
|
||||
active: false,
|
||||
...req.query,
|
||||
};
|
||||
const licenses = await License.query().onBuild((builder) => {
|
||||
builder.modify('filter', filter);
|
||||
builder.orderBy('createdAt', 'ASC');
|
||||
});
|
||||
return res.status(200).send({ licenses });
|
||||
}
|
||||
|
||||
/**
|
||||
* Catches all service errors.
|
||||
*/
|
||||
catchServiceErrors(error, req: Request, res: Response, next: NextFunction) {
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'PLAN_NOT_FOUND') {
|
||||
return res.status(400).send({
|
||||
errors: [{
|
||||
type: 'PLAN.NOT.FOUND',
|
||||
code: 100,
|
||||
message: 'The given plan not found.',
|
||||
}],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'LICENSE_NOT_FOUND') {
|
||||
return res.status(400).send({
|
||||
errors: [{
|
||||
type: 'LICENSE_NOT_FOUND',
|
||||
code: 200,
|
||||
message: 'The given license id not found.'
|
||||
}],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'LICENSE_ALREADY_DISABLED') {
|
||||
return res.status(400).send({
|
||||
errors: [{
|
||||
type: 'LICENSE.ALREADY.DISABLED',
|
||||
code: 200,
|
||||
message: 'License is already disabled.'
|
||||
}],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'NO_AVALIABLE_LICENSE_CODE') {
|
||||
return res.status(400).send({
|
||||
status: 110,
|
||||
message: 'There is no licenses availiable right now with the given period and plan.',
|
||||
code: 'NO.AVALIABLE.LICENSE.CODE',
|
||||
});
|
||||
}
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
import { Inject } from 'typedi';
|
||||
import { Request, Response } from 'express';
|
||||
import { Plan } from '@/system/models';
|
||||
import BaseController from '@/api/controllers/BaseController';
|
||||
import SubscriptionService from '@/services/Subscription/SubscriptionService';
|
||||
|
||||
export default class PaymentMethodController extends BaseController {
|
||||
@Inject()
|
||||
subscriptionService: SubscriptionService;
|
||||
|
||||
/**
|
||||
* Validate the given plan slug exists on the storage.
|
||||
*
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*
|
||||
* @return {Response|void}
|
||||
*/
|
||||
async validatePlanSlugExistance(req: Request, res: Response, next: Function) {
|
||||
const { planSlug } = this.matchedBodyData(req);
|
||||
const foundPlan = await Plan.query().where('slug', planSlug).first();
|
||||
|
||||
if (!foundPlan) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'PLAN.SLUG.NOT.EXISTS', code: 110 }],
|
||||
});
|
||||
}
|
||||
next();
|
||||
}
|
||||
}
|
||||
@@ -1,125 +0,0 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { NextFunction, Router, Request, Response } from 'express';
|
||||
import { check } from 'express-validator';
|
||||
import asyncMiddleware from '@/api/middleware/asyncMiddleware';
|
||||
import PaymentMethodController from '@/api/controllers/Subscription/PaymentMethod';
|
||||
import {
|
||||
NotAllowedChangeSubscriptionPlan,
|
||||
NoPaymentModelWithPricedPlan,
|
||||
PaymentAmountInvalidWithPlan,
|
||||
PaymentInputInvalid,
|
||||
VoucherCodeRequired,
|
||||
} from '@/exceptions';
|
||||
import { ILicensePaymentModel } from '@/interfaces';
|
||||
import instance from 'tsyringe/dist/typings/dependency-container';
|
||||
|
||||
@Service()
|
||||
export default class PaymentViaLicenseController extends PaymentMethodController {
|
||||
@Inject('logger')
|
||||
logger: any;
|
||||
|
||||
/**
|
||||
* Router constructor.
|
||||
*/
|
||||
router() {
|
||||
const router = Router();
|
||||
|
||||
router.post(
|
||||
'/payment',
|
||||
this.paymentViaLicenseSchema,
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.validatePlanSlugExistance.bind(this)),
|
||||
asyncMiddleware(this.paymentViaLicense.bind(this)),
|
||||
this.handleErrors,
|
||||
);
|
||||
return router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Payment via license validation schema.
|
||||
*/
|
||||
get paymentViaLicenseSchema() {
|
||||
return [
|
||||
check('plan_slug').exists().trim().escape(),
|
||||
check('license_code').exists().trim().escape(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the subscription payment via license code.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @return {Response}
|
||||
*/
|
||||
async paymentViaLicense(req: Request, res: Response, next: Function) {
|
||||
const { planSlug, licenseCode } = this.matchedBodyData(req);
|
||||
const { tenant } = req;
|
||||
|
||||
try {
|
||||
const licenseModel: ILicensePaymentModel = { licenseCode };
|
||||
|
||||
await this.subscriptionService.subscriptionViaLicense(
|
||||
tenant.id,
|
||||
planSlug,
|
||||
licenseModel
|
||||
);
|
||||
|
||||
return res.status(200).send({
|
||||
type: 'success',
|
||||
code: 'PAYMENT.SUCCESSFULLY.MADE',
|
||||
message: 'Payment via license has been made successfully.',
|
||||
});
|
||||
} catch (exception) {
|
||||
next(exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle service errors.
|
||||
* @param {Error} error
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
private handleErrors(
|
||||
exception: Error,
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) {
|
||||
const errorReasons = [];
|
||||
|
||||
if (exception instanceof VoucherCodeRequired) {
|
||||
errorReasons.push({
|
||||
type: 'VOUCHER_CODE_REQUIRED',
|
||||
code: 100,
|
||||
});
|
||||
}
|
||||
if (exception instanceof NoPaymentModelWithPricedPlan) {
|
||||
errorReasons.push({
|
||||
type: 'NO_PAYMENT_WITH_PRICED_PLAN',
|
||||
code: 140,
|
||||
});
|
||||
}
|
||||
if (exception instanceof NotAllowedChangeSubscriptionPlan) {
|
||||
errorReasons.push({
|
||||
type: 'NOT.ALLOWED.RENEW.SUBSCRIPTION.WHILE.ACTIVE',
|
||||
code: 120,
|
||||
});
|
||||
}
|
||||
if (errorReasons.length > 0) {
|
||||
return res.status(400).send({ errors: errorReasons });
|
||||
}
|
||||
if (exception instanceof PaymentInputInvalid) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'LICENSE.CODE.IS.INVALID', code: 120 }],
|
||||
});
|
||||
}
|
||||
if (exception instanceof PaymentAmountInvalidWithPlan) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'LICENSE.NOT.FOR.GIVEN.PLAN' }],
|
||||
});
|
||||
}
|
||||
next(exception);
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
import { Router, Request, Response, NextFunction } from 'express';
|
||||
import { Container, Service, Inject } from 'typedi';
|
||||
import JWTAuth from '@/api/middleware/jwtAuth';
|
||||
import TenancyMiddleware from '@/api/middleware/TenancyMiddleware';
|
||||
import AttachCurrentTenantUser from '@/api/middleware/AttachCurrentTenantUser';
|
||||
import PaymentViaLicenseController from '@/api/controllers/Subscription/PaymentViaLicense';
|
||||
import SubscriptionService from '@/services/Subscription/SubscriptionService';
|
||||
import asyncMiddleware from '@/api/middleware/asyncMiddleware';
|
||||
|
||||
@Service()
|
||||
export default class SubscriptionController {
|
||||
@Inject()
|
||||
subscriptionService: SubscriptionService;
|
||||
|
||||
/**
|
||||
* Router constructor.
|
||||
*/
|
||||
router() {
|
||||
const router = Router();
|
||||
|
||||
router.use(JWTAuth);
|
||||
router.use(AttachCurrentTenantUser);
|
||||
router.use(TenancyMiddleware);
|
||||
|
||||
router.use('/license', Container.get(PaymentViaLicenseController).router());
|
||||
router.get('/', asyncMiddleware(this.getSubscriptions.bind(this)));
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all subscriptions of the authenticated user's tenant.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
async getSubscriptions(req: Request, res: Response, next: NextFunction) {
|
||||
const { tenantId } = req;
|
||||
|
||||
try {
|
||||
const subscriptions = await this.subscriptionService.getSubscriptions(
|
||||
tenantId
|
||||
);
|
||||
return res.status(200).send({ subscriptions });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import { Container } from 'typedi';
|
||||
// Middlewares
|
||||
import JWTAuth from '@/api/middleware/jwtAuth';
|
||||
import AttachCurrentTenantUser from '@/api/middleware/AttachCurrentTenantUser';
|
||||
import SubscriptionMiddleware from '@/api/middleware/SubscriptionMiddleware';
|
||||
import TenancyMiddleware from '@/api/middleware/TenancyMiddleware';
|
||||
import EnsureTenantIsInitialized from '@/api/middleware/EnsureTenantIsInitialized';
|
||||
import SettingsMiddleware from '@/api/middleware/SettingsMiddleware';
|
||||
@@ -37,8 +36,6 @@ import Resources from './controllers/Resources';
|
||||
import ExchangeRates from '@/api/controllers/ExchangeRates';
|
||||
import Media from '@/api/controllers/Media';
|
||||
import Ping from '@/api/controllers/Ping';
|
||||
import Subscription from '@/api/controllers/Subscription';
|
||||
import Licenses from '@/api/controllers/Subscription/Licenses';
|
||||
import InventoryAdjustments from '@/api/controllers/Inventory/InventoryAdjustments';
|
||||
import asyncRenderMiddleware from './middleware/AsyncRenderMiddleware';
|
||||
import Jobs from './controllers/Jobs';
|
||||
@@ -69,8 +66,6 @@ export default () => {
|
||||
|
||||
app.use('/auth', Container.get(Authentication).router());
|
||||
app.use('/invite', Container.get(InviteUsers).nonAuthRouter());
|
||||
app.use('/licenses', Container.get(Licenses).router());
|
||||
app.use('/subscription', Container.get(Subscription).router());
|
||||
app.use('/organization', Container.get(Organization).router());
|
||||
app.use('/ping', Container.get(Ping).router());
|
||||
app.use('/jobs', Container.get(Jobs).router());
|
||||
@@ -83,7 +78,6 @@ export default () => {
|
||||
dashboard.use(JWTAuth);
|
||||
dashboard.use(AttachCurrentTenantUser);
|
||||
dashboard.use(TenancyMiddleware);
|
||||
dashboard.use(SubscriptionMiddleware('main'));
|
||||
dashboard.use(EnsureTenantIsInitialized);
|
||||
dashboard.use(SettingsMiddleware);
|
||||
dashboard.use(I18nAuthenticatedMiddlware);
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { Container } from 'typedi';
|
||||
|
||||
export default (subscriptionSlug = 'main') => async (
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) => {
|
||||
const { tenant, tenantId } = req;
|
||||
const Logger = Container.get('logger');
|
||||
const { subscriptionRepository } = Container.get('repositories');
|
||||
|
||||
if (!tenant) {
|
||||
throw new Error('Should load `TenancyMiddlware` before this middleware.');
|
||||
}
|
||||
Logger.info('[subscription_middleware] trying get tenant main subscription.');
|
||||
const subscription = await subscriptionRepository.getBySlugInTenant(
|
||||
subscriptionSlug,
|
||||
tenantId
|
||||
);
|
||||
// Validate in case there is no any already subscription.
|
||||
if (!subscription) {
|
||||
Logger.info('[subscription_middleware] tenant has no subscription.', {
|
||||
tenantId,
|
||||
});
|
||||
return res.boom.badRequest('Tenant has no subscription.', {
|
||||
errors: [{ type: 'TENANT.HAS.NO.SUBSCRIPTION' }],
|
||||
});
|
||||
}
|
||||
// Validate in case the subscription is inactive.
|
||||
else if (subscription.inactive()) {
|
||||
Logger.info(
|
||||
'[subscription_middleware] tenant main subscription is expired.',
|
||||
{ tenantId }
|
||||
);
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'ORGANIZATION.SUBSCRIPTION.INACTIVE' }],
|
||||
});
|
||||
}
|
||||
next();
|
||||
};
|
||||
@@ -1,15 +1,81 @@
|
||||
#!/usr/bin/env node
|
||||
import commander from 'commander';
|
||||
import color from 'colorette';
|
||||
import argv from 'getopts'
|
||||
import config from '../src/config';
|
||||
import {
|
||||
initSystemKnex,
|
||||
getAllSystemTenants,
|
||||
initTenantKnex,
|
||||
exit,
|
||||
success,
|
||||
log,
|
||||
} from './utils';
|
||||
import argv from 'getopts';
|
||||
import Knex from 'knex';
|
||||
import { knexSnakeCaseMappers } from 'objection';
|
||||
import config from '../config';
|
||||
|
||||
function initSystemKnex() {
|
||||
return Knex({
|
||||
client: config.system.db_client,
|
||||
connection: {
|
||||
host: config.system.db_host,
|
||||
user: config.system.db_user,
|
||||
password: config.system.db_password,
|
||||
database: config.system.db_name,
|
||||
charset: 'utf8',
|
||||
},
|
||||
migrations: {
|
||||
directory: config.system.migrations_dir,
|
||||
},
|
||||
seeds: {
|
||||
directory: config.system.seeds_dir,
|
||||
},
|
||||
pool: { min: 0, max: 7 },
|
||||
...knexSnakeCaseMappers({ upperCase: true }),
|
||||
});
|
||||
}
|
||||
|
||||
function initTenantKnex(organizationId) {
|
||||
return Knex({
|
||||
client: config.tenant.db_client,
|
||||
connection: {
|
||||
host: config.tenant.db_host,
|
||||
user: config.tenant.db_user,
|
||||
password: config.tenant.db_password,
|
||||
database: `${config.tenant.db_name_prefix}${organizationId}`,
|
||||
charset: config.tenant.charset,
|
||||
},
|
||||
migrations: {
|
||||
directory: config.tenant.migrations_dir,
|
||||
},
|
||||
seeds: {
|
||||
directory: config.tenant.seeds_dir,
|
||||
},
|
||||
pool: { min: 0, max: 5 },
|
||||
...knexSnakeCaseMappers({ upperCase: true }),
|
||||
});
|
||||
}
|
||||
function exit(text) {
|
||||
if (text instanceof Error) {
|
||||
console.error(
|
||||
color.red(`${text.detail ? `${text.detail}\n` : ''}${text.stack}`)
|
||||
);
|
||||
} else {
|
||||
console.error(color.red(text));
|
||||
}
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
function success(text) {
|
||||
console.log(text);
|
||||
process.exit(0);
|
||||
}
|
||||
function log(text) {
|
||||
console.log(text);
|
||||
}
|
||||
|
||||
function getAllSystemTenants(knex) {
|
||||
return knex('tenants');
|
||||
}
|
||||
|
||||
// module.exports = {
|
||||
// log,
|
||||
// success,
|
||||
// exit,
|
||||
// initSystemKnex,
|
||||
// };
|
||||
|
||||
// - bigcapital system:migrate:latest
|
||||
// - bigcapital system:migrate:rollback
|
||||
@@ -17,12 +83,12 @@ import {
|
||||
// - bigcapital tenants:migrate:latest --tenant_id=XXX
|
||||
// - bigcapital tenants:migrate:rollback
|
||||
// - bigcapital tenants:migrate:rollback --tenant_id=XXX
|
||||
// - bigcapital tenants:migrate:make
|
||||
// - bigcapital system:migrate:make
|
||||
// - bigcapital tenants:migrate:make
|
||||
// - bigcapital system:migrate:make
|
||||
// - bigcapital tenants:list
|
||||
|
||||
commander
|
||||
.command('system:migrate:rollback')
|
||||
.command('system:migrate:rollback')
|
||||
.description('Migrate the system database of the application.')
|
||||
.action(async () => {
|
||||
try {
|
||||
@@ -33,18 +99,17 @@ commander
|
||||
success(color.cyan('Already at the base migration'));
|
||||
}
|
||||
success(
|
||||
color.green(
|
||||
`Batch ${batchNo} rolled back: ${_log.length} migrations`
|
||||
) + (argv.verbose ? `\n${color.cyan(_log.join('\n'))}` : '')
|
||||
color.green(`Batch ${batchNo} rolled back: ${_log.length} migrations`) +
|
||||
(argv.verbose ? `\n${color.cyan(_log.join('\n'))}` : '')
|
||||
);
|
||||
} catch(error) {
|
||||
} catch (error) {
|
||||
exit(error);
|
||||
}
|
||||
});
|
||||
|
||||
commander
|
||||
.command('system:migrate:latest')
|
||||
.description('Rollback latest mgiration of the system database.')
|
||||
.description('Migrate latest mgiration of the system database.')
|
||||
.action(async () => {
|
||||
try {
|
||||
const sysKnex = await initSystemKnex();
|
||||
@@ -57,7 +122,7 @@ commander
|
||||
color.green(`Batch ${batchNo} run: ${log.length} migrations`) +
|
||||
(argv.verbose ? `\n${color.cyan(log.join('\n'))}` : '')
|
||||
);
|
||||
} catch(error) {
|
||||
} catch (error) {
|
||||
exit(error);
|
||||
}
|
||||
});
|
||||
@@ -67,85 +132,47 @@ commander
|
||||
.description('Created a named migration file to the system database.')
|
||||
.action(async (name) => {
|
||||
const sysKnex = await initSystemKnex();
|
||||
|
||||
sysKnex.migrate.make(name).then((name) => {
|
||||
success(color.green(`Created Migration: ${name}`));
|
||||
}).catch(exit)
|
||||
});
|
||||
|
||||
commander
|
||||
.command('tenants:migrate:make <name>')
|
||||
.description('Created a name migration file to the tenants databases.')
|
||||
.action(async (name) => {
|
||||
const sysKnex = await initTenantKnex();
|
||||
|
||||
sysKnex.migrate.make(name).then((name) => {
|
||||
success(color.green(`Created Migration: ${name}`));
|
||||
}).catch(exit)
|
||||
sysKnex.migrate
|
||||
.make(name)
|
||||
.then((name) => {
|
||||
success(color.green(`Created Migration: ${name}`));
|
||||
})
|
||||
.catch(exit);
|
||||
});
|
||||
|
||||
commander
|
||||
.command('tenants:list')
|
||||
.description('Retrieve a list of all system tenants databases.')
|
||||
.action(async (cmd) => {
|
||||
try{
|
||||
try {
|
||||
const sysKnex = await initSystemKnex();
|
||||
const tenants = await getAllSystemTenants(sysKnex);
|
||||
|
||||
tenants.forEach((tenant) => {
|
||||
const dbName = `${config.tenant.db_name_prefix}${tenant.organizationId}`;
|
||||
console.log(`ID: ${tenant.id} | Organization ID: ${tenant.organizationId} | DB Name: ${dbName}`);
|
||||
console.log(
|
||||
`ID: ${tenant.id} | Organization ID: ${tenant.organizationId} | DB Name: ${dbName}`
|
||||
);
|
||||
});
|
||||
} catch(error) { exit(error); };
|
||||
} catch (error) {
|
||||
exit(error);
|
||||
}
|
||||
success('---');
|
||||
});
|
||||
|
||||
commander
|
||||
.command('tenants:migrate:rollback')
|
||||
.description('Rollback the last batch of tenants migrations.')
|
||||
.option('-t, --tenant_id [tenant_id]', 'Which tenant id do you migrate.')
|
||||
.action(async (cmd) => {
|
||||
try {
|
||||
const sysKnex = await initSystemKnex();
|
||||
const tenants = await getAllSystemTenants(sysKnex);
|
||||
const tenantsOrgsIds = tenants.map((tenant) => tenant.organizationId);
|
||||
.command('tenants:migrate:make <name>')
|
||||
.description('Created a named migration file to the tenants database.')
|
||||
.action(async (name) => {
|
||||
const sysKnex = await initTenantKnex();
|
||||
|
||||
if (cmd.tenant_id && tenantsOrgsIds.indexOf(cmd.tenant_id) === -1) {
|
||||
exit(`The given tenant id ${cmd.tenant_id} is not exists.`);
|
||||
}
|
||||
|
||||
const migrateOpers = [];
|
||||
const migrateTenant = async (organizationId) => {
|
||||
try {
|
||||
const tenantKnex = await initTenantKnex(organizationId);
|
||||
const [batchNo, _log] = await tenantKnex.migrate.rollback();
|
||||
const tenantDb = `${config.tenant.db_name_prefix}${organizationId}`;
|
||||
|
||||
if (_log.length === 0) {
|
||||
log(color.cyan('Already at the base migration'));
|
||||
}
|
||||
log(
|
||||
color.green(
|
||||
`Tenant: ${tenantDb} > Batch ${batchNo} rolled back: ${_log.length} migrations`
|
||||
) + (argv.verbose ? `\n${color.cyan(_log.join('\n'))}` : '')
|
||||
);
|
||||
log('---------------');
|
||||
} catch (error) { exit(error); }
|
||||
};
|
||||
|
||||
if (!cmd.tenant_id) {
|
||||
tenants.forEach((tenant) => {
|
||||
const oper = migrateTenant(tenant.organizationId);
|
||||
migrateOpers.push(oper);
|
||||
});
|
||||
} else {
|
||||
const oper = migrateTenant(cmd.tenant_id);
|
||||
migrateOpers.push(oper);
|
||||
}
|
||||
Promise.all(migrateOpers).then(() => {
|
||||
success('All tenants are rollbacked.');
|
||||
});
|
||||
} catch (error) { exit(error); }
|
||||
sysKnex.migrate
|
||||
.make(name)
|
||||
.then((name) => {
|
||||
success(color.green(`Created Migration: ${name}`));
|
||||
})
|
||||
.catch(exit);
|
||||
});
|
||||
|
||||
commander
|
||||
@@ -156,8 +183,8 @@ commander
|
||||
try {
|
||||
const sysKnex = await initSystemKnex();
|
||||
const tenants = await getAllSystemTenants(sysKnex);
|
||||
const tenantsOrgsIds = tenants.map(tenant => tenant.organizationId);
|
||||
|
||||
const tenantsOrgsIds = tenants.map((tenant) => tenant.organizationId);
|
||||
|
||||
if (cmd.tenant_id && tenantsOrgsIds.indexOf(cmd.tenant_id) === -1) {
|
||||
exit(`The given tenant id ${cmd.tenant_id} is not exists.`);
|
||||
}
|
||||
@@ -174,14 +201,15 @@ commander
|
||||
log(color.cyan('Already up to date'));
|
||||
}
|
||||
log(
|
||||
color.green(`Tenant ${tenantDb} > Batch ${batchNo} run: ${_log.length} migrations`) +
|
||||
(argv.verbose ? `\n${color.cyan(log.join('\n'))}` : '')
|
||||
color.green(
|
||||
`Tenant ${tenantDb} > Batch ${batchNo} run: ${_log.length} migrations`
|
||||
) + (argv.verbose ? `\n${color.cyan(log.join('\n'))}` : '')
|
||||
);
|
||||
log('-------------------');
|
||||
} catch (error) {
|
||||
log(error);
|
||||
}
|
||||
}
|
||||
};
|
||||
if (!cmd.tenant_id) {
|
||||
tenants.forEach((tenant) => {
|
||||
const oper = migrateTenant(tenant.organizationId);
|
||||
@@ -200,4 +228,55 @@ commander
|
||||
}
|
||||
});
|
||||
|
||||
commander.parse(process.argv);
|
||||
commander
|
||||
.command('tenants:migrate:rollback')
|
||||
.description('Rollback the last batch of tenants migrations.')
|
||||
.option('-t, --tenant_id [tenant_id]', 'Which tenant id do you migrate.')
|
||||
.action(async (cmd) => {
|
||||
try {
|
||||
const sysKnex = await initSystemKnex();
|
||||
const tenants = await getAllSystemTenants(sysKnex);
|
||||
const tenantsOrgsIds = tenants.map((tenant) => tenant.organizationId);
|
||||
|
||||
if (cmd.tenant_id && tenantsOrgsIds.indexOf(cmd.tenant_id) === -1) {
|
||||
exit(`The given tenant id ${cmd.tenant_id} is not exists.`);
|
||||
}
|
||||
|
||||
const migrateOpers = [];
|
||||
const migrateTenant = async (organizationId) => {
|
||||
try {
|
||||
const tenantKnex = await initTenantKnex(organizationId);
|
||||
const [batchNo, _log] = await tenantKnex.migrate.rollback();
|
||||
const tenantDb = `${config.tenant.db_name_prefix}${organizationId}`;
|
||||
|
||||
if (_log.length === 0) {
|
||||
log(color.cyan('Already at the base migration'));
|
||||
}
|
||||
log(
|
||||
color.green(
|
||||
`Tenant: ${tenantDb} > Batch ${batchNo} rolled back: ${_log.length} migrations`
|
||||
) + (argv.verbose ? `\n${color.cyan(_log.join('\n'))}` : '')
|
||||
);
|
||||
log('---------------');
|
||||
} catch (error) {
|
||||
exit(error);
|
||||
}
|
||||
};
|
||||
|
||||
if (!cmd.tenant_id) {
|
||||
tenants.forEach((tenant) => {
|
||||
const oper = migrateTenant(tenant.organizationId);
|
||||
migrateOpers.push(oper);
|
||||
});
|
||||
} else {
|
||||
const oper = migrateTenant(cmd.tenant_id);
|
||||
migrateOpers.push(oper);
|
||||
}
|
||||
Promise.all(migrateOpers).then(() => {
|
||||
success('All tenants are rollbacked.');
|
||||
});
|
||||
} catch (error) {
|
||||
exit(error);
|
||||
}
|
||||
});
|
||||
|
||||
4
packages/server/src/commands/index.ts
Normal file
4
packages/server/src/commands/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import commander from 'commander';
|
||||
import './bigcapital';
|
||||
|
||||
commander.parse();
|
||||
@@ -9,7 +9,7 @@ if (envFound.error) {
|
||||
throw new Error("⚠️ Couldn't find .env file ⚠️");
|
||||
}
|
||||
|
||||
export default {
|
||||
module.exports = {
|
||||
/**
|
||||
* Your favorite port
|
||||
*/
|
||||
|
||||
@@ -6,4 +6,4 @@ class HttpException extends Error {
|
||||
this.status = status;
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
|
||||
|
||||
export default class NoPaymentModelWithPricedPlan {
|
||||
|
||||
constructor() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
|
||||
|
||||
export default class NotAllowedChangeSubscriptionPlan {
|
||||
|
||||
constructor() {
|
||||
this.name = "NotAllowedChangeSubscriptionPlan";
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
|
||||
|
||||
export default class PaymentAmountInvalidWithPlan{
|
||||
constructor() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
|
||||
|
||||
export default class PaymentInputInvalid {
|
||||
constructor() {}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
import ServiceError from './ServiceError';
|
||||
|
||||
|
||||
export default class ServiceErrors {
|
||||
errors: ServiceError[];
|
||||
|
||||
@@ -9,7 +8,8 @@ export default class ServiceErrors {
|
||||
}
|
||||
|
||||
hasType(errorType: string) {
|
||||
return this.errors
|
||||
.some((error: ServiceError) => error.errorType === errorType);
|
||||
return this.errors.some(
|
||||
(error: ServiceError) => error.errorType === errorType
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
|
||||
|
||||
export default class TenantAlreadyInitialized {
|
||||
constructor() {
|
||||
|
||||
}
|
||||
}
|
||||
constructor() {}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,3 @@
|
||||
|
||||
|
||||
|
||||
|
||||
export default class TenantAlreadySeeded {
|
||||
constructor() {
|
||||
|
||||
}
|
||||
}
|
||||
constructor() {}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
|
||||
|
||||
export default class TenantDatabaseNotBuilt {
|
||||
constructor() {
|
||||
|
||||
}
|
||||
}
|
||||
constructor() {}
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
|
||||
export default class VoucherCodeRequired {
|
||||
constructor() {
|
||||
this.name = 'VoucherCodeRequired';
|
||||
}
|
||||
}
|
||||
@@ -1,25 +1,15 @@
|
||||
import NotAllowedChangeSubscriptionPlan from './NotAllowedChangeSubscriptionPlan';
|
||||
import ServiceError from './ServiceError';
|
||||
import ServiceErrors from './ServiceErrors';
|
||||
import NoPaymentModelWithPricedPlan from './NoPaymentModelWithPricedPlan';
|
||||
import PaymentInputInvalid from './PaymentInputInvalid';
|
||||
import PaymentAmountInvalidWithPlan from './PaymentAmountInvalidWithPlan';
|
||||
import TenantAlreadyInitialized from './TenantAlreadyInitialized';
|
||||
import TenantAlreadySeeded from './TenantAlreadySeeded';
|
||||
import TenantDBAlreadyExists from './TenantDBAlreadyExists';
|
||||
import TenantDatabaseNotBuilt from './TenantDatabaseNotBuilt';
|
||||
import VoucherCodeRequired from './VoucherCodeRequired';
|
||||
|
||||
export {
|
||||
NotAllowedChangeSubscriptionPlan,
|
||||
NoPaymentModelWithPricedPlan,
|
||||
PaymentAmountInvalidWithPlan,
|
||||
ServiceError,
|
||||
ServiceErrors,
|
||||
PaymentInputInvalid,
|
||||
TenantAlreadyInitialized,
|
||||
TenantAlreadySeeded,
|
||||
TenantDBAlreadyExists,
|
||||
TenantDatabaseNotBuilt,
|
||||
VoucherCodeRequired,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
import Container from 'typedi';
|
||||
import SubscriptionService from '@/services/Subscription/Subscription';
|
||||
|
||||
export default class MailNotificationSubscribeEnd {
|
||||
/**
|
||||
* Job handler.
|
||||
* @param {Job} job -
|
||||
*/
|
||||
handler(job) {
|
||||
const { tenantId, phoneNumber, remainingDays } = job.attrs.data;
|
||||
|
||||
const subscriptionService = Container.get(SubscriptionService);
|
||||
const Logger = Container.get('logger');
|
||||
|
||||
Logger.info(
|
||||
`Send mail notification subscription end soon - started: ${job.attrs.data}`
|
||||
);
|
||||
|
||||
try {
|
||||
subscriptionService.mailMessages.sendRemainingTrialPeriod(
|
||||
phoneNumber,
|
||||
remainingDays
|
||||
);
|
||||
Logger.info(
|
||||
`Send mail notification subscription end soon - finished: ${job.attrs.data}`
|
||||
);
|
||||
} catch (error) {
|
||||
Logger.info(
|
||||
`Send mail notification subscription end soon - failed: ${job.attrs.data}, error: ${e}`
|
||||
);
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
import Container from 'typedi';
|
||||
import SubscriptionService from '@/services/Subscription/Subscription';
|
||||
|
||||
export default class MailNotificationTrialEnd {
|
||||
/**
|
||||
*
|
||||
* @param {Job} job -
|
||||
*/
|
||||
handler(job) {
|
||||
const { tenantId, phoneNumber, remainingDays } = job.attrs.data;
|
||||
|
||||
const subscriptionService = Container.get(SubscriptionService);
|
||||
const Logger = Container.get('logger');
|
||||
|
||||
Logger.info(
|
||||
`Send mail notification subscription end soon - started: ${job.attrs.data}`
|
||||
);
|
||||
|
||||
try {
|
||||
subscriptionService.mailMessages.sendRemainingTrialPeriod(
|
||||
phoneNumber,
|
||||
remainingDays
|
||||
);
|
||||
Logger.info(
|
||||
`Send mail notification subscription end soon - finished: ${job.attrs.data}`
|
||||
);
|
||||
} catch (error) {
|
||||
Logger.info(
|
||||
`Send mail notification subscription end soon - failed: ${job.attrs.data}, error: ${e}`
|
||||
);
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
import Container from 'typedi';
|
||||
import SubscriptionService from '@/services/Subscription/Subscription';
|
||||
|
||||
export default class SMSNotificationSubscribeEnd {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Job}job
|
||||
*/
|
||||
handler(job) {
|
||||
const { tenantId, phoneNumber, remainingDays } = job.attrs.data;
|
||||
|
||||
const subscriptionService = Container.get(SubscriptionService);
|
||||
const Logger = Container.get('logger');
|
||||
|
||||
Logger.info(`Send SMS notification subscription end soon - started: ${job.attrs.data}`);
|
||||
|
||||
try {
|
||||
subscriptionService.smsMessages.sendRemainingSubscriptionPeriod(
|
||||
phoneNumber, remainingDays,
|
||||
);
|
||||
Logger.info(`Send SMS notification subscription end soon - finished: ${job.attrs.data}`);
|
||||
} catch(error) {
|
||||
Logger.info(`Send SMS notification subscription end soon - failed: ${job.attrs.data}, error: ${e}`);
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
import Container from 'typedi';
|
||||
import SubscriptionService from '@/services/Subscription/Subscription';
|
||||
|
||||
export default class SMSNotificationTrialEnd {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Job}job
|
||||
*/
|
||||
handler(job) {
|
||||
const { tenantId, phoneNumber, remainingDays } = job.attrs.data;
|
||||
|
||||
const subscriptionService = Container.get(SubscriptionService);
|
||||
const Logger = Container.get('logger');
|
||||
|
||||
Logger.info(`Send notification subscription end soon - started: ${job.attrs.data}`);
|
||||
|
||||
try {
|
||||
subscriptionService.smsMessages.sendRemainingTrialPeriod(
|
||||
phoneNumber, remainingDays,
|
||||
);
|
||||
Logger.info(`Send notification subscription end soon - finished: ${job.attrs.data}`);
|
||||
} catch(error) {
|
||||
Logger.info(`Send notification subscription end soon - failed: ${job.attrs.data}, error: ${e}`);
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
import { Container } from 'typedi';
|
||||
import LicenseService from '@/services/Payment/License';
|
||||
|
||||
export default class SendLicenseViaEmailJob {
|
||||
/**
|
||||
* Constructor method.
|
||||
* @param agenda
|
||||
*/
|
||||
constructor(agenda) {
|
||||
agenda.define(
|
||||
'send-license-via-email',
|
||||
{ priority: 'high', concurrency: 1, },
|
||||
this.handler,
|
||||
);
|
||||
}
|
||||
|
||||
public async handler(job, done: Function): Promise<void> {
|
||||
const Logger = Container.get('logger');
|
||||
const licenseService = Container.get(LicenseService);
|
||||
const { email, licenseCode } = job.attrs.data;
|
||||
|
||||
Logger.info(`[send_license_via_mail] started: ${job.attrs.data}`);
|
||||
|
||||
try {
|
||||
await licenseService.mailMessages.sendMailLicense(licenseCode, email);
|
||||
Logger.info(`[send_license_via_mail] completed: ${job.attrs.data}`);
|
||||
done();
|
||||
} catch(e) {
|
||||
Logger.error(`[send_license_via_mail] ${job.attrs.data}, error: ${e}`);
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
import { Container } from 'typedi';
|
||||
import LicenseService from '@/services/Payment/License';
|
||||
|
||||
export default class SendLicenseViaPhoneJob {
|
||||
/**
|
||||
* Constructor method.
|
||||
*/
|
||||
constructor(agenda) {
|
||||
agenda.define(
|
||||
'send-license-via-phone',
|
||||
{ priority: 'high', concurrency: 1, },
|
||||
this.handler,
|
||||
);
|
||||
}
|
||||
|
||||
public async handler(job, done: Function): Promise<void> {
|
||||
const { phoneNumber, licenseCode } = job.attrs.data;
|
||||
|
||||
const Logger = Container.get('logger');
|
||||
const licenseService = Container.get(LicenseService);
|
||||
|
||||
Logger.debug(`Send license via phone number - started: ${job.attrs.data}`);
|
||||
|
||||
try {
|
||||
await licenseService.smsMessages.sendLicenseSMSMessage(phoneNumber, licenseCode);
|
||||
Logger.debug(`Send license via phone number - completed: ${job.attrs.data}`);
|
||||
done();
|
||||
} catch(e) {
|
||||
Logger.error(`Send license via phone number: ${job.attrs.data}, error: ${e}`);
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,12 +4,6 @@ import WelcomeSMSJob from 'jobs/WelcomeSMS';
|
||||
import ResetPasswordMailJob from 'jobs/ResetPasswordMail';
|
||||
import ComputeItemCost from 'jobs/ComputeItemCost';
|
||||
import RewriteInvoicesJournalEntries from 'jobs/writeInvoicesJEntries';
|
||||
import SendLicenseViaPhoneJob from 'jobs/SendLicensePhone';
|
||||
import SendLicenseViaEmailJob from 'jobs/SendLicenseEmail';
|
||||
import SendSMSNotificationSubscribeEnd from 'jobs/SMSNotificationSubscribeEnd';
|
||||
import SendSMSNotificationTrialEnd from 'jobs/SMSNotificationTrialEnd';
|
||||
import SendMailNotificationSubscribeEnd from 'jobs/MailNotificationSubscribeEnd';
|
||||
import SendMailNotificationTrialEnd from 'jobs/MailNotificationTrialEnd';
|
||||
import UserInviteMailJob from 'jobs/UserInviteMail';
|
||||
import OrganizationSetupJob from 'jobs/OrganizationSetup';
|
||||
import OrganizationUpgrade from 'jobs/OrganizationUpgrade';
|
||||
@@ -20,33 +14,11 @@ export default ({ agenda }: { agenda: Agenda }) => {
|
||||
new ResetPasswordMailJob(agenda);
|
||||
new WelcomeSMSJob(agenda);
|
||||
new UserInviteMailJob(agenda);
|
||||
new SendLicenseViaEmailJob(agenda);
|
||||
new SendLicenseViaPhoneJob(agenda);
|
||||
new ComputeItemCost(agenda);
|
||||
new RewriteInvoicesJournalEntries(agenda);
|
||||
new OrganizationSetupJob(agenda);
|
||||
new OrganizationUpgrade(agenda);
|
||||
new SmsNotification(agenda);
|
||||
|
||||
agenda.define(
|
||||
'send-sms-notification-subscribe-end',
|
||||
{ priority: 'nromal', concurrency: 1, },
|
||||
new SendSMSNotificationSubscribeEnd().handler,
|
||||
);
|
||||
agenda.define(
|
||||
'send-sms-notification-trial-end',
|
||||
{ priority: 'normal', concurrency: 1, },
|
||||
new SendSMSNotificationTrialEnd().handler,
|
||||
);
|
||||
agenda.define(
|
||||
'send-mail-notification-subscribe-end',
|
||||
{ priority: 'high', concurrency: 1, },
|
||||
new SendMailNotificationSubscribeEnd().handler
|
||||
);
|
||||
agenda.define(
|
||||
'send-mail-notification-trial-end',
|
||||
{ priority: 'high', concurrency: 1, },
|
||||
new SendMailNotificationTrialEnd().handler
|
||||
);
|
||||
agenda.start();
|
||||
};
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import Container from 'typedi';
|
||||
import {
|
||||
SystemUserRepository,
|
||||
SubscriptionRepository,
|
||||
TenantRepository,
|
||||
} from '@/system/repositories';
|
||||
|
||||
@@ -11,7 +10,6 @@ export default () => {
|
||||
|
||||
return {
|
||||
systemUserRepository: new SystemUserRepository(knex, cache),
|
||||
subscriptionRepository: new SubscriptionRepository(knex, cache),
|
||||
tenantRepository: new TenantRepository(knex, cache),
|
||||
};
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import TenancyService from '@/services/Tenancy/TenancyService';
|
||||
import { ServiceError } from '@/exceptions';
|
||||
import { IAccountDTO, IAccount, IAccountCreateDTO } from '@/interfaces';
|
||||
import AccountTypesUtils from '@/lib/AccountTypes';
|
||||
import { ERRORS } from './constants';
|
||||
import { ERRORS, MAX_ACCOUNTS_CHART_DEPTH } from './constants';
|
||||
|
||||
@Service()
|
||||
export class CommandAccountValidators {
|
||||
@@ -154,13 +154,13 @@ export class CommandAccountValidators {
|
||||
* parent account.
|
||||
* @param {IAccountCreateDTO} accountDTO
|
||||
* @param {IAccount} parentAccount
|
||||
* @param {string} baseCurrency -
|
||||
* @param {string} baseCurrency -
|
||||
* @throws {ServiceError(ERRORS.ACCOUNT_CURRENCY_NOT_SAME_PARENT_ACCOUNT)}
|
||||
*/
|
||||
public validateCurrentSameParentAccount = (
|
||||
accountDTO: IAccountCreateDTO,
|
||||
parentAccount: IAccount,
|
||||
baseCurrency: string,
|
||||
baseCurrency: string
|
||||
) => {
|
||||
// If the account DTO currency not assigned and the parent account has no base currency.
|
||||
if (
|
||||
@@ -208,4 +208,24 @@ export class CommandAccountValidators {
|
||||
}
|
||||
return account;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the max depth level of accounts chart.
|
||||
* @param {numebr} tenantId - Tenant id.
|
||||
* @param {number} parentAccountId - Parent account id.
|
||||
*/
|
||||
public async validateMaxParentAccountDepthLevels(
|
||||
tenantId: number,
|
||||
parentAccountId: number
|
||||
) {
|
||||
const { accountRepository } = this.tenancy.repositories(tenantId);
|
||||
|
||||
const accountsGraph = await accountRepository.getDependencyGraph();
|
||||
|
||||
const parentDependantsIds = accountsGraph.dependantsOf(parentAccountId);
|
||||
|
||||
if (parentDependantsIds.length >= MAX_ACCOUNTS_CHART_DEPTH) {
|
||||
throw new ServiceError(ERRORS.PARENT_ACCOUNT_EXCEEDED_THE_DEPTH_LEVEL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,6 +70,11 @@ export class CreateAccount {
|
||||
parentAccount,
|
||||
baseCurrency
|
||||
);
|
||||
// Validates the max depth level of accounts chart.
|
||||
await this.validator.validateMaxParentAccountDepthLevels(
|
||||
tenantId,
|
||||
accountDTO.parentAccountId
|
||||
);
|
||||
}
|
||||
// Validates the given account type supports the multi-currency.
|
||||
this.validator.validateAccountTypeSupportCurrency(accountDTO, baseCurrency);
|
||||
|
||||
@@ -5,6 +5,7 @@ import TenancyService from '@/services/Tenancy/TenancyService';
|
||||
import DynamicListingService from '@/services/DynamicListing/DynamicListService';
|
||||
import { AccountTransformer } from './AccountTransform';
|
||||
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
||||
import { flatToNestedArray } from '@/utils';
|
||||
|
||||
@Service()
|
||||
export class GetAccounts {
|
||||
@@ -53,11 +54,17 @@ export class GetAccounts {
|
||||
builder.modify('inactiveMode', filter.inactiveMode);
|
||||
});
|
||||
// Retrievs the formatted accounts collection.
|
||||
const transformedAccounts = await this.transformer.transform(
|
||||
const preTransformedAccounts = await this.transformer.transform(
|
||||
tenantId,
|
||||
accounts,
|
||||
new AccountTransformer()
|
||||
);
|
||||
// Transform accounts to nested array.
|
||||
const transformedAccounts = flatToNestedArray(preTransformedAccounts, {
|
||||
id: 'id',
|
||||
parentId: 'parentAccountId',
|
||||
});
|
||||
|
||||
return {
|
||||
accounts: transformedAccounts,
|
||||
filterMeta: dynamicList.getResponseMeta(),
|
||||
|
||||
@@ -13,8 +13,12 @@ export const ERRORS = {
|
||||
CLOSE_ACCOUNT_AND_TO_ACCOUNT_NOT_SAME_TYPE:
|
||||
'close_account_and_to_account_not_same_type',
|
||||
ACCOUNTS_NOT_FOUND: 'accounts_not_found',
|
||||
ACCOUNT_TYPE_NOT_SUPPORTS_MULTI_CURRENCY: 'ACCOUNT_TYPE_NOT_SUPPORTS_MULTI_CURRENCY',
|
||||
ACCOUNT_CURRENCY_NOT_SAME_PARENT_ACCOUNT: 'ACCOUNT_CURRENCY_NOT_SAME_PARENT_ACCOUNT',
|
||||
ACCOUNT_TYPE_NOT_SUPPORTS_MULTI_CURRENCY:
|
||||
'ACCOUNT_TYPE_NOT_SUPPORTS_MULTI_CURRENCY',
|
||||
ACCOUNT_CURRENCY_NOT_SAME_PARENT_ACCOUNT:
|
||||
'ACCOUNT_CURRENCY_NOT_SAME_PARENT_ACCOUNT',
|
||||
PARENT_ACCOUNT_EXCEEDED_THE_DEPTH_LEVEL:
|
||||
'PARENT_ACCOUNT_EXCEEDED_THE_DEPTH_LEVEL',
|
||||
};
|
||||
|
||||
// Default views columns.
|
||||
@@ -27,6 +31,8 @@ export const DEFAULT_VIEW_COLUMNS = [
|
||||
{ key: 'currencyCode', label: 'Currency' },
|
||||
];
|
||||
|
||||
export const MAX_ACCOUNTS_CHART_DEPTH = 5;
|
||||
|
||||
// Accounts default views.
|
||||
export const DEFAULT_VIEWS = [
|
||||
{
|
||||
@@ -43,7 +49,12 @@ export const DEFAULT_VIEWS = [
|
||||
slug: 'liabilities',
|
||||
rolesLogicExpression: '1',
|
||||
roles: [
|
||||
{ fieldKey: 'root_type', index: 1, comparator: 'equals', value: 'liability' },
|
||||
{
|
||||
fieldKey: 'root_type',
|
||||
index: 1,
|
||||
comparator: 'equals',
|
||||
value: 'liability',
|
||||
},
|
||||
],
|
||||
columns: DEFAULT_VIEW_COLUMNS,
|
||||
},
|
||||
@@ -52,7 +63,12 @@ export const DEFAULT_VIEWS = [
|
||||
slug: 'equity',
|
||||
rolesLogicExpression: '1',
|
||||
roles: [
|
||||
{ fieldKey: 'root_type', index: 1, comparator: 'equals', value: 'equity' },
|
||||
{
|
||||
fieldKey: 'root_type',
|
||||
index: 1,
|
||||
comparator: 'equals',
|
||||
value: 'equity',
|
||||
},
|
||||
],
|
||||
columns: DEFAULT_VIEW_COLUMNS,
|
||||
},
|
||||
@@ -61,7 +77,12 @@ export const DEFAULT_VIEWS = [
|
||||
slug: 'income',
|
||||
rolesLogicExpression: '1',
|
||||
roles: [
|
||||
{ fieldKey: 'root_type', index: 1, comparator: 'equals', value: 'income' },
|
||||
{
|
||||
fieldKey: 'root_type',
|
||||
index: 1,
|
||||
comparator: 'equals',
|
||||
value: 'income',
|
||||
},
|
||||
],
|
||||
columns: DEFAULT_VIEW_COLUMNS,
|
||||
},
|
||||
@@ -70,7 +91,12 @@ export const DEFAULT_VIEWS = [
|
||||
slug: 'expenses',
|
||||
rolesLogicExpression: '1',
|
||||
roles: [
|
||||
{ fieldKey: 'root_type', index: 1, comparator: 'equals', value: 'expense' },
|
||||
{
|
||||
fieldKey: 'root_type',
|
||||
index: 1,
|
||||
comparator: 'equals',
|
||||
value: 'expense',
|
||||
},
|
||||
],
|
||||
columns: DEFAULT_VIEW_COLUMNS,
|
||||
},
|
||||
|
||||
@@ -150,7 +150,6 @@ export default class OrganizationService {
|
||||
public async currentOrganization(tenantId: number): Promise<ITenant> {
|
||||
const tenant = await Tenant.query()
|
||||
.findById(tenantId)
|
||||
.withGraphFetched('subscriptions')
|
||||
.withGraphFetched('metadata');
|
||||
|
||||
this.throwIfTenantNotExists(tenant);
|
||||
|
||||
@@ -1,185 +0,0 @@
|
||||
import { Service, Container, Inject } from 'typedi';
|
||||
import cryptoRandomString from 'crypto-random-string';
|
||||
import { times } from 'lodash';
|
||||
import { License, Plan } from '@/system/models';
|
||||
import { ILicense, ISendLicenseDTO } from '@/interfaces';
|
||||
import LicenseMailMessages from '@/services/Payment/LicenseMailMessages';
|
||||
import LicenseSMSMessages from '@/services/Payment/LicenseSMSMessages';
|
||||
import { ServiceError } from '@/exceptions';
|
||||
|
||||
const ERRORS = {
|
||||
PLAN_NOT_FOUND: 'PLAN_NOT_FOUND',
|
||||
LICENSE_NOT_FOUND: 'LICENSE_NOT_FOUND',
|
||||
LICENSE_ALREADY_DISABLED: 'LICENSE_ALREADY_DISABLED',
|
||||
NO_AVALIABLE_LICENSE_CODE: 'NO_AVALIABLE_LICENSE_CODE',
|
||||
};
|
||||
|
||||
@Service()
|
||||
export default class LicenseService {
|
||||
@Inject()
|
||||
smsMessages: LicenseSMSMessages;
|
||||
|
||||
@Inject()
|
||||
mailMessages: LicenseMailMessages;
|
||||
|
||||
/**
|
||||
* Validate the plan existance on the storage.
|
||||
* @param {number} tenantId -
|
||||
* @param {string} planSlug - Plan slug.
|
||||
*/
|
||||
private async getPlanOrThrowError(planSlug: string) {
|
||||
const foundPlan = await Plan.query().where('slug', planSlug).first();
|
||||
|
||||
if (!foundPlan) {
|
||||
throw new ServiceError(ERRORS.PLAN_NOT_FOUND);
|
||||
}
|
||||
return foundPlan;
|
||||
}
|
||||
|
||||
/**
|
||||
* Valdiate the license existance on the storage.
|
||||
* @param {number} licenseId - License id.
|
||||
*/
|
||||
private async getLicenseOrThrowError(licenseId: number) {
|
||||
const foundLicense = await License.query().findById(licenseId);
|
||||
|
||||
if (!foundLicense) {
|
||||
throw new ServiceError(ERRORS.LICENSE_NOT_FOUND);
|
||||
}
|
||||
return foundLicense;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates whether the license id is disabled.
|
||||
* @param {ILicense} license
|
||||
*/
|
||||
private validateNotDisabledLicense(license: ILicense) {
|
||||
if (license.disabledAt) {
|
||||
throw new ServiceError(ERRORS.LICENSE_ALREADY_DISABLED);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the license code in the given period.
|
||||
* @param {number} licensePeriod
|
||||
* @return {Promise<ILicense>}
|
||||
*/
|
||||
public async generateLicense(
|
||||
licensePeriod: number,
|
||||
periodInterval: string = 'days',
|
||||
planSlug: string
|
||||
): ILicense {
|
||||
let licenseCode: string;
|
||||
let repeat: boolean = true;
|
||||
|
||||
// Retrieve plan or throw not found error.
|
||||
const plan = await this.getPlanOrThrowError(planSlug);
|
||||
|
||||
while (repeat) {
|
||||
licenseCode = cryptoRandomString({ length: 10, type: 'numeric' });
|
||||
const foundLicenses = await License.query().where(
|
||||
'license_code',
|
||||
licenseCode
|
||||
);
|
||||
|
||||
if (foundLicenses.length === 0) {
|
||||
repeat = false;
|
||||
}
|
||||
}
|
||||
return License.query().insert({
|
||||
licenseCode,
|
||||
licensePeriod,
|
||||
periodInterval,
|
||||
planId: plan.id,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates licenses.
|
||||
* @param {number} loop
|
||||
* @param {number} licensePeriod
|
||||
* @param {string} periodInterval
|
||||
* @param {number} planId
|
||||
*/
|
||||
public async generateLicenses(
|
||||
loop = 1,
|
||||
licensePeriod: number,
|
||||
periodInterval: string = 'days',
|
||||
planSlug: string
|
||||
) {
|
||||
const asyncOpers: Promise<any>[] = [];
|
||||
|
||||
times(loop, () => {
|
||||
const generateOper = this.generateLicense(
|
||||
licensePeriod,
|
||||
periodInterval,
|
||||
planSlug
|
||||
);
|
||||
asyncOpers.push(generateOper);
|
||||
});
|
||||
return Promise.all(asyncOpers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables the given license id on the storage.
|
||||
* @param {string} licenseSlug - License slug.
|
||||
* @return {Promise}
|
||||
*/
|
||||
public async disableLicense(licenseId: number) {
|
||||
const license = await this.getLicenseOrThrowError(licenseId);
|
||||
|
||||
this.validateNotDisabledLicense(license);
|
||||
|
||||
return License.markLicenseAsDisabled(license.id, 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the given license id from the storage.
|
||||
* @param licenseSlug {string} - License slug.
|
||||
*/
|
||||
public async deleteLicense(licenseSlug: string) {
|
||||
const license = await this.getPlanOrThrowError(licenseSlug);
|
||||
|
||||
return License.query().where('id', license.id).delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends license code to the given customer via SMS or mail message.
|
||||
* @param {string} licenseCode - License code.
|
||||
* @param {string} phoneNumber - Phone number.
|
||||
* @param {string} email - Email address.
|
||||
*/
|
||||
public async sendLicenseToCustomer(sendLicense: ISendLicenseDTO) {
|
||||
const agenda = Container.get('agenda');
|
||||
const { phoneNumber, email, period, periodInterval } = sendLicense;
|
||||
|
||||
// Retreive plan details byt the given plan slug.
|
||||
const plan = await this.getPlanOrThrowError(sendLicense.planSlug);
|
||||
|
||||
const license = await License.query()
|
||||
.modify('filterActiveLicense')
|
||||
.where('license_period', period)
|
||||
.where('period_interval', periodInterval)
|
||||
.where('plan_id', plan.id)
|
||||
.first();
|
||||
|
||||
if (!license) {
|
||||
throw new ServiceError(ERRORS.NO_AVALIABLE_LICENSE_CODE)
|
||||
}
|
||||
// Mark the license as used.
|
||||
await License.markLicenseAsSent(license.licenseCode);
|
||||
|
||||
if (sendLicense.email) {
|
||||
await agenda.schedule('1 second', 'send-license-via-email', {
|
||||
licenseCode: license.licenseCode,
|
||||
email,
|
||||
});
|
||||
}
|
||||
if (phoneNumber) {
|
||||
await agenda.schedule('1 second', 'send-license-via-phone', {
|
||||
licenseCode: license.licenseCode,
|
||||
phoneNumber,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
import { Container } from 'typedi';
|
||||
import Mail from '@/lib/Mail';
|
||||
import config from '@/config';
|
||||
export default class SubscriptionMailMessages {
|
||||
/**
|
||||
* Send license code to the given mail address.
|
||||
* @param {string} licenseCode
|
||||
* @param {email} email
|
||||
*/
|
||||
public async sendMailLicense(licenseCode: string, email: string) {
|
||||
const Logger = Container.get('logger');
|
||||
|
||||
const mail = new Mail()
|
||||
.setView('mail/LicenseReceive.html')
|
||||
.setSubject('Bigcapital - License code')
|
||||
.setTo(email)
|
||||
.setData({
|
||||
licenseCode,
|
||||
successEmail: config.customerSuccess.email,
|
||||
successPhoneNumber: config.customerSuccess.phoneNumber,
|
||||
});
|
||||
|
||||
await mail.send();
|
||||
Logger.info('[license_mail] sent successfully.');
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
import { License } from '@/system/models';
|
||||
import PaymentMethod from '@/services/Payment/PaymentMethod';
|
||||
import { Plan } from '@/system/models';
|
||||
import { IPaymentMethod, ILicensePaymentModel } from '@/interfaces';
|
||||
import {
|
||||
PaymentInputInvalid,
|
||||
PaymentAmountInvalidWithPlan,
|
||||
VoucherCodeRequired,
|
||||
} from '@/exceptions';
|
||||
|
||||
export default class LicensePaymentMethod
|
||||
extends PaymentMethod
|
||||
implements IPaymentMethod
|
||||
{
|
||||
/**
|
||||
* Payment subscription of organization via license code.
|
||||
* @param {ILicensePaymentModel} licensePaymentModel -
|
||||
*/
|
||||
public async payment(licensePaymentModel: ILicensePaymentModel, plan: Plan) {
|
||||
this.validateLicensePaymentModel(licensePaymentModel);
|
||||
|
||||
const license = await this.getLicenseOrThrowInvalid(licensePaymentModel);
|
||||
this.validatePaymentAmountWithPlan(license, plan);
|
||||
|
||||
// Mark the license code as used.
|
||||
return License.markLicenseAsUsed(licensePaymentModel.licenseCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the license code activation on the storage.
|
||||
* @param {ILicensePaymentModel} licensePaymentModel -
|
||||
*/
|
||||
private async getLicenseOrThrowInvalid(
|
||||
licensePaymentModel: ILicensePaymentModel
|
||||
) {
|
||||
const foundLicense = await License.query()
|
||||
.modify('filterActiveLicense')
|
||||
.where('license_code', licensePaymentModel.licenseCode)
|
||||
.first();
|
||||
|
||||
if (!foundLicense) {
|
||||
throw new PaymentInputInvalid();
|
||||
}
|
||||
return foundLicense;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the payment amount with given plan price.
|
||||
* @param {License} license
|
||||
* @param {Plan} plan
|
||||
*/
|
||||
private validatePaymentAmountWithPlan(license: License, plan: Plan) {
|
||||
if (license.planId !== plan.id) {
|
||||
throw new PaymentAmountInvalidWithPlan();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate voucher payload.
|
||||
* @param {ILicensePaymentModel} licenseModel -
|
||||
*/
|
||||
private validateLicensePaymentModel(licenseModel: ILicensePaymentModel) {
|
||||
if (!licenseModel || !licenseModel.licenseCode) {
|
||||
throw new VoucherCodeRequired();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
import { Container, Inject } from 'typedi';
|
||||
import SMSClient from '@/services/SMSClient';
|
||||
|
||||
export default class SubscriptionSMSMessages {
|
||||
@Inject('SMSClient')
|
||||
smsClient: SMSClient;
|
||||
|
||||
/**
|
||||
* Sends license code to the given phone number via SMS message.
|
||||
* @param {string} phoneNumber
|
||||
* @param {string} licenseCode
|
||||
*/
|
||||
public async sendLicenseSMSMessage(phoneNumber: string, licenseCode: string) {
|
||||
const message: string = `Your license card number: ${licenseCode}. If you need any help please contact us. Bigcapital.`;
|
||||
return this.smsClient.sendMessage(phoneNumber, message);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
import moment from 'moment';
|
||||
import { IPaymentModel } from '@/interfaces';
|
||||
|
||||
export default class PaymentMethod implements IPaymentModel {
|
||||
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
import { IPaymentMethod, IPaymentContext } from "interfaces";
|
||||
import { Plan } from '@/system/models';
|
||||
|
||||
export default class PaymentContext<PaymentModel> implements IPaymentContext{
|
||||
paymentMethod: IPaymentMethod;
|
||||
|
||||
/**
|
||||
* Constructor method.
|
||||
* @param {IPaymentMethod} paymentMethod
|
||||
*/
|
||||
constructor(paymentMethod: IPaymentMethod) {
|
||||
this.paymentMethod = paymentMethod;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {<PaymentModel>} paymentModel
|
||||
*/
|
||||
makePayment(paymentModel: PaymentModel, plan: Plan) {
|
||||
return this.paymentMethod.payment(paymentModel, plan);
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
import { Service } from "typedi";
|
||||
|
||||
@Service()
|
||||
export default class SubscriptionMailMessages {
|
||||
/**
|
||||
*
|
||||
* @param phoneNumber
|
||||
* @param remainingDays
|
||||
*/
|
||||
public async sendRemainingSubscriptionPeriod(phoneNumber: string, remainingDays: number) {
|
||||
const message: string = `
|
||||
Your remaining subscription is ${remainingDays} days,
|
||||
please renew your subscription before expire.
|
||||
`;
|
||||
this.smsClient.sendMessage(phoneNumber, message);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param phoneNumber
|
||||
* @param remainingDays
|
||||
*/
|
||||
public async sendRemainingTrialPeriod(phoneNumber: string, remainingDays: number) {
|
||||
const message: string = `
|
||||
Your remaining free trial is ${remainingDays} days,
|
||||
please subscription before ends, if you have any quation to contact us.`;
|
||||
|
||||
this.smsClient.sendMessage(phoneNumber, message);
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
import { Service, Inject } from 'typedi';
|
||||
import SMSClient from '@/services/SMSClient';
|
||||
|
||||
@Service()
|
||||
export default class SubscriptionSMSMessages {
|
||||
@Inject('SMSClient')
|
||||
smsClient: SMSClient;
|
||||
|
||||
/**
|
||||
* Send remaining subscription period SMS message.
|
||||
* @param {string} phoneNumber -
|
||||
* @param {number} remainingDays -
|
||||
*/
|
||||
public async sendRemainingSubscriptionPeriod(
|
||||
phoneNumber: string,
|
||||
remainingDays: number
|
||||
): Promise<void> {
|
||||
const message: string = `
|
||||
Your remaining subscription is ${remainingDays} days,
|
||||
please renew your subscription before expire.
|
||||
`;
|
||||
this.smsClient.sendMessage(phoneNumber, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send remaining trial period SMS message.
|
||||
* @param {string} phoneNumber -
|
||||
* @param {number} remainingDays -
|
||||
*/
|
||||
public async sendRemainingTrialPeriod(
|
||||
phoneNumber: string,
|
||||
remainingDays: number
|
||||
): Promise<void> {
|
||||
const message: string = `
|
||||
Your remaining free trial is ${remainingDays} days,
|
||||
please subscription before ends, if you have any quation to contact us.`;
|
||||
|
||||
this.smsClient.sendMessage(phoneNumber, message);
|
||||
}
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
import { Inject } from 'typedi';
|
||||
import { Tenant, Plan } from '@/system/models';
|
||||
import { IPaymentContext } from '@/interfaces';
|
||||
import { NotAllowedChangeSubscriptionPlan } from '@/exceptions';
|
||||
|
||||
export default class Subscription<PaymentModel> {
|
||||
paymentContext: IPaymentContext | null;
|
||||
|
||||
@Inject('logger')
|
||||
logger: any;
|
||||
|
||||
/**
|
||||
* Constructor method.
|
||||
* @param {IPaymentContext}
|
||||
*/
|
||||
constructor(payment?: IPaymentContext) {
|
||||
this.paymentContext = payment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Give the tenant a new subscription.
|
||||
* @param {Tenant} tenant
|
||||
* @param {Plan} plan
|
||||
* @param {string} invoiceInterval
|
||||
* @param {number} invoicePeriod
|
||||
* @param {string} subscriptionSlug
|
||||
*/
|
||||
protected async newSubscribtion(
|
||||
tenant,
|
||||
plan,
|
||||
invoiceInterval: string,
|
||||
invoicePeriod: number,
|
||||
subscriptionSlug: string = 'main'
|
||||
) {
|
||||
const subscription = await tenant
|
||||
.$relatedQuery('subscriptions')
|
||||
.modify('subscriptionBySlug', subscriptionSlug)
|
||||
.first();
|
||||
|
||||
// No allowed to re-new the the subscription while the subscription is active.
|
||||
if (subscription && subscription.active()) {
|
||||
throw new NotAllowedChangeSubscriptionPlan();
|
||||
|
||||
// In case there is already subscription associated to the given tenant renew it.
|
||||
} else if (subscription && subscription.inactive()) {
|
||||
await subscription.renew(invoiceInterval, invoicePeriod);
|
||||
|
||||
// No stored past tenant subscriptions create new one.
|
||||
} else {
|
||||
await tenant.newSubscription(
|
||||
plan.id,
|
||||
invoiceInterval,
|
||||
invoicePeriod,
|
||||
subscriptionSlug
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscripe to the given plan.
|
||||
* @param {Plan} plan
|
||||
* @throws {NotAllowedChangeSubscriptionPlan}
|
||||
*/
|
||||
public async subscribe(
|
||||
tenant: Tenant,
|
||||
plan: Plan,
|
||||
paymentModel?: PaymentModel,
|
||||
subscriptionSlug: string = 'main'
|
||||
) {
|
||||
await this.paymentContext.makePayment(paymentModel, plan);
|
||||
|
||||
return this.newSubscribtion(
|
||||
tenant,
|
||||
plan,
|
||||
plan.invoiceInterval,
|
||||
plan.invoicePeriod,
|
||||
subscriptionSlug
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
import moment from 'moment';
|
||||
|
||||
export default class SubscriptionPeriod {
|
||||
start: Date;
|
||||
end: Date;
|
||||
interval: string;
|
||||
count: number;
|
||||
|
||||
/**
|
||||
* Constructor method.
|
||||
* @param {string} interval -
|
||||
* @param {number} count -
|
||||
* @param {Date} start -
|
||||
*/
|
||||
constructor(interval: string = 'month', count: number, start?: Date) {
|
||||
this.interval = interval;
|
||||
this.count = count;
|
||||
this.start = start;
|
||||
|
||||
if (!start) {
|
||||
this.start = moment().toDate();
|
||||
}
|
||||
this.end = moment(start).add(count, interval).toDate();
|
||||
}
|
||||
|
||||
getStartDate() {
|
||||
return this.start;
|
||||
}
|
||||
|
||||
getEndDate() {
|
||||
return this.end;
|
||||
}
|
||||
|
||||
getInterval() {
|
||||
return this.interval;
|
||||
}
|
||||
|
||||
getIntervalCount() {
|
||||
return this.interval;
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { Plan, PlanSubscription, Tenant } from '@/system/models';
|
||||
import Subscription from '@/services/Subscription/Subscription';
|
||||
import LicensePaymentMethod from '@/services/Payment/LicensePaymentMethod';
|
||||
import PaymentContext from '@/services/Payment';
|
||||
import SubscriptionSMSMessages from '@/services/Subscription/SMSMessages';
|
||||
import SubscriptionMailMessages from '@/services/Subscription/MailMessages';
|
||||
import { ILicensePaymentModel } from '@/interfaces';
|
||||
import SubscriptionViaLicense from './SubscriptionViaLicense';
|
||||
|
||||
@Service()
|
||||
export default class SubscriptionService {
|
||||
@Inject()
|
||||
smsMessages: SubscriptionSMSMessages;
|
||||
|
||||
@Inject()
|
||||
mailMessages: SubscriptionMailMessages;
|
||||
|
||||
@Inject('logger')
|
||||
logger: any;
|
||||
|
||||
@Inject('repositories')
|
||||
sysRepositories: any;
|
||||
|
||||
/**
|
||||
* Handles the payment process via license code and than subscribe to
|
||||
* the given tenant.
|
||||
* @param {number} tenantId
|
||||
* @param {String} planSlug
|
||||
* @param {string} licenseCode
|
||||
* @return {Promise}
|
||||
*/
|
||||
public async subscriptionViaLicense(
|
||||
tenantId: number,
|
||||
planSlug: string,
|
||||
paymentModel: ILicensePaymentModel,
|
||||
subscriptionSlug: string = 'main'
|
||||
) {
|
||||
// Retrieve plan details.
|
||||
const plan = await Plan.query().findOne('slug', planSlug);
|
||||
|
||||
// Retrieve tenant details.
|
||||
const tenant = await Tenant.query().findById(tenantId);
|
||||
|
||||
// License payment method.
|
||||
const paymentViaLicense = new LicensePaymentMethod();
|
||||
|
||||
// Payment context.
|
||||
const paymentContext = new PaymentContext(paymentViaLicense);
|
||||
|
||||
// Subscription.
|
||||
const subscription = new SubscriptionViaLicense(paymentContext);
|
||||
|
||||
// Subscribe.
|
||||
await subscription.subscribe(tenant, plan, paymentModel, subscriptionSlug);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all subscription of the given tenant.
|
||||
* @param {number} tenantId
|
||||
*/
|
||||
public async getSubscriptions(tenantId: number) {
|
||||
const subscriptions = await PlanSubscription.query().where(
|
||||
'tenant_id',
|
||||
tenantId
|
||||
);
|
||||
return subscriptions;
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
import { License, Tenant, Plan } from '@/system/models';
|
||||
import Subscription from './Subscription';
|
||||
import { PaymentModel } from '@/interfaces';
|
||||
|
||||
export default class SubscriptionViaLicense extends Subscription<PaymentModel> {
|
||||
/**
|
||||
* Subscripe to the given plan.
|
||||
* @param {Plan} plan
|
||||
* @throws {NotAllowedChangeSubscriptionPlan}
|
||||
*/
|
||||
public async subscribe(
|
||||
tenant: Tenant,
|
||||
plan: Plan,
|
||||
paymentModel?: PaymentModel,
|
||||
subscriptionSlug: string = 'main'
|
||||
): Promise<void> {
|
||||
await this.paymentContext.makePayment(paymentModel, plan);
|
||||
|
||||
return this.newSubscriptionFromLicense(
|
||||
tenant,
|
||||
plan,
|
||||
paymentModel.licenseCode,
|
||||
subscriptionSlug
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* New subscription from the given license.
|
||||
* @param {Tanant} tenant
|
||||
* @param {Plab} plan
|
||||
* @param {string} licenseCode
|
||||
* @param {string} subscriptionSlug
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
private async newSubscriptionFromLicense(
|
||||
tenant,
|
||||
plan,
|
||||
licenseCode: string,
|
||||
subscriptionSlug: string = 'main'
|
||||
): Promise<void> {
|
||||
// License information.
|
||||
const licenseInfo = await License.query().findOne(
|
||||
'licenseCode',
|
||||
licenseCode
|
||||
);
|
||||
return this.newSubscribtion(
|
||||
tenant,
|
||||
plan,
|
||||
licenseInfo.periodInterval,
|
||||
licenseInfo.licensePeriod,
|
||||
subscriptionSlug
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
|
||||
exports.up = function(knex) {
|
||||
return knex.schema.createTable('subscriptions_plans', table => {
|
||||
table.increments();
|
||||
|
||||
table.string('name');
|
||||
table.string('description');
|
||||
table.decimal('price');
|
||||
table.string('currency', 3);
|
||||
|
||||
table.integer('trial_period');
|
||||
table.string('trial_interval');
|
||||
|
||||
table.integer('invoice_period');
|
||||
table.string('invoice_interval');
|
||||
table.timestamps();
|
||||
});
|
||||
};
|
||||
|
||||
exports.down = function(knex) {
|
||||
return knex.schema.dropTableIfExists('subscriptions_plans')
|
||||
};
|
||||
@@ -1,30 +0,0 @@
|
||||
|
||||
exports.up = function(knex) {
|
||||
return knex.schema.createTable('subscription_plans', table => {
|
||||
table.increments();
|
||||
table.string('slug');
|
||||
table.string('name');
|
||||
table.string('desc');
|
||||
table.boolean('active');
|
||||
|
||||
table.decimal('price').unsigned();
|
||||
table.string('currency', 3);
|
||||
|
||||
table.decimal('trial_period').nullable();
|
||||
table.string('trial_interval').nullable();
|
||||
|
||||
table.decimal('invoice_period').nullable();
|
||||
table.string('invoice_interval').nullable();
|
||||
|
||||
table.integer('index').unsigned();
|
||||
table.timestamps();
|
||||
}).then(() => {
|
||||
return knex.seed.run({
|
||||
specific: 'seed_subscriptions_plans.js',
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
exports.down = function(knex) {
|
||||
return knex.schema.dropTableIfExists('subscription_plans')
|
||||
};
|
||||
@@ -1,15 +0,0 @@
|
||||
|
||||
exports.up = function(knex) {
|
||||
return knex.schema.createTable('subscription_plan_features', table => {
|
||||
table.increments();
|
||||
table.integer('plan_id').unsigned().index().references('id').inTable('subscription_plans');
|
||||
table.string('slug');
|
||||
table.string('name');
|
||||
table.string('description');
|
||||
table.timestamps();
|
||||
});
|
||||
};
|
||||
|
||||
exports.down = function(knex) {
|
||||
return knex.schema.dropTableIfExists('subscription_plan_features');
|
||||
};
|
||||
@@ -1,22 +0,0 @@
|
||||
|
||||
exports.up = function(knex) {
|
||||
return knex.schema.createTable('subscription_plan_subscriptions', table => {
|
||||
table.increments('id');
|
||||
table.string('slug');
|
||||
|
||||
table.integer('plan_id').unsigned().index().references('id').inTable('subscription_plans');
|
||||
table.bigInteger('tenant_id').unsigned().index().references('id').inTable('tenants');
|
||||
|
||||
table.dateTime('starts_at').nullable();
|
||||
table.dateTime('ends_at').nullable();
|
||||
|
||||
table.dateTime('cancels_at').nullable();
|
||||
table.dateTime('canceled_at').nullable();
|
||||
|
||||
table.timestamps();
|
||||
});
|
||||
};
|
||||
|
||||
exports.down = function(knex) {
|
||||
return knex.schema.dropTableIfExists('subscription_plan_subscriptions');
|
||||
};
|
||||
@@ -1,22 +0,0 @@
|
||||
|
||||
exports.up = function(knex) {
|
||||
return knex.schema.createTable('subscription_licenses', (table) => {
|
||||
table.increments();
|
||||
|
||||
table.string('license_code').unique().index();
|
||||
table.integer('plan_id').unsigned().index().references('id').inTable('subscription_plans');
|
||||
|
||||
table.integer('license_period').unsigned();
|
||||
table.string('period_interval');
|
||||
|
||||
table.dateTime('sent_at').index();
|
||||
table.dateTime('disabled_at').index();
|
||||
table.dateTime('used_at').index();
|
||||
|
||||
table.timestamps();
|
||||
})
|
||||
};
|
||||
|
||||
exports.down = function(knex) {
|
||||
return knex.schema.dropTableIfExists('subscription_licenses');
|
||||
};
|
||||
@@ -1,129 +0,0 @@
|
||||
import { Model, mixin } from 'objection';
|
||||
import moment from 'moment';
|
||||
import SystemModel from '@/system/models/SystemModel';
|
||||
|
||||
export default class License extends SystemModel {
|
||||
/**
|
||||
* Table name.
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'subscription_licenses';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamps columns.
|
||||
*/
|
||||
get timestamps() {
|
||||
return ['createdAt', 'updatedAt'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Model modifiers.
|
||||
*/
|
||||
static get modifiers() {
|
||||
return {
|
||||
// Filters active licenses.
|
||||
filterActiveLicense(query) {
|
||||
query.where('disabled_at', null);
|
||||
query.where('used_at', null);
|
||||
},
|
||||
|
||||
// Find license by its code or id.
|
||||
findByCodeOrId(query, id, code) {
|
||||
if (id) {
|
||||
query.where('id', id);
|
||||
}
|
||||
if (code) {
|
||||
query.where('license_code', code);
|
||||
}
|
||||
},
|
||||
|
||||
// Filters licenses list.
|
||||
filter(builder, licensesFilter) {
|
||||
if (licensesFilter.active) {
|
||||
builder.modify('filterActiveLicense');
|
||||
}
|
||||
if (licensesFilter.disabled) {
|
||||
builder.whereNot('disabled_at', null);
|
||||
}
|
||||
if (licensesFilter.used) {
|
||||
builder.whereNot('used_at', null);
|
||||
}
|
||||
if (licensesFilter.sent) {
|
||||
builder.whereNot('sent_at', null);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const Plan = require('system/models/Subscriptions/Plan');
|
||||
|
||||
return {
|
||||
plan: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: Plan.default,
|
||||
join: {
|
||||
from: 'subscription_licenses.planId',
|
||||
to: 'subscriptions_plans.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the given license code from the storage.
|
||||
* @param {string} licenseCode
|
||||
* @return {Promise}
|
||||
*/
|
||||
static deleteLicense(licenseCode, viaAttribute = 'license_code') {
|
||||
return this.query().where(viaAttribute, licenseCode).delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks the given license code as disabled on the storage.
|
||||
* @param {string} licenseCode
|
||||
* @return {Promise}
|
||||
*/
|
||||
static markLicenseAsDisabled(licenseCode, viaAttribute = 'license_code') {
|
||||
return this.query().where(viaAttribute, licenseCode).patch({
|
||||
disabled_at: moment().toMySqlDateTime(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks the given license code as sent on the storage.
|
||||
* @param {string} licenseCode
|
||||
*/
|
||||
static markLicenseAsSent(licenseCode, viaAttribute = 'license_code') {
|
||||
return this.query().where(viaAttribute, licenseCode).patch({
|
||||
sent_at: moment().toMySqlDateTime(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks the given license code as used on the storage.
|
||||
* @param {string} licenseCode
|
||||
* @return {Promise}
|
||||
*/
|
||||
static markLicenseAsUsed(licenseCode, viaAttribute = 'license_code') {
|
||||
return this.query().where(viaAttribute, licenseCode).patch({
|
||||
used_at: moment().toMySqlDateTime(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {IIPlan} plan
|
||||
* @return {boolean}
|
||||
*/
|
||||
isEqualPlanPeriod(plan) {
|
||||
return (
|
||||
this.invoicePeriod === plan.invoiceInterval &&
|
||||
license.licensePeriod === license.periodInterval
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
import { Model, mixin } from 'objection';
|
||||
import SystemModel from '@/system/models/SystemModel';
|
||||
import { PlanSubscription } from '..';
|
||||
|
||||
export default class Plan extends mixin(SystemModel) {
|
||||
/**
|
||||
* Table name.
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'subscription_plans';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamps columns.
|
||||
*/
|
||||
get timestamps() {
|
||||
return ['createdAt', 'updatedAt'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Defined virtual attributes.
|
||||
*/
|
||||
static get virtualAttributes() {
|
||||
return ['isFree', 'hasTrial'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Model modifiers.
|
||||
*/
|
||||
static get modifiers() {
|
||||
return {
|
||||
getFeatureBySlug(builder, featureSlug) {
|
||||
builder.where('slug', featureSlug);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const PlanSubscription = require('system/models/Subscriptions/PlanSubscription');
|
||||
|
||||
return {
|
||||
/**
|
||||
* The plan may have many subscriptions.
|
||||
*/
|
||||
subscriptions: {
|
||||
relation: Model.HasManyRelation,
|
||||
modelClass: PlanSubscription.default,
|
||||
join: {
|
||||
from: 'subscription_plans.id',
|
||||
to: 'subscription_plan_subscriptions.planId',
|
||||
},
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if plan is free.
|
||||
* @return {boolean}
|
||||
*/
|
||||
isFree() {
|
||||
return this.price <= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if plan is paid.
|
||||
* @return {boolean}
|
||||
*/
|
||||
isPaid() {
|
||||
return !this.isFree();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if plan has trial.
|
||||
* @return {boolean}
|
||||
*/
|
||||
hasTrial() {
|
||||
return this.trialPeriod && this.trialInterval;
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
import { Model, mixin } from 'objection';
|
||||
import SystemModel from '@/system/models/SystemModel';
|
||||
|
||||
export default class PlanFeature extends mixin(SystemModel) {
|
||||
/**
|
||||
* Table name.
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'subscriptions.plan_features';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamps columns.
|
||||
*/
|
||||
static get timestamps() {
|
||||
return ['createdAt', 'updatedAt'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const Plan = require('system/models/Subscriptions/Plan');
|
||||
|
||||
return {
|
||||
plan: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: Plan.default,
|
||||
join: {
|
||||
from: 'subscriptions.plan_features.planId',
|
||||
to: 'subscriptions.plans.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,164 +0,0 @@
|
||||
import { Model, mixin } from 'objection';
|
||||
import SystemModel from '@/system/models/SystemModel';
|
||||
import moment from 'moment';
|
||||
import SubscriptionPeriod from '@/services/Subscription/SubscriptionPeriod';
|
||||
|
||||
export default class PlanSubscription extends mixin(SystemModel) {
|
||||
/**
|
||||
* Table name.
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'subscription_plan_subscriptions';
|
||||
}
|
||||
|
||||
/**
|
||||
* Timestamps columns.
|
||||
*/
|
||||
get timestamps() {
|
||||
return ['createdAt', 'updatedAt'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Defined virtual attributes.
|
||||
*/
|
||||
static get virtualAttributes() {
|
||||
return ['active', 'inactive', 'ended', 'onTrial'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifiers queries.
|
||||
*/
|
||||
static get modifiers() {
|
||||
return {
|
||||
activeSubscriptions(builder) {
|
||||
const dateFormat = 'YYYY-MM-DD HH:mm:ss';
|
||||
const now = moment().format(dateFormat);
|
||||
|
||||
builder.where('ends_at', '>', now);
|
||||
builder.where('trial_ends_at', '>', now);
|
||||
},
|
||||
|
||||
inactiveSubscriptions() {
|
||||
builder.modify('endedTrial');
|
||||
builder.modify('endedPeriod');
|
||||
},
|
||||
|
||||
subscriptionBySlug(builder, subscriptionSlug) {
|
||||
builder.where('slug', subscriptionSlug);
|
||||
},
|
||||
|
||||
endedTrial(builder) {
|
||||
const dateFormat = 'YYYY-MM-DD HH:mm:ss';
|
||||
const endDate = moment().format(dateFormat);
|
||||
|
||||
builder.where('ends_at', '<=', endDate);
|
||||
},
|
||||
|
||||
endedPeriod(builder) {
|
||||
const dateFormat = 'YYYY-MM-DD HH:mm:ss';
|
||||
const endDate = moment().format(dateFormat);
|
||||
|
||||
builder.where('trial_ends_at', '<=', endDate);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Relations mappings.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const Tenant = require('system/models/Tenant');
|
||||
const Plan = require('system/models/Subscriptions/Plan');
|
||||
|
||||
return {
|
||||
/**
|
||||
* Plan subscription belongs to tenant.
|
||||
*/
|
||||
tenant: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: Tenant.default,
|
||||
join: {
|
||||
from: 'subscription_plan_subscriptions.tenantId',
|
||||
to: 'tenants.id',
|
||||
},
|
||||
},
|
||||
|
||||
/**
|
||||
* Plan description belongs to plan.
|
||||
*/
|
||||
plan: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: Plan.default,
|
||||
join: {
|
||||
from: 'subscription_plan_subscriptions.planId',
|
||||
to: 'subscription_plans.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if subscription is active.
|
||||
* @return {Boolean}
|
||||
*/
|
||||
active() {
|
||||
return !this.ended() || this.onTrial();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if subscription is inactive.
|
||||
* @return {Boolean}
|
||||
*/
|
||||
inactive() {
|
||||
return !this.active();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if subscription period has ended.
|
||||
* @return {Boolean}
|
||||
*/
|
||||
ended() {
|
||||
return this.endsAt ? moment().isAfter(this.endsAt) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if subscription is currently on trial.
|
||||
* @return {Boolean}
|
||||
*/
|
||||
onTrial() {
|
||||
return this.trailEndsAt ? moment().isAfter(this.trailEndsAt) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set new period from the given details.
|
||||
* @param {string} invoiceInterval
|
||||
* @param {number} invoicePeriod
|
||||
* @param {string} start
|
||||
*
|
||||
* @return {Object}
|
||||
*/
|
||||
static setNewPeriod(invoiceInterval, invoicePeriod, start) {
|
||||
const period = new SubscriptionPeriod(
|
||||
invoiceInterval,
|
||||
invoicePeriod,
|
||||
start,
|
||||
);
|
||||
|
||||
const startsAt = period.getStartDate();
|
||||
const endsAt = period.getEndDate();
|
||||
|
||||
return { startsAt, endsAt };
|
||||
}
|
||||
|
||||
/**
|
||||
* Renews subscription period.
|
||||
* @Promise
|
||||
*/
|
||||
renew(invoiceInterval, invoicePeriod) {
|
||||
const { startsAt, endsAt } = PlanSubscription.setNewPeriod(
|
||||
invoiceInterval,
|
||||
invoicePeriod,
|
||||
);
|
||||
return this.$query().update({ startsAt, endsAt });
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,8 @@
|
||||
import moment from 'moment';
|
||||
import { Model } from 'objection';
|
||||
import uniqid from 'uniqid';
|
||||
import SubscriptionPeriod from '@/services/Subscription/SubscriptionPeriod';
|
||||
import BaseModel from 'models/Model';
|
||||
import TenantMetadata from './TenantMetadata';
|
||||
import PlanSubscription from './Subscriptions/PlanSubscription';
|
||||
|
||||
export default class Tenant extends BaseModel {
|
||||
/**
|
||||
@@ -49,33 +47,13 @@ export default class Tenant extends BaseModel {
|
||||
return !!this.upgradeJobId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Query modifiers.
|
||||
*/
|
||||
static modifiers() {
|
||||
return {
|
||||
subscriptions(builder) {
|
||||
builder.withGraphFetched('subscriptions');
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Relations mappings.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const PlanSubscription = require('./Subscriptions/PlanSubscription');
|
||||
const TenantMetadata = require('./TenantMetadata');
|
||||
|
||||
return {
|
||||
subscriptions: {
|
||||
relation: Model.HasManyRelation,
|
||||
modelClass: PlanSubscription.default,
|
||||
join: {
|
||||
from: 'tenants.id',
|
||||
to: 'subscription_plan_subscriptions.tenantId',
|
||||
},
|
||||
},
|
||||
metadata: {
|
||||
relation: Model.HasOneRelation,
|
||||
modelClass: TenantMetadata.default,
|
||||
@@ -86,55 +64,6 @@ export default class Tenant extends BaseModel {
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the subscribed plans ids.
|
||||
* @return {number[]}
|
||||
*/
|
||||
async subscribedPlansIds() {
|
||||
const { subscriptions } = this;
|
||||
return chain(subscriptions).map('planId').unq();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {*} planId
|
||||
* @param {*} invoiceInterval
|
||||
* @param {*} invoicePeriod
|
||||
* @param {*} subscriptionSlug
|
||||
* @returns
|
||||
*/
|
||||
newSubscription(planId, invoiceInterval, invoicePeriod, subscriptionSlug) {
|
||||
return Tenant.newSubscription(
|
||||
this.id,
|
||||
planId,
|
||||
invoiceInterval,
|
||||
invoicePeriod,
|
||||
subscriptionSlug,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Records a new subscription for the associated tenant.
|
||||
*/
|
||||
static newSubscription(
|
||||
tenantId,
|
||||
planId,
|
||||
invoiceInterval,
|
||||
invoicePeriod,
|
||||
subscriptionSlug
|
||||
) {
|
||||
const period = new SubscriptionPeriod(invoiceInterval, invoicePeriod);
|
||||
|
||||
return PlanSubscription.query().insert({
|
||||
tenantId,
|
||||
slug: subscriptionSlug,
|
||||
planId,
|
||||
startsAt: period.getStartDate(),
|
||||
endsAt: period.getEndDate(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new tenant with random organization id.
|
||||
*/
|
||||
@@ -185,9 +114,9 @@ export default class Tenant extends BaseModel {
|
||||
|
||||
/**
|
||||
* Marks the given tenant as upgrading.
|
||||
* @param {number} tenantId
|
||||
* @param {string} upgradeJobId
|
||||
* @returns
|
||||
* @param {number} tenantId
|
||||
* @param {string} upgradeJobId
|
||||
* @returns
|
||||
*/
|
||||
static markAsUpgrading(tenantId, upgradeJobId) {
|
||||
return this.query().update({ upgradeJobId }).where({ id: tenantId });
|
||||
@@ -195,8 +124,8 @@ export default class Tenant extends BaseModel {
|
||||
|
||||
/**
|
||||
* Markes the given tenant as upgraded.
|
||||
* @param {number} tenantId
|
||||
* @returns
|
||||
* @param {number} tenantId
|
||||
* @returns
|
||||
*/
|
||||
static markAsUpgraded(tenantId) {
|
||||
return this.query().update({ upgradeJobId: null }).where({ id: tenantId });
|
||||
|
||||
@@ -1,22 +1,7 @@
|
||||
|
||||
import Plan from './Subscriptions/Plan';
|
||||
import PlanFeature from './Subscriptions/PlanFeature';
|
||||
import PlanSubscription from './Subscriptions/PlanSubscription';
|
||||
import License from './Subscriptions/License';
|
||||
import Tenant from './Tenant';
|
||||
import TenantMetadata from './TenantMetadata';
|
||||
import SystemUser from './SystemUser';
|
||||
import PasswordReset from './PasswordReset';
|
||||
import Invite from './Invite';
|
||||
|
||||
export {
|
||||
Plan,
|
||||
PlanFeature,
|
||||
PlanSubscription,
|
||||
License,
|
||||
Tenant,
|
||||
TenantMetadata,
|
||||
SystemUser,
|
||||
PasswordReset,
|
||||
Invite,
|
||||
}
|
||||
export { Tenant, TenantMetadata, SystemUser, PasswordReset, Invite };
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
import SystemRepository from '@/system/repositories/SystemRepository';
|
||||
import { PlanSubscription } from '@/system/models';
|
||||
|
||||
export default class SubscriptionRepository extends SystemRepository {
|
||||
/**
|
||||
* Gets the repository's model.
|
||||
*/
|
||||
get model() {
|
||||
return PlanSubscription.bindKnex(this.knex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve subscription from a given slug in specific tenant.
|
||||
* @param {string} slug
|
||||
* @param {number} tenantId
|
||||
*/
|
||||
getBySlugInTenant(slug: string, tenantId: number) {
|
||||
const cacheKey = this.getCacheKey('getBySlugInTenant', slug, tenantId);
|
||||
|
||||
return this.cache.get(cacheKey, () => {
|
||||
return PlanSubscription.query()
|
||||
.findOne('slug', slug)
|
||||
.where('tenant_id', tenantId);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,4 @@
|
||||
import SystemUserRepository from '@/system/repositories/SystemUserRepository';
|
||||
import SubscriptionRepository from '@/system/repositories/SubscriptionRepository';
|
||||
import TenantRepository from '@/system/repositories/TenantRepository';
|
||||
|
||||
export {
|
||||
SystemUserRepository,
|
||||
SubscriptionRepository,
|
||||
TenantRepository,
|
||||
};
|
||||
export { SystemUserRepository, TenantRepository };
|
||||
|
||||
0
packages/server/src/system/seeds/.gitkeep
Normal file
0
packages/server/src/system/seeds/.gitkeep
Normal file
@@ -1,66 +0,0 @@
|
||||
|
||||
exports.seed = (knex) => {
|
||||
// Deletes ALL existing entries
|
||||
return knex('subscription_plans').del()
|
||||
.then(() => {
|
||||
// Inserts seed entries
|
||||
return knex('subscription_plans').insert([
|
||||
{
|
||||
name: 'Essentials',
|
||||
slug: 'essentials-monthly',
|
||||
price: 100,
|
||||
active: true,
|
||||
currency: 'LYD',
|
||||
trial_period: 7,
|
||||
trial_interval: 'days',
|
||||
},
|
||||
{
|
||||
name: 'Essentials',
|
||||
slug: 'essentials-yearly',
|
||||
price: 1200,
|
||||
active: true,
|
||||
currency: 'LYD',
|
||||
trial_period: 12,
|
||||
trial_interval: 'months',
|
||||
},
|
||||
{
|
||||
name: 'Pro',
|
||||
slug: 'pro-monthly',
|
||||
price: 200,
|
||||
active: true,
|
||||
currency: 'LYD',
|
||||
trial_period: 1,
|
||||
trial_interval: 'months',
|
||||
},
|
||||
{
|
||||
name: 'Pro',
|
||||
slug: 'pro-yearly',
|
||||
price: 500,
|
||||
active: true,
|
||||
currency: 'LYD',
|
||||
invoice_period: 12,
|
||||
invoice_interval: 'month',
|
||||
index: 2,
|
||||
},
|
||||
{
|
||||
name: 'Plus',
|
||||
slug: 'plus-monthly',
|
||||
price: 200,
|
||||
active: true,
|
||||
currency: 'LYD',
|
||||
trial_period: 1,
|
||||
trial_interval: 'months',
|
||||
},
|
||||
{
|
||||
name: 'Plus',
|
||||
slug: 'plus-yearly',
|
||||
price: 500,
|
||||
active: true,
|
||||
currency: 'LYD',
|
||||
invoice_period: 12,
|
||||
invoice_interval: 'month',
|
||||
index: 2,
|
||||
},
|
||||
]);
|
||||
});
|
||||
};
|
||||
@@ -4,17 +4,20 @@ USER root
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY ./package.json /app/package.json
|
||||
COPY ./package-lock.json /app/package-lock.json
|
||||
# Install dependencies
|
||||
COPY package.json ./
|
||||
COPY lerna.json ./
|
||||
|
||||
COPY ./packages/webapp/package.json /app/packages/webapp/package.json
|
||||
|
||||
RUN npm install
|
||||
RUN npm run bootstrap
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN npm run build
|
||||
# Build webapp package
|
||||
COPY ./packages/webapp /app/packages/webapp
|
||||
RUN npm run build:webapp
|
||||
|
||||
FROM nginx
|
||||
|
||||
COPY ./nginx/sites/default.conf /etc/nginx/conf.d/default.conf
|
||||
|
||||
COPY --from=build /app/build /usr/share/nginx/html
|
||||
COPY ./packages/webapp/nginx/sites/default.conf /etc/nginx/conf.d/default.conf
|
||||
COPY --from=build /app/packages/webapp/build /usr/share/nginx/html
|
||||
|
||||
@@ -1,68 +1 @@
|
||||
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
|
||||
|
||||
## Available Scripts
|
||||
|
||||
In the project directory, you can run:
|
||||
|
||||
### `npm start`
|
||||
|
||||
Runs the app in the development mode.<br />
|
||||
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
|
||||
|
||||
The page will reload if you make edits.<br />
|
||||
You will also see any lint errors in the console.
|
||||
|
||||
### `npm test`
|
||||
|
||||
Launches the test runner in the interactive watch mode.<br />
|
||||
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
|
||||
|
||||
### `npm run build`
|
||||
|
||||
Builds the app for production to the `build` folder.<br />
|
||||
It correctly bundles React in production mode and optimizes the build for the best performance.
|
||||
|
||||
The build is minified and the filenames include the hashes.<br />
|
||||
Your app is ready to be deployed!
|
||||
|
||||
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
|
||||
|
||||
### `npm run eject`
|
||||
|
||||
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
|
||||
|
||||
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
|
||||
|
||||
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
|
||||
|
||||
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
|
||||
|
||||
## Learn More
|
||||
|
||||
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
|
||||
|
||||
To learn React, check out the [React documentation](https://reactjs.org/).
|
||||
|
||||
### Code Splitting
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
|
||||
|
||||
### Analyzing the Bundle Size
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
|
||||
|
||||
### Making a Progressive Web App
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
|
||||
|
||||
### Advanced Configuration
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
|
||||
|
||||
### Deployment
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
|
||||
|
||||
### `npm run build` fails to minify
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
|
||||
# @bigcapital/webapp
|
||||
44
packages/webapp/package-lock.json
generated
44
packages/webapp/package-lock.json
generated
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "@bigcapital/webpack",
|
||||
"name": "@bigcapital/webapp",
|
||||
"version": "1.7.1",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
@@ -1144,6 +1144,11 @@
|
||||
"@babel/plugin-transform-typescript": "^7.9.0"
|
||||
}
|
||||
},
|
||||
"@babel/regjsgen": {
|
||||
"version": "0.8.0",
|
||||
"resolved": "https://registry.npmjs.org/@babel/regjsgen/-/regjsgen-0.8.0.tgz",
|
||||
"integrity": "sha512-x/rqGMdzj+fWZvCOYForTghzbtqPDZ5gPwaoNGHdgDfF2QA/XZbCBp4Moo5scrkAMPhB7z26XM/AaHuIJdgauA=="
|
||||
},
|
||||
"@babel/runtime": {
|
||||
"version": "7.20.13",
|
||||
"resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.20.13.tgz",
|
||||
@@ -3516,9 +3521,9 @@
|
||||
}
|
||||
},
|
||||
"async-each": {
|
||||
"version": "1.0.5",
|
||||
"resolved": "https://registry.npmjs.org/async-each/-/async-each-1.0.5.tgz",
|
||||
"integrity": "sha512-5QzqtU3BlagehwmdoqwaS2FBQF2P5eL6vFqXwNsb5jwoEsmtfAXg1ocFvW7I6/gGLFhBMKwcMwZuy7uv/Bo9jA=="
|
||||
"version": "1.0.6",
|
||||
"resolved": "https://registry.npmjs.org/async-each/-/async-each-1.0.6.tgz",
|
||||
"integrity": "sha512-c646jH1avxr+aVpndVMeAfYw7wAa6idufrlN3LPA4PmKS0QEGp6PIC9nwz0WQkkvBGAMEki3pFdtxaF39J9vvg=="
|
||||
},
|
||||
"async-foreach": {
|
||||
"version": "0.1.3",
|
||||
@@ -4681,9 +4686,9 @@
|
||||
}
|
||||
},
|
||||
"caniuse-lite": {
|
||||
"version": "1.0.30001450",
|
||||
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001450.tgz",
|
||||
"integrity": "sha512-qMBmvmQmFXaSxexkjjfMvD5rnDL0+m+dUMZKoDYsGG8iZN29RuYh9eRoMvKsT6uMAWlyUUGDEQGJJYjzCIO9ew=="
|
||||
"version": "1.0.30001451",
|
||||
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001451.tgz",
|
||||
"integrity": "sha512-XY7UbUpGRatZzoRft//5xOa69/1iGJRBlrieH6QYrkKLIFn3m7OVEJ81dSrKoy2BnKsdbX5cLrOispZNYo9v2w=="
|
||||
},
|
||||
"capture-exit": {
|
||||
"version": "2.0.0",
|
||||
@@ -6015,9 +6020,9 @@
|
||||
"integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow=="
|
||||
},
|
||||
"electron-to-chromium": {
|
||||
"version": "1.4.285",
|
||||
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.285.tgz",
|
||||
"integrity": "sha512-47o4PPgxfU1KMNejz+Dgaodf7YTcg48uOfV1oM6cs3adrl2+7R+dHkt3Jpxqo0LRCbGJEzTKMUt0RdvByb/leg=="
|
||||
"version": "1.4.289",
|
||||
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.289.tgz",
|
||||
"integrity": "sha512-relLdMfPBxqGCxy7Gyfm1HcbRPcFUJdlgnCPVgQ23sr1TvUrRJz0/QPoGP0+x41wOVSTN/Wi3w6YDgHiHJGOzg=="
|
||||
},
|
||||
"elliptic": {
|
||||
"version": "6.5.4",
|
||||
@@ -10984,9 +10989,9 @@
|
||||
}
|
||||
},
|
||||
"node-releases": {
|
||||
"version": "2.0.9",
|
||||
"resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.9.tgz",
|
||||
"integrity": "sha512-2xfmOrRkGogbTK9R6Leda0DGiXeY3p2NJpy4+gNCffdUvV6mdEJnaDEic1i3Ec2djAo8jWYoJMR5PB0MSMpxUA=="
|
||||
"version": "2.0.10",
|
||||
"resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.10.tgz",
|
||||
"integrity": "sha512-5GFldHPXVG/YZmFzJvKK2zDSzPKhEp0+ZR5SVaoSag9fsL5YgHbUHDfnG5494ISANDcK4KwPXAx2xqVEydmd7w=="
|
||||
},
|
||||
"node-sass": {
|
||||
"version": "4.14.1",
|
||||
@@ -14129,23 +14134,18 @@
|
||||
"integrity": "sha512-pq2bWo9mVD43nbts2wGv17XLiNLya+GklZ8kaDLV2Z08gDCsGpnKn9BFMepvWuHCbyVvY7J5o5+BVvoQbmlJLg=="
|
||||
},
|
||||
"regexpu-core": {
|
||||
"version": "5.2.2",
|
||||
"resolved": "https://registry.npmjs.org/regexpu-core/-/regexpu-core-5.2.2.tgz",
|
||||
"integrity": "sha512-T0+1Zp2wjF/juXMrMxHxidqGYn8U4R+zleSJhX9tQ1PUsS8a9UtYfbsF9LdiVgNX3kiX8RNaKM42nfSgvFJjmw==",
|
||||
"version": "5.3.0",
|
||||
"resolved": "https://registry.npmjs.org/regexpu-core/-/regexpu-core-5.3.0.tgz",
|
||||
"integrity": "sha512-ZdhUQlng0RoscyW7jADnUZ25F5eVtHdMyXSb2PiwafvteRAOJUjFoUPEYZSIfP99fBIs3maLIRfpEddT78wAAQ==",
|
||||
"requires": {
|
||||
"@babel/regjsgen": "^0.8.0",
|
||||
"regenerate": "^1.4.2",
|
||||
"regenerate-unicode-properties": "^10.1.0",
|
||||
"regjsgen": "^0.7.1",
|
||||
"regjsparser": "^0.9.1",
|
||||
"unicode-match-property-ecmascript": "^2.0.0",
|
||||
"unicode-match-property-value-ecmascript": "^2.1.0"
|
||||
}
|
||||
},
|
||||
"regjsgen": {
|
||||
"version": "0.7.1",
|
||||
"resolved": "https://registry.npmjs.org/regjsgen/-/regjsgen-0.7.1.tgz",
|
||||
"integrity": "sha512-RAt+8H2ZEzHeYWxZ3H2z6tF18zyyOnlcdaafLrm21Bguj7uZy6ULibiAFdXEtKQY4Sy7wDTwDiOazasMLc4KPA=="
|
||||
},
|
||||
"regjsparser": {
|
||||
"version": "0.9.1",
|
||||
"resolved": "https://registry.npmjs.org/regjsparser/-/regjsparser-0.9.1.tgz",
|
||||
|
||||
@@ -99,7 +99,7 @@
|
||||
"yup": "^0.28.1"
|
||||
},
|
||||
"scripts": {
|
||||
"dev": "craco start",
|
||||
"dev": "PORT=4000 craco start",
|
||||
"build": "craco build",
|
||||
"test": "node scripts/test.js",
|
||||
"storybook": "start-storybook -p 6006"
|
||||
|
||||
@@ -14,19 +14,16 @@ import GlobalHotkeys from './GlobalHotkeys';
|
||||
import DashboardProvider from './DashboardProvider';
|
||||
import DrawersContainer from '@/components/DrawersContainer';
|
||||
import AlertsContainer from '@/containers/AlertsContainer';
|
||||
import EnsureSubscriptionIsActive from '../Guards/EnsureSubscriptionIsActive';
|
||||
|
||||
/**
|
||||
* Dashboard preferences.
|
||||
*/
|
||||
function DashboardPreferences() {
|
||||
return (
|
||||
<EnsureSubscriptionIsActive>
|
||||
<DashboardSplitPane>
|
||||
<Sidebar />
|
||||
<PreferencesPage />
|
||||
</DashboardSplitPane>
|
||||
</EnsureSubscriptionIsActive>
|
||||
<DashboardSplitPane>
|
||||
<Sidebar />
|
||||
<PreferencesPage />
|
||||
</DashboardSplitPane>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,26 +7,20 @@ import {
|
||||
} from '@/hooks/query';
|
||||
import { useSplashLoading } from '@/hooks/state';
|
||||
import { useWatch, useWatchImmediate, useWhen } from '@/hooks';
|
||||
import { useSubscription } from '@/hooks/state';
|
||||
import { setCookie, getCookie } from '@/utils';
|
||||
|
||||
/**
|
||||
* Dashboard meta async booting.
|
||||
* - Fetches the dashboard meta only if the organization subscribe is active.
|
||||
* - Once the dashboard meta query is loading display dashboard splash screen.
|
||||
* - Fetches the dashboard meta in booting state.
|
||||
* - Once the dashboard meta query started loading display dashboard splash screen.
|
||||
*/
|
||||
export function useDashboardMetaBoot() {
|
||||
const { isSubscriptionActive } = useSubscription();
|
||||
|
||||
const {
|
||||
data: dashboardMeta,
|
||||
isLoading: isDashboardMetaLoading,
|
||||
isSuccess: isDashboardMetaSuccess,
|
||||
} = useDashboardMeta({
|
||||
keepPreviousData: true,
|
||||
|
||||
// Avoid run the query if the organization subscription is not active.
|
||||
enabled: isSubscriptionActive,
|
||||
});
|
||||
const [startLoading, stopLoading] = useSplashLoading();
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import { useHistory } from 'react-router-dom';
|
||||
import {
|
||||
CollapsibleList,
|
||||
MenuItem,
|
||||
@@ -7,29 +8,29 @@ import {
|
||||
Boundary,
|
||||
} from '@blueprintjs/core';
|
||||
import withBreadcrumbs from 'react-router-breadcrumbs-hoc';
|
||||
import { getDashboardRoutes } from '@/routes/dashboard';
|
||||
import { useHistory } from 'react-router-dom';
|
||||
|
||||
function DashboardBreadcrumbs({ breadcrumbs }){
|
||||
function DashboardBreadcrumbs({ breadcrumbs }) {
|
||||
const history = useHistory();
|
||||
|
||||
return(
|
||||
return (
|
||||
<CollapsibleList
|
||||
className={Classes.BREADCRUMBS}
|
||||
dropdownTarget={<span className={Classes.BREADCRUMBS_COLLAPSED} />}
|
||||
collapseFrom={Boundary.START}
|
||||
visibleItemCount={0}>
|
||||
{
|
||||
breadcrumbs.map(({ breadcrumb,match })=>{
|
||||
return (<MenuItem
|
||||
key={match.url}
|
||||
icon={'folder-close'}
|
||||
text={breadcrumb}
|
||||
onClick={() => history.push(match.url) } />)
|
||||
})
|
||||
}
|
||||
className={Classes.BREADCRUMBS}
|
||||
dropdownTarget={<span className={Classes.BREADCRUMBS_COLLAPSED} />}
|
||||
collapseFrom={Boundary.START}
|
||||
visibleItemCount={0}
|
||||
>
|
||||
{breadcrumbs.map(({ breadcrumb, match }) => {
|
||||
return (
|
||||
<MenuItem
|
||||
key={match.url}
|
||||
icon={'folder-close'}
|
||||
text={breadcrumb}
|
||||
onClick={() => history.push(match.url)}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</CollapsibleList>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
export default withBreadcrumbs([])(DashboardBreadcrumbs)
|
||||
export default withBreadcrumbs([])(DashboardBreadcrumbs);
|
||||
|
||||
@@ -3,15 +3,13 @@ import React from 'react';
|
||||
import { Route, Switch } from 'react-router-dom';
|
||||
|
||||
import { getDashboardRoutes } from '@/routes/dashboard';
|
||||
import EnsureSubscriptionsIsActive from '../Guards/EnsureSubscriptionsIsActive';
|
||||
import EnsureSubscriptionsIsInactive from '../Guards/EnsureSubscriptionsIsInactive';
|
||||
import DashboardPage from './DashboardPage';
|
||||
|
||||
/**
|
||||
* Dashboard inner route content.
|
||||
*/
|
||||
function DashboardContentRouteContent({ route }) {
|
||||
const content = (
|
||||
return (
|
||||
<DashboardPage
|
||||
name={route.name}
|
||||
Component={route.component}
|
||||
@@ -23,21 +21,6 @@ function DashboardContentRouteContent({ route }) {
|
||||
defaultSearchResource={route.defaultSearchResource}
|
||||
/>
|
||||
);
|
||||
return route.subscriptionActive ? (
|
||||
<EnsureSubscriptionsIsInactive
|
||||
subscriptionTypes={route.subscriptionActive}
|
||||
children={content}
|
||||
redirectTo={'/billing'}
|
||||
/>
|
||||
) : route.subscriptionInactive ? (
|
||||
<EnsureSubscriptionsIsActive
|
||||
subscriptionTypes={route.subscriptionInactive}
|
||||
children={content}
|
||||
redirectTo={'/'}
|
||||
/>
|
||||
) : (
|
||||
content
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,57 +10,19 @@ import {
|
||||
Tooltip,
|
||||
Position,
|
||||
} from '@blueprintjs/core';
|
||||
import { FormattedMessage as T } from '@/components';
|
||||
import { FormattedMessage as T, Icon, Hint, If } from '@/components';
|
||||
|
||||
import DashboardTopbarUser from '@/components/Dashboard/TopbarUser';
|
||||
import DashboardBreadcrumbs from '@/components/Dashboard/DashboardBreadcrumbs';
|
||||
import DashboardBackLink from '@/components/Dashboard/DashboardBackLink';
|
||||
import { Icon, Hint, If } from '@/components';
|
||||
|
||||
import withUniversalSearchActions from '@/containers/UniversalSearch/withUniversalSearchActions';
|
||||
import withDashboardActions from '@/containers/Dashboard/withDashboardActions';
|
||||
import withDashboard from '@/containers/Dashboard/withDashboard';
|
||||
|
||||
import QuickNewDropdown from '@/containers/QuickNewDropdown/QuickNewDropdown';
|
||||
import { DashboardHamburgerButton, DashboardQuickSearchButton } from './_components';
|
||||
import { compose } from '@/utils';
|
||||
import withSubscriptions from '@/containers/Subscriptions/withSubscriptions';
|
||||
import { useGetUniversalSearchTypeOptions } from '@/containers/UniversalSearch/utils';
|
||||
|
||||
function DashboardTopbarSubscriptionMessage() {
|
||||
return (
|
||||
<div class="dashboard__topbar-subscription-msg">
|
||||
<span>
|
||||
<T id={'dashboard.subscription_msg.period_over'} />
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function DashboardHamburgerButton({ ...props }) {
|
||||
return (
|
||||
<Button minimal={true} {...props}>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="20"
|
||||
height="20"
|
||||
viewBox="0 0 20 20"
|
||||
role="img"
|
||||
focusable="false"
|
||||
>
|
||||
<title>
|
||||
<T id={'menu'} />
|
||||
</title>
|
||||
<path
|
||||
stroke="currentColor"
|
||||
stroke-linecap="round"
|
||||
stroke-miterlimit="5"
|
||||
stroke-width="2"
|
||||
d="M4 7h15M4 12h15M4 17h15"
|
||||
></path>
|
||||
</svg>
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dashboard topbar.
|
||||
@@ -79,10 +41,6 @@ function DashboardTopbar({
|
||||
|
||||
// #withGlobalSearch
|
||||
openGlobalSearch,
|
||||
|
||||
// #withSubscriptions
|
||||
isSubscriptionActive,
|
||||
isSubscriptionInactive,
|
||||
}) {
|
||||
const history = useHistory();
|
||||
|
||||
@@ -137,28 +95,22 @@ function DashboardTopbar({
|
||||
</div>
|
||||
|
||||
<div class="dashboard__topbar-right">
|
||||
<If condition={isSubscriptionInactive}>
|
||||
<DashboardTopbarSubscriptionMessage />
|
||||
</If>
|
||||
|
||||
<Navbar class="dashboard__topbar-navbar">
|
||||
<NavbarGroup>
|
||||
<If condition={isSubscriptionActive}>
|
||||
<DashboardQuickSearchButton
|
||||
onClick={() => openGlobalSearch(true)}
|
||||
/>
|
||||
<QuickNewDropdown />
|
||||
<DashboardQuickSearchButton
|
||||
onClick={() => openGlobalSearch(true)}
|
||||
/>
|
||||
<QuickNewDropdown />
|
||||
|
||||
<Tooltip
|
||||
content={<T id={'notifications'} />}
|
||||
position={Position.BOTTOM}
|
||||
>
|
||||
<Button
|
||||
className={Classes.MINIMAL}
|
||||
icon={<Icon icon={'notification-24'} iconSize={20} />}
|
||||
/>
|
||||
</Tooltip>
|
||||
</If>
|
||||
<Tooltip
|
||||
content={<T id={'notifications'} />}
|
||||
position={Position.BOTTOM}
|
||||
>
|
||||
<Button
|
||||
className={Classes.MINIMAL}
|
||||
icon={<Icon icon={'notification-24'} iconSize={20} />}
|
||||
/>
|
||||
</Tooltip>
|
||||
|
||||
<Button
|
||||
className={Classes.MINIMAL}
|
||||
@@ -186,31 +138,4 @@ export default compose(
|
||||
pageHint,
|
||||
})),
|
||||
withDashboardActions,
|
||||
withSubscriptions(
|
||||
({ isSubscriptionActive, isSubscriptionInactive }) => ({
|
||||
isSubscriptionActive,
|
||||
isSubscriptionInactive,
|
||||
}),
|
||||
'main',
|
||||
),
|
||||
)(DashboardTopbar);
|
||||
|
||||
/**
|
||||
* Dashboard quick search button.
|
||||
*/
|
||||
function DashboardQuickSearchButton({ ...rest }) {
|
||||
const searchTypeOptions = useGetUniversalSearchTypeOptions();
|
||||
|
||||
// Can't continue if there is no any search type option.
|
||||
if (searchTypeOptions.length <= 0) {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
<Button
|
||||
className={Classes.MINIMAL}
|
||||
icon={<Icon icon={'search-24'} iconSize={20} />}
|
||||
text={<T id={'quick_find'} />}
|
||||
{...rest}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import { Button, Classes } from '@blueprintjs/core';
|
||||
import { useGetUniversalSearchTypeOptions } from '@/containers/UniversalSearch/utils';
|
||||
import { Icon, FormattedMessage as T } from '@/components';
|
||||
|
||||
export function DashboardTopbarSubscriptionMessage() {
|
||||
return (
|
||||
<div class="dashboard__topbar-subscription-msg">
|
||||
<span>
|
||||
<T id={'dashboard.subscription_msg.period_over'} />
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function DashboardHamburgerButton({ ...props }) {
|
||||
return (
|
||||
<Button minimal={true} {...props}>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="20"
|
||||
height="20"
|
||||
viewBox="0 0 20 20"
|
||||
role="img"
|
||||
focusable="false"
|
||||
>
|
||||
<title>
|
||||
<T id={'menu'} />
|
||||
</title>
|
||||
<path
|
||||
stroke="currentColor"
|
||||
stroke-linecap="round"
|
||||
stroke-miterlimit="5"
|
||||
stroke-width="2"
|
||||
d="M4 7h15M4 12h15M4 17h15"
|
||||
></path>
|
||||
</svg>
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dashboard quick search button.
|
||||
*/
|
||||
export function DashboardQuickSearchButton({ ...rest }) {
|
||||
const searchTypeOptions = useGetUniversalSearchTypeOptions();
|
||||
|
||||
// Can't continue if there is no any search type option.
|
||||
if (searchTypeOptions.length <= 0) {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
<Button
|
||||
className={Classes.MINIMAL}
|
||||
icon={<Icon icon={'search-24'} iconSize={20} />}
|
||||
text={<T id={'quick_find'} />}
|
||||
{...rest}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
import DashboardTopbar from './DashboardTopbar';
|
||||
|
||||
export default DashboardTopbar;
|
||||
@@ -9,25 +9,21 @@ import {
|
||||
Popover,
|
||||
Position,
|
||||
} from '@blueprintjs/core';
|
||||
import { If, FormattedMessage as T } from '@/components';
|
||||
import { FormattedMessage as T } from '@/components';
|
||||
|
||||
import { firstLettersArgs } from '@/utils';
|
||||
import { useAuthActions } from '@/hooks/state';
|
||||
|
||||
import withDialogActions from '@/containers/Dialog/withDialogActions';
|
||||
import withSubscriptions from '@/containers/Subscriptions/withSubscriptions';
|
||||
|
||||
import { useAuthenticatedAccount } from '@/hooks/query';
|
||||
import { compose } from '@/utils';
|
||||
import { firstLettersArgs, compose } from '@/utils';
|
||||
|
||||
/**
|
||||
* Dashboard topbar user.
|
||||
*/
|
||||
function DashboardTopbarUser({
|
||||
// #withDialogActions
|
||||
openDialog,
|
||||
|
||||
// #withSubscriptions
|
||||
isSubscriptionActive,
|
||||
}) {
|
||||
const history = useHistory();
|
||||
const { setLogout } = useAuthActions();
|
||||
@@ -62,16 +58,14 @@ function DashboardTopbarUser({
|
||||
}
|
||||
/>
|
||||
<MenuDivider />
|
||||
<If condition={isSubscriptionActive}>
|
||||
<MenuItem
|
||||
text={<T id={'keyboard_shortcuts'} />}
|
||||
onClick={onKeyboardShortcut}
|
||||
/>
|
||||
<MenuItem
|
||||
text={<T id={'preferences'} />}
|
||||
onClick={() => history.push('/preferences')}
|
||||
/>
|
||||
</If>
|
||||
<MenuItem
|
||||
text={<T id={'keyboard_shortcuts'} />}
|
||||
onClick={onKeyboardShortcut}
|
||||
/>
|
||||
<MenuItem
|
||||
text={<T id={'preferences'} />}
|
||||
onClick={() => history.push('/preferences')}
|
||||
/>
|
||||
<MenuItem text={<T id={'logout'} />} onClick={onClickLogout} />
|
||||
</Menu>
|
||||
}
|
||||
@@ -87,8 +81,4 @@ function DashboardTopbarUser({
|
||||
}
|
||||
export default compose(
|
||||
withDialogActions,
|
||||
withSubscriptions(
|
||||
({ isSubscriptionActive }) => ({ isSubscriptionActive }),
|
||||
'main',
|
||||
),
|
||||
)(DashboardTopbarUser);
|
||||
|
||||
@@ -72,6 +72,7 @@ export default function TableCell({ cell, row, index }) {
|
||||
[`td-${cell.column.id}`]: cell.column.id,
|
||||
[`td-${cellType}-type`]: !!cellType,
|
||||
}),
|
||||
tabindex: 0,
|
||||
onClick: handleCellClick,
|
||||
})}
|
||||
>
|
||||
|
||||
@@ -2,9 +2,6 @@
|
||||
import intl from 'react-intl-universal';
|
||||
|
||||
export const getSetupWizardSteps = () => [
|
||||
{
|
||||
label: intl.get('setup.plan.plans'),
|
||||
},
|
||||
{
|
||||
label: intl.get('setup.plan.getting_started'),
|
||||
},
|
||||
|
||||
@@ -5,7 +5,6 @@ import { Features } from '@/constants/features';
|
||||
import {
|
||||
ISidebarMenuItemType,
|
||||
ISidebarMenuOverlayIds,
|
||||
ISidebarSubscriptionAbility,
|
||||
} from '@/containers/Dashboard/Sidebar/interfaces';
|
||||
import {
|
||||
ReportsAction,
|
||||
@@ -24,9 +23,7 @@ import {
|
||||
ManualJournalAction,
|
||||
ExpenseAction,
|
||||
CashflowAction,
|
||||
ProjectAction,
|
||||
PreferencesAbility,
|
||||
SubscriptionBillingAbility,
|
||||
} from '@/constants/abilityOption';
|
||||
|
||||
export const SidebarMenu = [
|
||||
@@ -781,19 +778,6 @@ export const SidebarMenu = [
|
||||
ability: PreferencesAbility.Mutate,
|
||||
},
|
||||
},
|
||||
{
|
||||
text: <T id={'billing'} />,
|
||||
href: '/billing',
|
||||
type: ISidebarMenuItemType.Link,
|
||||
subscription: [
|
||||
ISidebarSubscriptionAbility.Expired,
|
||||
ISidebarSubscriptionAbility.Active,
|
||||
],
|
||||
permission: {
|
||||
subject: AbilitySubject.SubscriptionBilling,
|
||||
ability: SubscriptionBillingAbility.View,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user