@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,200 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.accessToken = exports.verifyPermissionGroup = exports.getGroupPermission = exports.getGroupSetting = exports.verifyStaff = exports.verifyAccessToken = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const access_token_1 = require("../models/user/access_token");
6
+ const helper_1 = require("../utils/helper");
7
+ const authError_1 = require("../errors/authError");
8
+ const errors_1 = require("../errors");
9
+ const auth_1 = require("../utils/auth");
10
+ const group_1 = require("../models/user/group");
11
+ const group_permission_1 = require("../models/user/group_permission");
12
+ const user_1 = require("../utils/user");
13
+ const sequelize_1 = require("sequelize");
14
+ const verifyAccessToken = (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
15
+ try {
16
+ if (req.headers['authorization']) {
17
+ const ref = res.locals.ref;
18
+ const type = req.headers['authorization'].split(' ')[0];
19
+ const token = req.headers['authorization'].split(' ')[1];
20
+ if (type.toLocaleUpperCase() != "BEARER") {
21
+ throw new errors_1.CustomError(authError_1.authError.TOKEN_IS_INVALID);
22
+ }
23
+ let data = yield (0, helper_1.getTokenDataFromTokenBySecret)(token, (0, helper_1.getENV)('JWT_SECRET'));
24
+ const tokenData = yield access_token_1.AccessTokenModel.schema(ref, "_").findOne({
25
+ where: { token, user_id: data.id },
26
+ raw: true,
27
+ attributes: ['expired_at', 'setting']
28
+ });
29
+ if (!tokenData) {
30
+ throw new errors_1.CustomError(authError_1.authError.TOKEN_IS_INVALID);
31
+ }
32
+ res.locals.auth_id = data.id;
33
+ let setting = JSON.parse(tokenData['setting']);
34
+ res.locals.group_id = setting['group_id'];
35
+ res.locals.related_id = setting['organ_id'];
36
+ if (setting['b2b_group_id']) {
37
+ res.locals.b2b_group_id = setting['b2b_group_id'];
38
+ }
39
+ if (setting['team_id']) {
40
+ res.locals.team_id = setting['team_id'].split(',');
41
+ }
42
+ if (!res.locals.verify_organ) {
43
+ res.locals.organ_id = setting['organ_id'];
44
+ }
45
+ const now = (0, helper_1.getDateTime)();
46
+ const then = (0, helper_1.getDateTime)(tokenData['expired_at']);
47
+ if (then > now) {
48
+ return next();
49
+ }
50
+ else {
51
+ yield new auth_1.AuthUtil(ref).refreshAccessTokenByToken(token);
52
+ throw new errors_1.CustomError(authError_1.authError.TOKEN_EXPIRED);
53
+ }
54
+ }
55
+ throw new errors_1.CustomError(authError_1.authError.TOKEN_IS_INVALID);
56
+ }
57
+ catch (e) {
58
+ if (e.code > 505 || !e.code) {
59
+ return res.status(401 /* HTTP.UNAUTHORIZED */).send({
60
+ code: 401 /* HTTP.UNAUTHORIZED */,
61
+ json: { code: authError_1.authError.TOKEN_IS_INVALID.message }
62
+ });
63
+ }
64
+ else {
65
+ let err = new errors_1.CustomError(e);
66
+ return res.status(401 /* HTTP.UNAUTHORIZED */).send(err);
67
+ }
68
+ }
69
+ });
70
+ exports.verifyAccessToken = verifyAccessToken;
71
+ const verifyStaff = (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
72
+ return (0, exports.verifyAccessToken)(req, res, () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
73
+ const ref = res.locals.ref;
74
+ try {
75
+ let count = yield group_1.UserGroupModel.schema(ref, "_").count({ where: { id: res.locals.group_id, type_group: 'staff' } });
76
+ if (!count) {
77
+ throw new errors_1.CustomError(authError_1.authError.ACCESS_DENINED);
78
+ }
79
+ return next();
80
+ }
81
+ catch (e) {
82
+ if (e.code == 403 || !e.code) {
83
+ return res.status(403 /* HTTP.FORBIDDEN */).send({
84
+ code: 403 /* HTTP.FORBIDDEN */,
85
+ json: { code: authError_1.authError.ACCESS_DENINED.message }
86
+ });
87
+ }
88
+ else {
89
+ let err = new errors_1.CustomError(e);
90
+ return res.status(401 /* HTTP.UNAUTHORIZED */).send(err);
91
+ }
92
+ }
93
+ }));
94
+ });
95
+ exports.verifyStaff = verifyStaff;
96
+ const getGroupSetting = (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
97
+ try {
98
+ let ref = res.locals.ref;
99
+ let group_id = res.locals.group_id;
100
+ let data = yield (0, user_1.getGroupSetting)(ref, group_id);
101
+ if (data) {
102
+ res.locals.group_shared = data['shared'];
103
+ res.locals.group_setting = data['setting'] || {};
104
+ return next();
105
+ }
106
+ throw new errors_1.CustomError(authError_1.authError.ACCESS_DENINED);
107
+ }
108
+ catch (e) {
109
+ let err = new errors_1.CustomError(e);
110
+ return res.status(403 /* HTTP.FORBIDDEN */).send(err);
111
+ }
112
+ });
113
+ exports.getGroupSetting = getGroupSetting;
114
+ function getGroupPermission(value, { role = "", param_key = "query" } = {}) {
115
+ return function (req, res, next) {
116
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
117
+ let module = (typeof value == "function") ? value(req[param_key]) : value;
118
+ try {
119
+ let ref = res.locals.ref;
120
+ let organ_id = res.locals.organ_id;
121
+ let group_id = res.locals.group_id;
122
+ let permission = {};
123
+ return (0, exports.getGroupSetting)(req, res, () => tslib_1.__awaiter(this, void 0, void 0, function* () {
124
+ permission['group_setting'] = res.locals.group_setting;
125
+ permission['admin'] = (res.locals.group_shared == 1);
126
+ if (!res.locals.group_shared) {
127
+ permission = yield (0, user_1.getGroupPermission)(ref, group_id, module, { permission, group_shared: res.locals.group_shared, organ_id });
128
+ if (role && !permission[role]) {
129
+ let err = new errors_1.CustomError(authError_1.authError.ACCESS_DENINED);
130
+ return res.status(403 /* HTTP.FORBIDDEN */).send(err);
131
+ }
132
+ }
133
+ res.locals.option = res.locals.option || {};
134
+ res.locals.option['permission'] = permission;
135
+ return next();
136
+ }));
137
+ }
138
+ catch (e) {
139
+ let err = new errors_1.CustomError(e);
140
+ return res.status(403 /* HTTP.FORBIDDEN */).send(err);
141
+ }
142
+ });
143
+ };
144
+ }
145
+ exports.getGroupPermission = getGroupPermission;
146
+ ;
147
+ function verifyPermissionGroup(value, { role = "", param_key = "query" } = {}) {
148
+ return function (req, res, next) {
149
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
150
+ let module = (typeof value == "function") ? value(req[param_key]) : value;
151
+ try {
152
+ let ref = res.locals.ref;
153
+ let organ_id = res.locals.organ_id;
154
+ let group_id = res.locals.group_id;
155
+ if (!role) {
156
+ switch (req.method) {
157
+ case "GET":
158
+ role = "r";
159
+ break;
160
+ case "POST":
161
+ role = "c";
162
+ break;
163
+ case "PUT":
164
+ role = "u";
165
+ break;
166
+ case "DELETE":
167
+ role = "d";
168
+ break;
169
+ }
170
+ }
171
+ return (0, exports.getGroupSetting)(req, res, () => tslib_1.__awaiter(this, void 0, void 0, function* () {
172
+ if (!res.locals.group_shared) {
173
+ let exist = yield group_permission_1.UserGroupPermissionModel.schema(ref, "_").count({
174
+ where: { organ_id, group_id, module, roles: { [sequelize_1.Op.like]: "%" + role + "%" } }
175
+ });
176
+ if (exist) {
177
+ return next();
178
+ }
179
+ let err = new errors_1.CustomError(authError_1.authError.ACCESS_DENINED);
180
+ return res.status(403 /* HTTP.FORBIDDEN */).send(err);
181
+ }
182
+ return next();
183
+ }));
184
+ }
185
+ catch (e) {
186
+ let err = new errors_1.CustomError(e);
187
+ return res.status(403 /* HTTP.FORBIDDEN */).send(err);
188
+ }
189
+ });
190
+ };
191
+ }
192
+ exports.verifyPermissionGroup = verifyPermissionGroup;
193
+ ;
194
+ const accessToken = (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
195
+ if (req.headers['authorization']) {
196
+ return (0, exports.verifyAccessToken)(req, res, next);
197
+ }
198
+ return next();
199
+ });
200
+ exports.accessToken = accessToken;
@@ -0,0 +1,4 @@
1
+ import { Request, Response } from 'express';
2
+ export declare const heathCheck: (req: Request, res: Response) => Promise<void>;
3
+ export declare const paramRef: (req: Request, res: Response, next: any) => Promise<any>;
4
+ export declare const coreRef: (req: Request, res: Response, next: any) => Promise<any>;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.coreRef = exports.paramRef = exports.heathCheck = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const site_1 = require("../models/core/site");
6
+ const errors_1 = require("../errors");
7
+ const database_1 = require("../database");
8
+ const redis_1 = require("../database/redis");
9
+ const heathCheck = (req, res) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
10
+ let data = { status: { mysql: true, mongo: true, redis: true }, reason: {} }, status = 200 /* HTTP.OK */;
11
+ // mysql
12
+ try {
13
+ yield database_1.default.authenticate();
14
+ }
15
+ catch (err) {
16
+ let parent = err['parent'] || {};
17
+ data['reason']['mysql'] = parent['code'] || "Unable to connect to database";
18
+ data['status']['mysql'] = false;
19
+ status = 503 /* HTTP.SERVICE_UNAVAILABLE */;
20
+ }
21
+ // redis
22
+ if (redis_1.redisError) {
23
+ data['reason']['redis'] = redis_1.redisError;
24
+ data['status']['redis'] = false;
25
+ status = 503 /* HTTP.SERVICE_UNAVAILABLE */;
26
+ }
27
+ res.status(status).send(data);
28
+ });
29
+ exports.heathCheck = heathCheck;
30
+ const paramRef = (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
31
+ try {
32
+ let ref = req.params.ref;
33
+ if (!ref)
34
+ throw new errors_1.CustomError(errors_1.generalError.NOT_FOUND);
35
+ let exist = yield site_1.CoreSiteModel.schema("core", "_").count({ where: { ref } });
36
+ if (exist) {
37
+ res.locals.ref = ref;
38
+ res.locals.user_id = 1;
39
+ return next();
40
+ }
41
+ }
42
+ catch (err) { }
43
+ let err = new errors_1.CustomError(errors_1.generalError.NOT_FOUND);
44
+ return res.status(err.code).send(err);
45
+ });
46
+ exports.paramRef = paramRef;
47
+ const coreRef = (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
48
+ res.locals.ref = "core";
49
+ res.locals.option = { ref_core: true };
50
+ return next();
51
+ });
52
+ exports.coreRef = coreRef;
@@ -0,0 +1,16 @@
1
+ import { Request, Response } from 'express';
2
+ export declare const getAuthParam: (req: Request, res: Response, next: any) => void;
3
+ export declare const getStremParam: (req: Request, res: Response, next: any) => void;
4
+ export declare const getQueryParam: (req: Request, res: Response, next: any) => void;
5
+ export declare const getQueryParamCUD: (req: Request, res: Response, next: any) => void;
6
+ export declare const getQueryParamFile: (req: Request, res: Response, next: any) => void;
7
+ export declare const changeQueryParam: (value?: {}, { skipSystem }?: {
8
+ skipSystem?: boolean;
9
+ }) => (req: Request, res: Response, next: any) => any;
10
+ export declare const changeOptionParam: (value?: {}, { skipSystem }?: {
11
+ skipSystem?: boolean;
12
+ }) => (req: Request, res: Response, next: any) => any;
13
+ export declare const changeBodyParam: (value?: {}, { skipSystem, omit }?: {
14
+ skipSystem?: boolean;
15
+ omit?: any[];
16
+ }) => (req: Request, res: Response, next: any) => any;
@@ -0,0 +1,220 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.changeBodyParam = exports.changeOptionParam = exports.changeQueryParam = exports.getQueryParamFile = exports.getQueryParamCUD = exports.getQueryParam = exports.getStremParam = exports.getAuthParam = void 0;
4
+ const helper_1 = require("../utils/helper");
5
+ const _ = require("lodash");
6
+ const getAuthParam = (req, res, next) => {
7
+ res.locals.option['hostname'] = ((0, helper_1.getENV)('NODE_ENV') == 'development' ? 'http' : 'https') + '://' + req.get('host');
8
+ if (res.locals.auth_id) {
9
+ res.locals.option['auth_id'] = res.locals.auth_id;
10
+ }
11
+ if (res.locals.web_id) {
12
+ res.locals.option['web_id'] = res.locals.web_id;
13
+ }
14
+ if (res.locals.organ_id) {
15
+ res.locals.option['organ_id'] = res.locals.organ_id;
16
+ }
17
+ if (res.locals.parent_organ_id) {
18
+ res.locals.option['parent_organ_id'] = res.locals.parent_organ_id;
19
+ }
20
+ next();
21
+ };
22
+ exports.getAuthParam = getAuthParam;
23
+ const getStremParam = (req, res, next) => {
24
+ res.locals.option = res.locals.option || {};
25
+ res.locals.option['excludeObj'] = {};
26
+ res.locals.option['lang_code'] = 'en';
27
+ if (req.query.include) {
28
+ try {
29
+ let array = String(req.query.include).split(',');
30
+ res.locals.include = _.zipObject(array, array);
31
+ }
32
+ catch (err) {
33
+ res.locals.include = {};
34
+ }
35
+ }
36
+ if (req.query.require) {
37
+ if (req.query.require == "1") {
38
+ res.locals.option['require'] = "yes";
39
+ }
40
+ else if (req.query.require == "0") {
41
+ res.locals.option['require'] = "no";
42
+ }
43
+ }
44
+ return (0, exports.getAuthParam)(req, res, () => {
45
+ if (req.query.type) {
46
+ res.locals.option['type'] = req.query.type;
47
+ }
48
+ if (req.query.section == "1") {
49
+ res.locals.option['section'] = req.query.section;
50
+ }
51
+ if (req.query.lang_code) {
52
+ res.locals.option['lang_code'] = req.query.lang_code;
53
+ }
54
+ if (req.query.currency) {
55
+ res.locals.option['currency'] = req.query.currency;
56
+ }
57
+ if (req.query.field_type) {
58
+ let arr = String(req.query.field_type).split(',');
59
+ let obj = arr.reduce((total, item) => {
60
+ total[item] = true;
61
+ return total;
62
+ }, {});
63
+ res.locals.option['field_type'] = _.pick(obj, ['text', 'choice', 'bullets', 'relationship']);
64
+ }
65
+ if (req.query.exclude) {
66
+ try {
67
+ let arr = String(req.query.exclude).split(',');
68
+ res.locals.option['exclude'] = arr;
69
+ res.locals.option['excludeObj'] = arr.reduce((total, item) => {
70
+ total[item] = item;
71
+ return total;
72
+ }, {});
73
+ }
74
+ catch (err) { }
75
+ }
76
+ return next();
77
+ });
78
+ };
79
+ exports.getStremParam = getStremParam;
80
+ const getQueryParam = (req, res, next) => {
81
+ res.locals.option = res.locals.option || {};
82
+ res.locals.option['lang_code'] = 'en';
83
+ res.locals.option['currency'] = 'thb';
84
+ if (req.query.include) {
85
+ try {
86
+ let array = String(req.query.include).split(',');
87
+ res.locals.include = _.zipObject(array, array);
88
+ }
89
+ catch (err) {
90
+ res.locals.include = {};
91
+ }
92
+ }
93
+ else {
94
+ res.locals.include = {};
95
+ }
96
+ if (req.query.limit) {
97
+ res.locals.option['limit'] = Number(req.query.limit);
98
+ }
99
+ if (req.query.offset) {
100
+ res.locals.option['offset'] = Number(req.query.offset);
101
+ }
102
+ if (req.query.currency) {
103
+ res.locals.option['currency'] = req.query.currency;
104
+ }
105
+ return (0, exports.getAuthParam)(req, res, () => {
106
+ if (req.query.order) {
107
+ let order = String(req.query.order).split(',') || [];
108
+ if (order.length > 1 && order[1] == 'desc' || order.length > 1 && order[1] == 'asc') {
109
+ res.locals.option['order'] = order;
110
+ }
111
+ }
112
+ if (req.query.price) {
113
+ let price = _.sortBy(String(req.query.price).split(',').map(Number) || []);
114
+ res.locals.option['price'] = price;
115
+ }
116
+ if (req.query.category) {
117
+ res.locals.option['category'] = String(req.query.category).split(',');
118
+ }
119
+ if (req.query.lang_code) {
120
+ res.locals.option['lang_code'] = req.query.lang_code;
121
+ }
122
+ if (req.query.no_image && req.query.no_image == "1") {
123
+ res.locals.option['no_image'] = true;
124
+ }
125
+ return next();
126
+ });
127
+ };
128
+ exports.getQueryParam = getQueryParam;
129
+ const getQueryParamCUD = (req, res, next) => {
130
+ res.locals.option = res.locals.option || {};
131
+ if (req.query.include) {
132
+ try {
133
+ let array = String(req.query.include).split(',');
134
+ res.locals.include = _.zipObject(array, array);
135
+ }
136
+ catch (err) {
137
+ res.locals.include = {};
138
+ }
139
+ }
140
+ if (req.query.type) {
141
+ res.locals.option['type'] = req.query.type;
142
+ }
143
+ if (req.query.records) {
144
+ res.locals.option['records'] = String(req.query.records).split(',');
145
+ }
146
+ if (req.query.ids) {
147
+ res.locals.option['ids'] = String(req.query.ids).split(',').slice(0, 25);
148
+ }
149
+ return (0, exports.getAuthParam)(req, res, () => {
150
+ if (req.query.lang_code) {
151
+ res.locals.option['lang_code'] = req.query.lang_code;
152
+ }
153
+ return next();
154
+ });
155
+ };
156
+ exports.getQueryParamCUD = getQueryParamCUD;
157
+ const getQueryParamFile = (req, res, next) => {
158
+ return (0, exports.getQueryParam)(req, res, () => {
159
+ let addition = {};
160
+ if (req.query.type) {
161
+ res.locals.option['type'] = req.query.type;
162
+ }
163
+ if (req.query.file_tag_slug) {
164
+ addition['slug'] = String(req.query.file_tag_slug).split(',');
165
+ }
166
+ if (req.query.file_tag_type) {
167
+ addition['type'] = String(req.query.file_tag_type).split(',');
168
+ }
169
+ if (req.query.group) {
170
+ res.locals.option['group'] = req.query.group;
171
+ }
172
+ if (addition['slug'] || addition['type']) {
173
+ let key = addition['slug'] ? 'slug' : 'type';
174
+ let opposite = (key == 'slug') ? 'type' : 'slug';
175
+ res.locals.option['file_tag'] = addition[key].reduce((total, item, index) => {
176
+ let include = {};
177
+ if (addition[opposite] && addition[opposite][index]) {
178
+ include[opposite] = addition[opposite][index];
179
+ }
180
+ total.push(Object.assign({ [key]: item }, include));
181
+ return total;
182
+ }, []);
183
+ }
184
+ return next();
185
+ });
186
+ };
187
+ exports.getQueryParamFile = getQueryParamFile;
188
+ const changeQueryParam = (value = {}, { skipSystem = false } = {}) => (req, res, next) => {
189
+ if (skipSystem && req.params.web_system_token) {
190
+ let bol = (typeof req.params.web != "undefined") || (req.params.web_system == req.params.web);
191
+ if (bol)
192
+ return next();
193
+ }
194
+ req.query = Object.assign(Object.assign({}, req.query || {}), value);
195
+ return next();
196
+ };
197
+ exports.changeQueryParam = changeQueryParam;
198
+ const changeOptionParam = (value = {}, { skipSystem = false } = {}) => (req, res, next) => {
199
+ if (skipSystem && req.params.web_system_token) {
200
+ let bol = (typeof req.params.web != "undefined") || (req.params.web_system == req.params.web);
201
+ if (bol)
202
+ return next();
203
+ }
204
+ res.locals.option = Object.assign(Object.assign({}, res.locals.option || {}), value);
205
+ return next();
206
+ };
207
+ exports.changeOptionParam = changeOptionParam;
208
+ const changeBodyParam = (value = {}, { skipSystem = false, omit = [] } = {}) => (req, res, next) => {
209
+ if (skipSystem && req.params.web_system_token) {
210
+ let bol = (typeof req.params.web != "undefined") || (req.params.web_system == req.params.web);
211
+ if (bol)
212
+ return next();
213
+ }
214
+ req.body = Object.assign(Object.assign({}, req.body || {}), value);
215
+ if (omit.length) {
216
+ req.body = _.omit(req.body, omit);
217
+ }
218
+ return next();
219
+ };
220
+ exports.changeBodyParam = changeBodyParam;
@@ -0,0 +1,2 @@
1
+ export declare function existTable(model: any): (req: any, res: any, next: any) => Promise<any>;
2
+ export declare const ref: (req: any, res: any, next: any) => Promise<any>;
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ref = exports.existTable = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const jwt = require("jsonwebtoken");
6
+ const helper_1 = require("../utils/helper");
7
+ const errors_1 = require("../errors");
8
+ const authError_1 = require("../errors/authError");
9
+ const db_1 = require("../utils/db");
10
+ const site_token_1 = require("../models/system/site_token");
11
+ function existTable(model) {
12
+ return function (req, res, next) {
13
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
14
+ let table = model.getTableName();
15
+ if (!(yield (0, db_1.getExistTable)(res.locals.ref + "_" + table))) {
16
+ let err = new errors_1.CustomError(errors_1.generalError.NOT_FOUND);
17
+ return res.status(err.code).send(err);
18
+ }
19
+ next();
20
+ });
21
+ };
22
+ }
23
+ exports.existTable = existTable;
24
+ const ref = (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
25
+ try {
26
+ const token = req.headers['x-client-site'];
27
+ if (token) {
28
+ let data = jwt.verify(token, (0, helper_1.getENV)('REF_SECRET'));
29
+ if (!data['ref'] || !data['type'] || !data['web_id'])
30
+ throw new errors_1.CustomError(errors_1.generalError.NOT_FOUND);
31
+ if (data['type'] == "ref") {
32
+ req.params.web_system_token = true;
33
+ req.params.web_system = data['web_id'];
34
+ }
35
+ else if (data['type'] == "site") {
36
+ req.params.web_token = true;
37
+ req.params.web = data['web_id'];
38
+ }
39
+ else {
40
+ throw new errors_1.CustomError(errors_1.generalError.NOT_FOUND);
41
+ }
42
+ // check renew
43
+ if (data['url']) {
44
+ let exist = yield site_token_1.SystemSiteTokenModel.schema(data.ref, "_").count({
45
+ where: { token, type: "renew", entity_value: data['url'] }
46
+ });
47
+ if (!exist)
48
+ throw new errors_1.CustomError(errors_1.generalError.NOT_FOUND);
49
+ }
50
+ res.locals.ref = data.ref;
51
+ return next();
52
+ }
53
+ }
54
+ catch (err) { }
55
+ let err = new errors_1.CustomError(authError_1.authError.TOKEN_SITE_IS_WRONG);
56
+ return res.status(err.code).send(err);
57
+ });
58
+ exports.ref = ref;
@@ -0,0 +1,2 @@
1
+ import { Request, Response } from 'express';
2
+ export declare const verifyRefreshToken: (req: Request, res: Response, next: any) => Promise<any>;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.verifyRefreshToken = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const refresh_token_1 = require("../models/user/refresh_token");
6
+ const helper_1 = require("../utils/helper");
7
+ const authError_1 = require("../errors/authError");
8
+ const errors_1 = require("../errors");
9
+ const auth_1 = require("../utils/auth");
10
+ const verifyRefreshToken = (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
11
+ try {
12
+ if (req.headers['authorization']) {
13
+ const type = req.headers['authorization'].split(' ')[0];
14
+ const token = req.headers['authorization'].split(' ')[1];
15
+ const ref = res.locals.ref;
16
+ if (type.toLocaleUpperCase() != "BEARER") {
17
+ throw new errors_1.CustomError(authError_1.authError.TOKEN_IS_INVALID);
18
+ }
19
+ const data = yield (0, helper_1.getTokenDataFromTokenBySecret)(token, (0, helper_1.getENV)('JWT_SECRET'));
20
+ const tokenData = yield refresh_token_1.RefreshTokenModel.schema(ref, "_").findOne({
21
+ where: {
22
+ token: token,
23
+ user_id: data.id
24
+ },
25
+ raw: true,
26
+ attributes: ['expired_at']
27
+ });
28
+ if (!tokenData) {
29
+ throw new errors_1.CustomError(authError_1.authError.TOKEN_IS_INVALID);
30
+ }
31
+ const now = (0, helper_1.getDateTime)();
32
+ const then = (0, helper_1.getDateTime)(tokenData['expired_at']);
33
+ if (then > now) {
34
+ return next();
35
+ }
36
+ else {
37
+ yield new auth_1.AuthUtil(ref).revokeTokenByToken(token);
38
+ throw new errors_1.CustomError(authError_1.authError.TOKEN_EXPIRED);
39
+ }
40
+ }
41
+ throw new errors_1.CustomError(authError_1.authError.TOKEN_IS_INVALID);
42
+ }
43
+ catch (e) {
44
+ if (e.code > 505 || !e.code) {
45
+ return res.status(401 /* HTTP.UNAUTHORIZED */).send({
46
+ code: 401 /* HTTP.UNAUTHORIZED */,
47
+ json: { code: authError_1.authError.TOKEN_IS_INVALID.message }
48
+ });
49
+ }
50
+ else {
51
+ let err = new errors_1.CustomError(e);
52
+ return res.status(401 /* HTTP.UNAUTHORIZED */).send(err);
53
+ }
54
+ }
55
+ });
56
+ exports.verifyRefreshToken = verifyRefreshToken;
@@ -0,0 +1,6 @@
1
+ import { Request, Response } from 'express';
2
+ export declare const site: (req: Request, res: Response, next: any) => Promise<any>;
3
+ export declare const organByWeb: (req: Request, res: Response, next: any) => Promise<any>;
4
+ export declare const verifyVendor: (req: Request, res: Response, next: any) => Promise<any>;
5
+ export declare const verifyAccess: (req: Request, res: Response, next: any) => Promise<any>;
6
+ export declare const verifySubOrgan: (req: Request, res: Response, next: any) => Promise<any>;