@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.
Files changed (161) hide show
  1. package/constants/header.d.ts +10 -0
  2. package/constants/header.js +2 -0
  3. package/constants/http.d.ts +44 -0
  4. package/constants/http.js +2 -0
  5. package/database/index.d.ts +3 -0
  6. package/database/index.js +34 -0
  7. package/database/redis.d.ts +15 -0
  8. package/database/redis.js +68 -0
  9. package/errors/authError.d.ts +55 -0
  10. package/errors/authError.js +57 -0
  11. package/errors/index.d.ts +32 -0
  12. package/errors/index.js +113 -0
  13. package/index.d.ts +1 -0
  14. package/index.js +4 -0
  15. package/interface/param.d.ts +41 -0
  16. package/interface/param.js +2 -0
  17. package/middlewares/accessToken.d.ts +13 -0
  18. package/middlewares/accessToken.js +200 -0
  19. package/middlewares/coreSite.d.ts +4 -0
  20. package/middlewares/coreSite.js +52 -0
  21. package/middlewares/paramQuery.d.ts +16 -0
  22. package/middlewares/paramQuery.js +220 -0
  23. package/middlewares/refSite.d.ts +2 -0
  24. package/middlewares/refSite.js +58 -0
  25. package/middlewares/refreshToken.d.ts +2 -0
  26. package/middlewares/refreshToken.js +56 -0
  27. package/middlewares/systemSite.d.ts +6 -0
  28. package/middlewares/systemSite.js +106 -0
  29. package/middlewares/webSite.d.ts +7 -0
  30. package/middlewares/webSite.js +109 -0
  31. package/models/core/city.d.ts +66 -0
  32. package/models/core/city.js +79 -0
  33. package/models/core/country.d.ts +35 -0
  34. package/models/core/country.js +44 -0
  35. package/models/core/district.d.ts +17 -0
  36. package/models/core/district.js +23 -0
  37. package/models/core/domain.d.ts +57 -0
  38. package/models/core/domain.js +75 -0
  39. package/models/core/site.d.ts +60 -0
  40. package/models/core/site.js +70 -0
  41. package/models/core/state.d.ts +49 -0
  42. package/models/core/state.js +58 -0
  43. package/models/core/zipcode.d.ts +5 -0
  44. package/models/core/zipcode.js +28 -0
  45. package/models/stream/field.d.ts +60 -0
  46. package/models/stream/field.js +66 -0
  47. package/models/stream/field_section.d.ts +33 -0
  48. package/models/stream/field_section.js +39 -0
  49. package/models/stream/stream.d.ts +89 -0
  50. package/models/stream/stream.js +97 -0
  51. package/models/system/currency.d.ts +5 -0
  52. package/models/system/currency.js +32 -0
  53. package/models/system/email_log.d.ts +5 -0
  54. package/models/system/email_log.js +54 -0
  55. package/models/system/email_sender.d.ts +22 -0
  56. package/models/system/email_sender.js +53 -0
  57. package/models/system/email_template.d.ts +5 -0
  58. package/models/system/email_template.js +49 -0
  59. package/models/system/file.d.ts +98 -0
  60. package/models/system/file.js +111 -0
  61. package/models/system/folder.d.ts +59 -0
  62. package/models/system/folder.js +78 -0
  63. package/models/system/language.d.ts +44 -0
  64. package/models/system/language.js +50 -0
  65. package/models/system/metadata.d.ts +5 -0
  66. package/models/system/metadata.js +24 -0
  67. package/models/system/module.d.ts +5 -0
  68. package/models/system/module.js +35 -0
  69. package/models/system/notification_log.d.ts +25 -0
  70. package/models/system/notification_log.js +76 -0
  71. package/models/system/notification_module.d.ts +5 -0
  72. package/models/system/notification_module.js +35 -0
  73. package/models/system/notification_setting.d.ts +22 -0
  74. package/models/system/notification_setting.js +44 -0
  75. package/models/system/notification_template.d.ts +5 -0
  76. package/models/system/notification_template.js +49 -0
  77. package/models/system/notification_type.d.ts +5 -0
  78. package/models/system/notification_type.js +38 -0
  79. package/models/system/payment_transaction.d.ts +5 -0
  80. package/models/system/payment_transaction.js +46 -0
  81. package/models/system/pdf_page.d.ts +5 -0
  82. package/models/system/pdf_page.js +39 -0
  83. package/models/system/pdf_template.d.ts +22 -0
  84. package/models/system/pdf_template.js +67 -0
  85. package/models/system/review_template.d.ts +5 -0
  86. package/models/system/review_template.js +35 -0
  87. package/models/system/review_vote.d.ts +5 -0
  88. package/models/system/review_vote.js +36 -0
  89. package/models/system/session.d.ts +5 -0
  90. package/models/system/session.js +19 -0
  91. package/models/system/setting.d.ts +15 -0
  92. package/models/system/setting.js +22 -0
  93. package/models/system/site_token.d.ts +25 -0
  94. package/models/system/site_token.js +31 -0
  95. package/models/system/sms_log.d.ts +4 -0
  96. package/models/system/sms_log.js +52 -0
  97. package/models/system/sms_setting.d.ts +4 -0
  98. package/models/system/sms_setting.js +39 -0
  99. package/models/system/sms_template.d.ts +5 -0
  100. package/models/system/sms_template.js +48 -0
  101. package/models/system/tax.d.ts +5 -0
  102. package/models/system/tax.js +33 -0
  103. package/models/system/website.d.ts +56 -0
  104. package/models/system/website.js +66 -0
  105. package/models/system/zone.d.ts +57 -0
  106. package/models/system/zone.js +141 -0
  107. package/models/user/access_token.d.ts +37 -0
  108. package/models/user/access_token.js +46 -0
  109. package/models/user/address.d.ts +69 -0
  110. package/models/user/address.js +119 -0
  111. package/models/user/b2b_group.d.ts +57 -0
  112. package/models/user/b2b_group.js +63 -0
  113. package/models/user/group.d.ts +28 -0
  114. package/models/user/group.js +34 -0
  115. package/models/user/group_permission.d.ts +4 -0
  116. package/models/user/group_permission.js +27 -0
  117. package/models/user/notification_token.d.ts +32 -0
  118. package/models/user/notification_token.js +39 -0
  119. package/models/user/organ.d.ts +85 -0
  120. package/models/user/organ.js +100 -0
  121. package/models/user/profile.d.ts +31 -0
  122. package/models/user/profile.js +37 -0
  123. package/models/user/refresh_token.d.ts +41 -0
  124. package/models/user/refresh_token.js +51 -0
  125. package/models/user/team.d.ts +59 -0
  126. package/models/user/team.js +71 -0
  127. package/models/user/user.d.ts +79 -0
  128. package/models/user/user.js +100 -0
  129. package/models/user/user_organ.d.ts +52 -0
  130. package/models/user/user_organ.js +87 -0
  131. package/package.json +59 -0
  132. package/utils/auth.d.ts +25 -0
  133. package/utils/auth.js +137 -0
  134. package/utils/config.d.ts +8 -0
  135. package/utils/config.js +6 -0
  136. package/utils/core.d.ts +6 -0
  137. package/utils/core.js +39 -0
  138. package/utils/currency.d.ts +16 -0
  139. package/utils/currency.js +38 -0
  140. package/utils/db.d.ts +94 -0
  141. package/utils/db.js +399 -0
  142. package/utils/file.d.ts +232 -0
  143. package/utils/file.js +772 -0
  144. package/utils/helper.d.ts +113 -0
  145. package/utils/helper.js +508 -0
  146. package/utils/index.d.ts +10 -0
  147. package/utils/index.js +13 -0
  148. package/utils/language.d.ts +18 -0
  149. package/utils/language.js +48 -0
  150. package/utils/metadata.d.ts +18 -0
  151. package/utils/metadata.js +50 -0
  152. package/utils/session.d.ts +37 -0
  153. package/utils/session.js +60 -0
  154. package/utils/setting.d.ts +14 -0
  155. package/utils/setting.js +70 -0
  156. package/utils/stream.d.ts +303 -0
  157. package/utils/stream.js +1893 -0
  158. package/utils/user.d.ts +61 -0
  159. package/utils/user.js +282 -0
  160. package/utils/validator.d.ts +56 -0
  161. 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
+ }
@@ -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;
@@ -0,0 +1,8 @@
1
+ interface ConfigOpt {
2
+ fileDestination?: string;
3
+ fileDelete?: boolean;
4
+ [key: string]: any;
5
+ }
6
+ export declare let config: ConfigOpt;
7
+ export declare const setConfig: (opt: ConfigOpt) => ConfigOpt;
8
+ export {};
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setConfig = exports.config = void 0;
4
+ exports.config = {};
5
+ const setConfig = (opt) => exports.config = opt;
6
+ exports.setConfig = setConfig;
@@ -0,0 +1,6 @@
1
+ export declare const verifyDomainCors: (origin: any, { where }?: {
2
+ where?: {};
3
+ }) => Promise<boolean>;
4
+ export declare const refreshDomainCors: ({ where }?: {
5
+ where?: {};
6
+ }) => Promise<{}>;
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]>;