blixify-server 0.1.18 → 0.1.20

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.
@@ -17,13 +17,13 @@ export declare class MongoWrapper {
17
17
  tableId: string;
18
18
  constructor(mongoDB: any, collection: string, isProd: boolean, config: SecurityConfig, modelChecker: (obj: any, ignore?: boolean) => boolean, lib: WrapperLib, tableId?: string);
19
19
  parseModel: (data: any) => any;
20
- initBatchCreate: (req: any, res: any) => Promise<void>;
21
- initCreate: (req: any, res: any) => Promise<void>;
20
+ initBatchCreate: (req: any, res: any, workflow?: ((dataList: any[]) => Promise<any>) | undefined) => Promise<void>;
21
+ initCreate: (req: any, res: any, workflow?: ((data: any) => Promise<any>) | undefined) => Promise<void>;
22
22
  initGet: (req: any, res: any) => Promise<void>;
23
23
  initBatchUpdate: (req: any, res: any) => Promise<void>;
24
24
  initUpdate: (req: any, res: any) => Promise<void>;
25
- initBatchDelete: (req: any, res: any) => Promise<void>;
26
- initDelete: (req: any, res: any) => Promise<void>;
25
+ initBatchDelete: (req: any, res: any, workflow?: ((dataList: any[]) => Promise<any>) | undefined) => Promise<void>;
26
+ initDelete: (req: any, res: any, workflow?: ((data: any) => Promise<any>) | undefined) => Promise<void>;
27
27
  initList: (req: any, res: any) => Promise<void>;
28
28
  init: () => any;
29
29
  }
@@ -1 +1 @@
1
- {"version":3,"file":"mongoWrapper.d.ts","sourceRoot":"","sources":["../../src/apis/mongoWrapper.ts"],"names":[],"mappings":"AAQA,OAAO,cAAc,MAAM,yBAAyB,CAAC;AAErD,MAAM,WAAW,UAAU;IACzB,OAAO,CAAC,EAAE,GAAG,CAAC;CACf;AAED;;;;GAIG;AACH,qBAAa,YAAY;IACvB,OAAO,EAAE,GAAG,CAAM;IAClB,UAAU,SAAM;IAChB,MAAM,UAAS;IACf,MAAM,EAAE,cAAc,CAQpB;IACF,GAAG,EAAE,UAAU,CAAC;IAEhB,YAAY,QAAS,GAAG,aAEtB;IACF,OAAO,SAAM;gBAGX,OAAO,EAAE,GAAG,EACZ,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,OAAO,CAAC,EAAE,MAAM;IAYlB,UAAU,SAAU,GAAG,SAIrB;IAEF,eAAe,QAAe,GAAG,OAAO,GAAG,mBA+CzC;IAEF,UAAU,QAAe,GAAG,OAAO,GAAG,mBA6BpC;IAEF,OAAO,QAAe,GAAG,OAAO,GAAG,mBA8BjC;IAEF,eAAe,QAAe,GAAG,OAAO,GAAG,mBAuEzC;IAEF,UAAU,QAAe,GAAG,OAAO,GAAG,mBA4DpC;IAEF,eAAe,QAAe,GAAG,OAAO,GAAG,mBAoHzC;IAEF,UAAU,QAAe,GAAG,OAAO,GAAG,mBAkCpC;IAEF,QAAQ,QAAe,GAAG,OAAO,GAAG,mBAsOlC;IAEF,IAAI,YA0DF;CACH"}
1
+ {"version":3,"file":"mongoWrapper.d.ts","sourceRoot":"","sources":["../../src/apis/mongoWrapper.ts"],"names":[],"mappings":"AAQA,OAAO,cAAc,MAAM,yBAAyB,CAAC;AAErD,MAAM,WAAW,UAAU;IACzB,OAAO,CAAC,EAAE,GAAG,CAAC;CACf;AAED;;;;GAIG;AACH,qBAAa,YAAY;IACvB,OAAO,EAAE,GAAG,CAAM;IAClB,UAAU,SAAM;IAChB,MAAM,UAAS;IACf,MAAM,EAAE,cAAc,CAQpB;IACF,GAAG,EAAE,UAAU,CAAC;IAEhB,YAAY,QAAS,GAAG,aAEtB;IACF,OAAO,SAAM;gBAGX,OAAO,EAAE,GAAG,EACZ,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,OAAO,CAAC,EAAE,MAAM;IAYlB,UAAU,SAAU,GAAG,SAIrB;IAEF,eAAe,QACR,GAAG,OACH,GAAG,yBACc,GAAG,EAAE,KAAK,QAAQ,GAAG,CAAC,gCAiD5C;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC,gCA+BtC;IAEF,OAAO,QAAe,GAAG,OAAO,GAAG,mBA8BjC;IAEF,eAAe,QAAe,GAAG,OAAO,GAAG,mBAuEzC;IAEF,UAAU,QAAe,GAAG,OAAO,GAAG,mBA4DpC;IAEF,eAAe,QACR,GAAG,OACH,GAAG,yBACc,GAAG,EAAE,KAAK,QAAQ,GAAG,CAAC,gCAuH5C;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC,gCAmCtC;IAEF,QAAQ,QAAe,GAAG,OAAO,GAAG,mBAsOlC;IAEF,IAAI,YA0DF;CACH"}
@@ -1 +1 @@
1
- "use strict";var __awaiter=this&&this.__awaiter||function(e,n,d,r){return new(d=d||Promise)(function(i,t){function s(e){try{o(r.next(e))}catch(e){t(e)}}function a(e){try{o(r.throw(e))}catch(e){t(e)}}function o(e){var t;e.done?i(e.value):((t=e.value)instanceof d?t:new d(function(e){e(t)})).then(s,a)}o((r=r.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_1=__importDefault(require("moment")),QueryModel_1=require("../model/QueryModel");class MongoWrapper{constructor(e,t,i,s,a,o,n){this.mongoDB="",this.collection="",this.isProd=!1,this.config={baseConfig:[],opsConfig:{read:[],create:[],update:[],delete:[]}},this.modelChecker=e=>!1,this.tableId="",this.parseModel=e=>(delete e._id,e.baseUpdatedAt=(0,moment_1.default)().toDate(),e),this.initBatchCreate=(a,o)=>__awaiter(this,void 0,void 0,function*(){try{if(Array.isArray(a.body.data)){let t=!0;a.body.data.map(e=>{this.modelChecker(e)||(t=!1)}),t||o.status(400).json({err:"Invalid Model Structure"});var e,s=(0,QueryModel_1.checkBaseConfig)(this.config,a);let i=!0;yield Promise.all(a.body.data.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"update",a,e)||(i=!1)})),s&&i?(e=a.body.data.map(e=>Object.assign(Object.assign({},e),{baseUpdatedAt:(0,moment_1.default)().toDate()})),yield this.mongoDB.db(this.tableId).collection(this.collection).insertMany(e),o.send({success:!0})):o.status(400).json({err:"Invalid Security Configuration"})}else o.status(400).json({err:"Invalid Fields"})}catch(e){o.status(400).json({err:e})}}),this.initCreate=(i,s)=>__awaiter(this,void 0,void 0,function*(){try{var e,t;this.modelChecker(i.body.data)?(e=(0,QueryModel_1.checkBaseConfig)(this.config,i),t=(0,QueryModel_1.checkOpsConfig)(this.config,"create",i,i.body.data),e&&t?(i.body.data.baseUpdatedAt=(0,moment_1.default)().toDate(),yield this.mongoDB.db(this.tableId).collection(this.collection).insertOne(i.body.data),s.send({success:!0})):s.status(400).json({err:"Invalid Security Configuration"})):s.status(400).json({err:"Invalid Model Structure"})}catch(e){s.status(400).json({err:e})}}),this.initGet=(a,o)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s=a.body.id;s?(e=yield this.mongoDB.db(this.tableId).collection(this.collection).findOne({_id:s}),t=(0,QueryModel_1.checkBaseConfig)(this.config,a),i=(0,QueryModel_1.checkOpsConfig)(this.config,"read",a,e),t&&i?o.send({data:e}):o.status(400).json({err:"Invalid Security Configuration"})):o.status(400).json({err:"Invalid Fields"})}catch(e){o.status(400).json({err:e})}}),this.initBatchUpdate=(o,n)=>__awaiter(this,void 0,void 0,function*(){try{var e=this.modelChecker(o.body.data)&&o.body.id;if(Array.isArray(o.body.id)){var s=o.body.data.map(e=>Object.assign(Object.assign({},e),{baseUpdatedAt:(0,moment_1.default)().toDate()})),i=(0,QueryModel_1.checkBaseConfig)(this.config,o);let t=!0;if(yield Promise.all(o.body.data.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"update",o,e)||(t=!1)})),i&&t){const a=this.mongoDB.db(this.tableId).collection(this.collection);if(o.body.sensitive){let i=!0;yield Promise.all(o.body.id.map(t=>__awaiter(this,void 0,void 0,function*(){var e=yield a.findOne({_id:t});(0,moment_1.default)(o.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(e.baseUpdatedAt))?n.status(400).json({err:"Refresh Sensitive Model"}):i=!1}))),i&&(yield a.updateMany({_id:{$in:e}},{$set:Object.assign({},s)}),n.send({success:!0}))}else yield a.updateMany({_id:{$in:e}},{$set:Object.assign({},s)}),n.send({success:!0})}else n.status(400).json({err:"Invalid Security Configuration"})}}catch(e){}}),this.initUpdate=(d,r)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s,a,o,n=this.modelChecker(d.body.data)&&d.body.id;n?(e=(0,QueryModel_1.checkBaseConfig)(this.config,d),t=(0,QueryModel_1.checkOpsConfig)(this.config,"update",d,d.body.data),e&&t?(i=this.mongoDB.db(this.tableId).collection(this.collection),d.body.sensitive?(s=yield i.findOne({_id:n}),(0,moment_1.default)(d.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(s.baseUpdatedAt))?(a=this.parseModel(d.body.data),yield i.updateOne({_id:n},{$set:Object.assign({},a)}),r.send({success:!0})):r.status(400).json({err:"Refresh Sensitive Model"})):(o=this.parseModel(d.body.data),yield i.updateOne({_id:n},{$set:Object.assign({},o)}),r.send({success:!0}))):r.status(400).json({err:"Invalid Security Configuration"})):r.status(400).json({err:"Invalid Model Structure"})}catch(e){r.status(400).json({err:e})}}),this.initBatchDelete=(l,u)=>__awaiter(this,void 0,void 0,function*(){try{if(Array.isArray(l.body.id)){var e=this.mongoDB.db(this.tableId).collection(this.collection),i=yield e.find({_id:{$in:l.body.id}}),s=(0,QueryModel_1.checkBaseConfig)(this.config,l),a=yield i.toArray();let t=!0;0<a.length&&(yield Promise.all(a.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"delete",l,e)||(t=!1)}))),s&&t?(yield e.deleteMany({_id:{$in:l.body.id}}),u.send({success:!0})):u.status(400).json({err:"Invalid Security Configuration"})}else{var o=l.body.query;const c={};o&&0<o.length&&o.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&&(c.$or=[],e.searchIds.map(e=>{c.$or.push({[e]:{$regex:s,$options:"i"}})}));break;case"=":c[i]=s;break;case"!=":c[i]={$ne:s};break;case">":c[i]={$gt:s};break;case"<":c[i]={$lt:s};break;case"><":Array.isArray(s)&&2<=s.length&&(c[i]={$gte:s[0],$lte:s[1]});break;case"in":c[i]={$in:s}}});var n=this.mongoDB.db(this.tableId).collection(this.collection),d=yield n.find(c).toArray(),r=(0,QueryModel_1.checkBaseConfig)(this.config,l);let t=!0;0<d.length&&d.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"delete",l,e)||(t=!1)}),r&&t?(yield n.deleteMany(c),u.send({success:!0})):u.status(400).json({err:"Invalid Security Configuration"})}}catch(e){u.status(400).json({err:e})}}),this.initDelete=(o,n)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s,a=o.body.id;a?(t=yield(e=this.mongoDB.db(this.tableId).collection(this.collection)).findOne({_id:a}),i=(0,QueryModel_1.checkBaseConfig)(this.config,o),s=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",o,t),i&&s?(yield e.findOneAndDelete({_id:a}),n.send({success:!0})):n.status(400).json({err:"Invalid Security Configuration"})):n.status(400).json({err:"Invalid Fields"})}catch(e){n.status(400).json({err:e})}}),this.initList=(A,B)=>__awaiter(this,void 0,void 0,function*(){var e,t,i,s,a,o;try{var n=(0,QueryModel_1.checkBaseConfig)(this.config,A);if(n){var d=A.body.aggregate,r=null!=(e=A.body.query)?e:[];const Y={};if(r&&0<r.length&&r.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}}}),d){var c=[],l=null!=(t=d.queryId)?t:"",u=null!=(i=d.dateId)?i:"baseUpdatedAt";for(const O of null!=(s=d.range)?s:[]){Object.assign({},Y);var h=O.split("-"),[y,g=(0,moment_1.default)().format("DD/MM/YYYY HH:mm:ss")]=h,f=2===h.length,b=(0,moment_1.default)(y,"DD/MM/YYYY").isValid(),m=(0,moment_1.default)(g,"DD/MM/YYYY").isValid(),p=(0,moment_1.default)(y,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss"),v=(0,moment_1.default)(g,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss");if(!b||!m)return void B.status(400).json({err:"Invalid Aggregate Range Configuration"});var _=f?{$match:Object.assign(Object.assign({},Y),{[u]:{$gte:new Date(p),$lte:new Date(v)}})}:{$match:Object.assign(Object.assign({},Y),{[u]:{$lte:new Date(p)}})},M=[];let e=!1;var D=f?O:"Begining until "+(0,moment_1.default)(y).format("DD/MM/YYYY");switch(d.type){case"avg":M.push(_,{$group:{_id:D,value:{$avg:"$"+l}}});break;case"sum":M.push(_,{$group:{_id:D,value:{$sum:"$"+l}}});break;case"count":e=!0}if(0===M.length&&!e)return void B.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(Y).count({}):null!=(o=null==(a=(yield C.aggregate(M).toArray())[0])?void 0:a.value)?o:0,c.push({_id:D,value:t})}B.send({data:c})}else{let e=this.mongoDB.db(this.tableId).collection(this.collection).find(Y);var j,I,k=yield e.count({}),$=(A.body.cursor&&(e=e.skip(A.body.cursor)),A.body.limit&&(e=e.limit(A.body.limit)),A.body.sort&&(I={[(j=A.body.sort).sortId]:"asc"===j.type?1:-1},e=e.sort(I)),yield e.toArray());let t=!0;0<$.length&&$.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"read",A,e)||(t=!1)}),n&&t?B.send({data:$,count:k}):B.status(400).json({err:"Invalid Security Configuration"})}}else B.status(400).json({err:"Invalid Security Configuration"})}catch(e){B.status(400).json({err:e})}}),this.init=()=>{var e=this.lib.express.Router();return e.post("/create",this.initCreate),e.post("/batchCreate",this.initBatchCreate),e.post("/get",this.initGet),e.post("/update",this.initUpdate),e.post("/batchUpdate",this.initBatchUpdate),e.post("/delete",this.initDelete),e.post("/batchDelete",this.initBatchDelete),e.post("/list",this.initList),e},this.mongoDB=e,this.collection=t,this.isProd=i,this.config=s,this.modelChecker=a,this.lib=o;e=this.isProd?"prod":"dev";this.tableId=n?n+"-"+e:e}}exports.MongoWrapper=MongoWrapper;
1
+ "use strict";var __awaiter=this&&this.__awaiter||function(e,n,d,r){return new(d=d||Promise)(function(i,t){function s(e){try{o(r.next(e))}catch(e){t(e)}}function a(e){try{o(r.throw(e))}catch(e){t(e)}}function o(e){var t;e.done?i(e.value):((t=e.value)instanceof d?t:new d(function(e){e(t)})).then(s,a)}o((r=r.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_1=__importDefault(require("moment")),QueryModel_1=require("../model/QueryModel");class MongoWrapper{constructor(e,t,i,s,a,o,n){this.mongoDB="",this.collection="",this.isProd=!1,this.config={baseConfig:[],opsConfig:{read:[],create:[],update:[],delete:[]}},this.modelChecker=e=>!1,this.tableId="",this.parseModel=e=>(delete e._id,e.baseUpdatedAt=(0,moment_1.default)().toDate(),e),this.initBatchCreate=(a,o,n)=>__awaiter(this,void 0,void 0,function*(){try{if(Array.isArray(a.body.data)){let t=!0;a.body.data.map(e=>{this.modelChecker(e)||(t=!1)}),t||o.status(400).json({err:"Invalid Model Structure"});var e,s=(0,QueryModel_1.checkBaseConfig)(this.config,a);let i=!0;yield Promise.all(a.body.data.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"update",a,e)||(i=!1)})),s&&i?(e=a.body.data.map(e=>Object.assign(Object.assign({},e),{baseUpdatedAt:(0,moment_1.default)().toDate()})),n&&n(e),yield this.mongoDB.db(this.tableId).collection(this.collection).insertMany(e),o.send({success:!0})):o.status(400).json({err:"Invalid Security Configuration"})}else o.status(400).json({err:"Invalid Fields"})}catch(e){o.status(400).json({err:e})}}),this.initCreate=(i,s,a)=>__awaiter(this,void 0,void 0,function*(){try{var e,t;this.modelChecker(i.body.data)?(e=(0,QueryModel_1.checkBaseConfig)(this.config,i),t=(0,QueryModel_1.checkOpsConfig)(this.config,"create",i,i.body.data),e&&t?(i.body.data.baseUpdatedAt=(0,moment_1.default)().toDate(),a&&(yield a(i.body.data)),yield this.mongoDB.db(this.tableId).collection(this.collection).insertOne(i.body.data),s.send({success:!0})):s.status(400).json({err:"Invalid Security Configuration"})):s.status(400).json({err:"Invalid Model Structure"})}catch(e){s.status(400).json({err:e})}}),this.initGet=(a,o)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s=a.body.id;s?(e=yield this.mongoDB.db(this.tableId).collection(this.collection).findOne({_id:s}),t=(0,QueryModel_1.checkBaseConfig)(this.config,a),i=(0,QueryModel_1.checkOpsConfig)(this.config,"read",a,e),t&&i?o.send({data:e}):o.status(400).json({err:"Invalid Security Configuration"})):o.status(400).json({err:"Invalid Fields"})}catch(e){o.status(400).json({err:e})}}),this.initBatchUpdate=(o,n)=>__awaiter(this,void 0,void 0,function*(){try{var e=this.modelChecker(o.body.data)&&o.body.id;if(Array.isArray(o.body.id)){var s=o.body.data.map(e=>Object.assign(Object.assign({},e),{baseUpdatedAt:(0,moment_1.default)().toDate()})),i=(0,QueryModel_1.checkBaseConfig)(this.config,o);let t=!0;if(yield Promise.all(o.body.data.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"update",o,e)||(t=!1)})),i&&t){const a=this.mongoDB.db(this.tableId).collection(this.collection);if(o.body.sensitive){let i=!0;yield Promise.all(o.body.id.map(t=>__awaiter(this,void 0,void 0,function*(){var e=yield a.findOne({_id:t});(0,moment_1.default)(o.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(e.baseUpdatedAt))?n.status(400).json({err:"Refresh Sensitive Model"}):i=!1}))),i&&(yield a.updateMany({_id:{$in:e}},{$set:Object.assign({},s)}),n.send({success:!0}))}else yield a.updateMany({_id:{$in:e}},{$set:Object.assign({},s)}),n.send({success:!0})}else n.status(400).json({err:"Invalid Security Configuration"})}}catch(e){}}),this.initUpdate=(d,r)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s,a,o,n=this.modelChecker(d.body.data)&&d.body.id;n?(e=(0,QueryModel_1.checkBaseConfig)(this.config,d),t=(0,QueryModel_1.checkOpsConfig)(this.config,"update",d,d.body.data),e&&t?(i=this.mongoDB.db(this.tableId).collection(this.collection),d.body.sensitive?(s=yield i.findOne({_id:n}),(0,moment_1.default)(d.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(s.baseUpdatedAt))?(a=this.parseModel(d.body.data),yield i.updateOne({_id:n},{$set:Object.assign({},a)}),r.send({success:!0})):r.status(400).json({err:"Refresh Sensitive Model"})):(o=this.parseModel(d.body.data),yield i.updateOne({_id:n},{$set:Object.assign({},o)}),r.send({success:!0}))):r.status(400).json({err:"Invalid Security Configuration"})):r.status(400).json({err:"Invalid Model Structure"})}catch(e){r.status(400).json({err:e})}}),this.initBatchDelete=(c,u,h)=>__awaiter(this,void 0,void 0,function*(){try{if(Array.isArray(c.body.id)){var e=this.mongoDB.db(this.tableId).collection(this.collection),i=yield e.find({_id:{$in:c.body.id}}),s=(0,QueryModel_1.checkBaseConfig)(this.config,c),a=yield i.toArray();let t=!0;0<a.length&&(yield Promise.all(a.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"delete",c,e)||(t=!1)}))),s&&t?(h&&h(a),yield e.deleteMany({_id:{$in:c.body.id}}),u.send({success:!0})):u.status(400).json({err:"Invalid Security Configuration"})}else{var o=c.body.query;const l={};o&&0<o.length&&o.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&&(l.$or=[],e.searchIds.map(e=>{l.$or.push({[e]:{$regex:s,$options:"i"}})}));break;case"=":l[i]=s;break;case"!=":l[i]={$ne:s};break;case">":l[i]={$gt:s};break;case"<":l[i]={$lt:s};break;case"><":Array.isArray(s)&&2<=s.length&&(l[i]={$gte:s[0],$lte:s[1]});break;case"in":l[i]={$in:s}}});var n=this.mongoDB.db(this.tableId).collection(this.collection),d=yield n.find(l).toArray(),r=(0,QueryModel_1.checkBaseConfig)(this.config,c);let t=!0;0<d.length&&d.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"delete",c,e)||(t=!1)}),h&&h(d),r&&t?(yield n.deleteMany(l),u.send({success:!0})):u.status(400).json({err:"Invalid Security Configuration"})}}catch(e){u.status(400).json({err:e})}}),this.initDelete=(o,n,d)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s,a=o.body.id;a?(t=yield(e=this.mongoDB.db(this.tableId).collection(this.collection)).findOne({_id:a}),i=(0,QueryModel_1.checkBaseConfig)(this.config,o),s=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",o,t),i&&s?(d&&d(t),yield e.findOneAndDelete({_id:a}),n.send({success:!0})):n.status(400).json({err:"Invalid Security Configuration"})):n.status(400).json({err:"Invalid Fields"})}catch(e){n.status(400).json({err:e})}}),this.initList=(A,B)=>__awaiter(this,void 0,void 0,function*(){var e,t,i,s,a,o;try{var n=(0,QueryModel_1.checkBaseConfig)(this.config,A);if(n){var d=A.body.aggregate,r=null!=(e=A.body.query)?e:[];const Y={};if(r&&0<r.length&&r.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}}}),d){var l=[],c=null!=(t=d.queryId)?t:"",u=null!=(i=d.dateId)?i:"baseUpdatedAt";for(const O of null!=(s=d.range)?s:[]){Object.assign({},Y);var h=O.split("-"),[y,g=(0,moment_1.default)().format("DD/MM/YYYY HH:mm:ss")]=h,f=2===h.length,b=(0,moment_1.default)(y,"DD/MM/YYYY").isValid(),m=(0,moment_1.default)(g,"DD/MM/YYYY").isValid(),p=(0,moment_1.default)(y,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss"),v=(0,moment_1.default)(g,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss");if(!b||!m)return void B.status(400).json({err:"Invalid Aggregate Range Configuration"});var _=f?{$match:Object.assign(Object.assign({},Y),{[u]:{$gte:new Date(p),$lte:new Date(v)}})}:{$match:Object.assign(Object.assign({},Y),{[u]:{$lte:new Date(p)}})},M=[];let e=!1;var D=f?O:"Begining until "+(0,moment_1.default)(y).format("DD/MM/YYYY");switch(d.type){case"avg":M.push(_,{$group:{_id:D,value:{$avg:"$"+c}}});break;case"sum":M.push(_,{$group:{_id:D,value:{$sum:"$"+c}}});break;case"count":e=!0}if(0===M.length&&!e)return void B.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(Y).count({}):null!=(o=null==(a=(yield C.aggregate(M).toArray())[0])?void 0:a.value)?o:0,l.push({_id:D,value:t})}B.send({data:l})}else{let e=this.mongoDB.db(this.tableId).collection(this.collection).find(Y);var j,I,k=yield e.count({}),$=(A.body.cursor&&(e=e.skip(A.body.cursor)),A.body.limit&&(e=e.limit(A.body.limit)),A.body.sort&&(I={[(j=A.body.sort).sortId]:"asc"===j.type?1:-1},e=e.sort(I)),yield e.toArray());let t=!0;0<$.length&&$.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"read",A,e)||(t=!1)}),n&&t?B.send({data:$,count:k}):B.status(400).json({err:"Invalid Security Configuration"})}}else B.status(400).json({err:"Invalid Security Configuration"})}catch(e){B.status(400).json({err:e})}}),this.init=()=>{var e=this.lib.express.Router();return e.post("/create",this.initCreate),e.post("/batchCreate",this.initBatchCreate),e.post("/get",this.initGet),e.post("/update",this.initUpdate),e.post("/batchUpdate",this.initBatchUpdate),e.post("/delete",this.initDelete),e.post("/batchDelete",this.initBatchDelete),e.post("/list",this.initList),e},this.mongoDB=e,this.collection=t,this.isProd=i,this.config=s,this.modelChecker=a,this.lib=o;e=this.isProd?"prod":"dev";this.tableId=n?n+"-"+e:e}}exports.MongoWrapper=MongoWrapper;
@@ -7,9 +7,10 @@
7
7
  */
8
8
  export declare class SecurityMiddleware {
9
9
  checkAuthentication: (token: string) => Promise<string>;
10
+ checkOrgId: (userId: string) => Promise<string>;
10
11
  checkRole: (userId: string) => Promise<string>;
11
12
  checkAPIToken: (apiToken: string) => Promise<boolean>;
12
- constructor(checkAuthentication: (token: string) => Promise<string>, checkRole: (userId: string) => Promise<string>, checkAPIToken: (apiToken: string) => Promise<boolean>);
13
+ constructor(checkAuthentication: (token: string) => Promise<string>, checkOrgId: (userId: string) => Promise<string>, checkRole: (userId: string) => Promise<string>, checkAPIToken: (apiToken: string) => Promise<boolean>);
13
14
  /**
14
15
  * 1. Check User Authentication
15
16
  * 2. Check User Role
@@ -1 +1 @@
1
- {"version":3,"file":"security.d.ts","sourceRoot":"","sources":["../../src/apis/security.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,qBAAa,kBAAkB;IAE7B,mBAAmB,UAAiB,MAAM,qBAExC;IACF,SAAS,WAAkB,MAAM,qBAE/B;IACF,aAAa,aAAoB,MAAM,sBAErC;gBAIA,mBAAmB,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,EACvD,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,EAC9C,aAAa,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC;IAOvD;;;;OAIG;IACH,IAAI,UAG8C,GAAG,OAAO,GAAG,SAAS,GAAG;CA6B5E"}
1
+ {"version":3,"file":"security.d.ts","sourceRoot":"","sources":["../../src/apis/security.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,qBAAa,kBAAkB;IAE7B,mBAAmB,UAAiB,MAAM,qBAExC;IACF,UAAU,WAAkB,MAAM,qBAEhC;IACF,SAAS,WAAkB,MAAM,qBAE/B;IACF,aAAa,aAAoB,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;CA8B5E"}
@@ -1 +1 @@
1
- "use strict";var __awaiter=this&&this.__awaiter||function(e,r,d,u){return new(d=d||Promise)(function(i,t){function o(e){try{c(u.next(e))}catch(e){t(e)}}function n(e){try{c(u.throw(e))}catch(e){t(e)}}function c(e){var t;e.done?i(e.value):((t=e.value)instanceof d?t:new d(function(e){e(t)})).then(o,n)}c((u=u.apply(e,r||[])).next())})};Object.defineProperty(exports,"__esModule",{value:!0}),exports.SecurityMiddleware=void 0;class SecurityMiddleware{constructor(e,t,i){this.checkAuthentication=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.checkRole=t,this.checkAPIToken=i}init(){const o=this;return function(t,e,i){return __awaiter(this,void 0,void 0,function*(){var e;delete t.body.bm_userId,delete t.body.bm_userRole,delete t.body.bm_apiToken,t.body.userToken&&(e=yield o.checkAuthentication(t.body.userToken))&&(t.body.bm_userId=e,e=yield o.checkRole(e))&&(t.body.bm_userRole=e),t.body.apiToken&&(e=t.body.apiToken,e=yield o.checkAPIToken(e),t.body.bm_apiToken=e),i&&i()})}}}exports.SecurityMiddleware=SecurityMiddleware;
1
+ "use strict";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(){const 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":"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,YA2B/D,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,WACjB,cAAc,QAChB,MAAM,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,OACxC,GAAG,OACH,GAAG,YA+CT,CAAC"}
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,YAuB/D,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,WACjB,cAAc,QAChB,MAAM,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,OACxC,GAAG,OACH,GAAG,YAqDT,CAAC"}
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.checkOpsConfig=exports.checkBaseConfig=void 0;const checkBaseConfig=(e,o)=>{let s=!0;return 0<e.baseConfig.length&&e.baseConfig.map(e=>{switch(e.type){case"api":o.body.bm_apiToken||(s=!1);break;case"auth":o.body.bm_userId||(s=!1);break;case"doc":break;case"role":o.body.bm_userRole!==e.value&&(s=!1)}}),s},checkOpsConfig=(exports.checkBaseConfig=checkBaseConfig,(e,o,s,a)=>{let c=!1,r;return 0<(r="read"===o?e.opsConfig.read:"create"===o?e.opsConfig.create:"update"===o?e.opsConfig.update:e.opsConfig.delete).length?r.map(e=>{switch(e.type){case"api":s.body.bm_apiToken&&(c=!0);break;case"auth":s.body.bm_userId&&(c=!0);break;case"doc":JSON.stringify(s.body.bm_userId)===JSON.stringify(a[e.value])&&(c=!0);break;case"role":s.body.bm_userRole===e.value&&(c=!0)}}):c=!0,c});exports.checkOpsConfig=checkOpsConfig;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.checkOpsConfig=exports.checkBaseConfig=void 0;const checkBaseConfig=(e,o)=>{let s=!0;return 0<e.baseConfig.length&&e.baseConfig.map(e=>{switch(e.type){case"api":o.body.bm_apiToken||(s=!1);break;case"auth":o.body.bm_userId||(s=!1);break;case"role":o.body.bm_userRole!==e.value&&(s=!1)}}),s},checkOpsConfig=(exports.checkBaseConfig=checkBaseConfig,(e,o,s,r)=>{let a=!1,c;return 0<(c="read"===o?e.opsConfig.read:"create"===o?e.opsConfig.create:"update"===o?e.opsConfig.update:e.opsConfig.delete).length?c.map(e=>{switch(e.type){case"api":s.body.bm_apiToken&&(a=!0);break;case"auth":s.body.bm_userId&&(a=!0);break;case"doc":s.body.bm_userId&&JSON.stringify(s.body.bm_userId)===JSON.stringify(r[e.value])&&(a=!0);break;case"org":s.body.bm_userOrg&&JSON.stringify(s.body.bm_userOrg)===JSON.stringify(r[e.value])&&(a=!0);break;case"role":s.body.bm_userRole&&s.body.bm_userRole===e.value&&(a=!0)}}):a=!0,a});exports.checkOpsConfig=checkOpsConfig;
@@ -1,13 +1,14 @@
1
1
  /**
2
2
  * @type Selection
3
3
  * auth - bm_userId is not empty
4
+ * org - bm_userOrg equal to value
4
5
  * role - bm_userRole equal to value
5
6
  * api - bm_apiToken is true
6
7
  * doc - class.value equals to userId
7
8
  *
8
9
  */
9
10
  interface Config {
10
- type: "auth" | "role" | "api" | "doc";
11
+ type: "auth" | "org" | "role" | "api" | "doc";
11
12
  value: string;
12
13
  }
13
14
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"SecurityConfig.d.ts","sourceRoot":"","sources":["../../src/model/SecurityConfig.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,UAAU,MAAM;IACd,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,KAAK,GAAG,KAAK,CAAC;IACtC,KAAK,EAAE,MAAM,CAAC;CACf;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"}
1
+ {"version":3,"file":"SecurityConfig.d.ts","sourceRoot":"","sources":["../../src/model/SecurityConfig.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,UAAU,MAAM;IACd,IAAI,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,KAAK,CAAC;IAC9C,KAAK,EAAE,MAAM,CAAC;CACf;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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blixify-server",
3
- "version": "0.1.18",
3
+ "version": "0.1.20",
4
4
  "license": "MIT",
5
5
  "main": "dist/apis/index.js",
6
6
  "private": false,