blixify-server 0.1.39 → 0.1.41
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 @@
|
|
|
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");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 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=(p,m,v)=>__awaiter(this,void 0,void 0,function*(){var e,i,s;try{if(this.modelChecker(p.body.data)&&p.body.query){var o=p.body.unsetData,a=null!=(e=p.body.query)?e:[];const y=p.body.isOr?"$or":"$and",f={};var n=this.parseModel(null!=(i=p.body.data)?i:[]),r=(a&&0<a.length&&(f[y]=[],a.some(e=>"search"===e.type)&&(f.$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=>{f.$or.push({[e]:{$regex:s,$options:"i"}})});break;case"=":f[y].push({[i]:s});break;case"!=":f[y].push({[i]:{$ne:s}});break;case">":f[y].push({[i]:{$gt:s}});break;case"<":f[y].push({[i]:{$lt:s}});break;case"><":Array.isArray(s)&&2<=s.length&&(f[y].push({[i]:{$lt:s}}),f[y]={[i]:{$gte:s[0],$lte:s[1]}});break;case"in":f[y].push({[i]:{$in:s}})}})),(0,QueryModel_1.checkBaseConfig)(this.config,p));let t=!0;var d=this.mongoDB.db(this.tableId).collection(this.collection),l=yield(yield d.find(Object.assign({},f))).toArray();const b=[];if(yield Promise.all(l.map(e=>{p.body.sensitive&&!(0,moment_timezone_1.default)(p.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",p,e)||(t=!1);e=(0,utils_1.compareUpdatedFields)(e,p.body.data).workflowUpdateFields;b.concat(e)})),r&&t){const g={};var u={},c=(o&&(o.forEach(e=>{g[e]=""}),u.$unset=Object.assign({},g)),n&&(u.$set=Object.assign({},n)),v&&(yield v(b)),yield d.updateMany(f,u),b.map(e=>e._id)),h={success:c};if(null!=(s=p.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=(y,f,b)=>__awaiter(this,void 0,void 0,function*(){var e,t,i;try{var s=this.modelChecker(y.body.data)&&y.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(y.body,a),(0,QueryModel_1.checkBaseConfig)(this.config,y)),r=(0,QueryModel_1.checkOpsConfig)(this.config,"update",y,a);if(n&&r){if(y.body.unique&&null!=(e=y.body.data)&&e[y.body.unique]){var d=yield o.findOne({[y.body.unique]:y.body.data[y.body.unique]});if(d&&y.body.id!==d._id)return void f.status(400).json({err:y.body.data[y.body.unique]+" exists"})}var{workflowUpdateFields:l,updatedFields:u}=(0,utils_1.compareUpdatedFields)(a,y.body.data),c={success:!0},h=(b&&(yield b(l)),this.parseModel(u));if(y.body.sensitive)if((0,moment_timezone_1.default)(y.body.data.baseUpdatedAt).isAfter((0,moment_timezone_1.default)(a.baseUpdatedAt))){if(yield o.updateOne({_id:s},{$set:Object.assign({},h)}),null!=(t=y.body)&&t.stopRes)return c;f.send(c)}else f.status(400).json({err:"Refresh Sensitive Model"});else{if(yield o.updateOne({_id:s},{$set:Object.assign({},h)}),null!=(i=y.body)&&i.stopRes)return c;f.send(c)}}else f.status(400).json({err:"Invalid Security Configuration"})}else f.status(400).json({err:"Invalid Model Structure"})}catch(e){this.debug&&this.debug(null,null,e),f.status(400).json({err:e})}}),this.initBatchDelete=(b,g,p)=>__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){p&&(yield p(n)),yield s.deleteMany({_id:{$in:b.body.id}});var r={success:b.body.id};if(null!=(e=b.body)&&e.stopRes)return r;g.send(r)}else g.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 f=[];if(0<u.length&&u.map(e=>{f.push(e._id),(0,QueryModel_1.checkOpsConfig)(this.config,"delete",b,e)||(t=!1)}),p&&(yield p(u)),c&&t){yield l.deleteMany(y);var h={success:f};if(null!=(i=b.body)&&i.stopRes)return h;g.send(h)}else g.status(400).json({err:"Invalid Security Configuration"})}}catch(e){g.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,a,n,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:[];const w={};if(c&&0<c.length&&c.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&&(w.$or=[],e.searchIds.map(e=>{w.$or.push({[e]:{$regex:s,$options:"i"}})}));break;case"=":w[i]=s;break;case"!=":w[i]={$ne:s};break;case">":w[i]={$gt:s};break;case"<":w[i]={$lt:s};break;case"><":Array.isArray(s)&&2<=s.length&&(w[i]={$gte:s[0],$lte:s[1]});break;case"in":w[i]={$in:s}}}),u){var h=[],y=null!=(t=u.queryId)?t:"",f=null!=(i=u.dateId)?i:"baseUpdatedAt";for(const Q of null!=(s=u.range)?s:[]){Object.assign({},w);var b=Q.split("-"),[g,p=(0,moment_timezone_1.default)().format("DD/MM/YYYY HH:mm:ss")]=b,m=2===b.length,v=(0,moment_timezone_1.default)(g,"DD/MM/YYYY").isValid(),_=(0,moment_timezone_1.default)(p,"DD/MM/YYYY").isValid(),M=(0,moment_timezone_1.default)(g,"DD/MM/YYYY HH:mm:ss").tz("Asia/Kuala_lumpur").format("YYYY-MM-DD HH:mm:ss"),$=(0,moment_timezone_1.default)(p,"DD/MM/YYYY HH:mm:ss").tz("Asia/Kuala_lumpur").format("YYYY-MM-DD HH:mm:ss");if(!v||!_)return void z.status(400).json({err:"Invalid Aggregate Range Configuration"});var k=m?{$match:Object.assign(Object.assign({},w),{[f]:{$gte:new Date(M),$lte:new Date($)}})}:{$match:Object.assign(Object.assign({},w),{[f]:{$lte:new Date(M)}})},D=[];let e=!1;var I=m?Q:"Begining until "+(0,moment_timezone_1.default)(g,"DD/MM/YYYY HH:mm:ss").format("DD/MM/YYYY");switch(u.type){case"avg":D.push(k,{$group:{_id:I,value:{$avg:"$"+y}}});break;case"sum":D.push(k,{$group:{_id:I,value:{$sum:"$"+y}}});break;case"count":e=!0}if(0===D.length&&!e)return void z.status(400).json({err:"Invalid Aggregate Configuration"});var j=this.mongoDB.db(this.tableId).collection(this.collection);let t=0;t=e?yield j.find(k.$match).count({}):null!=(a=null==(o=(yield j.aggregate(D).toArray())[0])?void 0:o.value)?a:0,h.push({_id:I,value:t})}var C={data:h};if(null!=(n=U.body)&&n.stopRes)return C;z.send(C)}else{let e=this.mongoDB.db(this.tableId).collection(this.collection).find(w);var Y,O,A,q=yield e.count({}),B=(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&&(O={[(Y=U.body.sort).sortId]:"asc"===Y.type?1:-1},e=e.sort(O)),yield e.toArray());let t=!0;if(0<B.length&&B.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"read",U,e)||(t=!1)}),l&&t)return A={data:B,count:q},null!=(d=U.body)&&d.stopRes?A:void z.send(A);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";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");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 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=(p,m,v)=>__awaiter(this,void 0,void 0,function*(){var e,i,s;try{if(this.modelChecker(p.body.data)&&p.body.query){var o=p.body.unsetData,a=null!=(e=p.body.query)?e:[];const y=p.body.isOr?"$or":"$and",b={};var n=this.parseModel(null!=(i=p.body.data)?i:[]),r=(a&&0<a.length&&(b[y]=[],a.some(e=>"search"===e.type)&&(b.$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=>{b.$or.push({[e]:{$regex:s,$options:"i"}})});break;case"=":b[y].push({[i]:s});break;case"!=":b[y].push({[i]:{$ne:s}});break;case">":b[y].push({[i]:{$gt:s}});break;case"<":b[y].push({[i]:{$lt:s}});break;case"><":Array.isArray(s)&&2<=s.length&&(b[y].push({[i]:{$lt:s}}),b[y]={[i]:{$gte:s[0],$lte:s[1]}});break;case"in":b[y].push({[i]:{$in:s}})}})),(0,QueryModel_1.checkBaseConfig)(this.config,p));let t=!0;var d=this.mongoDB.db(this.tableId).collection(this.collection),l=yield(yield d.find(Object.assign({},b))).toArray();const f=[];if(yield Promise.all(l.map(e=>{p.body.sensitive&&!(0,moment_timezone_1.default)(p.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",p,e)||(t=!1);e=(0,utils_1.compareUpdatedFields)(e,p.body.data).workflowUpdateFields;f.concat(e)})),r&&t){const g={};var u={},c=(o&&(o.forEach(e=>{g[e]=""}),u.$unset=Object.assign({},g)),n&&(u.$set=Object.assign({},n)),v&&(yield v(f)),yield d.updateMany(b,u),f.map(e=>e._id)),h={success:c};if(null!=(s=p.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=(y,b,f)=>__awaiter(this,void 0,void 0,function*(){var e,t,i;try{var s=this.modelChecker(y.body.data)&&y.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(y.body,a),(0,QueryModel_1.checkBaseConfig)(this.config,y)),r=(0,QueryModel_1.checkOpsConfig)(this.config,"update",y,a);if(n&&r){if(y.body.unique&&null!=(e=y.body.data)&&e[y.body.unique]){var d=yield o.findOne({[y.body.unique]:y.body.data[y.body.unique]});if(d&&y.body.id!==d._id)return void b.status(400).json({err:y.body.data[y.body.unique]+" exists"})}var{workflowUpdateFields:l,updatedFields:u}=(0,utils_1.compareUpdatedFields)(a,y.body.data),c={success:!0},h=(f&&(yield f(l)),this.parseModel(u));if(y.body.sensitive)if((0,moment_timezone_1.default)(y.body.data.baseUpdatedAt).isAfter((0,moment_timezone_1.default)(a.baseUpdatedAt))){if(yield o.updateOne({_id:s},{$set:Object.assign({},h)}),null!=(t=y.body)&&t.stopRes)return c;b.send(c)}else b.status(400).json({err:"Refresh Sensitive Model"});else{if(yield o.updateOne({_id:s},{$set:Object.assign({},h)}),null!=(i=y.body)&&i.stopRes)return c;b.send(c)}}else b.status(400).json({err:"Invalid Security Configuration"})}else b.status(400).json({err:"Invalid Model Structure"})}catch(e){this.debug&&this.debug(null,null,e),b.status(400).json({err:e})}}),this.initBatchDelete=(f,g,p)=>__awaiter(this,void 0,void 0,function*(){var e,i;try{if(Array.isArray(f.body.id)){var s=this.mongoDB.db(this.tableId).collection(this.collection),o=yield s.find({_id:{$in:f.body.id}}),a=(0,QueryModel_1.checkBaseConfig)(this.config,f),n=yield o.toArray();let t=!0;if(0<n.length&&(yield Promise.all(n.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"delete",f,e)||(t=!1)}))),a&&t){p&&(yield p(n)),yield s.deleteMany({_id:{$in:f.body.id}});var r={success:f.body.id};if(null!=(e=f.body)&&e.stopRes)return r;g.send(r)}else g.status(400).json({err:"Invalid Security Configuration"})}else{var d=f.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,f);let t=!0;const b=[];if(0<u.length&&u.map(e=>{b.push(e._id),(0,QueryModel_1.checkOpsConfig)(this.config,"delete",f,e)||(t=!1)}),p&&(yield p(u)),c&&t){yield l.deleteMany(y);var h={success:b};if(null!=(i=f.body)&&i.stopRes)return h;g.send(h)}else g.status(400).json({err:"Invalid Security Configuration"})}}catch(e){g.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,a,n,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:[];const w={};if(c&&0<c.length&&c.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&&(w.$or=[],e.searchIds.map(e=>{w.$or.push({[e]:{$regex:s,$options:"i"}})}));break;case"=":w[i]=s;break;case"!=":w[i]={$ne:s};break;case">":w[i]={$gt:s};break;case"<":w[i]={$lt:s};break;case"><":Array.isArray(s)&&2<=s.length&&(w[i]={$gte:s[0],$lte:s[1]});break;case"in":w[i]={$in:s}}}),u){var h=[],y=null!=(t=u.queryId)?t:"",b=null!=(i=u.dateId)?i:"baseUpdatedAt";for(const Q of null!=(s=u.range)?s:[]){Object.assign({},w);var f=Q.split("-"),[g,p=(0,moment_timezone_1.default)().format("DD/MM/YYYY HH:mm:ss")]=f,m=2===f.length,v=(0,moment_timezone_1.default)(g,"DD/MM/YYYY").isValid(),_=(0,moment_timezone_1.default)(p,"DD/MM/YYYY").isValid(),$=(0,moment_timezone_1.default)(g,"DD/MM/YYYY HH:mm:ss").tz("Asia/Kuala_lumpur").toDate(),M=(0,moment_timezone_1.default)(p,"DD/MM/YYYY HH:mm:ss").tz("Asia/Kuala_lumpur").toDate();if(!v||!_)return void z.status(400).json({err:"Invalid Aggregate Range Configuration"});var k=m?{$match:Object.assign(Object.assign({},w),{[b]:{$gte:$,$lte:M}})}:{$match:Object.assign(Object.assign({},w),{[b]:{$lte:$}})},I=[];let e=!1;var j=m?Q:"Begining until "+(0,moment_timezone_1.default)(g,"DD/MM/YYYY HH:mm:ss").format("DD/MM/YYYY");switch(u.type){case"avg":I.push(k,{$group:{_id:j,value:{$avg:"$"+y}}});break;case"sum":I.push(k,{$group:{_id:j,value:{$sum:"$"+y}}});break;case"count":e=!0}if(0===I.length&&!e)return void z.status(400).json({err:"Invalid Aggregate Configuration"});var C=this.mongoDB.db(this.tableId).collection(this.collection);let t=0;t=e?yield C.find(k.$match).count({}):null!=(a=null==(o=(yield C.aggregate(I).toArray())[0])?void 0:o.value)?a:0,h.push({_id:j,value:t})}var D={data:h};if(null!=(n=U.body)&&n.stopRes)return D;z.send(D)}else{let e=this.mongoDB.db(this.tableId).collection(this.collection).find(w);var O,A,q,B=yield e.count({}),Y=(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&&(A={[(O=U.body.sort).sortId]:"asc"===O.type?1:-1},e=e.sort(A)),yield e.toArray());let t=!0;if(0<Y.length&&Y.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"read",U,e)||(t=!1)}),l&&t)return q={data:Y,count:B},null!=(d=U.body)&&d.stopRes?q:void z.send(q);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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"QueryModel.d.ts","sourceRoot":"","sources":["../../src/model/QueryModel.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAE9C;;;;;;;;;;GAUG;AACH,MAAM,WAAW,KAAK;IACpB,IAAI,EAAE,QAAQ,GAAG,GAAG,GAAG,IAAI,GAAG,GAAG,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,CAAC;IACtD,KAAK,EAAE,GAAG,CAAC;IACX,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB;AAED;;;;;;GAMG;AACH,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,OAAO,GAAG,KAAK,GAAG,KAAK,GAAG,OAAO,CAAC;IACxC,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;CAClB;AAED,MAAM,WAAW,IAAI;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,KAAK,GAAG,MAAM,CAAC;CACtB;AAED;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,WAAY,cAAc,OAAO,GAAG,YAmC/D,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,WACjB,cAAc,QAChB,MAAM,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,OACxC,GAAG,OACH,GAAG,
|
|
1
|
+
{"version":3,"file":"QueryModel.d.ts","sourceRoot":"","sources":["../../src/model/QueryModel.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAE9C;;;;;;;;;;GAUG;AACH,MAAM,WAAW,KAAK;IACpB,IAAI,EAAE,QAAQ,GAAG,GAAG,GAAG,IAAI,GAAG,GAAG,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,CAAC;IACtD,KAAK,EAAE,GAAG,CAAC;IACX,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB;AAED;;;;;;GAMG;AACH,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,OAAO,GAAG,KAAK,GAAG,KAAK,GAAG,OAAO,CAAC;IACxC,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;CAClB;AAED,MAAM,WAAW,IAAI;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,KAAK,GAAG,MAAM,CAAC;CACtB;AAED;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,WAAY,cAAc,OAAO,GAAG,YAmC/D,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,WACjB,cAAc,QAChB,MAAM,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,OACxC,GAAG,OACH,GAAG,YAyDT,CAAC"}
|
package/dist/model/QueryModel.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.checkOpsConfig=exports.checkBaseConfig=void 0;const checkBaseConfig=(e,r)=>{let
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.checkOpsConfig=exports.checkBaseConfig=void 0;const checkBaseConfig=(e,r)=>{let a=!0;return 0<e.baseConfig.length&&e.baseConfig.map(e=>{switch(e.type){case"domain":r.headers.referer||(a=!1);break;case"android":r.headers["x-android-package"]||(a=!1);break;case"androidSHA":r.headers["x-android-cert"]||(a=!1);break;case"ios":r.headers["x-ios-bundle-identifier"]||(a=!1);break;case"api":r.body.bm_apiToken||(a=!1);break;case"auth":r.body.bm_userId||(a=!1);break;case"role":r.body.bm_userRole!==e.value&&(a=!1)}}),a},checkOpsConfig=(exports.checkBaseConfig=checkBaseConfig,(e,r,a,s)=>{let o=!1,i;return 0<(i="read"===r?e.opsConfig.read:"create"===r?e.opsConfig.create:"update"===r?e.opsConfig.update:e.opsConfig.delete).length?i.map(e=>{switch(e.type){case"auth":a.body.bm_userId&&(o=!0);break;case"doc":if(a.body.bm_userId){const r=JSON.stringify(a.body.bm_userId);Array.isArray(e.value)?e.value.map(e=>{r===JSON.stringify(s[e])&&(o=!0)}):r===JSON.stringify(s[e.value])&&(o=!0)}break;case"org":a.body.bm_userOrg&&!Array.isArray(e.value)&&JSON.stringify(a.body.bm_userOrg)===JSON.stringify(s[e.value])&&(o=!0);break;case"role":a.body.bm_userRole&&a.body.bm_userRole===e.value&&(o=!0)}}):o=!0,o});exports.checkOpsConfig=checkOpsConfig;
|
|
@@ -6,10 +6,11 @@
|
|
|
6
6
|
* api - bm_apiToken is true
|
|
7
7
|
* doc - class.value equals to userId
|
|
8
8
|
*
|
|
9
|
+
* value - string[] is only used for doc
|
|
9
10
|
*/
|
|
10
11
|
interface Config {
|
|
11
12
|
type: "auth" | "org" | "role" | "api" | "doc" | "domain" | "ios" | "android" | "androidSHA";
|
|
12
|
-
value: string;
|
|
13
|
+
value: string | string[];
|
|
13
14
|
}
|
|
14
15
|
/**
|
|
15
16
|
* @baseConfig - AND Condition
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SecurityConfig.d.ts","sourceRoot":"","sources":["../../src/model/SecurityConfig.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"SecurityConfig.d.ts","sourceRoot":"","sources":["../../src/model/SecurityConfig.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,UAAU,MAAM;IACd,IAAI,EACA,MAAM,GACN,KAAK,GACL,MAAM,GACN,KAAK,GACL,KAAK,GACL,QAAQ,GACR,KAAK,GACL,SAAS,GACT,YAAY,CAAC;IACjB,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;CAC1B;AAED;;;GAGG;AACH,MAAM,CAAC,OAAO,WAAW,cAAc;IACrC,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,SAAS,EAAE;QACT,IAAI,EAAE,MAAM,EAAE,CAAC;QACf,MAAM,EAAE,MAAM,EAAE,CAAC;QACjB,MAAM,EAAE,MAAM,EAAE,CAAC;QACjB,MAAM,EAAE,MAAM,EAAE,CAAC;KAClB,CAAC;CACH"}
|