blixify-server 0.1.65 → 0.1.67

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.
@@ -1 +1,943 @@
1
- "use strict";var __awaiter=this&&this.__awaiter||function(e,n,r,d){return new(r=r||Promise)(function(i,t){function s(e){try{a(d.next(e))}catch(e){t(e)}}function o(e){try{a(d.throw(e))}catch(e){t(e)}}function a(e){var t;e.done?i(e.value):((t=e.value)instanceof r?t:new r(function(e){e(t)})).then(s,o)}a((d=d.apply(e,n||[])).next())})},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.MongoWrapper=void 0;const moment_timezone_1=__importDefault(require("moment-timezone")),QueryModel_1=require("../model/QueryModel"),utils_1=require("./utils"),handleParseQueryFilter=(e,n)=>{const r={},d={};return e&&0<e.length&&e.map(e=>{var t;let i=null!=(t=e.queryId)?t:"";n&&(i=n+"."+i);const s=e.value,o={};let a=!1;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&e.searchIds.map(e=>{a=!0;let t=s;t.includes("+")&&(t="\\"+t),o[e]={$regex:""+t,$options:"i"}});break;case"=":o[i]=s;break;case"!=":o[i]={$ne:s};break;case">":o[i]={$gt:s};break;case"<":o[i]={$lt:s};break;case"><":Array.isArray(s)&&2<=s.length&&(o[i]={$gte:s[0],$lte:s[1]});break;case"in":o[i]={$in:s}}e.orQuery&&(a=!0),e.orQueryGroupId&&o[i]&&(d[e.orQueryGroupId]?d[e.orQueryGroupId].$and.push({[i]:o[i]}):d[e.orQueryGroupId]={$and:[{[i]:o[i]}]},delete o[i]),Object.keys(o).map(e=>{a?r.$or?r.$or.push({[e]:o[e]}):r.$or=[{[e]:o[e]}]:r[e]=o[e]})}),Object.keys(d).map(e=>{r.$or?r.$or.push(d[e]):r.$or=[d[e]]}),r};class MongoWrapper{constructor(e,t,i,s,o,a,n,r){this.mongoDB="",this.collection="",this.isProd=!1,this.config={baseConfig:[],opsConfig:{read:[],create:[],update:[],delete:[]}},this.tableId="",this.modelChecker=e=>!1,this.parseModel=e=>(delete e._id,e.baseUpdatedAt=(0,moment_timezone_1.default)().toDate(),e),this.initBatchCreate=(u,c,h)=>__awaiter(this,void 0,void 0,function*(){var e;try{if(Array.isArray(u.body.data)){let t=!0;u.body.data.map(e=>{this.modelChecker(e)||(t=!1)}),t||c.status(400).json({err:"Invalid Model Structure"});var s=(0,QueryModel_1.checkBaseConfig)(this.config,u);let i=!0;const d=[];if(yield Promise.all(u.body.data.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"update",u,e)||(i=!1),u.body.unique&&null!=e&&e[u.body.unique]&&d.push(e[u.body.unique])})),s&&i){var o=this.mongoDB.db(this.tableId).collection(this.collection);if(0<d.length){var a=yield(yield o.find({[u.body.unique]:{$in:d}})).toArray();if(0<a.length)return void c.status(400).json({err:`Unique:${a.map(e=>e[u.body.unique])} exists`})}const l=[];var n=u.body.data.map(e=>(l.push(e._id),Object.assign(Object.assign({},e),{baseUpdatedAt:(0,moment_timezone_1.default)().toDate()}))),r=(h&&(yield h(n)),yield o.insertMany(n),{success:l});if(null!=(e=u.body)&&e.stopRes)return r;c.send(r)}else c.status(400).json({err:"Invalid Security Configuration"})}else c.status(400).json({err:"Invalid Fields"})}catch(e){c.status(400).json({err:e})}}),this.initCreate=(n,r,d)=>__awaiter(this,void 0,void 0,function*(){var e,t;try{if(this.modelChecker(n.body.data)){var i=(0,QueryModel_1.checkBaseConfig)(this.config,n),s=(0,QueryModel_1.checkOpsConfig)(this.config,"create",n,n.body.data);if(i&&s){var o=this.mongoDB.db(this.tableId).collection(this.collection);if(n.body.data.baseUpdatedAt=(0,moment_timezone_1.default)().toDate(),n.body.unique&&null!=(e=n.body.data)&&e[n.body.unique])if(yield o.findOne({[n.body.unique]:n.body.data[n.body.unique]}))return void r.status(400).json({err:`Unique:${n.body.data[n.body.unique]} exists`});d&&(yield d(n.body.data)),yield o.insertOne(n.body.data);var a={success:!0};if(null!=(t=n.body)&&t.stopRes)return a;r.send(a)}else r.status(400).json({err:"Invalid Security Configuration"})}else r.status(400).json({err:"Invalid Model Structure"})}catch(e){r.status(400).json({err:e})}}),this.initGet=(n,r)=>__awaiter(this,void 0,void 0,function*(){var e;try{var t=n.body.id;if(t){var i,s=yield this.mongoDB.db(this.tableId).collection(this.collection).findOne({_id:t}),o=(0,QueryModel_1.checkBaseConfig)(this.config,n),a=(0,QueryModel_1.checkOpsConfig)(this.config,"read",n,s);if(o&&a)return i={data:s},null!=(e=n.body)&&e.stopRes?i:void r.send(i);r.status(400).json({err:"Invalid Security Configuration"})}else r.status(400).json({err:"Invalid Fields"})}catch(e){r.status(400).json({err:e})}}),this.initBatchUpdate=(g,m,v)=>__awaiter(this,void 0,void 0,function*(){var e,i,s;try{if(this.modelChecker(g.body.data)&&g.body.query||g.body.unsetData){var o=g.body.unsetData,a=null!=(e=g.body.query)?e:[];const y=g.body.isOr?"$or":"$and",p={};var n=this.parseModel(null!=(i=g.body.data)?i:[]),r=(a&&0<a.length&&(p[y]=[],a.some(e=>"search"===e.type)&&(p.$or=[]),a.map(e=>{var t,i=null!=(t=e.queryId)?t:"";const s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&e.searchIds.map(e=>{p.$or.push({[e]:{$regex:s,$options:"i"}})});break;case"=":p[y].push({[i]:s});break;case"!=":p[y].push({[i]:{$ne:s}});break;case">":p[y].push({[i]:{$gt:s}});break;case"<":p[y].push({[i]:{$lt:s}});break;case"><":Array.isArray(s)&&2<=s.length&&(p[y].push({[i]:{$lt:s}}),p[y]={[i]:{$gte:s[0],$lte:s[1]}});break;case"in":p[y].push({[i]:{$in:s}})}})),(0,QueryModel_1.checkBaseConfig)(this.config,g));let t=!0;var d=this.mongoDB.db(this.tableId).collection(this.collection),l=yield(yield d.find(Object.assign({},p))).toArray();const b=[];if(yield Promise.all(l.map(e=>{g.body.sensitive&&!(0,moment_timezone_1.default)(g.body.data.baseUpdatedAt).isAfter((0,moment_timezone_1.default)(e.baseUpdatedAt))&&m.status(400).json({err:"Refresh Sensitive Model"}),(0,QueryModel_1.checkOpsConfig)(this.config,"update",g,e)||(t=!1);e=(0,utils_1.compareUpdatedFields)(e,g.body.data).workflowUpdateFields;b.push(e)})),r&&t){const f={};var u={},c=(o&&(o.forEach(e=>{f[e]=""}),u.$unset=Object.assign({},f)),n&&(u.$set=Object.assign({},n)),v&&(yield v(b)),yield d.updateMany(p,u),b.map(e=>e._id)),h={success:c};if(null!=(s=g.body)&&s.stopRes)return h;m.send(h)}else m.status(400).json({err:"Invalid Security Configuration"})}}catch(e){m.status(400).json({err:e})}}),this.initUpdate=(h,y,p)=>__awaiter(this,void 0,void 0,function*(){var e,t,i;try{var s=this.modelChecker(h.body.data)&&h.body.id;if(s){var o=this.mongoDB.db(this.tableId).collection(this.collection),a=yield o.findOne({_id:s}),n=(this.debug&&this.debug(h.body,a),(0,QueryModel_1.checkBaseConfig)(this.config,h)),r=(0,QueryModel_1.checkOpsConfig)(this.config,"update",h,a);if(n&&r){if(h.body.unique&&null!=(e=h.body.data)&&e[h.body.unique]){var d=yield o.findOne({[h.body.unique]:h.body.data[h.body.unique]});if(d&&h.body.id!==d._id)return void y.status(400).json({err:h.body.data[h.body.unique]+" exists"})}var l=(0,utils_1.compareUpdatedFields)(a,h.body.data)["updatedFields"],u={success:!0},c=(p&&(yield p(l)),this.parseModel(l));if(h.body.sensitive)if((0,moment_timezone_1.default)(h.body.data.baseUpdatedAt).isAfter((0,moment_timezone_1.default)(a.baseUpdatedAt))){if(yield o.updateOne({_id:s},{$set:Object.assign({},c)}),null!=(t=h.body)&&t.stopRes)return u;y.send(u)}else y.status(400).json({err:"Refresh Sensitive Model"});else{if(yield o.updateOne({_id:s},{$set:Object.assign({},c)}),null!=(i=h.body)&&i.stopRes)return u;y.send(u)}}else y.status(400).json({err:"Invalid Security Configuration"})}else y.status(400).json({err:"Invalid Model Structure"})}catch(e){this.debug&&this.debug(null,null,e),y.status(400).json({err:e})}}),this.initBatchDelete=(b,f,g)=>__awaiter(this,void 0,void 0,function*(){var e,i;try{if(Array.isArray(b.body.id)){var s=this.mongoDB.db(this.tableId).collection(this.collection),o=yield s.find({_id:{$in:b.body.id}}),a=(0,QueryModel_1.checkBaseConfig)(this.config,b),n=yield o.toArray();let t=!0;if(0<n.length&&(yield Promise.all(n.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"delete",b,e)||(t=!1)}))),a&&t){g&&(yield g(n)),yield s.deleteMany({_id:{$in:b.body.id}});var r={success:b.body.id};if(null!=(e=b.body)&&e.stopRes)return r;f.send(r)}else f.status(400).json({err:"Invalid Security Configuration"})}else{var d=b.body.query;const y={};d&&0<d.length&&d.map(e=>{var t,i=null!=(t=e.queryId)?t:"";const s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&(y.$or=[],e.searchIds.map(e=>{y.$or.push({[e]:{$regex:s,$options:"i"}})}));break;case"=":y[i]=s;break;case"!=":y[i]={$ne:s};break;case">":y[i]={$gt:s};break;case"<":y[i]={$lt:s};break;case"><":Array.isArray(s)&&2<=s.length&&(y[i]={$gte:s[0],$lte:s[1]});break;case"in":y[i]={$in:s}}});var l=this.mongoDB.db(this.tableId).collection(this.collection),u=yield l.find(y).toArray(),c=(0,QueryModel_1.checkBaseConfig)(this.config,b);let t=!0;const p=[];if(0<u.length&&u.map(e=>{p.push(e._id),(0,QueryModel_1.checkOpsConfig)(this.config,"delete",b,e)||(t=!1)}),g&&(yield g(u)),c&&t){yield l.deleteMany(y);var h={success:p};if(null!=(i=b.body)&&i.stopRes)return h;f.send(h)}else f.status(400).json({err:"Invalid Security Configuration"})}}catch(e){f.status(400).json({err:e})}}),this.initDelete=(r,d,l)=>__awaiter(this,void 0,void 0,function*(){var e;try{var t=r.body.id;if(t){var i=this.mongoDB.db(this.tableId).collection(this.collection),s=yield i.findOne({_id:t}),o=(0,QueryModel_1.checkBaseConfig)(this.config,r),a=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",r,s);if(o&&a){l&&(yield l(s)),yield i.findOneAndDelete({_id:t});var n={success:!0};if(null!=(e=r.body)&&e.stopRes)return n;d.send(n)}else d.status(400).json({err:"Invalid Security Configuration"})}else d.status(400).json({err:"Invalid Fields"})}catch(e){d.status(400).json({err:e})}}),this.initList=(U,z)=>__awaiter(this,void 0,void 0,function*(){var e,t,i,s,o,n,a,r,d;try{var l=(0,QueryModel_1.checkBaseConfig)(this.config,U);if(l){var u=U.body.aggregate,c=null!=(e=U.body.query)?e:[],h=handleParseQueryFilter(c);if(u){var y=[],p=null!=(t=u.queryId)?t:"",b=null!=(i=u.dateId)?i:"baseUpdatedAt";for(const B of null!=(s=u.range)?s:[(0,moment_timezone_1.default)().format("DD/MM/YYYY HH:mm:ss")]){Object.assign({},h);var f=B.split("-"),[g,m=(0,moment_timezone_1.default)().format("DD/MM/YYYY HH:mm:ss")]=f,v=2===f.length,_=(0,moment_timezone_1.default)(g,"DD/MM/YYYY").isValid(),$=(0,moment_timezone_1.default)(m,"DD/MM/YYYY").isValid(),I=(0,moment_timezone_1.default)(g,"DD/MM/YYYY HH:mm:ss").tz("Asia/Kuala_lumpur").toDate(),M=(0,moment_timezone_1.default)(m,"DD/MM/YYYY HH:mm:ss").tz("Asia/Kuala_lumpur").toDate();if(!_||!$)return void z.status(400).json({err:"Invalid Aggregate Range Configuration"});const Q=[v?{$match:Object.assign(Object.assign({},h),{[b]:{$gte:I,$lte:M}})}:{$match:Object.assign(Object.assign({},h),{[b]:{$lte:I}})}],w=v?B:"Begining until "+(0,moment_timezone_1.default)(g,"DD/MM/YYYY HH:mm:ss").format("DD/MM/YYYY");let a=!1;if(u.unwind&&0<u.unwind.length&&u.unwind.map((e,t)=>{var i,s="model"+t;let o="";0<t&&(o="model"+(t-1)+".");t=handleParseQueryFilter(null!=(t=e.query)?t:[],s);null!=(i=e.unique)&&i.list&&Q.push({$unwind:"$"+e.localField}),Q.push({$lookup:{from:e.collectionId,localField:o+e.localField,foreignField:"_id",as:s}},{$unwind:"$"+s},{$match:t}),null!=(i=e.unique)&&i.type&&(a=!0,s="uniqueUnwindId",Q.push({$group:{_id:w,uniqueUnwindId:{$addToSet:"$"+e.localField}}}),"count"===e.unique.type?Q.push({$project:{value:{$size:"$"+s}}}):Q.push({$project:{_id:0,value:"$"+s}}))}),!a){let e=w;switch(u.groupId&&(e="$"+u.groupId),u.type){case"avg":Q.push({$group:{_id:e,value:{$avg:"$"+p}}});break;case"sum":Q.push({$group:{_id:e,value:{$sum:"$"+p}}});break;case"count":Q.push({$group:{_id:e,value:{$sum:1}}})}}if(0===Q.length)return void z.status(400).json({err:"Invalid Aggregate Configuration"});var k=this.mongoDB.db(this.tableId).collection(this.collection);let e=0;var j=yield k.aggregate(Q).toArray();e=u.groupId?j:null!=(n=null==(o=j[0])?void 0:o.value)?n:0,y.push({_id:w,value:e})}var D={data:y};if(null!=(a=U.body)&&a.stopRes)return D;z.send(D)}else{let e=this.mongoDB.db(this.tableId).collection(this.collection).find(h);var C,q,O,Y=yield e.count({}),A=(U.body.cursor&&(e=e.skip(U.body.cursor)),U.body.stopLimit||(e=e.limit(null!=(r=U.body.limit)?r:10)),U.body.sort&&(q={[(C=U.body.sort).sortId]:"asc"===C.type?1:-1},e=e.sort(q)),yield e.toArray());let t=!0;if(0<A.length&&A.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"read",U,e)||(t=!1)}),l&&t)return O={data:A,count:Y},null!=(d=U.body)&&d.stopRes?O:void z.send(O);z.status(400).json({err:"Invalid Security Configuration"})}}else z.status(400).json({err:"Invalid Security Configuration"})}catch(e){z.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("/batchCreate",(e,t)=>{this.initBatchCreate(e,t)}),e.post("/get",(e,t)=>{this.initGet(e,t)}),e.post("/update",(e,t)=>{this.initUpdate(e,t)}),e.post("/batchUpdate",(e,t)=>{this.initBatchUpdate(e,t)}),e.post("/delete",(e,t)=>{this.initDelete(e,t)}),e.post("/batchDelete",(e,t)=>{this.initBatchDelete(e,t)}),e.post("/list",(e,t)=>{this.initList(e,t)}),e},this.mongoDB=e,this.collection=t,this.isProd=i,this.config=s,this.modelChecker=o,this.lib=a;e=this.isProd?"prod":"dev";this.tableId=n?n+"-"+e:e,this.debug=r}}exports.MongoWrapper=MongoWrapper;
1
+ "use strict";
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.MongoWrapper = 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
+ const handleParseQueryFilter = (queryList, prefix) => {
20
+ const queryFilter = {};
21
+ const queryGroupValues = {};
22
+ if (queryList && queryList.length > 0) {
23
+ queryList.map((eachQuery) => {
24
+ var _a;
25
+ let queryId = (_a = eachQuery.queryId) !== null && _a !== void 0 ? _a : "";
26
+ if (prefix)
27
+ queryId = `${prefix}.${queryId}`;
28
+ const value = eachQuery.value;
29
+ const queryValues = {};
30
+ let orOption = false;
31
+ switch (eachQuery.type) {
32
+ case "search":
33
+ if (eachQuery.searchIds && eachQuery.searchIds.length > 0) {
34
+ eachQuery.searchIds.map((eachSearchId) => {
35
+ orOption = true;
36
+ let searchValue = value;
37
+ if (searchValue.includes("+"))
38
+ searchValue = "\\" + searchValue;
39
+ queryValues[eachSearchId] = {
40
+ $regex: `${searchValue}`,
41
+ $options: "i",
42
+ };
43
+ });
44
+ }
45
+ break;
46
+ case "=":
47
+ queryValues[queryId] = value;
48
+ break;
49
+ case "!=":
50
+ queryValues[queryId] = {
51
+ $ne: value,
52
+ };
53
+ break;
54
+ case ">":
55
+ queryValues[queryId] = {
56
+ $gt: value,
57
+ };
58
+ break;
59
+ case "<":
60
+ queryValues[queryId] = {
61
+ $lt: value,
62
+ };
63
+ break;
64
+ case "><":
65
+ if (Array.isArray(value) && value.length >= 2) {
66
+ queryValues[queryId] = {
67
+ $gte: value[0],
68
+ $lte: value[1],
69
+ };
70
+ }
71
+ break;
72
+ case "in":
73
+ queryValues[queryId] = {
74
+ $in: value,
75
+ };
76
+ break;
77
+ default:
78
+ break;
79
+ }
80
+ if (eachQuery.orQuery)
81
+ orOption = true;
82
+ //INFO : Delete Query Values as it's added outside
83
+ if (eachQuery.orQueryGroupId) {
84
+ const groupValue = queryValues[queryId];
85
+ if (groupValue) {
86
+ if (queryGroupValues[eachQuery.orQueryGroupId]) {
87
+ queryGroupValues[eachQuery.orQueryGroupId]["$and"].push({
88
+ [queryId]: queryValues[queryId],
89
+ });
90
+ }
91
+ else {
92
+ queryGroupValues[eachQuery.orQueryGroupId] = {
93
+ $and: [{ [queryId]: queryValues[queryId] }],
94
+ };
95
+ }
96
+ delete queryValues[queryId];
97
+ }
98
+ }
99
+ Object.keys(queryValues).map((eachKey) => {
100
+ if (orOption) {
101
+ if (queryFilter["$or"])
102
+ queryFilter["$or"].push({ [eachKey]: queryValues[eachKey] });
103
+ else
104
+ queryFilter["$or"] = [{ [eachKey]: queryValues[eachKey] }];
105
+ }
106
+ else {
107
+ queryFilter[eachKey] = queryValues[eachKey];
108
+ }
109
+ });
110
+ });
111
+ }
112
+ Object.keys(queryGroupValues).map((eachKey) => {
113
+ if (queryFilter["$or"])
114
+ queryFilter["$or"].push(queryGroupValues[eachKey]);
115
+ else
116
+ queryFilter["$or"] = [queryGroupValues[eachKey]];
117
+ });
118
+ return queryFilter;
119
+ };
120
+ /**
121
+ * @Wrapper
122
+ * collection - MongoDB Collection
123
+ *
124
+ */
125
+ class MongoWrapper {
126
+ constructor(mongoDB, collection, isProd, config, modelChecker, lib, tableId, debug) {
127
+ this.mongoDB = "";
128
+ this.collection = "";
129
+ this.isProd = false;
130
+ this.config = {
131
+ baseConfig: [],
132
+ opsConfig: {
133
+ read: [],
134
+ create: [],
135
+ update: [],
136
+ delete: [],
137
+ },
138
+ };
139
+ this.tableId = "";
140
+ // eslint-disable-next-line
141
+ this.modelChecker = (obj) => {
142
+ return false;
143
+ };
144
+ this.parseModel = (data) => {
145
+ delete data["_id"];
146
+ data["baseUpdatedAt"] = (0, moment_timezone_1.default)().toDate();
147
+ return data;
148
+ };
149
+ this.initBatchCreate = (req, res, workflow) => __awaiter(this, void 0, void 0, function* () {
150
+ var _a;
151
+ try {
152
+ if (Array.isArray(req.body.data)) {
153
+ let valid = true;
154
+ req.body.data.map((eachData) => {
155
+ if (!this.modelChecker(eachData)) {
156
+ valid = false;
157
+ }
158
+ });
159
+ if (!valid) {
160
+ res.status(400).json({ err: "Invalid Model Structure" });
161
+ }
162
+ //INFO : Security Checker
163
+ const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
164
+ let validOpsConfig = true;
165
+ const uniqueDataField = [];
166
+ yield Promise.all(req.body.data.map((eachData) => {
167
+ if (!(0, QueryModel_1.checkOpsConfig)(this.config, "update", req, eachData)) {
168
+ validOpsConfig = false;
169
+ }
170
+ if (req.body.unique && (eachData === null || eachData === void 0 ? void 0 : eachData[req.body.unique]))
171
+ uniqueDataField.push(eachData[req.body.unique]);
172
+ }));
173
+ if (!validBaseConfig || !validOpsConfig) {
174
+ res.status(400).json({ err: "Invalid Security Configuration" });
175
+ return;
176
+ }
177
+ const mongoCollection = this.mongoDB
178
+ .db(this.tableId)
179
+ .collection(this.collection);
180
+ if (uniqueDataField.length > 0) {
181
+ const existsDataRes = yield mongoCollection.find({
182
+ [req.body.unique]: { $in: uniqueDataField },
183
+ });
184
+ const existsDataList = yield existsDataRes.toArray();
185
+ if (existsDataList.length > 0) {
186
+ res.status(400).json({
187
+ err: `Unique:${existsDataList.map((eachData) => eachData[req.body.unique])} exists`,
188
+ });
189
+ return;
190
+ }
191
+ }
192
+ const ids = [];
193
+ const batchCreateDataList = req.body.data.map((eachData) => {
194
+ ids.push(eachData._id);
195
+ return Object.assign(Object.assign({}, eachData), { baseUpdatedAt: (0, moment_timezone_1.default)().toDate() });
196
+ });
197
+ if (workflow)
198
+ yield workflow(batchCreateDataList);
199
+ yield mongoCollection.insertMany(batchCreateDataList);
200
+ const resBody = { success: ids };
201
+ if ((_a = req.body) === null || _a === void 0 ? void 0 : _a.stopRes)
202
+ return resBody;
203
+ res.send(resBody);
204
+ }
205
+ else {
206
+ res.status(400).json({ err: "Invalid Fields" });
207
+ }
208
+ }
209
+ catch (err) {
210
+ res.status(400).json({ err: err });
211
+ }
212
+ });
213
+ this.initCreate = (req, res, workflow) => __awaiter(this, void 0, void 0, function* () {
214
+ var _b, _c;
215
+ try {
216
+ const valid = this.modelChecker(req.body.data);
217
+ if (valid) {
218
+ //INFO : Security Checker
219
+ const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
220
+ const validOpsConfig = (0, QueryModel_1.checkOpsConfig)(this.config, "create", req, req.body.data);
221
+ if (!validBaseConfig || !validOpsConfig) {
222
+ res.status(400).json({ err: "Invalid Security Configuration" });
223
+ return;
224
+ }
225
+ const mongoCollection = this.mongoDB
226
+ .db(this.tableId)
227
+ .collection(this.collection);
228
+ req.body.data["baseUpdatedAt"] = (0, moment_timezone_1.default)().toDate();
229
+ if (req.body.unique && ((_b = req.body.data) === null || _b === void 0 ? void 0 : _b[req.body.unique])) {
230
+ const isDataExists = yield mongoCollection.findOne({
231
+ [req.body.unique]: req.body.data[req.body.unique],
232
+ });
233
+ if (isDataExists) {
234
+ res
235
+ .status(400)
236
+ .json({ err: `Unique:${req.body.data[req.body.unique]} exists` });
237
+ return;
238
+ }
239
+ }
240
+ if (workflow)
241
+ yield workflow(req.body.data);
242
+ yield mongoCollection.insertOne(req.body.data);
243
+ const resBody = { success: true };
244
+ if ((_c = req.body) === null || _c === void 0 ? void 0 : _c.stopRes)
245
+ return resBody;
246
+ res.send(resBody);
247
+ }
248
+ else {
249
+ res.status(400).json({ err: "Invalid Model Structure" });
250
+ }
251
+ }
252
+ catch (err) {
253
+ res.status(400).json({ err: err });
254
+ }
255
+ });
256
+ this.initGet = (req, res) => __awaiter(this, void 0, void 0, function* () {
257
+ var _d;
258
+ try {
259
+ const valid = req.body.id;
260
+ if (valid) {
261
+ //INFO : Security Checker
262
+ const mongoCollection = this.mongoDB
263
+ .db(this.tableId)
264
+ .collection(this.collection);
265
+ const mongoData = yield mongoCollection.findOne({
266
+ _id: valid,
267
+ });
268
+ const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
269
+ const validOpsConfig = (0, QueryModel_1.checkOpsConfig)(this.config, "read", req, mongoData);
270
+ if (!validBaseConfig || !validOpsConfig) {
271
+ res.status(400).json({ err: "Invalid Security Configuration" });
272
+ return;
273
+ }
274
+ const resBody = { data: mongoData };
275
+ if ((_d = req.body) === null || _d === void 0 ? void 0 : _d.stopRes)
276
+ return resBody;
277
+ res.send(resBody);
278
+ }
279
+ else {
280
+ res.status(400).json({ err: "Invalid Fields" });
281
+ }
282
+ }
283
+ catch (err) {
284
+ res.status(400).json({ err: err });
285
+ }
286
+ });
287
+ this.initBatchUpdate = (req, res, workflow) => __awaiter(this, void 0, void 0, function* () {
288
+ var _e, _f, _g;
289
+ try {
290
+ const valid = (this.modelChecker(req.body.data) && req.body.query) ||
291
+ req.body.unsetData;
292
+ if (valid) {
293
+ const unsetData = req.body.unsetData;
294
+ const queryList = (_e = req.body.query) !== null && _e !== void 0 ? _e : [];
295
+ const queryType = req.body.isOr ? "$or" : "$and";
296
+ const queryFilter = {};
297
+ const data = this.parseModel((_f = req.body.data) !== null && _f !== void 0 ? _f : []);
298
+ // INFO : Step 1 - Query
299
+ if (queryList && queryList.length > 0) {
300
+ queryFilter[queryType] = [];
301
+ // INFO : To Ensure There is OR Filter on Search
302
+ if (queryList.some((eachQuery) => eachQuery.type === "search")) {
303
+ queryFilter["$or"] = [];
304
+ }
305
+ queryList.map((eachQuery) => {
306
+ var _a;
307
+ const queryId = (_a = eachQuery.queryId) !== null && _a !== void 0 ? _a : "";
308
+ const value = eachQuery.value;
309
+ switch (eachQuery.type) {
310
+ case "search":
311
+ if (eachQuery.searchIds && eachQuery.searchIds.length > 0) {
312
+ eachQuery.searchIds.map((eachSearchId) => {
313
+ queryFilter["$or"].push({
314
+ [eachSearchId]: { $regex: value, $options: "i" },
315
+ });
316
+ });
317
+ }
318
+ break;
319
+ case "=":
320
+ queryFilter[queryType].push({
321
+ [queryId]: value,
322
+ });
323
+ break;
324
+ case "!=":
325
+ queryFilter[queryType].push({
326
+ [queryId]: { $ne: value },
327
+ });
328
+ break;
329
+ case ">":
330
+ queryFilter[queryType].push({
331
+ [queryId]: { $gt: value },
332
+ });
333
+ break;
334
+ case "<":
335
+ queryFilter[queryType].push({
336
+ [queryId]: { $lt: value },
337
+ });
338
+ break;
339
+ case "><":
340
+ if (Array.isArray(value) && value.length >= 2) {
341
+ queryFilter[queryType].push({
342
+ [queryId]: { $lt: value },
343
+ });
344
+ queryFilter[queryType] = {
345
+ [queryId]: { $gte: value[0], $lte: value[1] },
346
+ };
347
+ }
348
+ break;
349
+ case "in":
350
+ queryFilter[queryType].push({
351
+ [queryId]: { $in: value },
352
+ });
353
+ break;
354
+ default:
355
+ break;
356
+ }
357
+ });
358
+ }
359
+ const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
360
+ //INFO : Security Checker
361
+ let validOpsConfig = true;
362
+ const mongoCollection = this.mongoDB
363
+ .db(this.tableId)
364
+ .collection(this.collection);
365
+ const mongoDataRes = yield mongoCollection.find(Object.assign({}, queryFilter));
366
+ const mongoDataList = yield mongoDataRes.toArray();
367
+ const workflowUpdateFieldsList = [];
368
+ yield Promise.all(mongoDataList.map((eachData) => {
369
+ // INFO : Check Sensitive
370
+ if (req.body.sensitive) {
371
+ if (!(0, moment_timezone_1.default)(req.body.data.baseUpdatedAt).isAfter((0, moment_timezone_1.default)(eachData.baseUpdatedAt))) {
372
+ res.status(400).json({ err: "Refresh Sensitive Model" });
373
+ }
374
+ }
375
+ // INFO : Check Ops Config
376
+ if (!(0, QueryModel_1.checkOpsConfig)(this.config, "update", req, eachData)) {
377
+ validOpsConfig = false;
378
+ }
379
+ const { workflowUpdateFields } = (0, utils_1.compareUpdatedFields)(eachData, req.body.data);
380
+ workflowUpdateFieldsList.push(workflowUpdateFields);
381
+ }));
382
+ if (!validBaseConfig || !validOpsConfig) {
383
+ res.status(400).json({ err: "Invalid Security Configuration" });
384
+ return;
385
+ }
386
+ const $unset = {};
387
+ const condition = {};
388
+ if (unsetData) {
389
+ unsetData.forEach((eachField) => {
390
+ $unset[eachField] = "";
391
+ });
392
+ condition["$unset"] = Object.assign({}, $unset);
393
+ }
394
+ if (data) {
395
+ condition["$set"] = Object.assign({}, data);
396
+ }
397
+ if (workflow)
398
+ yield workflow(workflowUpdateFieldsList);
399
+ yield mongoCollection.updateMany(queryFilter, condition);
400
+ const ids = workflowUpdateFieldsList.map((eachData) => eachData._id);
401
+ const resBody = { success: ids };
402
+ if ((_g = req.body) === null || _g === void 0 ? void 0 : _g.stopRes)
403
+ return resBody;
404
+ res.send(resBody);
405
+ }
406
+ }
407
+ catch (err) {
408
+ res.status(400).json({ err: err });
409
+ }
410
+ });
411
+ this.initUpdate = (req, res, workflow) => __awaiter(this, void 0, void 0, function* () {
412
+ var _h, _j, _k;
413
+ try {
414
+ const valid = this.modelChecker(req.body.data) && req.body.id;
415
+ if (valid) {
416
+ const mongoCollection = this.mongoDB
417
+ .db(this.tableId)
418
+ .collection(this.collection);
419
+ const mongoData = yield mongoCollection.findOne({
420
+ _id: valid,
421
+ });
422
+ if (this.debug)
423
+ this.debug(req.body, mongoData);
424
+ //INFO : Security Checker
425
+ const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
426
+ const validOpsConfig = (0, QueryModel_1.checkOpsConfig)(this.config, "update", req, mongoData);
427
+ if (!validBaseConfig || !validOpsConfig) {
428
+ res.status(400).json({ err: "Invalid Security Configuration" });
429
+ return;
430
+ }
431
+ if (req.body.unique && ((_h = req.body.data) === null || _h === void 0 ? void 0 : _h[req.body.unique])) {
432
+ const isDataExists = yield mongoCollection.findOne({
433
+ [req.body.unique]: req.body.data[req.body.unique],
434
+ });
435
+ if (isDataExists && req.body.id !== isDataExists._id) {
436
+ res
437
+ .status(400)
438
+ .json({ err: `${req.body.data[req.body.unique]} exists` });
439
+ return;
440
+ }
441
+ }
442
+ const { updatedFields } = (0, utils_1.compareUpdatedFields)(mongoData, req.body.data);
443
+ const resBody = { success: true };
444
+ if (workflow)
445
+ yield workflow(updatedFields);
446
+ const data = this.parseModel(updatedFields);
447
+ if (req.body.sensitive) {
448
+ if ((0, moment_timezone_1.default)(req.body.data.baseUpdatedAt).isAfter((0, moment_timezone_1.default)(mongoData.baseUpdatedAt))) {
449
+ yield mongoCollection.updateOne({
450
+ _id: valid,
451
+ }, {
452
+ $set: Object.assign({}, data),
453
+ });
454
+ if ((_j = req.body) === null || _j === void 0 ? void 0 : _j.stopRes)
455
+ return resBody;
456
+ res.send(resBody);
457
+ }
458
+ else {
459
+ res.status(400).json({ err: "Refresh Sensitive Model" });
460
+ }
461
+ }
462
+ else {
463
+ yield mongoCollection.updateOne({
464
+ _id: valid,
465
+ }, {
466
+ $set: Object.assign({}, data),
467
+ });
468
+ if ((_k = req.body) === null || _k === void 0 ? void 0 : _k.stopRes)
469
+ return resBody;
470
+ res.send(resBody);
471
+ }
472
+ }
473
+ else {
474
+ res.status(400).json({ err: "Invalid Model Structure" });
475
+ }
476
+ }
477
+ catch (err) {
478
+ if (this.debug)
479
+ this.debug(null, null, err);
480
+ res.status(400).json({ err: err });
481
+ }
482
+ });
483
+ this.initBatchDelete = (req, res, workflow) => __awaiter(this, void 0, void 0, function* () {
484
+ var _l, _m;
485
+ try {
486
+ if (Array.isArray(req.body.id)) {
487
+ const mongoCollection = this.mongoDB
488
+ .db(this.tableId)
489
+ .collection(this.collection);
490
+ const mongoQuery = yield mongoCollection.find({
491
+ _id: { $in: req.body.id },
492
+ });
493
+ const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
494
+ const dataList = yield mongoQuery.toArray();
495
+ //INFO : Security Checker
496
+ let validOpsConfig = true;
497
+ if (dataList.length > 0) {
498
+ yield Promise.all(dataList.map((eachData) => {
499
+ if (!(0, QueryModel_1.checkOpsConfig)(this.config, "delete", req, eachData)) {
500
+ validOpsConfig = false;
501
+ }
502
+ }));
503
+ }
504
+ if (!validBaseConfig || !validOpsConfig) {
505
+ res.status(400).json({ err: "Invalid Security Configuration" });
506
+ return;
507
+ }
508
+ if (workflow)
509
+ yield workflow(dataList);
510
+ yield mongoCollection.deleteMany({
511
+ _id: { $in: req.body.id },
512
+ });
513
+ const resBody = { success: req.body.id };
514
+ if ((_l = req.body) === null || _l === void 0 ? void 0 : _l.stopRes)
515
+ return resBody;
516
+ res.send(resBody);
517
+ }
518
+ else {
519
+ const queryList = req.body.query;
520
+ const queryFilter = {};
521
+ if (queryList && queryList.length > 0) {
522
+ queryList.map((eachQuery) => {
523
+ var _a;
524
+ const queryId = (_a = eachQuery.queryId) !== null && _a !== void 0 ? _a : "";
525
+ const value = eachQuery.value;
526
+ switch (eachQuery.type) {
527
+ case "search":
528
+ if (eachQuery.searchIds && eachQuery.searchIds.length > 0) {
529
+ queryFilter["$or"] = [];
530
+ eachQuery.searchIds.map((eachSearchId) => {
531
+ queryFilter["$or"].push({
532
+ [eachSearchId]: { $regex: value, $options: "i" },
533
+ });
534
+ });
535
+ }
536
+ break;
537
+ case "=":
538
+ queryFilter[queryId] = value;
539
+ break;
540
+ case "!=":
541
+ queryFilter[queryId] = {
542
+ $ne: value,
543
+ };
544
+ break;
545
+ case ">":
546
+ queryFilter[queryId] = {
547
+ $gt: value,
548
+ };
549
+ break;
550
+ case "<":
551
+ queryFilter[queryId] = {
552
+ $lt: value,
553
+ };
554
+ break;
555
+ case "><":
556
+ if (Array.isArray(value) && value.length >= 2) {
557
+ queryFilter[queryId] = {
558
+ $gte: value[0],
559
+ $lte: value[1],
560
+ };
561
+ }
562
+ break;
563
+ case "in":
564
+ queryFilter[queryId] = {
565
+ $in: value,
566
+ };
567
+ break;
568
+ default:
569
+ break;
570
+ }
571
+ });
572
+ }
573
+ const mongoCollection = this.mongoDB
574
+ .db(this.tableId)
575
+ .collection(this.collection);
576
+ const mongoQuery = mongoCollection.find(queryFilter);
577
+ const dataList = yield mongoQuery.toArray();
578
+ const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
579
+ //INFO : Security Checker
580
+ let validOpsConfig = true;
581
+ const ids = [];
582
+ if (dataList.length > 0) {
583
+ dataList.map((eachData) => {
584
+ ids.push(eachData._id);
585
+ if (!(0, QueryModel_1.checkOpsConfig)(this.config, "delete", req, eachData)) {
586
+ validOpsConfig = false;
587
+ }
588
+ });
589
+ }
590
+ if (workflow)
591
+ yield workflow(dataList);
592
+ if (!validBaseConfig || !validOpsConfig) {
593
+ res.status(400).json({ err: "Invalid Security Configuration" });
594
+ return;
595
+ }
596
+ yield mongoCollection.deleteMany(queryFilter);
597
+ const resBody = { success: ids };
598
+ if ((_m = req.body) === null || _m === void 0 ? void 0 : _m.stopRes)
599
+ return resBody;
600
+ res.send(resBody);
601
+ }
602
+ }
603
+ catch (err) {
604
+ res.status(400).json({ err: err });
605
+ }
606
+ });
607
+ this.initDelete = (req, res, workflow) => __awaiter(this, void 0, void 0, function* () {
608
+ var _o;
609
+ try {
610
+ const valid = req.body.id;
611
+ if (valid) {
612
+ const mongoCollection = this.mongoDB
613
+ .db(this.tableId)
614
+ .collection(this.collection);
615
+ //INFO : Security Checker
616
+ const mongoData = yield mongoCollection.findOne({
617
+ _id: valid,
618
+ });
619
+ const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
620
+ const validOpsConfig = (0, QueryModel_1.checkOpsConfig)(this.config, "delete", req, mongoData);
621
+ if (!validBaseConfig || !validOpsConfig) {
622
+ res.status(400).json({ err: "Invalid Security Configuration" });
623
+ return;
624
+ }
625
+ if (workflow)
626
+ yield workflow(mongoData);
627
+ yield mongoCollection.findOneAndDelete({
628
+ _id: valid,
629
+ });
630
+ const resBody = { success: true };
631
+ if ((_o = req.body) === null || _o === void 0 ? void 0 : _o.stopRes)
632
+ return resBody;
633
+ res.send(resBody);
634
+ }
635
+ else {
636
+ res.status(400).json({ err: "Invalid Fields" });
637
+ }
638
+ }
639
+ catch (err) {
640
+ res.status(400).json({ err: err });
641
+ }
642
+ });
643
+ this.initList = (req, res) => __awaiter(this, void 0, void 0, function* () {
644
+ var _p, _q, _r, _s, _t, _u, _v, _w, _x;
645
+ try {
646
+ //INFO : Security Checker
647
+ const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
648
+ if (validBaseConfig) {
649
+ const aggregate = req.body.aggregate;
650
+ const queryList = (_p = req.body.query) !== null && _p !== void 0 ? _p : [];
651
+ let queryFilter = {};
652
+ // INFO : Step 1 - Query
653
+ queryFilter = handleParseQueryFilter(queryList);
654
+ if (aggregate) {
655
+ // INFO : Step 2 - Aggregate
656
+ const dataList = [];
657
+ const queryId = (_q = aggregate.queryId) !== null && _q !== void 0 ? _q : "";
658
+ const dateId = (_r = aggregate.dateId) !== null && _r !== void 0 ? _r : "baseUpdatedAt";
659
+ const dateRanges = (_s = aggregate.range) !== null && _s !== void 0 ? _s : [
660
+ (0, moment_timezone_1.default)().format("DD/MM/YYYY HH:mm:ss"),
661
+ ];
662
+ for (const dateRange of dateRanges) {
663
+ let matchFilter = {
664
+ $match: Object.assign({}, queryFilter),
665
+ };
666
+ const date = dateRange.split("-");
667
+ const [startDate, endDate = (0, moment_timezone_1.default)().format("DD/MM/YYYY HH:mm:ss"),] = date;
668
+ const isDuration = date.length === 2;
669
+ const isValidStartDate = (0, moment_timezone_1.default)(startDate, "DD/MM/YYYY").isValid();
670
+ const isValidEndDate = (0, moment_timezone_1.default)(endDate, "DD/MM/YYYY").isValid();
671
+ const isoStartDate = (0, moment_timezone_1.default)(startDate, "DD/MM/YYYY HH:mm:ss")
672
+ .tz("Asia/Kuala_lumpur")
673
+ .toDate();
674
+ const isoEndDate = (0, moment_timezone_1.default)(endDate, "DD/MM/YYYY HH:mm:ss")
675
+ .tz("Asia/Kuala_lumpur")
676
+ .toDate();
677
+ if (!isValidStartDate || !isValidEndDate) {
678
+ res
679
+ .status(400)
680
+ .json({ err: "Invalid Aggregate Range Configuration" });
681
+ return;
682
+ }
683
+ matchFilter = isDuration
684
+ ? {
685
+ $match: Object.assign(Object.assign({}, queryFilter), { [dateId]: {
686
+ $gte: isoStartDate,
687
+ $lte: isoEndDate,
688
+ } }),
689
+ }
690
+ : {
691
+ $match: Object.assign(Object.assign({}, queryFilter), { [dateId]: {
692
+ $lte: isoStartDate,
693
+ } }),
694
+ };
695
+ const aggregationPipeline = [matchFilter];
696
+ const id = isDuration
697
+ ? dateRange
698
+ : `Begining until ${(0, moment_timezone_1.default)(startDate, "DD/MM/YYYY HH:mm:ss").format("DD/MM/YYYY")}`;
699
+ //INFO : Check for Unwinding
700
+ let unwindUnique = false;
701
+ if (aggregate.unwind && aggregate.unwind.length > 0) {
702
+ aggregate.unwind.map((eachUnwind, index) => {
703
+ var _a, _b, _c;
704
+ const tempModel = "model" + index;
705
+ let referenceModel = "";
706
+ if (index > 0)
707
+ referenceModel = "model" + (index - 1) + ".";
708
+ const modelQueryFilter = handleParseQueryFilter((_a = eachUnwind.query) !== null && _a !== void 0 ? _a : [], tempModel);
709
+ if ((_b = eachUnwind.unique) === null || _b === void 0 ? void 0 : _b.list)
710
+ aggregationPipeline.push({
711
+ $unwind: "$" + eachUnwind.localField,
712
+ });
713
+ aggregationPipeline.push({
714
+ $lookup: {
715
+ from: eachUnwind.collectionId,
716
+ localField: referenceModel + eachUnwind.localField,
717
+ foreignField: "_id",
718
+ as: tempModel,
719
+ },
720
+ }, {
721
+ $unwind: "$" + tempModel,
722
+ }, {
723
+ $match: modelQueryFilter,
724
+ });
725
+ if ((_c = eachUnwind.unique) === null || _c === void 0 ? void 0 : _c.type) {
726
+ unwindUnique = true;
727
+ const uniqueUnwindName = "uniqueUnwindId";
728
+ aggregationPipeline.push({
729
+ $group: {
730
+ _id: id,
731
+ [uniqueUnwindName]: {
732
+ $addToSet: "$" + eachUnwind.localField,
733
+ },
734
+ },
735
+ });
736
+ if (eachUnwind.unique.type === "count")
737
+ aggregationPipeline.push({
738
+ $project: {
739
+ value: { $size: "$" + uniqueUnwindName },
740
+ },
741
+ });
742
+ else
743
+ aggregationPipeline.push({
744
+ $project: { _id: 0, value: "$" + uniqueUnwindName },
745
+ });
746
+ }
747
+ });
748
+ }
749
+ if (!unwindUnique) {
750
+ let groupId = id;
751
+ if (aggregate.groupId)
752
+ groupId = "$" + aggregate.groupId;
753
+ switch (aggregate.type) {
754
+ case "avg":
755
+ aggregationPipeline.push({
756
+ $group: {
757
+ _id: groupId,
758
+ value: { $avg: `$${queryId}` },
759
+ },
760
+ });
761
+ break;
762
+ case "sum":
763
+ aggregationPipeline.push({
764
+ $group: {
765
+ _id: groupId,
766
+ value: { $sum: `$${queryId}` },
767
+ },
768
+ });
769
+ break;
770
+ case "count":
771
+ aggregationPipeline.push({
772
+ $group: {
773
+ _id: groupId,
774
+ value: { $sum: 1 },
775
+ },
776
+ });
777
+ break;
778
+ default:
779
+ break;
780
+ }
781
+ }
782
+ if (aggregationPipeline.length === 0) {
783
+ res.status(400).json({ err: "Invalid Aggregate Configuration" });
784
+ return;
785
+ }
786
+ const mongoCollection = this.mongoDB
787
+ .db(this.tableId)
788
+ .collection(this.collection);
789
+ let value = 0;
790
+ const mongoQuery = mongoCollection.aggregate(aggregationPipeline);
791
+ const result = yield mongoQuery.toArray();
792
+ if (aggregate.groupId)
793
+ value = result;
794
+ else
795
+ value = (_u = (_t = result[0]) === null || _t === void 0 ? void 0 : _t.value) !== null && _u !== void 0 ? _u : 0;
796
+ dataList.push({
797
+ _id: id,
798
+ value,
799
+ });
800
+ }
801
+ const resBody = {
802
+ data: dataList,
803
+ };
804
+ if ((_v = req.body) === null || _v === void 0 ? void 0 : _v.stopRes)
805
+ return resBody;
806
+ res.send(resBody);
807
+ }
808
+ else {
809
+ const mongoCollection = this.mongoDB
810
+ .db(this.tableId)
811
+ .collection(this.collection);
812
+ let mongoQuery = mongoCollection.find(queryFilter);
813
+ const count = yield mongoQuery.count({});
814
+ //INFO : Step 2 - Pagination
815
+ if (req.body.cursor) {
816
+ mongoQuery = mongoQuery.skip(req.body.cursor);
817
+ }
818
+ //INFO : Step 3 - Limit
819
+ if (!req.body.stopLimit) {
820
+ mongoQuery = mongoQuery.limit((_w = req.body.limit) !== null && _w !== void 0 ? _w : 10);
821
+ }
822
+ //INFO : Step 4 - Sort
823
+ if (req.body.sort) {
824
+ const sortReq = req.body.sort;
825
+ const sortQuery = {
826
+ [sortReq.sortId]: sortReq.type === "asc" ? 1 : -1,
827
+ };
828
+ mongoQuery = mongoQuery.sort(sortQuery);
829
+ }
830
+ const dataList = yield mongoQuery.toArray();
831
+ //INFO : Security Checker
832
+ let validOpsConfig = true;
833
+ if (dataList.length > 0) {
834
+ dataList.map((eachData) => {
835
+ if (!(0, QueryModel_1.checkOpsConfig)(this.config, "read", req, eachData)) {
836
+ validOpsConfig = false;
837
+ }
838
+ });
839
+ }
840
+ if (!validBaseConfig || !validOpsConfig) {
841
+ res.status(400).json({ err: "Invalid Security Configuration" });
842
+ return;
843
+ }
844
+ const resBody = {
845
+ data: dataList,
846
+ count,
847
+ };
848
+ if ((_x = req.body) === null || _x === void 0 ? void 0 : _x.stopRes)
849
+ return resBody;
850
+ res.send(resBody);
851
+ }
852
+ }
853
+ else {
854
+ res.status(400).json({ err: "Invalid Security Configuration" });
855
+ }
856
+ }
857
+ catch (err) {
858
+ res.status(400).json({ err: err });
859
+ }
860
+ });
861
+ this.init = () => {
862
+ const router = this.lib.express.Router();
863
+ /**
864
+ * @Input :
865
+ * data - Model
866
+ * unique - string (Optional)
867
+ */
868
+ router.post("/create", (req, res) => {
869
+ this.initCreate(req, res);
870
+ });
871
+ /**
872
+ * @Input :
873
+ * data - Model
874
+ * unique - string (Optional)
875
+ */
876
+ router.post("/batchCreate", (req, res) => {
877
+ this.initBatchCreate(req, res);
878
+ });
879
+ /**
880
+ * @Input :
881
+ * id - string
882
+ */
883
+ router.post("/get", (req, res) => {
884
+ this.initGet(req, res);
885
+ });
886
+ /**
887
+ * @Input :
888
+ * data - Model
889
+ * id - string
890
+ * unique - string (Optional)
891
+ */
892
+ router.post("/update", (req, res) => {
893
+ this.initUpdate(req, res);
894
+ });
895
+ /**
896
+ * @Input :
897
+ * data - Model
898
+ * query - Query[]
899
+ * unsetData - string[] (Unset Attribute Id) (Optional)
900
+ * isOr - boolean (Optional)
901
+ */
902
+ router.post("/batchUpdate", (req, res) => {
903
+ this.initBatchUpdate(req, res);
904
+ });
905
+ /**
906
+ * @Input :
907
+ * id - string
908
+ */
909
+ router.post("/delete", (req, res) => {
910
+ this.initDelete(req, res);
911
+ });
912
+ /**
913
+ * @Input :
914
+ * id - string[]
915
+ */
916
+ router.post("/batchDelete", (req, res) => {
917
+ this.initBatchDelete(req, res);
918
+ });
919
+ /**
920
+ * @Input
921
+ * query - Query interface (Optional)
922
+ * cursor - any (Optional)
923
+ * limit - number (Optional)
924
+ * sort - Sort interface (Optional)
925
+ * stopLimit - boolean to retreive all data (Optional)
926
+ */
927
+ router.post("/list", (req, res) => {
928
+ this.initList(req, res);
929
+ });
930
+ return router;
931
+ };
932
+ this.mongoDB = mongoDB;
933
+ this.collection = collection;
934
+ this.isProd = isProd;
935
+ this.config = config;
936
+ this.modelChecker = modelChecker;
937
+ this.lib = lib;
938
+ const tableSuffix = this.isProd ? "prod" : "dev";
939
+ this.tableId = tableId ? `${tableId}-${tableSuffix}` : tableSuffix;
940
+ this.debug = debug;
941
+ }
942
+ }
943
+ exports.MongoWrapper = MongoWrapper;