blixify-server 0.3.28 → 1.0.0
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/dist/apis/authWrapper.d.ts.map +1 -1
- package/dist/apis/authWrapper.js +1 -228
- package/dist/apis/crypto.d.ts.map +1 -1
- package/dist/apis/crypto.js +1 -63
- package/dist/apis/dayjs.d.ts +3 -0
- package/dist/apis/dayjs.d.ts.map +1 -0
- package/dist/apis/dayjs.js +1 -0
- package/dist/apis/fbWrapper.d.ts +6 -6
- package/dist/apis/fbWrapper.d.ts.map +1 -1
- package/dist/apis/fbWrapper.js +1 -839
- package/dist/apis/googleAnalyticsWrapper.d.ts.map +1 -1
- package/dist/apis/googleAnalyticsWrapper.js +1 -223
- package/dist/apis/index.js +1 -21
- package/dist/apis/mondayWrapper.d.ts +2 -2
- package/dist/apis/mondayWrapper.d.ts.map +1 -1
- package/dist/apis/mondayWrapper.js +32 -527
- package/dist/apis/mongoWrapper.d.ts +6 -6
- package/dist/apis/mongoWrapper.d.ts.map +1 -1
- package/dist/apis/mongoWrapper.js +1 -1524
- package/dist/apis/postgresqlWrapper.d.ts +3 -3
- package/dist/apis/postgresqlWrapper.d.ts.map +1 -1
- package/dist/apis/postgresqlWrapper.js +1 -514
- package/dist/apis/security.d.ts.map +1 -1
- package/dist/apis/security.js +1 -80
- package/dist/apis/trackVisionWrapper.d.ts.map +1 -1
- package/dist/apis/trackVisionWrapper.js +1 -150
- package/dist/apis/uploadWrapper.d.ts.map +1 -1
- package/dist/apis/uploadWrapper.js +1 -481
- package/dist/apis/utils.d.ts +1 -1
- package/dist/apis/utils.d.ts.map +1 -1
- package/dist/apis/utils.js +1 -25
- package/dist/buildtsconfig.tsbuildinfo +1 -0
- package/dist/model/Log.js +1 -2
- package/dist/model/QueryModel.d.ts.map +1 -1
- package/dist/model/QueryModel.js +1 -118
- package/dist/model/SecurityConfig.js +1 -2
- package/package.json +21 -12
|
@@ -17,16 +17,16 @@ export declare class PostgreSQLWrapper {
|
|
|
17
17
|
modelChecker: (obj: any) => boolean;
|
|
18
18
|
constructor(pgDB: any, collection: string, isProd: boolean, config: SecurityConfig, modelChecker: (obj: any, ignore?: boolean) => boolean, lib: WrapperLib, debug?: (reqBody: any, curBody: any, errMsg: any) => void);
|
|
19
19
|
buildUpdateQuery(pgData: any): string;
|
|
20
|
-
initCreate: (req: any, res: any, workflow?: (
|
|
20
|
+
initCreate: (req: any, res: any, workflow?: (data: any) => Promise<any>) => Promise<{
|
|
21
21
|
success: boolean;
|
|
22
22
|
} | undefined>;
|
|
23
23
|
initGet: (req: any, res: any) => Promise<{
|
|
24
24
|
data: null;
|
|
25
25
|
} | undefined>;
|
|
26
|
-
initUpdate: (req: any, res: any, workflow?: (
|
|
26
|
+
initUpdate: (req: any, res: any, workflow?: (data: any) => Promise<any>) => Promise<{
|
|
27
27
|
success: boolean;
|
|
28
28
|
} | undefined>;
|
|
29
|
-
initDelete: (req: any, res: any, workflow?: (
|
|
29
|
+
initDelete: (req: any, res: any, workflow?: (data: any) => Promise<any>) => Promise<{
|
|
30
30
|
success: boolean;
|
|
31
31
|
} | undefined>;
|
|
32
32
|
initList: (req: any, res: any) => Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"postgresqlWrapper.d.ts","sourceRoot":"","sources":["../../src/apis/postgresqlWrapper.ts"],"names":[],"mappings":"AAQA,OAAO,cAAc,MAAM,yBAAyB,CAAC;AAGrD,MAAM,WAAW,UAAU;IACzB,OAAO,CAAC,EAAE,GAAG,CAAC;CACf;AAED;;;;GAIG;AACH,qBAAa,iBAAiB;IAC5B,IAAI,EAAE,GAAG,CAAM;IACf,UAAU,SAAM;IAChB,MAAM,UAAS;IACf,MAAM,EAAE,cAAc,CAQpB;IACF,GAAG,EAAE,UAAU,CAAC;IAChB,KAAK,EAAE,GAAG,CAAC;IAEX,YAAY,
|
|
1
|
+
{"version":3,"file":"postgresqlWrapper.d.ts","sourceRoot":"","sources":["../../src/apis/postgresqlWrapper.ts"],"names":[],"mappings":"AAQA,OAAO,cAAc,MAAM,yBAAyB,CAAC;AAGrD,MAAM,WAAW,UAAU;IACzB,OAAO,CAAC,EAAE,GAAG,CAAC;CACf;AAED;;;;GAIG;AACH,qBAAa,iBAAiB;IAC5B,IAAI,EAAE,GAAG,CAAM;IACf,UAAU,SAAM;IAChB,MAAM,UAAS;IACf,MAAM,EAAE,cAAc,CAQpB;IACF,GAAG,EAAE,UAAU,CAAC;IAChB,KAAK,EAAE,GAAG,CAAC;IAEX,YAAY,GAAI,KAAK,GAAG,aAEtB;gBAGA,IAAI,EAAE,GAAG,EACT,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,OAAO,EACf,MAAM,EAAE,cAAc,EACtB,YAAY,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,OAAO,KAAK,OAAO,EACrD,GAAG,EAAE,UAAU,EACf,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,KAAK,IAAI;IAqB3D,gBAAgB,CAAC,MAAM,EAAE,GAAG;IAM5B,UAAU,GACR,KAAK,GAAG,EACR,KAAK,GAAG,EACR,WAAW,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC,GAAG,CAAC;;mBAuDtC;IAEF,OAAO,GAAU,KAAK,GAAG,EAAE,KAAK,GAAG;;mBAiCjC;IAEF,UAAU,GACR,KAAK,GAAG,EACR,KAAK,GAAG,EACR,WAAW,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC,GAAG,CAAC;;mBAgFtC;IAEF,UAAU,GACR,KAAK,GAAG,EACR,KAAK,GAAG,EACR,WAAW,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC,GAAG,CAAC;;mBAmCtC;IAEF,QAAQ,GAAU,KAAK,GAAG,EAAE,KAAK,GAAG,mBAgPlC;IAEF,IAAI,YA+EF;CACH"}
|
|
@@ -1,514 +1 @@
|
|
|
1
|
-
"
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.PostgreSQLWrapper = void 0;
|
|
16
|
-
const moment_timezone_1 = __importDefault(require("moment-timezone"));
|
|
17
|
-
const QueryModel_1 = require("../model/QueryModel");
|
|
18
|
-
const utils_1 = require("./utils");
|
|
19
|
-
/**
|
|
20
|
-
* @Wrapper
|
|
21
|
-
* collection - PostgreSQL Collection
|
|
22
|
-
*
|
|
23
|
-
*/
|
|
24
|
-
class PostgreSQLWrapper {
|
|
25
|
-
constructor(pgDB, collection, isProd, config, modelChecker, lib, debug) {
|
|
26
|
-
this.pgDB = "";
|
|
27
|
-
this.collection = "";
|
|
28
|
-
this.isProd = false;
|
|
29
|
-
this.config = {
|
|
30
|
-
baseConfig: [],
|
|
31
|
-
opsConfig: {
|
|
32
|
-
read: [],
|
|
33
|
-
create: [],
|
|
34
|
-
update: [],
|
|
35
|
-
delete: [],
|
|
36
|
-
},
|
|
37
|
-
};
|
|
38
|
-
// eslint-disable-next-line
|
|
39
|
-
this.modelChecker = (obj) => {
|
|
40
|
-
return false;
|
|
41
|
-
};
|
|
42
|
-
this.initCreate = (req, res, workflow) => __awaiter(this, void 0, void 0, function* () {
|
|
43
|
-
var _a, _b;
|
|
44
|
-
try {
|
|
45
|
-
const valid = this.modelChecker(req.body.data);
|
|
46
|
-
if (valid) {
|
|
47
|
-
//INFO : Security Checker
|
|
48
|
-
const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
|
|
49
|
-
const validOpsConfig = (0, QueryModel_1.checkOpsConfig)(this.config, "create", req, req.body.data);
|
|
50
|
-
if (!validBaseConfig || !validOpsConfig) {
|
|
51
|
-
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
// req.body.data["baseUpdatedAt"] = moment().toDate();
|
|
55
|
-
if (req.body.unique && ((_a = req.body.data) === null || _a === void 0 ? void 0 : _a[req.body.unique])) {
|
|
56
|
-
const uniqueField = req.body.unique;
|
|
57
|
-
const uniqueValue = req.body.data[uniqueField];
|
|
58
|
-
const { rowCount } = yield this.pgDB.query(`SELECT * FROM ${this.collection} WHERE ${uniqueField} = $1 LIMIT 1`, [uniqueValue]);
|
|
59
|
-
if (rowCount > 0) {
|
|
60
|
-
res
|
|
61
|
-
.status(400)
|
|
62
|
-
.json({ err: `Unique:${req.body.data[req.body.unique]} exists` });
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
if (workflow)
|
|
67
|
-
yield workflow(req.body.data);
|
|
68
|
-
const columns = Object.keys(req.body.data).join(", ");
|
|
69
|
-
const values = Object.values(req.body.data);
|
|
70
|
-
const placeholders = values
|
|
71
|
-
.map((_, index) => `$${index + 1}`)
|
|
72
|
-
.join(", ");
|
|
73
|
-
yield this.pgDB.query(`INSERT INTO ${this.collection} (${columns}) VALUES (${placeholders})`, values);
|
|
74
|
-
const resBody = { success: true };
|
|
75
|
-
if ((_b = req.body) === null || _b === void 0 ? void 0 : _b.stopRes)
|
|
76
|
-
return resBody;
|
|
77
|
-
res.send(resBody);
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
res.status(400).json({ err: "Invalid Model Structure" });
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
catch (err) {
|
|
84
|
-
res.status(400).json({ err: err });
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
this.initGet = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
88
|
-
var _c;
|
|
89
|
-
try {
|
|
90
|
-
const valid = req.body.id;
|
|
91
|
-
if (valid) {
|
|
92
|
-
//INFO : Security Checker
|
|
93
|
-
const pgResult = yield this.pgDB.query(`SELECT * FROM ${this.collection} WHERE id = $1 LIMIT 1`, [valid]);
|
|
94
|
-
const resBody = { data: null };
|
|
95
|
-
if (pgResult.rowCount > 0) {
|
|
96
|
-
const pgData = pgResult.rows[0];
|
|
97
|
-
const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
|
|
98
|
-
const validOpsConfig = (0, QueryModel_1.checkOpsConfig)(this.config, "read", req, pgData);
|
|
99
|
-
if (!validBaseConfig || !validOpsConfig) {
|
|
100
|
-
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
resBody["data"] = pgData;
|
|
104
|
-
}
|
|
105
|
-
if ((_c = req.body) === null || _c === void 0 ? void 0 : _c.stopRes)
|
|
106
|
-
return resBody;
|
|
107
|
-
res.send(resBody);
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
res.status(400).json({ err: "Invalid Fields" });
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
catch (err) {
|
|
114
|
-
res.status(400).json({ err: err });
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
this.initUpdate = (req, res, workflow) => __awaiter(this, void 0, void 0, function* () {
|
|
118
|
-
var _d, _e, _f;
|
|
119
|
-
try {
|
|
120
|
-
const valid = this.modelChecker(req.body.data) && req.body.id;
|
|
121
|
-
if (valid) {
|
|
122
|
-
const pgResult = yield this.pgDB.query(`SELECT * FROM ${this.collection} WHERE id = $1`, [valid]);
|
|
123
|
-
if (pgResult.rowCount > 0) {
|
|
124
|
-
const pgData = pgResult.rows[0];
|
|
125
|
-
if (this.debug)
|
|
126
|
-
this.debug(req.body, pgData);
|
|
127
|
-
const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
|
|
128
|
-
const validOpsConfig = (0, QueryModel_1.checkOpsConfig)(this.config, "update", req, pgData);
|
|
129
|
-
if (!validBaseConfig || !validOpsConfig) {
|
|
130
|
-
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
if (req.body.unique && ((_d = req.body.data) === null || _d === void 0 ? void 0 : _d[req.body.unique])) {
|
|
134
|
-
const uniqueField = req.body.unique;
|
|
135
|
-
const uniqueValue = req.body.data[uniqueField];
|
|
136
|
-
const { rowCount } = yield this.pgDB.query(`SELECT * FROM ${this.collection} WHERE ${uniqueField} = $1 LIMIT 1`, [uniqueValue]);
|
|
137
|
-
if (rowCount > 0) {
|
|
138
|
-
res.status(400).json({
|
|
139
|
-
err: `Unique:${req.body.data[req.body.unique]} exists`,
|
|
140
|
-
});
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
const { updatedFields } = (0, utils_1.compareUpdatedFields)(pgData, req.body.data);
|
|
145
|
-
const resBody = { success: true };
|
|
146
|
-
if (workflow)
|
|
147
|
-
yield workflow(updatedFields);
|
|
148
|
-
// const data = this.parseModel(updatedFields);
|
|
149
|
-
//INFO : Security Checker
|
|
150
|
-
const updateQuery = this.buildUpdateQuery(updatedFields);
|
|
151
|
-
const sqlQuery = `UPDATE ${this.collection} SET ${updateQuery} WHERE id = $${Object.keys(updatedFields).length + 1}`;
|
|
152
|
-
const values = [...Object.values(updatedFields), valid];
|
|
153
|
-
if (req.body.sensitive) {
|
|
154
|
-
if ((0, moment_timezone_1.default)(req.body.data.baseUpdatedAt).isAfter((0, moment_timezone_1.default)(pgData.baseUpdatedAt))) {
|
|
155
|
-
yield this.pgDB.query(sqlQuery, values);
|
|
156
|
-
if ((_e = req.body) === null || _e === void 0 ? void 0 : _e.stopRes)
|
|
157
|
-
return resBody;
|
|
158
|
-
res.send(resBody);
|
|
159
|
-
}
|
|
160
|
-
else {
|
|
161
|
-
res.status(400).json({ err: "Refresh Sensitive Model" });
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
else {
|
|
165
|
-
yield this.pgDB.query(sqlQuery, values);
|
|
166
|
-
if ((_f = req.body) === null || _f === void 0 ? void 0 : _f.stopRes)
|
|
167
|
-
return resBody;
|
|
168
|
-
res.send(resBody);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
else
|
|
172
|
-
throw "No Data Found";
|
|
173
|
-
}
|
|
174
|
-
else {
|
|
175
|
-
throw "Invalid Model Structure";
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
catch (err) {
|
|
179
|
-
if (this.debug)
|
|
180
|
-
this.debug(null, null, err);
|
|
181
|
-
res.status(400).json({ err: err });
|
|
182
|
-
}
|
|
183
|
-
});
|
|
184
|
-
this.initDelete = (req, res, workflow) => __awaiter(this, void 0, void 0, function* () {
|
|
185
|
-
var _g;
|
|
186
|
-
try {
|
|
187
|
-
const valid = req.body.id;
|
|
188
|
-
if (valid) {
|
|
189
|
-
const pgResult = yield this.pgDB.query(`SELECT * FROM ${this.collection} WHERE id = $1 LIMIT 1`, [valid]);
|
|
190
|
-
if (pgResult.rowCount > 0) {
|
|
191
|
-
const pgData = pgResult.rows[0];
|
|
192
|
-
const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
|
|
193
|
-
const validOpsConfig = (0, QueryModel_1.checkOpsConfig)(this.config, "read", req, pgData);
|
|
194
|
-
if (!validBaseConfig || !validOpsConfig) {
|
|
195
|
-
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
|
-
if (workflow)
|
|
199
|
-
yield workflow(pgData);
|
|
200
|
-
yield this.pgDB.query(`DELETE FROM ${this.collection} WHERE id = $1`, [valid]);
|
|
201
|
-
const resBody = { success: true };
|
|
202
|
-
if ((_g = req.body) === null || _g === void 0 ? void 0 : _g.stopRes)
|
|
203
|
-
return resBody;
|
|
204
|
-
res.send(resBody);
|
|
205
|
-
}
|
|
206
|
-
else
|
|
207
|
-
throw "No Data Found";
|
|
208
|
-
}
|
|
209
|
-
else
|
|
210
|
-
throw "Invalid Fields";
|
|
211
|
-
}
|
|
212
|
-
catch (err) {
|
|
213
|
-
res.status(400).json({ err: err });
|
|
214
|
-
}
|
|
215
|
-
});
|
|
216
|
-
this.initList = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
217
|
-
var _h, _j, _k, _l, _m;
|
|
218
|
-
try {
|
|
219
|
-
//INFO : Security Checker
|
|
220
|
-
const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
|
|
221
|
-
if (validBaseConfig) {
|
|
222
|
-
const aggregate = req.body.aggregate;
|
|
223
|
-
const queryList = (_h = req.body.query) !== null && _h !== void 0 ? _h : [];
|
|
224
|
-
if (aggregate) {
|
|
225
|
-
const queryId = (_j = aggregate.queryId) !== null && _j !== void 0 ? _j : "";
|
|
226
|
-
const dateId = (_k = aggregate.dateId) !== null && _k !== void 0 ? _k : "baseUpdatedAt";
|
|
227
|
-
const dateRanges = (_l = aggregate.range) !== null && _l !== void 0 ? _l : [];
|
|
228
|
-
const dataList = [];
|
|
229
|
-
for (const dateRange of dateRanges) {
|
|
230
|
-
//INFO : Step 1 - Query
|
|
231
|
-
let query = `SELECT * FROM ${this.collection}`;
|
|
232
|
-
const whereClauses = [];
|
|
233
|
-
if (queryList && queryList.length > 0) {
|
|
234
|
-
queryList.map((eachQuery) => {
|
|
235
|
-
var _a;
|
|
236
|
-
const queryId = (_a = eachQuery.queryId) !== null && _a !== void 0 ? _a : "";
|
|
237
|
-
const value = eachQuery.value;
|
|
238
|
-
switch (eachQuery.type) {
|
|
239
|
-
case "=":
|
|
240
|
-
whereClauses.push(`${queryId} = ${value}`);
|
|
241
|
-
break;
|
|
242
|
-
case "!=":
|
|
243
|
-
whereClauses.push(`${queryId} != ${value}`);
|
|
244
|
-
break;
|
|
245
|
-
case ">":
|
|
246
|
-
whereClauses.push(`${queryId} > ${value}`);
|
|
247
|
-
break;
|
|
248
|
-
case "<":
|
|
249
|
-
whereClauses.push(`${queryId} < ${value}`);
|
|
250
|
-
break;
|
|
251
|
-
case "><":
|
|
252
|
-
if (Array.isArray(value) && value.length >= 2) {
|
|
253
|
-
whereClauses.push(`${queryId} BETWEEN ${value[0]} AND ${value[1]}`);
|
|
254
|
-
}
|
|
255
|
-
break;
|
|
256
|
-
case "in":
|
|
257
|
-
whereClauses.push(`${queryId} IN (${value
|
|
258
|
-
.map((eachValue) => `'${eachValue}'`)
|
|
259
|
-
.join(", ")})`);
|
|
260
|
-
break;
|
|
261
|
-
default:
|
|
262
|
-
break;
|
|
263
|
-
}
|
|
264
|
-
});
|
|
265
|
-
}
|
|
266
|
-
const date = dateRange.split("-");
|
|
267
|
-
const [startDate, endDate = (0, moment_timezone_1.default)().format("DD/MM/YYYY HH:mm:ss"),] = date;
|
|
268
|
-
const isDuration = date.length === 2;
|
|
269
|
-
const isValidStartDate = (0, moment_timezone_1.default)(startDate, "DD/MM/YYYY").isValid();
|
|
270
|
-
const isValidEndDate = (0, moment_timezone_1.default)(endDate, "DD/MM/YYYY").isValid();
|
|
271
|
-
const startMoment = (0, moment_timezone_1.default)(startDate, "DD/MM/YYYY HH:mm:ss").tz("Asia/Kuala_lumpur");
|
|
272
|
-
const endMoment = (0, moment_timezone_1.default)(endDate, "DD/MM/YYYY HH:mm:ss").tz("Asia/Kuala_lumpur");
|
|
273
|
-
const isoStartDate = startMoment.toDate();
|
|
274
|
-
const queryStartDate = startMoment.toISOString();
|
|
275
|
-
const queryEndDate = endMoment.toISOString();
|
|
276
|
-
if (!isValidStartDate || !isValidEndDate) {
|
|
277
|
-
res
|
|
278
|
-
.status(400)
|
|
279
|
-
.json({ err: "Invalid Aggregate Range Configuration" });
|
|
280
|
-
return;
|
|
281
|
-
}
|
|
282
|
-
const id = isDuration
|
|
283
|
-
? dateRange
|
|
284
|
-
: `Begining until ${(0, moment_timezone_1.default)(isoStartDate).format("DD/MM/YYYY")}`;
|
|
285
|
-
if (isDuration) {
|
|
286
|
-
whereClauses.push(`${dateId} BETWEEN '${queryStartDate}' AND '${queryEndDate}'`);
|
|
287
|
-
}
|
|
288
|
-
else {
|
|
289
|
-
whereClauses.push(`${dateId} < '${queryStartDate}'`);
|
|
290
|
-
}
|
|
291
|
-
const allWhereClauses = `${whereClauses.length > 0
|
|
292
|
-
? "WHERE " + whereClauses.join(" AND ")
|
|
293
|
-
: ""}`;
|
|
294
|
-
let value = 0;
|
|
295
|
-
switch (aggregate.type) {
|
|
296
|
-
case "count":
|
|
297
|
-
query = `SELECT COUNT(*) as count FROM ${this.collection} ${allWhereClauses}`;
|
|
298
|
-
const countResult = yield this.pgDB.query(query);
|
|
299
|
-
value = parseInt(countResult.rows[0].count, 10);
|
|
300
|
-
break;
|
|
301
|
-
case "sum":
|
|
302
|
-
query = `SELECT SUM(${queryId}) as sum FROM ${this.collection} ${allWhereClauses}`;
|
|
303
|
-
const sumResult = yield this.pgDB.query(query);
|
|
304
|
-
value = parseFloat(sumResult.rows[0].sum);
|
|
305
|
-
break;
|
|
306
|
-
case "avg":
|
|
307
|
-
query = `SELECT AVG(${queryId}) as avg FROM ${this.collection} ${allWhereClauses}`;
|
|
308
|
-
const avgResult = yield this.pgDB.query(query);
|
|
309
|
-
value = parseFloat(avgResult.rows[0].avg);
|
|
310
|
-
break;
|
|
311
|
-
case "value":
|
|
312
|
-
query = `SELECT * FROM ${this.collection} ${allWhereClauses} ORDER BY ${dateId} DESC LIMIT 1`;
|
|
313
|
-
const valueResult = yield this.pgDB.query(query);
|
|
314
|
-
if (valueResult.rows.length > 0)
|
|
315
|
-
value = valueResult.rows[0][queryId];
|
|
316
|
-
break;
|
|
317
|
-
default:
|
|
318
|
-
break;
|
|
319
|
-
}
|
|
320
|
-
dataList.push({
|
|
321
|
-
_id: id,
|
|
322
|
-
value: value,
|
|
323
|
-
});
|
|
324
|
-
}
|
|
325
|
-
res.send({
|
|
326
|
-
data: dataList,
|
|
327
|
-
});
|
|
328
|
-
}
|
|
329
|
-
else {
|
|
330
|
-
//INFO : Step 1 - Query
|
|
331
|
-
let query = `SELECT * FROM ${this.collection}`;
|
|
332
|
-
const whereClauses = [];
|
|
333
|
-
if (queryList && queryList.length > 0) {
|
|
334
|
-
queryList.map((eachQuery) => {
|
|
335
|
-
var _a;
|
|
336
|
-
const queryId = (_a = eachQuery.queryId) !== null && _a !== void 0 ? _a : "";
|
|
337
|
-
const value = eachQuery.value;
|
|
338
|
-
switch (eachQuery.type) {
|
|
339
|
-
case "search":
|
|
340
|
-
if (eachQuery.searchIds && eachQuery.searchIds.length > 0) {
|
|
341
|
-
whereClauses.push(`${eachQuery.searchIds[0]} ILIKE '${value}%'`);
|
|
342
|
-
}
|
|
343
|
-
break;
|
|
344
|
-
case "=":
|
|
345
|
-
whereClauses.push(`${queryId} = '${value}'`);
|
|
346
|
-
break;
|
|
347
|
-
case "!=":
|
|
348
|
-
whereClauses.push(`${queryId} != '${value}'`);
|
|
349
|
-
break;
|
|
350
|
-
case ">":
|
|
351
|
-
whereClauses.push(`${queryId} > '${value}'`);
|
|
352
|
-
break;
|
|
353
|
-
case "<":
|
|
354
|
-
whereClauses.push(`${queryId} < '${value}'`);
|
|
355
|
-
break;
|
|
356
|
-
case "><":
|
|
357
|
-
if (Array.isArray(value) && value.length >= 2) {
|
|
358
|
-
whereClauses.push(`${queryId} BETWEEN '${value[0]}' AND '${value[1]}'`);
|
|
359
|
-
}
|
|
360
|
-
break;
|
|
361
|
-
case "in":
|
|
362
|
-
whereClauses.push(`${queryId} IN (${value
|
|
363
|
-
.map((eachValue) => `'${eachValue}'`)
|
|
364
|
-
.join(", ")})`);
|
|
365
|
-
break;
|
|
366
|
-
default:
|
|
367
|
-
break;
|
|
368
|
-
}
|
|
369
|
-
});
|
|
370
|
-
}
|
|
371
|
-
const allWhereClauses = `${whereClauses.length > 0 ? "WHERE " + whereClauses.join(" AND ") : ""}`;
|
|
372
|
-
query += ` ${allWhereClauses}`;
|
|
373
|
-
//INFO : Step 2 - Sort
|
|
374
|
-
if (req.body.sort) {
|
|
375
|
-
const sortReq = req.body.sort;
|
|
376
|
-
query += ` ORDER BY ${sortReq.sortId} ${sortReq.type === "asc" ? "ASC" : "DESC"}`;
|
|
377
|
-
}
|
|
378
|
-
//INFO : Step 3 - Pagination
|
|
379
|
-
if (req.body.cursor) {
|
|
380
|
-
query += ` OFFSET ${req.body.cursor}`;
|
|
381
|
-
}
|
|
382
|
-
//INFO : Step 4 - Limit
|
|
383
|
-
if (!req.body.stopLimit) {
|
|
384
|
-
const limit = (_m = req.body.limit) !== null && _m !== void 0 ? _m : 10;
|
|
385
|
-
query += ` LIMIT ${limit}`;
|
|
386
|
-
}
|
|
387
|
-
const { rows, rowCount } = yield this.pgDB.query(query);
|
|
388
|
-
//INFO : Security Checker
|
|
389
|
-
let validOpsConfig = true;
|
|
390
|
-
const dataList = [];
|
|
391
|
-
if (rows) {
|
|
392
|
-
rows.forEach((eachData) => {
|
|
393
|
-
dataList.push(eachData);
|
|
394
|
-
if (!(0, QueryModel_1.checkOpsConfig)(this.config, "read", req, eachData)) {
|
|
395
|
-
validOpsConfig = false;
|
|
396
|
-
}
|
|
397
|
-
});
|
|
398
|
-
}
|
|
399
|
-
if (!validBaseConfig || !validOpsConfig) {
|
|
400
|
-
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
401
|
-
return;
|
|
402
|
-
}
|
|
403
|
-
res.send({
|
|
404
|
-
data: dataList,
|
|
405
|
-
count: rowCount,
|
|
406
|
-
});
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
else {
|
|
410
|
-
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
catch (err) {
|
|
414
|
-
if (err.message)
|
|
415
|
-
res.status(400).json({ err: err.message });
|
|
416
|
-
else
|
|
417
|
-
res.status(400).json({ err: err });
|
|
418
|
-
}
|
|
419
|
-
});
|
|
420
|
-
this.init = () => {
|
|
421
|
-
const router = this.lib.express.Router();
|
|
422
|
-
/**
|
|
423
|
-
* @Input :
|
|
424
|
-
* data - Model
|
|
425
|
-
* unique - string (Optional)
|
|
426
|
-
*/
|
|
427
|
-
router.post("/create", (req, res) => {
|
|
428
|
-
this.initCreate(req, res);
|
|
429
|
-
});
|
|
430
|
-
/**
|
|
431
|
-
* @Input :
|
|
432
|
-
* data - Model
|
|
433
|
-
* unique - string (Optional)
|
|
434
|
-
*/
|
|
435
|
-
// router.post("/batchCreate", (req: any, res: any) => {
|
|
436
|
-
// this.initBatchCreate(req, res);
|
|
437
|
-
// });
|
|
438
|
-
/**
|
|
439
|
-
* @Input :
|
|
440
|
-
* id - string
|
|
441
|
-
*/
|
|
442
|
-
router.post("/get", (req, res) => {
|
|
443
|
-
this.initGet(req, res);
|
|
444
|
-
});
|
|
445
|
-
/**
|
|
446
|
-
* @Input :
|
|
447
|
-
* data - Model
|
|
448
|
-
* id - string
|
|
449
|
-
* unique - string (Optional)
|
|
450
|
-
*/
|
|
451
|
-
router.post("/update", (req, res) => {
|
|
452
|
-
this.initUpdate(req, res);
|
|
453
|
-
});
|
|
454
|
-
/**
|
|
455
|
-
* @Input :
|
|
456
|
-
* data - Model
|
|
457
|
-
* query - Query[]
|
|
458
|
-
* unsetData - string[] (Unset Attribute Id) (Optional)
|
|
459
|
-
* isOr - boolean (Optional)
|
|
460
|
-
*/
|
|
461
|
-
// router.post("/batchUpdate", (req: any, res: any) => {
|
|
462
|
-
// this.initBatchUpdate(req, res);
|
|
463
|
-
// });
|
|
464
|
-
/**
|
|
465
|
-
* @Input :
|
|
466
|
-
* id - string
|
|
467
|
-
*/
|
|
468
|
-
router.post("/delete", (req, res) => {
|
|
469
|
-
this.initDelete(req, res);
|
|
470
|
-
});
|
|
471
|
-
/**
|
|
472
|
-
* @Input :
|
|
473
|
-
* id - string[]
|
|
474
|
-
*/
|
|
475
|
-
// router.post("/batchDelete", (req: any, res: any) => {
|
|
476
|
-
// this.initBatchDelete(req, res);
|
|
477
|
-
// });
|
|
478
|
-
/**
|
|
479
|
-
* @Input
|
|
480
|
-
* query - Query interface (Optional)
|
|
481
|
-
* cursor - any (Optional)
|
|
482
|
-
* limit - number (Optional)
|
|
483
|
-
* sort - Sort interface (Optional)
|
|
484
|
-
* stopLimit - boolean to retreive all data (Optional)
|
|
485
|
-
*/
|
|
486
|
-
router.post("/list", (req, res) => {
|
|
487
|
-
this.initList(req, res);
|
|
488
|
-
});
|
|
489
|
-
return router;
|
|
490
|
-
};
|
|
491
|
-
this.pgDB = pgDB;
|
|
492
|
-
this.collection = collection;
|
|
493
|
-
this.isProd = isProd;
|
|
494
|
-
this.config = config;
|
|
495
|
-
this.modelChecker = modelChecker;
|
|
496
|
-
this.lib = lib;
|
|
497
|
-
this.debug = debug;
|
|
498
|
-
}
|
|
499
|
-
//TODO: Parse Model
|
|
500
|
-
//TODO: Batch Create & Update
|
|
501
|
-
//TODO: Change table schema dynanically
|
|
502
|
-
//TODO: Count algorithm is wrong
|
|
503
|
-
// parseModel = (data: any) => {
|
|
504
|
-
// delete data["_id"];
|
|
505
|
-
// data["baseUpdatedAt"] = moment().toDate();
|
|
506
|
-
// return data;
|
|
507
|
-
// };
|
|
508
|
-
buildUpdateQuery(pgData) {
|
|
509
|
-
return Object.keys(pgData)
|
|
510
|
-
.map((key, index) => `${key} = $${index + 1}`)
|
|
511
|
-
.join(", ");
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
exports.PostgreSQLWrapper = PostgreSQLWrapper;
|
|
1
|
+
var __awaiter=this&&this.__awaiter||function(e,o,n,u){return new(n=n||Promise)(function(i,t){function s(e){try{r(u.next(e))}catch(e){t(e)}}function a(e){try{r(u.throw(e))}catch(e){t(e)}}function r(e){var t;e.done?i(e.value):((t=e.value)instanceof n?t:new n(function(e){e(t)})).then(s,a)}r((u=u.apply(e,o||[])).next())})},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.PostgreSQLWrapper=void 0;let dayjs_1=__importDefault(require("./dayjs")),QueryModel_1=require("../model/QueryModel"),utils_1=require("./utils");class PostgreSQLWrapper{constructor(e,t,i,s,a,r,o){this.pgDB="",this.collection="",this.isProd=!1,this.config={baseConfig:[],opsConfig:{read:[],create:[],update:[],delete:[]}},this.modelChecker=e=>!1,this.initCreate=(c,h,y)=>__awaiter(this,void 0,void 0,function*(){var e,t;try{if(this.modelChecker(c.body.data)){var i=(0,QueryModel_1.checkBaseConfig)(this.config,c),s=(0,QueryModel_1.checkOpsConfig)(this.config,"create",c,c.body.data);if(i&&s){if(c.body.unique&&null!=(e=c.body.data)&&e[c.body.unique]){var a=c.body.unique,r=c.body.data[a],o=(yield this.pgDB.query(`SELECT * FROM ${this.collection} WHERE ${a} = $1 LIMIT 1`,[r])).rowCount;if(0<o)return void h.status(400).json({err:`Unique:${c.body.data[c.body.unique]} exists`})}y&&(yield y(c.body.data));var n=Object.keys(c.body.data).join(", "),u=Object.values(c.body.data),d=u.map((e,t)=>"$"+(t+1)).join(", "),l=(yield this.pgDB.query(`INSERT INTO ${this.collection} (${n}) VALUES (${d})`,u),{success:!0});if(null!=(t=c.body)&&t.stopRes)return l;h.send(l)}else h.status(400).json({err:"Invalid Security Configuration"})}else h.status(400).json({err:"Invalid Model Structure"})}catch(e){h.status(400).json({err:e})}}),this.initGet=(n,u)=>__awaiter(this,void 0,void 0,function*(){var e;try{var t=n.body.id;if(t){var i=yield this.pgDB.query(`SELECT * FROM ${this.collection} WHERE id = $1 LIMIT 1`,[t]),s={data:null};if(0<i.rowCount){var a=i.rows[0],r=(0,QueryModel_1.checkBaseConfig)(this.config,n),o=(0,QueryModel_1.checkOpsConfig)(this.config,"read",n,a);if(!r||!o)return void u.status(400).json({err:"Invalid Security Configuration"});s.data=a}if(null!=(e=n.body)&&e.stopRes)return s;u.send(s)}else u.status(400).json({err:"Invalid Fields"})}catch(e){u.status(400).json({err:e})}}),this.initUpdate=(g,v,b)=>__awaiter(this,void 0,void 0,function*(){var e,t,i;try{var s=this.modelChecker(g.body.data)&&g.body.id;if(!s)throw"Invalid Model Structure";var a=yield this.pgDB.query(`SELECT * FROM ${this.collection} WHERE id = $1`,[s]);if(!(0<a.rowCount))throw"No Data Found";var r=a.rows[0],o=(this.debug&&this.debug(g.body,r),(0,QueryModel_1.checkBaseConfig)(this.config,g)),n=(0,QueryModel_1.checkOpsConfig)(this.config,"update",g,r);if(o&&n){if(g.body.unique&&null!=(e=g.body.data)&&e[g.body.unique]){var u=g.body.unique,d=g.body.data[u],l=(yield this.pgDB.query(`SELECT * FROM ${this.collection} WHERE ${u} = $1 LIMIT 1`,[d])).rowCount;if(0<l)return void v.status(400).json({err:`Unique:${g.body.data[g.body.unique]} exists`})}var c=(0,utils_1.compareUpdatedFields)(r,g.body.data).updatedFields,h={success:!0},y=(b&&(yield b(c)),this.buildUpdateQuery(c)),p=`UPDATE ${this.collection} SET ${y} WHERE id = $`+(Object.keys(c).length+1),f=[...Object.values(c),s];if(g.body.sensitive)if((0,dayjs_1.default)(g.body.data.baseUpdatedAt).isAfter((0,dayjs_1.default)(r.baseUpdatedAt))){if(yield this.pgDB.query(p,f),null!=(t=g.body)&&t.stopRes)return h;v.send(h)}else v.status(400).json({err:"Refresh Sensitive Model"});else{if(yield this.pgDB.query(p,f),null!=(i=g.body)&&i.stopRes)return h;v.send(h)}}else v.status(400).json({err:"Invalid Security Configuration"})}catch(e){this.debug&&this.debug(null,null,e),v.status(400).json({err:e})}}),this.initDelete=(n,u,d)=>__awaiter(this,void 0,void 0,function*(){var e;try{var t=n.body.id;if(!t)throw"Invalid Fields";var i=yield this.pgDB.query(`SELECT * FROM ${this.collection} WHERE id = $1 LIMIT 1`,[t]);if(!(0<i.rowCount))throw"No Data Found";var s=i.rows[0],a=(0,QueryModel_1.checkBaseConfig)(this.config,n),r=(0,QueryModel_1.checkOpsConfig)(this.config,"read",n,s);if(a&&r){d&&(yield d(s)),yield this.pgDB.query(`DELETE FROM ${this.collection} WHERE id = $1`,[t]);var o={success:!0};if(null!=(e=n.body)&&e.stopRes)return o;u.send(o)}else u.status(400).json({err:"Invalid Security Configuration"})}catch(e){u.status(400).json({err:e})}}),this.initList=(L,T)=>__awaiter(this,void 0,void 0,function*(){var e,t,i,s,r;try{var o=(0,QueryModel_1.checkBaseConfig)(this.config,L);if(o){var n=L.body.aggregate,u=null!=(e=L.body.query)?e:[];if(n){var d,l=null!=(t=n.queryId)?t:"",c=null!=(i=n.dateId)?i:"baseUpdatedAt",a=null!=(s=n.range)?s:[],h=[];for(d of a){let e="SELECT * FROM "+this.collection,a=[];u&&0<u.length&&u.map(e=>{var t,i=null!=(t=e.queryId)?t:"",s=e.value;switch(e.type){case"=":a.push(i+" = "+s);break;case"!=":a.push(i+" != "+s);break;case">":a.push(i+" > "+s);break;case"<":a.push(i+" < "+s);break;case"><":Array.isArray(s)&&2<=s.length&&a.push(`${i} BETWEEN ${s[0]} AND `+s[1]);break;case"in":a.push(`${i} IN (${s.map(e=>`'${e}'`).join(", ")})`)}});var y=d.split("-"),[p,f=(0,dayjs_1.default)().format("DD/MM/YYYY HH:mm:ss")]=y,g=2===y.length,v=(0,dayjs_1.default)(p,"DD/MM/YYYY").isValid(),b=(0,dayjs_1.default)(f,"DD/MM/YYYY").isValid(),E=(0,dayjs_1.default)(p,"DD/MM/YYYY HH:mm:ss").tz("Asia/Kuala_lumpur"),$=(0,dayjs_1.default)(f,"DD/MM/YYYY HH:mm:ss").tz("Asia/Kuala_lumpur"),_=E.toDate(),M=E.toISOString(),D=$.toISOString();if(!v||!b)return void T.status(400).json({err:"Invalid Aggregate Range Configuration"});var C=g?d:"Begining until "+(0,dayjs_1.default)(_).format("DD/MM/YYYY"),j=(g?a.push(c+` BETWEEN '${M}' AND '${D}'`):a.push(c+` < '${M}'`),0<a.length?"WHERE "+a.join(" AND "):"");let t=0;switch(n.type){case"count":e=`SELECT COUNT(*) as count FROM ${this.collection} `+j;var I=yield this.pgDB.query(e);t=parseInt(I.rows[0].count,10);break;case"sum":e=`SELECT SUM(${l}) as sum FROM ${this.collection} `+j;var S=yield this.pgDB.query(e);t=parseFloat(S.rows[0].sum);break;case"avg":e=`SELECT AVG(${l}) as avg FROM ${this.collection} `+j;var R=yield this.pgDB.query(e);t=parseFloat(R.rows[0].avg);break;case"value":e=`SELECT * FROM ${this.collection} ${j} ORDER BY ${c} DESC LIMIT 1`;var m=yield this.pgDB.query(e);0<m.rows.length&&(t=m.rows[0][l])}h.push({_id:C,value:t})}T.send({data:h})}else{let e="SELECT * FROM "+this.collection,a=[];u&&0<u.length&&u.map(e=>{var t,i=null!=(t=e.queryId)?t:"",s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&a.push(e.searchIds[0]+` ILIKE '${s}%'`);break;case"=":a.push(i+` = '${s}'`);break;case"!=":a.push(i+` != '${s}'`);break;case">":a.push(i+` > '${s}'`);break;case"<":a.push(i+` < '${s}'`);break;case"><":Array.isArray(s)&&2<=s.length&&a.push(`${i} BETWEEN '${s[0]}' AND '${s[1]}'`);break;case"in":a.push(`${i} IN (${s.map(e=>`'${e}'`).join(", ")})`)}});var w,k,q=0<a.length?"WHERE "+a.join(" AND "):"",{rows:O,rowCount:B}=(e+=" "+q,L.body.sort&&(w=L.body.sort,e+=` ORDER BY ${w.sortId} `+("asc"===w.type?"ASC":"DESC")),L.body.cursor&&(e+=" OFFSET "+L.body.cursor),L.body.stopLimit||(k=null!=(r=L.body.limit)?r:10,e+=" LIMIT "+k),yield this.pgDB.query(e));let t=!0,i=[];O&&O.forEach(e=>{i.push(e),(0,QueryModel_1.checkOpsConfig)(this.config,"read",L,e)||(t=!1)}),o&&t?T.send({data:i,count:B}):T.status(400).json({err:"Invalid Security Configuration"})}}else T.status(400).json({err:"Invalid Security Configuration"})}catch(e){e.message?T.status(400).json({err:e.message}):T.status(400).json({err:e})}}),this.init=()=>{var e=this.lib.express.Router();return e.post("/create",(e,t)=>{this.initCreate(e,t)}),e.post("/get",(e,t)=>{this.initGet(e,t)}),e.post("/update",(e,t)=>{this.initUpdate(e,t)}),e.post("/delete",(e,t)=>{this.initDelete(e,t)}),e.post("/list",(e,t)=>{this.initList(e,t)}),e},this.pgDB=e,this.collection=t,this.isProd=i,this.config=s,this.modelChecker=a,this.lib=r,this.debug=o}buildUpdateQuery(e){return Object.keys(e).map((e,t)=>e+" = $"+(t+1)).join(", ")}}exports.PostgreSQLWrapper=PostgreSQLWrapper;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"security.d.ts","sourceRoot":"","sources":["../../src/apis/security.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,qBAAa,kBAAkB;IAE7B,mBAAmB,
|
|
1
|
+
{"version":3,"file":"security.d.ts","sourceRoot":"","sources":["../../src/apis/security.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,qBAAa,kBAAkB;IAE7B,mBAAmB,GAAU,OAAO,MAAM,qBAExC;IACF,UAAU,GAAU,QAAQ,MAAM,qBAEhC;IACF,SAAS,GAAU,QAAQ,MAAM,qBAE/B;IACF,aAAa,GAAU,UAAU,MAAM,sBAErC;gBAIA,mBAAmB,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,EACvD,UAAU,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,EAC/C,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,EAC9C,aAAa,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC;IAQvD;;;;OAIG;IACH,IAAI,UAG8C,GAAG,OAAO,GAAG,SAAS,GAAG;CAgC5E"}
|
package/dist/apis/security.js
CHANGED
|
@@ -1,80 +1 @@
|
|
|
1
|
-
"
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.SecurityMiddleware = void 0;
|
|
13
|
-
/**
|
|
14
|
-
* Security for our server
|
|
15
|
-
* 1. Add Admin Token
|
|
16
|
-
* 2. Add CORS Policy - Protect web browser
|
|
17
|
-
* 3. Basic helmet security configurations
|
|
18
|
-
* 4. Prevent DDOS from rate limiting
|
|
19
|
-
*/
|
|
20
|
-
class SecurityMiddleware {
|
|
21
|
-
/* eslint-enable */
|
|
22
|
-
constructor(checkAuthentication, checkOrgId, checkRole, checkAPIToken) {
|
|
23
|
-
/* eslint-disable */
|
|
24
|
-
this.checkAuthentication = (token) => __awaiter(this, void 0, void 0, function* () {
|
|
25
|
-
return "";
|
|
26
|
-
});
|
|
27
|
-
this.checkOrgId = (userId) => __awaiter(this, void 0, void 0, function* () {
|
|
28
|
-
return "";
|
|
29
|
-
});
|
|
30
|
-
this.checkRole = (userId) => __awaiter(this, void 0, void 0, function* () {
|
|
31
|
-
return "";
|
|
32
|
-
});
|
|
33
|
-
this.checkAPIToken = (apiToken) => __awaiter(this, void 0, void 0, function* () {
|
|
34
|
-
return false;
|
|
35
|
-
});
|
|
36
|
-
this.checkAuthentication = checkAuthentication;
|
|
37
|
-
this.checkOrgId = checkOrgId;
|
|
38
|
-
this.checkRole = checkRole;
|
|
39
|
-
this.checkAPIToken = checkAPIToken;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* 1. Check User Authentication
|
|
43
|
-
* 2. Check User Role
|
|
44
|
-
* 3. Check API Token
|
|
45
|
-
*/
|
|
46
|
-
init() {
|
|
47
|
-
// eslint-disable-next-line
|
|
48
|
-
const wrapper = this;
|
|
49
|
-
const securityMiddleware = function (req, res, next) {
|
|
50
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
-
delete req.body["bm_userId"];
|
|
52
|
-
delete req.body["bm_userOrg"];
|
|
53
|
-
delete req.body["bm_userRole"];
|
|
54
|
-
delete req.body["bm_apiToken"];
|
|
55
|
-
if (req.body.userToken) {
|
|
56
|
-
const userId = yield wrapper.checkAuthentication(req.body.userToken);
|
|
57
|
-
if (userId) {
|
|
58
|
-
req.body["bm_userId"] = userId;
|
|
59
|
-
const userOrgId = yield wrapper.checkOrgId(userId);
|
|
60
|
-
if (userOrgId)
|
|
61
|
-
req.body["bm_userOrg"] = userOrgId;
|
|
62
|
-
const userRole = yield wrapper.checkRole(userId);
|
|
63
|
-
if (userRole)
|
|
64
|
-
req.body["bm_userRole"] = userRole;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
if (req.body.apiToken) {
|
|
68
|
-
const apiToken = req.body.apiToken;
|
|
69
|
-
const valid = yield wrapper.checkAPIToken(apiToken);
|
|
70
|
-
req.body["bm_apiToken"] = valid;
|
|
71
|
-
}
|
|
72
|
-
if (next) {
|
|
73
|
-
next();
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
};
|
|
77
|
-
return securityMiddleware;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
exports.SecurityMiddleware = SecurityMiddleware;
|
|
1
|
+
var __awaiter=this&&this.__awaiter||function(e,c,d,u){return new(d=d||Promise)(function(i,t){function o(e){try{r(u.next(e))}catch(e){t(e)}}function n(e){try{r(u.throw(e))}catch(e){t(e)}}function r(e){var t;e.done?i(e.value):((t=e.value)instanceof d?t:new d(function(e){e(t)})).then(o,n)}r((u=u.apply(e,c||[])).next())})};Object.defineProperty(exports,"__esModule",{value:!0}),exports.SecurityMiddleware=void 0;class SecurityMiddleware{constructor(e,t,i,o){this.checkAuthentication=e=>__awaiter(this,void 0,void 0,function*(){return""}),this.checkOrgId=e=>__awaiter(this,void 0,void 0,function*(){return""}),this.checkRole=e=>__awaiter(this,void 0,void 0,function*(){return""}),this.checkAPIToken=e=>__awaiter(this,void 0,void 0,function*(){return!1}),this.checkAuthentication=e,this.checkOrgId=t,this.checkRole=i,this.checkAPIToken=o}init(){let n=this;return function(i,e,o){return __awaiter(this,void 0,void 0,function*(){var e,t;delete i.body.bm_userId,delete i.body.bm_userOrg,delete i.body.bm_userRole,delete i.body.bm_apiToken,i.body.userToken&&(e=yield n.checkAuthentication(i.body.userToken))&&(i.body.bm_userId=e,(t=yield n.checkOrgId(e))&&(i.body.bm_userOrg=t),t=yield n.checkRole(e))&&(i.body.bm_userRole=t),i.body.apiToken&&(e=i.body.apiToken,t=yield n.checkAPIToken(e),i.body.bm_apiToken=t),o&&o()})}}}exports.SecurityMiddleware=SecurityMiddleware;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trackVisionWrapper.d.ts","sourceRoot":"","sources":["../../src/apis/trackVisionWrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAEjD,qBAAa,kBAAkB;IAC7B,MAAM,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAkC;IAClE,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,GAAG,EAAE,UAAU,CAAC;gBAEJ,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU;IAM/D,sBAAsB,
|
|
1
|
+
{"version":3,"file":"trackVisionWrapper.d.ts","sourceRoot":"","sources":["../../src/apis/trackVisionWrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAEjD,qBAAa,kBAAkB;IAC7B,MAAM,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAkC;IAClE,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,GAAG,EAAE,UAAU,CAAC;gBAEJ,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU;IAM/D,sBAAsB,GAAU,UAAU,MAAM,EAAE,MAAM,GAAG,kBAoBzD;IAEF,2BAA2B,GAAU,KAAK,GAAG,EAAE,KAAK,GAAG,mBAUrD;IAEF,mCAAmC,GAAU,KAAK,GAAG,EAAE,KAAK,GAAG,mBAW7D;IAEF,gCAAgC,GAAU,KAAK,GAAG,EAAE,KAAK,GAAG,mBAe1D;IAEF,qBAAqB,GAAU,KAAK,GAAG,EAAE,KAAK,GAAG,mBAU/C;IAEF,8BAA8B,GAAU,KAAK,GAAG,EAAE,KAAK,GAAG,mBAUxD;IAEF,sBAAsB,GAAU,KAAK,GAAG,EAAE,KAAK,GAAG,mBAUhD;IAEF,2BAA2B,GAAU,KAAK,GAAG,EAAE,KAAK,GAAG,mBAcrD;IAEF,IAAI,YA0DF;CACH"}
|