@digione/node-custom-api 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/constants/header.d.ts +10 -0
- package/constants/header.js +2 -0
- package/constants/http.d.ts +44 -0
- package/constants/http.js +2 -0
- package/database/index.d.ts +3 -0
- package/database/index.js +34 -0
- package/database/redis.d.ts +15 -0
- package/database/redis.js +68 -0
- package/errors/authError.d.ts +55 -0
- package/errors/authError.js +57 -0
- package/errors/index.d.ts +32 -0
- package/errors/index.js +113 -0
- package/index.d.ts +1 -0
- package/index.js +4 -0
- package/interface/param.d.ts +41 -0
- package/interface/param.js +2 -0
- package/middlewares/accessToken.d.ts +13 -0
- package/middlewares/accessToken.js +200 -0
- package/middlewares/coreSite.d.ts +4 -0
- package/middlewares/coreSite.js +52 -0
- package/middlewares/paramQuery.d.ts +16 -0
- package/middlewares/paramQuery.js +220 -0
- package/middlewares/refSite.d.ts +2 -0
- package/middlewares/refSite.js +58 -0
- package/middlewares/refreshToken.d.ts +2 -0
- package/middlewares/refreshToken.js +56 -0
- package/middlewares/systemSite.d.ts +6 -0
- package/middlewares/systemSite.js +106 -0
- package/middlewares/webSite.d.ts +7 -0
- package/middlewares/webSite.js +109 -0
- package/models/core/city.d.ts +66 -0
- package/models/core/city.js +79 -0
- package/models/core/country.d.ts +35 -0
- package/models/core/country.js +44 -0
- package/models/core/district.d.ts +17 -0
- package/models/core/district.js +23 -0
- package/models/core/domain.d.ts +57 -0
- package/models/core/domain.js +75 -0
- package/models/core/site.d.ts +60 -0
- package/models/core/site.js +70 -0
- package/models/core/state.d.ts +49 -0
- package/models/core/state.js +58 -0
- package/models/core/zipcode.d.ts +5 -0
- package/models/core/zipcode.js +28 -0
- package/models/stream/field.d.ts +60 -0
- package/models/stream/field.js +66 -0
- package/models/stream/field_section.d.ts +33 -0
- package/models/stream/field_section.js +39 -0
- package/models/stream/stream.d.ts +89 -0
- package/models/stream/stream.js +97 -0
- package/models/system/currency.d.ts +5 -0
- package/models/system/currency.js +32 -0
- package/models/system/email_log.d.ts +5 -0
- package/models/system/email_log.js +54 -0
- package/models/system/email_sender.d.ts +22 -0
- package/models/system/email_sender.js +53 -0
- package/models/system/email_template.d.ts +5 -0
- package/models/system/email_template.js +49 -0
- package/models/system/file.d.ts +98 -0
- package/models/system/file.js +111 -0
- package/models/system/folder.d.ts +59 -0
- package/models/system/folder.js +78 -0
- package/models/system/language.d.ts +44 -0
- package/models/system/language.js +50 -0
- package/models/system/metadata.d.ts +5 -0
- package/models/system/metadata.js +24 -0
- package/models/system/module.d.ts +5 -0
- package/models/system/module.js +35 -0
- package/models/system/notification_log.d.ts +25 -0
- package/models/system/notification_log.js +76 -0
- package/models/system/notification_module.d.ts +5 -0
- package/models/system/notification_module.js +35 -0
- package/models/system/notification_setting.d.ts +22 -0
- package/models/system/notification_setting.js +44 -0
- package/models/system/notification_template.d.ts +5 -0
- package/models/system/notification_template.js +49 -0
- package/models/system/notification_type.d.ts +5 -0
- package/models/system/notification_type.js +38 -0
- package/models/system/payment_transaction.d.ts +5 -0
- package/models/system/payment_transaction.js +46 -0
- package/models/system/pdf_page.d.ts +5 -0
- package/models/system/pdf_page.js +39 -0
- package/models/system/pdf_template.d.ts +22 -0
- package/models/system/pdf_template.js +67 -0
- package/models/system/review_template.d.ts +5 -0
- package/models/system/review_template.js +35 -0
- package/models/system/review_vote.d.ts +5 -0
- package/models/system/review_vote.js +36 -0
- package/models/system/session.d.ts +5 -0
- package/models/system/session.js +19 -0
- package/models/system/setting.d.ts +15 -0
- package/models/system/setting.js +22 -0
- package/models/system/site_token.d.ts +25 -0
- package/models/system/site_token.js +31 -0
- package/models/system/sms_log.d.ts +4 -0
- package/models/system/sms_log.js +52 -0
- package/models/system/sms_setting.d.ts +4 -0
- package/models/system/sms_setting.js +39 -0
- package/models/system/sms_template.d.ts +5 -0
- package/models/system/sms_template.js +48 -0
- package/models/system/tax.d.ts +5 -0
- package/models/system/tax.js +33 -0
- package/models/system/website.d.ts +56 -0
- package/models/system/website.js +66 -0
- package/models/system/zone.d.ts +57 -0
- package/models/system/zone.js +141 -0
- package/models/user/access_token.d.ts +37 -0
- package/models/user/access_token.js +46 -0
- package/models/user/address.d.ts +69 -0
- package/models/user/address.js +119 -0
- package/models/user/b2b_group.d.ts +57 -0
- package/models/user/b2b_group.js +63 -0
- package/models/user/group.d.ts +28 -0
- package/models/user/group.js +34 -0
- package/models/user/group_permission.d.ts +4 -0
- package/models/user/group_permission.js +27 -0
- package/models/user/notification_token.d.ts +32 -0
- package/models/user/notification_token.js +39 -0
- package/models/user/organ.d.ts +85 -0
- package/models/user/organ.js +100 -0
- package/models/user/profile.d.ts +31 -0
- package/models/user/profile.js +37 -0
- package/models/user/refresh_token.d.ts +41 -0
- package/models/user/refresh_token.js +51 -0
- package/models/user/team.d.ts +59 -0
- package/models/user/team.js +71 -0
- package/models/user/user.d.ts +79 -0
- package/models/user/user.js +100 -0
- package/models/user/user_organ.d.ts +52 -0
- package/models/user/user_organ.js +87 -0
- package/package.json +59 -0
- package/utils/auth.d.ts +25 -0
- package/utils/auth.js +137 -0
- package/utils/config.d.ts +8 -0
- package/utils/config.js +6 -0
- package/utils/core.d.ts +6 -0
- package/utils/core.js +39 -0
- package/utils/currency.d.ts +16 -0
- package/utils/currency.js +38 -0
- package/utils/db.d.ts +94 -0
- package/utils/db.js +399 -0
- package/utils/file.d.ts +232 -0
- package/utils/file.js +772 -0
- package/utils/helper.d.ts +113 -0
- package/utils/helper.js +508 -0
- package/utils/index.d.ts +10 -0
- package/utils/index.js +13 -0
- package/utils/language.d.ts +18 -0
- package/utils/language.js +48 -0
- package/utils/metadata.d.ts +18 -0
- package/utils/metadata.js +50 -0
- package/utils/session.d.ts +37 -0
- package/utils/session.js +60 -0
- package/utils/setting.d.ts +14 -0
- package/utils/setting.js +70 -0
- package/utils/stream.d.ts +303 -0
- package/utils/stream.js +1893 -0
- package/utils/user.d.ts +61 -0
- package/utils/user.js +282 -0
- package/utils/validator.d.ts +56 -0
- package/utils/validator.js +269 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UserUserOrganModel = exports.UserUserAssociation = exports.UserUserOrganAssociation = exports.UserUserOrganAttr = exports.UserUserOrganTable = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const sequelize = require("sequelize");
|
|
6
|
+
const database_1 = require("../../database");
|
|
7
|
+
const user_1 = require("../../models/user/user");
|
|
8
|
+
const organ_1 = require("../../models/user/organ");
|
|
9
|
+
exports.UserUserOrganTable = "users_organs";
|
|
10
|
+
exports.UserUserOrganAttr = {
|
|
11
|
+
user_id: {
|
|
12
|
+
type: sequelize.INTEGER,
|
|
13
|
+
allowNull: false,
|
|
14
|
+
primaryKey: true
|
|
15
|
+
},
|
|
16
|
+
organ_id: {
|
|
17
|
+
type: sequelize.INTEGER,
|
|
18
|
+
allowNull: false,
|
|
19
|
+
primaryKey: true
|
|
20
|
+
},
|
|
21
|
+
created: sequelize.DATE,
|
|
22
|
+
group_id: {
|
|
23
|
+
type: sequelize.INTEGER,
|
|
24
|
+
allowNull: false
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const UserUserOrganModel = database_1.default.define("UserUserOrgan", exports.UserUserOrganAttr, {
|
|
28
|
+
tableName: exports.UserUserOrganTable
|
|
29
|
+
});
|
|
30
|
+
exports.UserUserOrganModel = UserUserOrganModel;
|
|
31
|
+
UserUserOrganModel.removeAttribute('id');
|
|
32
|
+
exports.UserUserOrganAssociation = {
|
|
33
|
+
organ: {
|
|
34
|
+
model: organ_1.UserOrganModel,
|
|
35
|
+
alias: "organ",
|
|
36
|
+
join: (ref, { required = false, where = {}, attributes = undefined } = {}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
37
|
+
return {
|
|
38
|
+
where,
|
|
39
|
+
required,
|
|
40
|
+
attributes,
|
|
41
|
+
model: exports.UserUserOrganAssociation.organ.model.schema(ref, "_"),
|
|
42
|
+
as: exports.UserUserOrganAssociation.organ.alias
|
|
43
|
+
};
|
|
44
|
+
})
|
|
45
|
+
},
|
|
46
|
+
user: {
|
|
47
|
+
model: user_1.UserModel,
|
|
48
|
+
alias: "user",
|
|
49
|
+
join: (ref, { attributes = undefined } = {}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
50
|
+
return {
|
|
51
|
+
attributes,
|
|
52
|
+
model: exports.UserUserOrganAssociation.user.model.schema(ref, "_"),
|
|
53
|
+
as: exports.UserUserOrganAssociation.user.alias
|
|
54
|
+
};
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
exports.UserUserAssociation = {
|
|
59
|
+
organ: {
|
|
60
|
+
model: UserUserOrganModel,
|
|
61
|
+
alias: "user_organ",
|
|
62
|
+
join: (ref, { where = {}, required = false, attributes = undefined, organ = false } = {}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
63
|
+
let include = [];
|
|
64
|
+
if (organ) {
|
|
65
|
+
include.push({
|
|
66
|
+
required: true,
|
|
67
|
+
attributes,
|
|
68
|
+
model: organ_1.UserOrganModel.schema(ref, "_"),
|
|
69
|
+
as: "organ"
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
where,
|
|
74
|
+
required,
|
|
75
|
+
attributes,
|
|
76
|
+
model: exports.UserUserAssociation.organ.model.schema(ref, "_"),
|
|
77
|
+
as: exports.UserUserAssociation.organ.alias,
|
|
78
|
+
include
|
|
79
|
+
};
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
// organ
|
|
84
|
+
UserUserOrganModel.belongsTo(exports.UserUserOrganAssociation.organ.model, { as: exports.UserUserOrganAssociation.organ.alias, targetKey: "id", foreignKey: "organ_id" });
|
|
85
|
+
// user
|
|
86
|
+
UserUserOrganModel.belongsTo(exports.UserUserOrganAssociation.user.model, { as: exports.UserUserOrganAssociation.user.alias, targetKey: "id", foreignKey: "user_id" });
|
|
87
|
+
user_1.UserModel.hasOne(exports.UserUserAssociation.organ.model, { as: exports.UserUserAssociation.organ.alias, foreignKey: "user_id" });
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@digione/node-custom-api",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Typescript node digione-api",
|
|
5
|
+
"author": "Monchai Jirayupong <monchai.j@seven.co.th>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "index.js",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"start": "ts-node dist/index.ts",
|
|
10
|
+
"serve": "nodemon",
|
|
11
|
+
"build": "rm -rf lib -R && tsc && cp package.json ./lib",
|
|
12
|
+
"lint": "tslint --project tsconfig.json --format stylish"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"typescript"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@joi/date": "2.1.0",
|
|
19
|
+
"aws-sdk": "2.288.0",
|
|
20
|
+
"bull": "4.12.2",
|
|
21
|
+
"dotenv": "16.4.5",
|
|
22
|
+
"express": "4.18.2",
|
|
23
|
+
"i18n": "0.15.1",
|
|
24
|
+
"jimp": "0.22.10",
|
|
25
|
+
"joi": "17.11.0",
|
|
26
|
+
"js-sha1": "0.7.0",
|
|
27
|
+
"jsonwebtoken": "9.0.2",
|
|
28
|
+
"lodash": "4.17.21",
|
|
29
|
+
"moment": "2.30.1",
|
|
30
|
+
"multer": "1.4.5-lts.1",
|
|
31
|
+
"mysql2": "3.9.1",
|
|
32
|
+
"numeral": "2.0.6",
|
|
33
|
+
"php-serialize": "4.1.1",
|
|
34
|
+
"raven": "2.6.4",
|
|
35
|
+
"sequelize": "6.35.2",
|
|
36
|
+
"short-unique-id": "5.0.3",
|
|
37
|
+
"uuid": "9.0.1"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/aws-sdk": "2.7.0",
|
|
41
|
+
"@types/express": "4.17.21",
|
|
42
|
+
"@types/i18n": "0.13.10",
|
|
43
|
+
"@types/ioredis": "5.0.0",
|
|
44
|
+
"@types/jimp": "0.2.28",
|
|
45
|
+
"@types/jsonwebtoken": "9.0.5",
|
|
46
|
+
"@types/lodash": "4.14.202",
|
|
47
|
+
"@types/moment": "2.13.0",
|
|
48
|
+
"@types/multer": "1.4.11",
|
|
49
|
+
"@types/node": "20.10.6",
|
|
50
|
+
"@types/numeral": "2.0.5",
|
|
51
|
+
"@types/raven": "2.5.7",
|
|
52
|
+
"@types/sequelize": "4.28.20",
|
|
53
|
+
"@types/uuid": "9.0.8",
|
|
54
|
+
"nodemon": "3.0.2",
|
|
55
|
+
"ts-node": "10.9.2",
|
|
56
|
+
"tslint": "6.1.3",
|
|
57
|
+
"typescript": "5.3.3"
|
|
58
|
+
}
|
|
59
|
+
}
|
package/utils/auth.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export declare const getIdFromReq: (req: any) => any;
|
|
2
|
+
export declare const getTokenFromReq: (req: any) => any;
|
|
3
|
+
export declare class AuthUtil {
|
|
4
|
+
ref: string;
|
|
5
|
+
constructor(ref: string);
|
|
6
|
+
create(id: any, { include }?: {
|
|
7
|
+
include?: {};
|
|
8
|
+
}): Promise<{
|
|
9
|
+
refresh_token: string;
|
|
10
|
+
access_token: string;
|
|
11
|
+
}>;
|
|
12
|
+
revokeTokenByToken(token: any): Promise<{
|
|
13
|
+
success: number;
|
|
14
|
+
token: any;
|
|
15
|
+
}>;
|
|
16
|
+
refreshAccessTokenByToken(token: any): Promise<{
|
|
17
|
+
access_token: string;
|
|
18
|
+
}>;
|
|
19
|
+
signNotificationTokenByToken(token: any, input?: any): Promise<{
|
|
20
|
+
success: number;
|
|
21
|
+
}>;
|
|
22
|
+
revokeNotificationTokenByToken(token: any): Promise<{
|
|
23
|
+
success: number;
|
|
24
|
+
}>;
|
|
25
|
+
}
|
package/utils/auth.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthUtil = exports.getTokenFromReq = exports.getIdFromReq = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const access_token_1 = require("../models/user/access_token");
|
|
6
|
+
const refresh_token_1 = require("../models/user/refresh_token");
|
|
7
|
+
const notification_token_1 = require("../models/user/notification_token");
|
|
8
|
+
const helper_1 = require("./helper");
|
|
9
|
+
const errors_1 = require("../errors");
|
|
10
|
+
const authError_1 = require("../errors/authError");
|
|
11
|
+
const jwt = require("jsonwebtoken");
|
|
12
|
+
const signToken = (id, expIn, opt = {}) => {
|
|
13
|
+
const date = (0, helper_1.getDateTime)();
|
|
14
|
+
return jwt.sign(Object.assign({ id: id, createAt: date, expAt: date.add(expIn, 'days') }, opt), (0, helper_1.getENV)('JWT_SECRET'));
|
|
15
|
+
};
|
|
16
|
+
const getIdFromReq = (req) => {
|
|
17
|
+
try {
|
|
18
|
+
const token = req.headers['authorization'].split(' ')[1];
|
|
19
|
+
const decode = jwt.verify(token, (0, helper_1.getENV)('JWT_SECRET'));
|
|
20
|
+
return decode.id;
|
|
21
|
+
}
|
|
22
|
+
catch (e) {
|
|
23
|
+
throw new errors_1.CustomError(authError_1.authError.TOKEN_IS_INVALID);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
exports.getIdFromReq = getIdFromReq;
|
|
27
|
+
const getTokenFromReq = (req) => req.headers['authorization'].split(' ')[1];
|
|
28
|
+
exports.getTokenFromReq = getTokenFromReq;
|
|
29
|
+
class AuthUtil {
|
|
30
|
+
constructor(ref) {
|
|
31
|
+
this.ref = ref;
|
|
32
|
+
}
|
|
33
|
+
create(id, { include = {} } = {}) {
|
|
34
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
let refreshTokenUUID = (0, helper_1.getUUID)();
|
|
36
|
+
let include_access = {};
|
|
37
|
+
if (Object.keys(include).length) {
|
|
38
|
+
include_access['setting'] = JSON.stringify(include);
|
|
39
|
+
}
|
|
40
|
+
let input = Object.assign({ id: refreshTokenUUID, user_id: id, expired_at: (0, helper_1.getDateTime)().add((0, helper_1.getENV)('REFRESH_TOKEN_AGE'), 'days'), token: signToken(id, (0, helper_1.getENV)('REFRESH_TOKEN_AGE'), { refresh_token_id: refreshTokenUUID }), accessToken: Object.assign({ user_id: id, expired_at: (0, helper_1.getDateTime)().add((0, helper_1.getENV)('ACCESS_TOKEN_AGE'), 'days'), token: signToken(id, (0, helper_1.getENV)('ACCESS_TOKEN_AGE'), { refresh_token_id: refreshTokenUUID }) }, include_access) }, include);
|
|
41
|
+
yield refresh_token_1.RefreshTokenModel.schema(this.ref, "_").create(input, {
|
|
42
|
+
include: [
|
|
43
|
+
{
|
|
44
|
+
as: 'accessToken',
|
|
45
|
+
model: access_token_1.AccessTokenModel.schema(this.ref, ("_"))
|
|
46
|
+
}
|
|
47
|
+
]
|
|
48
|
+
});
|
|
49
|
+
return {
|
|
50
|
+
refresh_token: input.token,
|
|
51
|
+
access_token: input.accessToken.token
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
revokeTokenByToken(token) {
|
|
56
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
57
|
+
try {
|
|
58
|
+
const tokenData = yield (0, helper_1.getTokenDataFromTokenBySecret)(token, (0, helper_1.getENV)('JWT_SECRET'));
|
|
59
|
+
const refreshTokenId = tokenData.refresh_token_id;
|
|
60
|
+
let data = yield refresh_token_1.RefreshTokenModel.schema(this.ref, "_").destroy({
|
|
61
|
+
where: { id: refreshTokenId }
|
|
62
|
+
});
|
|
63
|
+
if (!data) {
|
|
64
|
+
return Promise.reject(errors_1.generalError.NOT_FOUND);
|
|
65
|
+
}
|
|
66
|
+
return { success: 1, token };
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
return Promise.reject(errors_1.generalError.BAD_REQUEST);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
refreshAccessTokenByToken(token) {
|
|
74
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
try {
|
|
76
|
+
const tokenData = yield (0, helper_1.getTokenDataFromTokenBySecret)(token, (0, helper_1.getENV)('JWT_SECRET'));
|
|
77
|
+
const refreshTokenId = tokenData.refresh_token_id;
|
|
78
|
+
const userId = tokenData.id;
|
|
79
|
+
yield access_token_1.AccessTokenModel.schema(this.ref, "_").destroy({
|
|
80
|
+
where: { refresh_token_id: refreshTokenId }
|
|
81
|
+
});
|
|
82
|
+
let include_access = {}, attributes = (this.ref == 'core') ? ['group_id'] : ['organ_id', 'group_id', 'b2b_group_id', 'team_id'];
|
|
83
|
+
let refresh = yield refresh_token_1.RefreshTokenModel.schema(this.ref, "_").findOne({
|
|
84
|
+
where: { id: refreshTokenId }, attributes, raw: true
|
|
85
|
+
});
|
|
86
|
+
if (!refresh)
|
|
87
|
+
return Promise.reject(errors_1.generalError.BAD_REQUEST);
|
|
88
|
+
include_access['setting'] = JSON.stringify(refresh);
|
|
89
|
+
const newAccessToken = signToken(userId, (0, helper_1.getENV)('ACCESS_TOKEN_AGE'), { refresh_token_id: refreshTokenId });
|
|
90
|
+
yield access_token_1.AccessTokenModel.schema(this.ref, "_").create(Object.assign({ user_id: userId, refresh_token_id: refreshTokenId, expired_at: (0, helper_1.getDateTime)().add((0, helper_1.getENV)('ACCESS_TOKEN_AGE'), 'days').format("YYYY-MM-DD HH:mm:ss"), token: newAccessToken }, include_access));
|
|
91
|
+
return {
|
|
92
|
+
access_token: newAccessToken
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
catch (err) {
|
|
96
|
+
return Promise.reject(errors_1.generalError.BAD_REQUEST);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
signNotificationTokenByToken(token, input = {}) {
|
|
101
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
102
|
+
try {
|
|
103
|
+
const tokenData = yield (0, helper_1.getTokenDataFromTokenBySecret)(token, (0, helper_1.getENV)('JWT_SECRET'));
|
|
104
|
+
const refreshTokenId = tokenData.refresh_token_id;
|
|
105
|
+
const userId = tokenData.id;
|
|
106
|
+
if (refreshTokenId) {
|
|
107
|
+
yield notification_token_1.NotificationTokenModel.schema(this.ref, "_").destroy({
|
|
108
|
+
where: { refresh_token_id: refreshTokenId }
|
|
109
|
+
});
|
|
110
|
+
yield notification_token_1.NotificationTokenModel.schema(this.ref, "_").create(Object.assign({ user_id: userId, refresh_token_id: refreshTokenId }, input));
|
|
111
|
+
}
|
|
112
|
+
return { success: 1 };
|
|
113
|
+
}
|
|
114
|
+
catch (err) {
|
|
115
|
+
return Promise.reject(errors_1.generalError.BAD_REQUEST);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
revokeNotificationTokenByToken(token) {
|
|
120
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
121
|
+
try {
|
|
122
|
+
const tokenData = yield (0, helper_1.getTokenDataFromTokenBySecret)(token, (0, helper_1.getENV)('JWT_SECRET'));
|
|
123
|
+
const refreshTokenId = tokenData.refresh_token_id;
|
|
124
|
+
if (refreshTokenId) {
|
|
125
|
+
yield notification_token_1.NotificationTokenModel.schema(this.ref, "_").destroy({
|
|
126
|
+
where: { refresh_token_id: refreshTokenId }
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return { success: 1 };
|
|
130
|
+
}
|
|
131
|
+
catch (err) {
|
|
132
|
+
return Promise.reject(errors_1.generalError.BAD_REQUEST);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
exports.AuthUtil = AuthUtil;
|
package/utils/config.js
ADDED
package/utils/core.d.ts
ADDED
package/utils/core.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.refreshDomainCors = exports.verifyDomainCors = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const domain_1 = require("../models/core/domain");
|
|
6
|
+
const redis_1 = require("../database/redis");
|
|
7
|
+
const verifyDomainCors = (origin, { where = {} } = {}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
8
|
+
let index = "core_domain_cors";
|
|
9
|
+
where = Object.assign(Object.assign({}, where), { type: "cors", site_id: 0 });
|
|
10
|
+
if (redis_1.redisReady) {
|
|
11
|
+
let setting = yield (0, redis_1.redisGet)(index);
|
|
12
|
+
if (!setting) {
|
|
13
|
+
setting = yield (0, exports.refreshDomainCors)();
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
setting = JSON.parse(setting);
|
|
17
|
+
}
|
|
18
|
+
return (typeof setting[origin] != "undefined");
|
|
19
|
+
}
|
|
20
|
+
let count = yield domain_1.CoreDomainModel.schema("core", "_").count({ where: Object.assign({ domain: origin }, where) });
|
|
21
|
+
return count > 0;
|
|
22
|
+
});
|
|
23
|
+
exports.verifyDomainCors = verifyDomainCors;
|
|
24
|
+
const refreshDomainCors = ({ where = {} } = {}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
25
|
+
let index = "core_domain_cors", setting = {};
|
|
26
|
+
where = Object.assign(Object.assign({}, where), { type: "cors", site_id: 0 });
|
|
27
|
+
if (redis_1.redisReady) {
|
|
28
|
+
let query = (yield domain_1.CoreDomainModel.schema("core", "_").findAll({
|
|
29
|
+
where, raw: true, attributes: ['domain']
|
|
30
|
+
}));
|
|
31
|
+
setting = query.reduce((total, item) => {
|
|
32
|
+
total[item['domain']] = 1;
|
|
33
|
+
return total;
|
|
34
|
+
}, {});
|
|
35
|
+
yield (0, redis_1.redisSet)(index, JSON.stringify(setting));
|
|
36
|
+
}
|
|
37
|
+
return setting;
|
|
38
|
+
});
|
|
39
|
+
exports.refreshDomainCors = refreshDomainCors;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
interface OptionCurrency {
|
|
2
|
+
organ_id?: string | number;
|
|
3
|
+
[index: string]: any;
|
|
4
|
+
}
|
|
5
|
+
export declare class CurrencyUtil {
|
|
6
|
+
ref: string;
|
|
7
|
+
option: OptionCurrency;
|
|
8
|
+
constructor(ref: string, option?: OptionCurrency);
|
|
9
|
+
getCurrency({ where, attr_include, attr_vat, vat_value }?: {
|
|
10
|
+
where?: {};
|
|
11
|
+
attr_include?: any[];
|
|
12
|
+
attr_vat?: any[];
|
|
13
|
+
vat_value?: boolean;
|
|
14
|
+
}): Promise<any>;
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CurrencyUtil = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const currency_1 = require("../models/system/currency");
|
|
6
|
+
const organ_1 = require("../models/user/organ");
|
|
7
|
+
const sequelize_1 = require("sequelize");
|
|
8
|
+
class CurrencyUtil {
|
|
9
|
+
constructor(ref, option) {
|
|
10
|
+
this.ref = ref;
|
|
11
|
+
this.option = option || {};
|
|
12
|
+
}
|
|
13
|
+
getCurrency({ where = {}, attr_include = [], attr_vat = [], vat_value = false } = {}) {
|
|
14
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
where['enabled'] = '1';
|
|
16
|
+
let attributes = ['id', 'title', 'exch_rate', 'cur_decimal', 'cur_comma', 'cur_format_num', 'cur_format'].concat(attr_include);
|
|
17
|
+
let query = yield currency_1.SystemCurrencyModel.schema(this.ref, "_").findOne({
|
|
18
|
+
attributes, where, raw: true
|
|
19
|
+
});
|
|
20
|
+
if (query) {
|
|
21
|
+
if (this.option.organ_id) {
|
|
22
|
+
if (vat_value) {
|
|
23
|
+
let setting = (yield organ_1.UserOrganModel.schema(this.ref, "_").findOne({
|
|
24
|
+
raw: true,
|
|
25
|
+
where: { id: this.option.organ_id },
|
|
26
|
+
attributes: [...attr_vat, [(0, sequelize_1.literal)(`vat.tax_value`), 'vat_value']],
|
|
27
|
+
include: [yield organ_1.UserOrganAssociation.vat.join(this.ref, { attributes: [], required: true })]
|
|
28
|
+
})) || {};
|
|
29
|
+
setting['vat_value'] = setting['vat_value'] || 0;
|
|
30
|
+
query = Object.assign(Object.assign({}, query), setting);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return query;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.CurrencyUtil = CurrencyUtil;
|
package/utils/db.d.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import * as sequelize from 'sequelize';
|
|
2
|
+
import { ModelAttributeColumnOptions } from 'sequelize';
|
|
3
|
+
export declare const genWhere: (where?: {}, { as, delimiter, prefix, bracket }?: {
|
|
4
|
+
as?: string;
|
|
5
|
+
delimiter?: string;
|
|
6
|
+
prefix?: boolean;
|
|
7
|
+
bracket?: boolean;
|
|
8
|
+
}) => string;
|
|
9
|
+
export declare const select: (table: string, { where, attributes, association, limit, offset, order }?: {
|
|
10
|
+
where?: {};
|
|
11
|
+
attributes?: any[];
|
|
12
|
+
association?: any[];
|
|
13
|
+
limit?: number;
|
|
14
|
+
offset?: number;
|
|
15
|
+
order?: any[];
|
|
16
|
+
}) => Promise<string>;
|
|
17
|
+
export declare const genValue: (value: any) => any;
|
|
18
|
+
export declare const count: (table: string, { where, primaryKey }?: {
|
|
19
|
+
where?: {};
|
|
20
|
+
primaryKey?: string;
|
|
21
|
+
}) => Promise<any>;
|
|
22
|
+
export declare const findOne: (table: string, { where, attributes, association, order }?: {
|
|
23
|
+
where?: {};
|
|
24
|
+
attributes?: any[];
|
|
25
|
+
association?: any[];
|
|
26
|
+
order?: any[];
|
|
27
|
+
}) => Promise<object>;
|
|
28
|
+
export declare const findAll: (table: string, { where, attributes, association, limit, offset, order }?: {
|
|
29
|
+
where?: {};
|
|
30
|
+
attributes?: any[];
|
|
31
|
+
association?: any[];
|
|
32
|
+
limit?: number;
|
|
33
|
+
offset?: number;
|
|
34
|
+
order?: any[];
|
|
35
|
+
}) => Promise<Array<any>>;
|
|
36
|
+
export declare const update: (ref: string, table: string, input: any, { where, default_lang, index }?: {
|
|
37
|
+
where?: {};
|
|
38
|
+
default_lang?: string;
|
|
39
|
+
index?: string;
|
|
40
|
+
}) => Promise<[undefined, number] | {
|
|
41
|
+
[x: string]: any;
|
|
42
|
+
}>;
|
|
43
|
+
export declare const insert: (ref: string, table: string, input: any, { index }?: {
|
|
44
|
+
index?: string;
|
|
45
|
+
}) => Promise<{
|
|
46
|
+
[x: string]: any;
|
|
47
|
+
}>;
|
|
48
|
+
export declare const remove: (ref: string, table: string, { where }?: {
|
|
49
|
+
where?: {};
|
|
50
|
+
}) => Promise<number>;
|
|
51
|
+
export declare const max: (ref: string, table: string, field: any, { where }?: {
|
|
52
|
+
where?: {};
|
|
53
|
+
}) => Promise<object[]>;
|
|
54
|
+
export declare const getNextId: (ref: string, table: any) => Promise<any>;
|
|
55
|
+
export declare const getFieldAll: (table: string, { type }?: {
|
|
56
|
+
type?: boolean;
|
|
57
|
+
}) => Promise<any[]>;
|
|
58
|
+
export declare const truncate: (table: string) => Promise<[unknown[], unknown]>;
|
|
59
|
+
export declare const cloneTable: (old_table: string, new_table: string, { withRow }?: {
|
|
60
|
+
withRow?: boolean;
|
|
61
|
+
}) => Promise<boolean>;
|
|
62
|
+
export declare const createTable: (table: string, attributes: any, { removePrimary, field }?: {
|
|
63
|
+
removePrimary?: boolean;
|
|
64
|
+
field?: string;
|
|
65
|
+
}) => Promise<void>;
|
|
66
|
+
export declare const dropTable: (table: string) => Promise<void>;
|
|
67
|
+
export declare const renameTable: (before: string, after: string) => Promise<void>;
|
|
68
|
+
export declare const removeAttrReference: (obj: any, { field, removePrimary }?: {
|
|
69
|
+
field?: string;
|
|
70
|
+
removePrimary?: boolean;
|
|
71
|
+
}) => any;
|
|
72
|
+
export declare const getListTable: (search: string, { postfix }?: {
|
|
73
|
+
postfix?: boolean;
|
|
74
|
+
}) => Promise<any[]>;
|
|
75
|
+
export declare const getExistTable: (table: string) => Promise<boolean>;
|
|
76
|
+
export declare const getExistField: (table: string, field: string) => Promise<boolean>;
|
|
77
|
+
export declare const getChildren: (table: string, value: any, { level, max, where, array, key_index, key, init, lang_code }?: {
|
|
78
|
+
level?: number;
|
|
79
|
+
max?: number;
|
|
80
|
+
where?: {};
|
|
81
|
+
array?: any[];
|
|
82
|
+
key_index?: string;
|
|
83
|
+
key?: string;
|
|
84
|
+
init?: boolean;
|
|
85
|
+
lang_code?: boolean;
|
|
86
|
+
}) => Promise<Array<any>>;
|
|
87
|
+
export declare const addColumn: (table: string, field: string, schema: ModelAttributeColumnOptions) => Promise<any>;
|
|
88
|
+
export declare const addConstraint: (table: string, options: sequelize.AddConstraintOptions) => Promise<void>;
|
|
89
|
+
export declare const changeColumn: (table: string, field: string, schema: ModelAttributeColumnOptions, { remove_primarykey, remove_index }?: {
|
|
90
|
+
remove_primarykey?: boolean;
|
|
91
|
+
remove_index?: boolean;
|
|
92
|
+
}) => Promise<void>;
|
|
93
|
+
export declare const removeColumn: (table: string, field: string) => Promise<[unknown[], unknown]>;
|
|
94
|
+
export declare const cloneColumn: (table: string, old_field: string, new_field: string) => Promise<[undefined, number]>;
|