blixify-server 0.1.35 → 0.1.37

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.
@@ -16,7 +16,7 @@ export declare class FirebaseWrapper {
16
16
  initBatchCreate: (req: any, res: any, workflow?: ((dataList: any[]) => Promise<any>) | undefined) => Promise<void>;
17
17
  initCreate: (req: any, res: any, workflow?: ((data: any) => Promise<any>) | undefined) => Promise<void>;
18
18
  initGet: (req: any, res: any) => Promise<void>;
19
- initBatchUpdate: (req: any, res: any) => Promise<void>;
19
+ initBatchUpdate: (req: any, res: any, workflow?: ((data: any) => Promise<any>) | undefined) => Promise<void>;
20
20
  initUpdate: (req: any, res: any, workflow?: ((data: any) => Promise<any>) | undefined) => Promise<void>;
21
21
  initBatchDelete: (req: any, res: any, workflow?: ((dataList: any[]) => Promise<any>) | undefined) => Promise<void>;
22
22
  initDelete: (req: any, res: any, workflow?: ((data: any) => Promise<any>) | undefined) => Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"fbWrapper.d.ts","sourceRoot":"","sources":["../../src/apis/fbWrapper.ts"],"names":[],"mappings":"AAQA,OAAO,cAAc,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAE5C;;;GAGG;AACH,qBAAa,eAAe;IAC1B,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;gBAGA,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;IAUjB,UAAU,SAAU,GAAG,SAIrB;IAEF,eAAe,QACR,GAAG,OACH,GAAG,yBACc,GAAG,EAAE,KAAK,QAAQ,GAAG,CAAC,gCAkD5C;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC,gCAiCtC;IAEF,OAAO,QAAe,GAAG,OAAO,GAAG,mBA6BjC;IAEF,eAAe,QAAe,GAAG,OAAO,GAAG,mBA2HzC;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC,gCA8DtC;IAEF,eAAe,QACR,GAAG,OACH,GAAG,yBACc,GAAG,EAAE,KAAK,QAAQ,GAAG,CAAC,gCAsH5C;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC,gCA4CtC;IAEF,QAAQ,QAAe,GAAG,OAAO,GAAG,mBA+MlC;IAEF,IAAI,YA2EF;CACH"}
1
+ {"version":3,"file":"fbWrapper.d.ts","sourceRoot":"","sources":["../../src/apis/fbWrapper.ts"],"names":[],"mappings":"AAQA,OAAO,cAAc,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAG5C;;;GAGG;AACH,qBAAa,eAAe;IAC1B,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;gBAGA,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;IAUjB,UAAU,SAAU,GAAG,SAIrB;IAEF,eAAe,QACR,GAAG,OACH,GAAG,yBACc,GAAG,EAAE,KAAK,QAAQ,GAAG,CAAC,gCA4E5C;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC,gCA6CtC;IAEF,OAAO,QAAe,GAAG,OAAO,GAAG,mBA6BjC;IAEF,eAAe,QACR,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC,gCAqItC;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC,gCA8EtC;IAEF,eAAe,QACR,GAAG,OACH,GAAG,yBACc,GAAG,EAAE,KAAK,QAAQ,GAAG,CAAC,gCAwH5C;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC,gCA4CtC;IAEF,QAAQ,QAAe,GAAG,OAAO,GAAG,mBA4MlC;IAEF,IAAI,YA+EF;CACH"}
@@ -1 +1 @@
1
- "use strict";var __awaiter=this&&this.__awaiter||function(e,a,d,n){return new(d=d||Promise)(function(i,t){function s(e){try{o(n.next(e))}catch(e){t(e)}}function r(e){try{o(n.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,r)}o((n=n.apply(e,a||[])).next())})},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.FirebaseWrapper=void 0;const moment_1=__importDefault(require("moment")),QueryModel_1=require("../model/QueryModel");class FirebaseWrapper{constructor(e,t,i,s,r,o){this.fbAdmin="",this.collection="",this.isProd=!1,this.config={baseConfig:[],opsConfig:{read:[],create:[],update:[],delete:[]}},this.modelChecker=e=>!1,this.parseModel=e=>(delete e.id,e.baseUpdatedAt=(0,moment_1.default)().toDate(),e),this.initBatchCreate=(r,o,a)=>__awaiter(this,void 0,void 0,function*(){try{const s=this.fbAdmin.firestore().batch();if(Array.isArray(r.body.data)){let t=!0;if(r.body.data.map(e=>{this.modelChecker(e)||(t=!1)}),t){var e=r.body.data,i=(0,QueryModel_1.checkBaseConfig)(this.config,r);let t=!0;e.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"update",r,e)||(t=!1)}),i&&t?(a&&(yield a(e)),yield Promise.all(e.map(t=>__awaiter(this,void 0,void 0,function*(){t.baseUpdatedAt=(0,moment_1.default)().toDate();var e=this.fbAdmin.firestore().collection(this.collection).doc(t.id);s.set(e,Object.assign({},t))}))),yield s.commit(),o.send({success:!0})):o.status(400).json({err:"Invalid Security Configuration"})}else o.status(400).json({err:"Invalid Model Structure"})}else o.status(400).json({err:"Invalid Fields"})}catch(e){o.status(400).json({err:e})}}),this.initCreate=(i,s,r)=>__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(),r&&(yield r(i.body.data)),yield this.fbAdmin.firestore().collection(this.collection).doc(i.body.data.id).set(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=(o,a)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s,r=o.body.id;r?(e=yield this.fbAdmin.firestore().collection(this.collection).doc(r).get()).exists&&(t=e.data(),i=(0,QueryModel_1.checkBaseConfig)(this.config,o),s=(0,QueryModel_1.checkOpsConfig)(this.config,"read",o,t),i)&&s?a.send({data:t}):a.status(400).json({err:"Invalid Security Configuration"}):a.status(400).json({err:"Invalid Fields"})}catch(e){a.status(400).json({err:e})}}),this.initBatchUpdate=(y,b)=>__awaiter(this,void 0,void 0,function*(){var e,t;const i=this.fbAdmin.firestore().batch();try{var s=this.modelChecker(y.body.data)&&y.body.query,r=this.parseModel(null!=(e=y.body.data)?e:[]),o=null!=(t=y.body.query)?t:[],a=y.body.isOr||!1,d=y.body.unsetData;if(s){var n=this.fbAdmin.firestore().collection(this.collection);const u=[];o.forEach(e=>{var t,i=null!=(t=e.queryId)?t:"",s=e.value;switch(e.type){case"=":u.push(this.fbAdmin.firestore.Filter.where(i,"==",s));break;case"!=":u.push(this.fbAdmin.firestore.Filter.where(i,"!=",s));break;case">":u.push(this.fbAdmin.firestore.Filter.where(i,">",s));break;case"<":u.push(this.fbAdmin.firestore.Filter.where(i,"<",s));break;case"><":Array.isArray(s)&&2<=s.length&&u.push(this.fbAdmin.firestore.Filter.where(i,">=",s[0]),this.fbAdmin.firestore.Filter.where(i,"<=",s[1]));break;case"in":u.push(this.fbAdmin.firestore.Filter.where(i,"in",s))}});var c=a?this.fbAdmin.firestore.Filter.or(...u):this.fbAdmin.firestore.Filter.and(...u),l=yield n.where(c).get(),h=(0,QueryModel_1.checkBaseConfig)(this.config,y);let t=!0;const f=[];if(l&&l.forEach(e=>{e=e.data();f.push(e.id),y.body.sensitive&&!(0,moment_1.default)(y.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(1e3*e.baseUpdatedAt.seconds))&&b.status(400).json({err:"Refresh Sensitive Model"}),(0,QueryModel_1.checkOpsConfig)(this.config,"update",y,e)||(t=!1)}),h&&t){let t={};d&&d.forEach(e=>{t[e]=this.fbAdmin.firestore.FieldValue.delete()}),r&&(t=Object.assign(Object.assign({},r),t)),yield Promise.all(f.map(e=>{e=this.fbAdmin.firestore().collection(this.collection).doc(e);i.update(e,Object.assign({},t))})),yield i.commit(),b.send({success:!0})}else b.status(400).json({err:"Invalid Security Configuration"})}}catch(e){b.status(400).json({err:e})}}),this.initUpdate=(d,n,c)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s,r,o,a=this.modelChecker(d.body.data)&&d.body.id;a?(e=yield this.fbAdmin.firestore().collection(this.collection).doc(a).get()).exists&&(t=e.data(),i=(0,QueryModel_1.checkBaseConfig)(this.config,d),s=(0,QueryModel_1.checkOpsConfig)(this.config,"update",d,t),i)&&s?d.body.sensitive?(0,moment_1.default)(d.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(1e3*t.baseUpdatedAt.seconds))?(c&&(yield c(d.body.data)),r=this.parseModel(d.body.data),yield this.fbAdmin.firestore().collection(this.collection).doc(a).update(Object.assign({},r)),n.send({success:!0})):n.status(400).json({err:"Refresh Sensitive Model"}):(c&&(yield c(d.body.data)),o=this.parseModel(d.body.data),yield this.fbAdmin.firestore().collection(this.collection).doc(a).update(Object.assign({},o)),n.send({success:!0})):n.status(400).json({err:"Invalid Security Configuration"}):n.status(400).json({err:"Invalid Model Structure"})}catch(e){n.status(400).json({err:e})}}),this.initBatchDelete=(c,l,h)=>__awaiter(this,void 0,void 0,function*(){try{const d=this.fbAdmin.firestore().batch();if(Array.isArray(c.body.id)){var e=yield this.fbAdmin.firestore().collection(this.collection).where("id","in",c.body.id).get(),i=(0,QueryModel_1.checkBaseConfig)(this.config,c);let t=!0;const r=[];if(e&&e.forEach(e=>{e=e.data();r.push(e),(0,QueryModel_1.checkOpsConfig)(this.config,"delete",c,e)||(t=!1)}),!i||!t)return void l.status(400).json({err:"Invalid Security Configuration"});h&&(yield h(r)),yield Promise.all(c.body.id.map(t=>__awaiter(this,void 0,void 0,function*(){var e=yield this.fbAdmin.firestore().collection(this.collection).doc(t);d.delete(e)}))),yield d.commit(),l.send({success:!0})}else{let r=this.fbAdmin.firestore().collection(this.collection);var s=c.body.query,o=(s&&0<s.length&&s.map(e=>{var t,i=null!=(t=e.queryId)?t:"",s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&(r=r.orderBy(e.searchIds[0]).startAt(s).endAt(s+""));break;case"=":r=r.where(i,"==",s);break;case"!=":r=r.where(i,"!=",s);break;case">":r=r.where(i,">",s);break;case"<":r=r.where(i,"<",s);break;case"><":Array.isArray(s)&&2<=s.length&&(r=r.where(i,">=",s[0]).where(i,"<=",s[1]));break;case"in":r=r.where(i,"in",s)}}),yield r.get()),a=(0,QueryModel_1.checkBaseConfig)(this.config,c);let t=!0;const n=[];if(o&&o.forEach(e=>{e=e.data();n.push(e),(0,QueryModel_1.checkOpsConfig)(this.config,"delete",c,e)||(t=!1)}),h&&(yield h(n)),!a||!t)return void l.status(400).json({err:"Invalid Security Configuration"});o.forEach(e=>{d.delete(e.ref)}),yield d.commit()}l.send({success:!0})}catch(e){l.status(400).json({err:e})}}),this.initDelete=(o,a,d)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s,r=o.body.id;r?(e=yield this.fbAdmin.firestore().collection(this.collection).doc(r).get()).exists&&(t=e.data(),i=(0,QueryModel_1.checkBaseConfig)(this.config,o),s=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",o,t),i)&&s?(d&&(yield d(t)),yield this.fbAdmin.firestore().collection(this.collection).doc(r).delete(),a.send({success:!0})):a.status(400).json({err:"Invalid Security Configuration"}):a.status(400).json({err:"Invalid Fields"})}catch(e){a.status(400).json({err:e})}}),this.initList=(D,B)=>__awaiter(this,void 0,void 0,function*(){var e,t,i,s;try{var a=(0,QueryModel_1.checkBaseConfig)(this.config,D);if(a){let r=this.fbAdmin.firestore().collection(this.collection);var d=D.body.aggregate,n=null!=(e=D.body.query)?e:[];let o="";if(n&&0<n.length&&n.map(e=>{var t,i=null!=(t=e.queryId)?t:"",s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&(o=e.searchIds[0],r=r.orderBy(e.searchIds[0]).startAt(s).endAt(s+""));break;case"=":r=r.where(i,"==",s);break;case"!=":r=r.where(i,"!=",s);break;case">":r=r.where(i,">",s);break;case"<":r=r.where(i,"<",s);break;case"><":Array.isArray(s)&&2<=s.length&&(r=r.where(i,">=",s[0]).where(i,"<=",s[1]));break;case"in":r=r.where(i,"in",s)}}),d){const j=null!=(t=d.queryId)?t:"";var c=null!=(i=d.dateId)?i:"baseUpdatedAt",l=null!=(s=d.range)?s:[],h=[];for(const I of l){var u=I.split("-"),[f,y=(0,moment_1.default)().format("DD/MM/YYYY HH:mm:ss")]=u,b=2===u.length,m=(0,moment_1.default)(f,"DD/MM/YYYY").isValid(),g=(0,moment_1.default)(y,"DD/MM/YYYY").isValid(),p=(0,moment_1.default)(f,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss"),v=(0,moment_1.default)(y,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss");if(!m||!g)return void B.status(400).json({err:"Invalid Aggregate Range Configuration"});var _=b?I:"Begining until "+(0,moment_1.default)(f).format("DD/MM/YYYY");r=b?r.where(c,">=",(0,moment_1.default)(p).toDate()).where(c,"<=",(0,moment_1.default)(v).toDate()):r.where(c,"<",(0,moment_1.default)(p).toDate());let t=0;switch(d.type){case"count":var A=yield r.count().get();t=A.data().count;break;case"value":var M=yield(r=(r=r.limit(1)).orderBy(c,"desc")).get();M&&M.forEach(e=>{e=e.data(),e=JSON.parse(JSON.stringify(e));t=e[j]})}h.push({_id:_,value:t})}B.send({data:h})}else if(D.body.limit){if(r=r.limit(D.body.limit),D.body.sort){var w=D.body.sort;if(o&&w.sortId!==o)return void B.status(400).json({err:"Invalid Search Sort Configuration"});o||(r=r.orderBy(w.sortId,"asc"===w.type?"asc":"desc"))}var C,k=yield(r=D.body.cursor?r.startAfter(D.body.cursor):r).get();let t=!0;const Y=[];if(k&&k.forEach(e=>{e=e.data();Y.push(e),(0,QueryModel_1.checkOpsConfig)(this.config,"read",D,e)||(t=!1)}),a&&t){let t="";if(Y.length===D.body.limit){let e="id";D.body.sort&&(C=D.body.sort,e=C.sortId),t=Y[D.body.limit-1][e]}B.send({data:Y,count:t})}else B.status(400).json({err:"Invalid Security Configuration"})}else B.status(400).json({err:"Invalid Limit 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",(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.fbAdmin=e,this.collection=t,this.isProd=i,this.config=s,this.modelChecker=r,this.lib=o}}exports.FirebaseWrapper=FirebaseWrapper;
1
+ "use strict";var __awaiter=this&&this.__awaiter||function(e,a,d,n){return new(d=d||Promise)(function(i,t){function s(e){try{r(n.next(e))}catch(e){t(e)}}function o(e){try{r(n.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(s,o)}r((n=n.apply(e,a||[])).next())})},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.FirebaseWrapper=void 0;const moment_1=__importDefault(require("moment")),QueryModel_1=require("../model/QueryModel"),utils_1=require("./utils");class FirebaseWrapper{constructor(e,t,i,s,o,r){this.fbAdmin="",this.collection="",this.isProd=!1,this.config={baseConfig:[],opsConfig:{read:[],create:[],update:[],delete:[]}},this.modelChecker=e=>!1,this.parseModel=e=>(delete e.id,e.baseUpdatedAt=(0,moment_1.default)().toDate(),e),this.initBatchCreate=(c,l,u)=>__awaiter(this,void 0,void 0,function*(){try{const o=this.fbAdmin.firestore().batch(),r=this.fbAdmin.firestore().collection(this.collection);if(Array.isArray(c.body.data)){let t=!0;if(c.body.data.map(e=>{this.modelChecker(e)||(t=!1)}),t){var e=c.body.data,i=(0,QueryModel_1.checkBaseConfig)(this.config,c);let t=!0;const a=[],d=[];if(e.map(e=>{a.push(e.id),(0,QueryModel_1.checkOpsConfig)(this.config,"update",c,e)||(t=!1),c.body.unique&&null!=e&&e[c.body.unique]&&d.push(e[c.body.unique])}),i&&t){if(0<d.length){var s=yield r.find(c.body.unique,"in",d).get();const n=[];if(s.empty||s.forEach(e=>{e=e.data();n.push(e)}),0<n.length)return void l.status(400).json({err:`Unique:${n.map(e=>e[c.body.unique])} exists`})}u&&(yield u(e)),yield Promise.all(e.map(t=>__awaiter(this,void 0,void 0,function*(){t.baseUpdatedAt=(0,moment_1.default)().toDate();var e=r.doc(t.id);o.set(e,Object.assign({},t))}))),yield o.commit(),l.send({success:a})}else l.status(400).json({err:"Invalid Security Configuration"})}else l.status(400).json({err:"Invalid Model Structure"})}else l.status(400).json({err:"Invalid Fields"})}catch(e){l.status(400).json({err:e})}}),this.initCreate=(o,r,a)=>__awaiter(this,void 0,void 0,function*(){var e;try{if(this.modelChecker(o.body.data)){var t=(0,QueryModel_1.checkBaseConfig)(this.config,o),i=(0,QueryModel_1.checkOpsConfig)(this.config,"create",o,o.body.data);if(t&&i){var s=this.fbAdmin.firestore().collection(this.collection);if(o.body.data.baseUpdatedAt=(0,moment_1.default)().toDate(),o.body.unique&&null!=(e=o.body.data)&&e[o.body.unique])if(!(yield s.where(o.body.unique,"==",o.body.data[o.body.unique]).get()).empty)return void r.status(400).json({err:`Unique:${o.body.data[o.body.unique]} exists`});a&&(yield a(o.body.data)),yield s.doc(o.body.data.id).set(o.body.data),r.send({success:!0})}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=(r,a)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s,o=r.body.id;o?(e=yield this.fbAdmin.firestore().collection(this.collection).doc(o).get()).exists&&(t=e.data(),i=(0,QueryModel_1.checkBaseConfig)(this.config,r),s=(0,QueryModel_1.checkOpsConfig)(this.config,"read",r,t),i)&&s?a.send({data:t}):a.status(400).json({err:"Invalid Security Configuration"}):a.status(400).json({err:"Invalid Fields"})}catch(e){a.status(400).json({err:e})}}),this.initBatchUpdate=(b,m,p)=>__awaiter(this,void 0,void 0,function*(){var e,t;const s=this.fbAdmin.firestore().batch();try{var i=this.modelChecker(b.body.data)&&b.body.query,o=this.parseModel(null!=(e=b.body.data)?e:[]),r=null!=(t=b.body.query)?t:[],a=b.body.isOr||!1,d=b.body.unsetData;if(i){var n=this.fbAdmin.firestore().collection(this.collection);const h=[];r.forEach(e=>{var t,i=null!=(t=e.queryId)?t:"",s=e.value;switch(e.type){case"=":h.push(this.fbAdmin.firestore.Filter.where(i,"==",s));break;case"!=":h.push(this.fbAdmin.firestore.Filter.where(i,"!=",s));break;case">":h.push(this.fbAdmin.firestore.Filter.where(i,">",s));break;case"<":h.push(this.fbAdmin.firestore.Filter.where(i,"<",s));break;case"><":Array.isArray(s)&&2<=s.length&&h.push(this.fbAdmin.firestore.Filter.where(i,">=",s[0]),this.fbAdmin.firestore.Filter.where(i,"<=",s[1]));break;case"in":h.push(this.fbAdmin.firestore.Filter.where(i,"in",s))}});var c=a?this.fbAdmin.firestore.Filter.or(...h):this.fbAdmin.firestore.Filter.and(...h),l=yield n.where(c).get(),u=(0,QueryModel_1.checkBaseConfig)(this.config,b);let i=!0;const f=[],y=[];if(l&&l.forEach(e=>{var t=e.data(),t=(y.push(t.id),!b.body.sensitive||(0,moment_1.default)(b.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(1e3*t.baseUpdatedAt.seconds))||m.status(400).json({err:"Refresh Sensitive Model"}),(0,QueryModel_1.checkOpsConfig)(this.config,"update",b,t)||(i=!1),(0,utils_1.compareUpdatedFields)(e,b.body.data,"id"))["workflowUpdateFields"];f.concat(t)}),u&&i){let t={};d&&d.forEach(e=>{t[e]=this.fbAdmin.firestore.FieldValue.delete()}),o&&(t=Object.assign(Object.assign({},o),t)),p&&(yield p(f)),yield Promise.all(y.map(e=>{e=this.fbAdmin.firestore().collection(this.collection).doc(e);s.update(e,Object.assign({},t))})),yield s.commit(),m.send({success:y})}else m.status(400).json({err:"Invalid Security Configuration"})}}catch(e){m.status(400).json({err:e})}}),this.initUpdate=(u,h,f)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s,o,r,a,d,n,c,l=this.modelChecker(u.body.data)&&u.body.id;l?(t=yield(e=this.fbAdmin.firestore().collection(this.collection)).doc(l).get()).exists&&(i=t.data(),s=(0,QueryModel_1.checkBaseConfig)(this.config,u),o=(0,QueryModel_1.checkOpsConfig)(this.config,"update",u,i),s)&&o?({workflowUpdateFields:a,updatedFields:d}=(u.body.unique&&null!=i&&i[u.body.unique]&&((r=yield e.where(u.body.unique,"==",u.body.data[u.body.unique]).get()).empty||r.forEach(e=>{e=e.data();u.body.id!==e.id&&h.status(400).json({err:u.body.data[u.body.unique]+" exists"})})),(0,utils_1.compareUpdatedFields)(i,u.body.data,"id")),u.body.sensitive?(0,moment_1.default)(u.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(1e3*i.baseUpdatedAt.seconds))?(f&&(yield f(a)),n=this.parseModel(d),yield e.doc(l).update(Object.assign({},n)),h.send({success:!0})):h.status(400).json({err:"Refresh Sensitive Model"}):(f&&(yield f(a)),c=this.parseModel(d),yield e.doc(l).update(Object.assign({},c)),h.send({success:!0}))):h.status(400).json({err:"Invalid Security Configuration"}):h.status(400).json({err:"Invalid Model Structure"})}catch(e){h.status(400).json({err:e})}}),this.initBatchDelete=(l,u,h)=>__awaiter(this,void 0,void 0,function*(){try{const d=this.fbAdmin.firestore().batch();if(Array.isArray(l.body.id)){var e=yield this.fbAdmin.firestore().collection(this.collection).where("id","in",l.body.id).get(),i=(0,QueryModel_1.checkBaseConfig)(this.config,l);let t=!0;const o=[];e&&e.forEach(e=>{e=e.data();o.push(e),(0,QueryModel_1.checkOpsConfig)(this.config,"delete",l,e)||(t=!1)}),i&&t?(h&&(yield h(o)),yield Promise.all(l.body.id.map(t=>__awaiter(this,void 0,void 0,function*(){var e=yield this.fbAdmin.firestore().collection(this.collection).doc(t);d.delete(e)}))),yield d.commit(),u.send({success:l.body.id})):u.status(400).json({err:"Invalid Security Configuration"})}else{let o=this.fbAdmin.firestore().collection(this.collection);var s=l.body.query;const n=[];s&&0<s.length&&s.map(e=>{var t,i=null!=(t=e.queryId)?t:"",s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&(o=o.orderBy(e.searchIds[0]).startAt(s).endAt(s+""));break;case"=":o=o.where(i,"==",s);break;case"!=":o=o.where(i,"!=",s);break;case">":o=o.where(i,">",s);break;case"<":o=o.where(i,"<",s);break;case"><":Array.isArray(s)&&2<=s.length&&(o=o.where(i,">=",s[0]).where(i,"<=",s[1]));break;case"in":o=o.where(i,"in",s)}});var r=yield o.get(),a=(0,QueryModel_1.checkBaseConfig)(this.config,l);let t=!0;const c=[];r&&r.forEach(e=>{e=e.data();n.push(e.id),c.push(e),(0,QueryModel_1.checkOpsConfig)(this.config,"delete",l,e)||(t=!1)}),h&&(yield h(c)),a&&t?(r.forEach(e=>{d.delete(e.ref)}),yield d.commit(),u.send({success:n})):u.status(400).json({err:"Invalid Security Configuration"})}}catch(e){u.status(400).json({err:e})}}),this.initDelete=(r,a,d)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s,o=r.body.id;o?(e=yield this.fbAdmin.firestore().collection(this.collection).doc(o).get()).exists&&(t=e.data(),i=(0,QueryModel_1.checkBaseConfig)(this.config,r),s=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",r,t),i)&&s?(d&&(yield d(t)),yield this.fbAdmin.firestore().collection(this.collection).doc(o).delete(),a.send({success:!0})):a.status(400).json({err:"Invalid Security Configuration"}):a.status(400).json({err:"Invalid Fields"})}catch(e){a.status(400).json({err:e})}}),this.initList=(q,F)=>__awaiter(this,void 0,void 0,function*(){var e,t,i,s,a;try{var d=(0,QueryModel_1.checkBaseConfig)(this.config,q);if(d){let o=this.fbAdmin.firestore().collection(this.collection);var n=q.body.aggregate,c=null!=(e=q.body.query)?e:[];let r="";if(c&&0<c.length&&c.map(e=>{var t,i=null!=(t=e.queryId)?t:"",s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&(r=e.searchIds[0],o=o.orderBy(e.searchIds[0]).startAt(s).endAt(s+""));break;case"=":o=o.where(i,"==",s);break;case"!=":o=o.where(i,"!=",s);break;case">":o=o.where(i,">",s);break;case"<":o=o.where(i,"<",s);break;case"><":Array.isArray(s)&&2<=s.length&&(o=o.where(i,">=",s[0]).where(i,"<=",s[1]));break;case"in":o=o.where(i,"in",s)}}),n){const I=null!=(t=n.queryId)?t:"";var l=null!=(i=n.dateId)?i:"baseUpdatedAt",u=null!=(s=n.range)?s:[],h=[];for(const Y of u){var f=Y.split("-"),[y,b=(0,moment_1.default)().format("DD/MM/YYYY HH:mm:ss")]=f,m=2===f.length,p=(0,moment_1.default)(y,"DD/MM/YYYY").isValid(),g=(0,moment_1.default)(b,"DD/MM/YYYY").isValid(),v=(0,moment_1.default)(y,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss"),_=(0,moment_1.default)(b,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss");if(!p||!g)return void F.status(400).json({err:"Invalid Aggregate Range Configuration"});var A=m?Y:"Begining until "+(0,moment_1.default)(y).format("DD/MM/YYYY");o=m?o.where(l,">=",(0,moment_1.default)(v).toDate()).where(l,"<=",(0,moment_1.default)(_).toDate()):o.where(l,"<",(0,moment_1.default)(v).toDate());let t=0;switch(n.type){case"count":var w=yield o.count().get();t=w.data().count;break;case"value":var M=yield(o=(o=o.limit(1)).orderBy(l,"desc")).get();M&&M.forEach(e=>{e=e.data(),e=JSON.parse(JSON.stringify(e));t=e[I]})}h.push({_id:A,value:t})}F.send({data:h})}else{if(q.body.stopLimit||(o=o.limit(null!=(a=q.body.limit)?a:10)),q.body.sort){var k=q.body.sort;if(r&&k.sortId!==r)return void F.status(400).json({err:"Invalid Search Sort Configuration"});r||(o=o.orderBy(k.sortId,"asc"===k.type?"asc":"desc"))}var C,j=yield(o=q.body.cursor?o.startAfter(q.body.cursor):o).get();let t=!0;const D=[];if(j&&j.forEach(e=>{e=e.data();D.push(e),(0,QueryModel_1.checkOpsConfig)(this.config,"read",q,e)||(t=!1)}),d&&t){let t="";if(D.length===q.body.limit){let e="id";q.body.sort&&(C=q.body.sort,e=C.sortId),t=D[q.body.limit-1][e]}F.send({data:D,count:t})}else F.status(400).json({err:"Invalid Security Configuration"})}}else F.status(400).json({err:"Invalid Security Configuration"})}catch(e){F.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.fbAdmin=e,this.collection=t,this.isProd=i,this.config=s,this.modelChecker=o,this.lib=r}}exports.FirebaseWrapper=FirebaseWrapper;
@@ -19,7 +19,7 @@ export declare class MongoWrapper {
19
19
  constructor(mongoDB: any, collection: string, isProd: boolean, config: SecurityConfig, modelChecker: (obj: any, ignore?: boolean) => boolean, lib: WrapperLib, tableId?: string, debug?: (reqBody: any, curBody: any, errMsg: any) => void);
20
20
  parseModel: (data: any) => any;
21
21
  initBatchCreate: (req: any, res: any, workflow?: ((dataList: any[]) => Promise<any>) | undefined) => Promise<{
22
- success: boolean;
22
+ success: string[];
23
23
  } | undefined>;
24
24
  initCreate: (req: any, res: any, workflow?: ((data: any) => Promise<any>) | undefined) => Promise<{
25
25
  success: boolean;
@@ -27,14 +27,14 @@ export declare class MongoWrapper {
27
27
  initGet: (req: any, res: any) => Promise<{
28
28
  data: any;
29
29
  } | undefined>;
30
- initBatchUpdate: (req: any, res: any) => Promise<{
31
- success: boolean;
30
+ initBatchUpdate: (req: any, res: any, workflow?: ((data: any) => Promise<any>) | undefined) => Promise<{
31
+ success: any[];
32
32
  } | undefined>;
33
33
  initUpdate: (req: any, res: any, workflow?: ((data: any) => Promise<any>) | undefined) => Promise<{
34
34
  success: boolean;
35
35
  } | undefined>;
36
36
  initBatchDelete: (req: any, res: any, workflow?: ((dataList: any[]) => Promise<any>) | undefined) => Promise<{
37
- success: boolean;
37
+ success: any;
38
38
  } | undefined>;
39
39
  initDelete: (req: any, res: any, workflow?: ((data: any) => Promise<any>) | undefined) => Promise<{
40
40
  success: boolean;
@@ -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;IAChB,OAAO,SAAM;IACb,KAAK,EAAE,GAAG,CAAC;IAEX,YAAY,QAAS,GAAG,aAEtB;gBAGA,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,EAChB,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,KAAK,IAAI;IAa3D,UAAU,SAAU,GAAG,SAIrB;IAEF,eAAe,QACR,GAAG,OACH,GAAG,yBACc,GAAG,EAAE,KAAK,QAAQ,GAAG,CAAC;;mBAmD5C;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC;;mBAkCtC;IAEF,OAAO,QAAe,GAAG,OAAO,GAAG;;mBAgCjC;IAEF,eAAe,QAAe,GAAG,OAAO,GAAG;;mBAsIzC;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC;;mBAsEtC;IAEF,eAAe,QACR,GAAG,OACH,GAAG,yBACc,GAAG,EAAE,KAAK,QAAQ,GAAG,CAAC;;mBA2H5C;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC;;mBAqCtC;IAEF,QAAQ,QAAe,GAAG,OAAO,GAAG;;;;;mBA6OlC;IAEF,IAAI,YA4EF;CACH"}
1
+ {"version":3,"file":"mongoWrapper.d.ts","sourceRoot":"","sources":["../../src/apis/mongoWrapper.ts"],"names":[],"mappings":"AAQA,OAAO,cAAc,MAAM,yBAAyB,CAAC;AAGrD,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;IAChB,OAAO,SAAM;IACb,KAAK,EAAE,GAAG,CAAC;IAEX,YAAY,QAAS,GAAG,aAEtB;gBAGA,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,EAChB,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,KAAK,IAAI;IAa3D,UAAU,SAAU,GAAG,SAIrB;IAEF,eAAe,QACR,GAAG,OACH,GAAG,yBACc,GAAG,EAAE,KAAK,QAAQ,GAAG,CAAC;;mBAyE5C;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC;;mBAgDtC;IAEF,OAAO,QAAe,GAAG,OAAO,GAAG;;mBAgCjC;IAEF,eAAe,QACR,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC;;mBAkJtC;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC;;mBAwFtC;IAEF,eAAe,QACR,GAAG,OACH,GAAG,yBACc,GAAG,EAAE,KAAK,QAAQ,GAAG,CAAC;;mBA6H5C;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC;;mBAqCtC;IAEF,QAAQ,QAAe,GAAG,OAAO,GAAG;;;;;mBA2OlC;IAEF,IAAI,YA+EF;CACH"}
@@ -1 +1 @@
1
- "use strict";var __awaiter=this&&this.__awaiter||function(e,r,n,d){return new(n=n||Promise)(function(i,t){function s(e){try{o(d.next(e))}catch(e){t(e)}}function a(e){try{o(d.throw(e))}catch(e){t(e)}}function o(e){var t;e.done?i(e.value):((t=e.value)instanceof n?t:new n(function(e){e(t)})).then(s,a)}o((d=d.apply(e,r||[])).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,r,n){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_1.default)().toDate(),e),this.initBatchCreate=(r,n,d)=>__awaiter(this,void 0,void 0,function*(){var e;try{if(Array.isArray(r.body.data)){let t=!0;r.body.data.map(e=>{this.modelChecker(e)||(t=!1)}),t||n.status(400).json({err:"Invalid Model Structure"});var s=(0,QueryModel_1.checkBaseConfig)(this.config,r);let i=!0;if(yield Promise.all(r.body.data.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"update",r,e)||(i=!1)})),s&&i){var a=r.body.data.map(e=>Object.assign(Object.assign({},e),{baseUpdatedAt:(0,moment_1.default)().toDate()}));d&&(yield d(a));yield this.mongoDB.db(this.tableId).collection(this.collection).insertMany(a);var o={success:!0};if(null!=(e=r.body)&&e.stopRes)return o;n.send(o)}else n.status(400).json({err:"Invalid Security Configuration"})}else n.status(400).json({err:"Invalid Fields"})}catch(e){n.status(400).json({err:e})}}),this.initCreate=(a,o,r)=>__awaiter(this,void 0,void 0,function*(){var e;try{if(this.modelChecker(a.body.data)){var t=(0,QueryModel_1.checkBaseConfig)(this.config,a),i=(0,QueryModel_1.checkOpsConfig)(this.config,"create",a,a.body.data);if(t&&i){a.body.data.baseUpdatedAt=(0,moment_1.default)().toDate(),r&&(yield r(a.body.data));yield this.mongoDB.db(this.tableId).collection(this.collection).insertOne(a.body.data);var s={success:!0};if(null!=(e=a.body)&&e.stopRes)return s;o.send(s)}else o.status(400).json({err:"Invalid Security Configuration"})}else o.status(400).json({err:"Invalid Model Structure"})}catch(e){o.status(400).json({err:e})}}),this.initGet=(r,n)=>__awaiter(this,void 0,void 0,function*(){var e;try{var t=r.body.id;if(t){var i,s=yield this.mongoDB.db(this.tableId).collection(this.collection).findOne({_id:t}),a=(0,QueryModel_1.checkBaseConfig)(this.config,r),o=(0,QueryModel_1.checkOpsConfig)(this.config,"read",r,s);if(a&&o)return i={data:s},null!=(e=r.body)&&e.stopRes?i:void n.send(i);n.status(400).json({err:"Invalid Security Configuration"})}else n.status(400).json({err:"Invalid Fields"})}catch(e){n.status(400).json({err:e})}}),this.initBatchUpdate=(g,b)=>__awaiter(this,void 0,void 0,function*(){var e,i,s;try{if(this.modelChecker(g.body.data)&&g.body.query){var a=g.body.unsetData,o=null!=(e=g.body.query)?e:[];const h=g.body.isOr?"$or":"$and",y={};var r=this.parseModel(null!=(i=g.body.data)?i:[]),n=(o&&0<o.length&&(y[h]=[],o.some(e=>"search"===e.type)&&(y.$or=[]),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&&e.searchIds.map(e=>{y.$or.push({[e]:{$regex:s,$options:"i"}})});break;case"=":y[h].push({[i]:s});break;case"!=":y[h].push({[i]:{$ne:s}});break;case">":y[h].push({[i]:{$gt:s}});break;case"<":y[h].push({[i]:{$lt:s}});break;case"><":Array.isArray(s)&&2<=s.length&&(y[h].push({[i]:{$lt:s}}),y[h]={[i]:{$gte:s[0],$lte:s[1]}});break;case"in":y[h].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({},y))).toArray();if(yield Promise.all(l.map(e=>{g.body.sensitive&&!(0,moment_1.default)(g.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(e.baseUpdatedAt))&&b.status(400).json({err:"Refresh Sensitive Model"}),(0,QueryModel_1.checkOpsConfig)(this.config,"update",g,e)||(t=!1)})),n&&t){const f={};var c={},u=(a&&(a.forEach(e=>{f[e]=""}),c.$unset=Object.assign({},f)),r&&(c.$set=Object.assign({},r)),yield d.updateMany(y,c),{success:!0});if(null!=(s=g.body)&&s.stopRes)return u;b.send(u)}else b.status(400).json({err:"Invalid Security Configuration"})}}catch(e){b.status(400).json({err:e})}}),this.initUpdate=(c,u,h)=>__awaiter(this,void 0,void 0,function*(){var e,t;try{var i=this.modelChecker(c.body.data)&&c.body.id;if(i){var s=this.mongoDB.db(this.tableId).collection(this.collection),a=yield s.findOne({_id:i}),o=(this.debug&&this.debug(c.body,a),(0,QueryModel_1.checkBaseConfig)(this.config,c)),r=(0,QueryModel_1.checkOpsConfig)(this.config,"update",c,a);if(o&&r){var n={success:!0};if(c.body.sensitive)if((0,moment_1.default)(c.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(a.baseUpdatedAt))){h&&(yield h(c.body.data));var d=this.parseModel(c.body.data);if(yield s.updateOne({_id:i},{$set:Object.assign({},d)}),null!=(e=c.body)&&e.stopRes)return n;u.send(n)}else u.status(400).json({err:"Refresh Sensitive Model"});else{h&&(yield h(c.body.data));var l=this.parseModel(c.body.data);if(yield s.updateOne({_id:i},{$set:Object.assign({},l)}),null!=(t=c.body)&&t.stopRes)return n;u.send(n)}}else u.status(400).json({err:"Invalid Security Configuration"})}else u.status(400).json({err:"Invalid Model Structure"})}catch(e){this.debug&&this.debug(null,null,e),u.status(400).json({err:e})}}),this.initBatchDelete=(f,g,b)=>__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),a=yield s.find({_id:{$in:f.body.id}}),o=(0,QueryModel_1.checkBaseConfig)(this.config,f),r=yield a.toArray();let t=!0;if(0<r.length&&(yield Promise.all(r.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"delete",f,e)||(t=!1)}))),o&&t){b&&(yield b(r)),yield s.deleteMany({_id:{$in:f.body.id}});var n={success:!0};if(null!=(e=f.body)&&e.stopRes)return n;g.send(n)}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),c=yield l.find(y).toArray(),u=(0,QueryModel_1.checkBaseConfig)(this.config,f);let t=!0;if(0<c.length&&c.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"delete",f,e)||(t=!1)}),b&&(yield b(c)),u&&t){yield l.deleteMany(y);var h={success:!0};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=(n,d,l)=>__awaiter(this,void 0,void 0,function*(){var e;try{var t=n.body.id;if(t){var i=this.mongoDB.db(this.tableId).collection(this.collection),s=yield i.findOne({_id:t}),a=(0,QueryModel_1.checkBaseConfig)(this.config,n),o=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",n,s);if(a&&o){l&&(yield l(s)),yield i.findOneAndDelete({_id:t});var r={success:!0};if(null!=(e=n.body)&&e.stopRes)return r;d.send(r)}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=(R,S)=>__awaiter(this,void 0,void 0,function*(){var e,t,i,s,a,o,r,n;try{var d=(0,QueryModel_1.checkBaseConfig)(this.config,R);if(d){var l=R.body.aggregate,c=null!=(e=R.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}}}),l){var u=[],h=null!=(t=l.queryId)?t:"",y=null!=(i=l.dateId)?i:"baseUpdatedAt";for(const Q of null!=(s=l.range)?s:[]){Object.assign({},w);var f=Q.split("-"),[g,b=(0,moment_1.default)().format("DD/MM/YYYY HH:mm:ss")]=f,p=2===f.length,v=(0,moment_1.default)(g,"DD/MM/YYYY").isValid(),m=(0,moment_1.default)(b,"DD/MM/YYYY").isValid(),_=(0,moment_1.default)(g,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss"),M=(0,moment_1.default)(b,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss");if(!v||!m)return void S.status(400).json({err:"Invalid Aggregate Range Configuration"});var $=p?{$match:Object.assign(Object.assign({},w),{[y]:{$gte:new Date(_),$lte:new Date(M)}})}:{$match:Object.assign(Object.assign({},w),{[y]:{$lte:new Date(_)}})},D=[];let e=!1;var k=p?Q:"Begining until "+(0,moment_1.default)(g,"DD/MM/YYYY HH:mm:ss").format("DD/MM/YYYY");switch(l.type){case"avg":D.push($,{$group:{_id:k,value:{$avg:"$"+h}}});break;case"sum":D.push($,{$group:{_id:k,value:{$sum:"$"+h}}});break;case"count":e=!0}if(0===D.length&&!e)return void S.status(400).json({err:"Invalid Aggregate Configuration"});var I=this.mongoDB.db(this.tableId).collection(this.collection);let t=0;t=e?yield I.find($.$match).count({}):null!=(o=null==(a=(yield I.aggregate(D).toArray())[0])?void 0:a.value)?o:0,u.push({_id:k,value:t})}var C={data:u};if(null!=(r=R.body)&&r.stopRes)return C;S.send(C)}else{let e=this.mongoDB.db(this.tableId).collection(this.collection).find(w);var j,Y,O,A=yield e.count({}),B=(R.body.cursor&&(e=e.skip(R.body.cursor)),R.body.limit&&(e=e.limit(R.body.limit)),R.body.sort&&(Y={[(j=R.body.sort).sortId]:"asc"===j.type?1:-1},e=e.sort(Y)),yield e.toArray());let t=!0;if(0<B.length&&B.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"read",R,e)||(t=!1)}),d&&t)return O={data:B,count:A},null!=(n=R.body)&&n.stopRes?O:void S.send(O);S.status(400).json({err:"Invalid Security Configuration"})}}else S.status(400).json({err:"Invalid Security Configuration"})}catch(e){S.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=a,this.lib=o;e=this.isProd?"prod":"dev";this.tableId=r?r+"-"+e:e,this.debug=n}}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",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,"_id").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,"_id"),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;
@@ -22,6 +22,7 @@ export declare class UploadWrapper {
22
22
  deleteGoogleFolderContents: (folderRef: any) => Promise<void>;
23
23
  deleteAWSFolderContent: (bucketName: string, directoryPrefix: string) => Promise<void>;
24
24
  initDeleteDirectory: (req: any, res: any) => Promise<void>;
25
+ initBatchDeleteDirectory: (req: any, res: any) => Promise<void>;
25
26
  initGet: (req: any, res: any) => Promise<void>;
26
27
  init: () => any;
27
28
  }
@@ -1 +1 @@
1
- {"version":3,"file":"uploadWrapper.d.ts","sourceRoot":"","sources":["../../src/apis/uploadWrapper.ts"],"names":[],"mappings":"AACA,OAAO,cAAc,MAAM,yBAAyB,CAAC;AAIrD,UAAU,SAAS;IACjB,MAAM,EAAE,GAAG,CAAC;IACZ,UAAU,EAAE,GAAG,CAAC;IAChB,OAAO,CAAC,EAAE,GAAG,CAAC;IACd,IAAI,CAAC,EAAE,GAAG,CAAC;CACZ;AAED;;;GAGG;AACH,qBAAa,aAAa;IACxB,WAAW,EAAE,GAAG,CAAM;IACtB,MAAM,UAAS;IACf,MAAM,EAAE,cAAc,CAQpB;IACF,GAAG,EAAE,SAAS,CAAC;IACf,WAAW,SAAc;IAEzB,YAAY,QAAS,GAAG,sBAAsB,OAAO,aAUnD;gBAGA,WAAW,EAAE,GAAG,EAChB,MAAM,EAAE,OAAO,EACf,MAAM,EAAE,cAAc,EACtB,GAAG,EAAE,SAAS,EACd,WAAW,CAAC,EAAE,UAAU,GAAG,KAAK;IASlC,UAAU,QAAe,GAAG,OAAO,GAAG,QAAQ,QAAQ,GAAG,QAAQ,mBAkG/D;IAEF,UAAU,QAAe,GAAG,OAAO,GAAG,mBAuCpC;IAEF,0BAA0B,cAAqB,GAAG,mBAwBhD;IAEF,sBAAsB,eACR,MAAM,mBACD,MAAM,mBAiBvB;IAEF,mBAAmB,QAAe,GAAG,OAAO,GAAG,mBAqC7C;IAEF,OAAO,QAAe,GAAG,OAAO,GAAG,mBAgCjC;IAEF,IAAI,YAqEF;CACH"}
1
+ {"version":3,"file":"uploadWrapper.d.ts","sourceRoot":"","sources":["../../src/apis/uploadWrapper.ts"],"names":[],"mappings":"AACA,OAAO,cAAc,MAAM,yBAAyB,CAAC;AAIrD,UAAU,SAAS;IACjB,MAAM,EAAE,GAAG,CAAC;IACZ,UAAU,EAAE,GAAG,CAAC;IAChB,OAAO,CAAC,EAAE,GAAG,CAAC;IACd,IAAI,CAAC,EAAE,GAAG,CAAC;CACZ;AAED;;;GAGG;AACH,qBAAa,aAAa;IACxB,WAAW,EAAE,GAAG,CAAM;IACtB,MAAM,UAAS;IACf,MAAM,EAAE,cAAc,CAQpB;IACF,GAAG,EAAE,SAAS,CAAC;IACf,WAAW,SAAc;IAEzB,YAAY,QAAS,GAAG,sBAAsB,OAAO,aAUnD;gBAGA,WAAW,EAAE,GAAG,EAChB,MAAM,EAAE,OAAO,EACf,MAAM,EAAE,cAAc,EACtB,GAAG,EAAE,SAAS,EACd,WAAW,CAAC,EAAE,UAAU,GAAG,KAAK;IASlC,UAAU,QAAe,GAAG,OAAO,GAAG,QAAQ,QAAQ,GAAG,QAAQ,mBAkG/D;IAEF,UAAU,QAAe,GAAG,OAAO,GAAG,mBAuCpC;IAEF,0BAA0B,cAAqB,GAAG,mBAwBhD;IAEF,sBAAsB,eACR,MAAM,mBACD,MAAM,mBAiBvB;IAEF,mBAAmB,QAAe,GAAG,OAAO,GAAG,mBAqC7C;IAEF,wBAAwB,QAAe,GAAG,OAAO,GAAG,mBA2ClD;IAEF,OAAO,QAAe,GAAG,OAAO,GAAG,mBAgCjC;IAEF,IAAI,YA6EF;CACH"}
@@ -1 +1 @@
1
- "use strict";var __awaiter=this&&this.__awaiter||function(e,n,r,l){return new(r=r||Promise)(function(i,t){function s(e){try{o(l.next(e))}catch(e){t(e)}}function a(e){try{o(l.throw(e))}catch(e){t(e)}}function o(e){var t;e.done?i(e.value):((t=e.value)instanceof r?t:new r(function(e){e(t)})).then(s,a)}o((l=l.apply(e,n||[])).next())})},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.UploadWrapper=void 0;const QueryModel_1=require("../model/QueryModel"),axios_1=__importDefault(require("axios")),mime_1=__importDefault(require("mime"));class UploadWrapper{constructor(e,t,i,s,a){this.uploadAdmin="",this.isProd=!1,this.config={baseConfig:[],opsConfig:{read:[],create:[],update:[],delete:[]}},this.storageType="firebase",this.modelChecker=(e,t)=>!(!e.assetCollectionName||!e.assetParentId||!t&&!e.assetFileName),this.initUpload=(d,c,u)=>__awaiter(this,void 0,void 0,function*(){try{const l=d.body.data;if(this.modelChecker(l)&&(d.file||l.downloadLink)){var e=(0,QueryModel_1.checkBaseConfig)(this.config,d),t=(0,QueryModel_1.checkOpsConfig)(this.config,u,d,l);if(e&&t){const n=this.lib.randomUUID();if("aws"===this.storageType){var i,s=(n,r)=>new Promise((a,o)=>__awaiter(this,void 0,void 0,function*(){var e="buffer"===n,t=new this.uploadAdmin.S3;let i;e||(s=d.file.originalname.split(".").pop(),i="mbtiles"===s?"application/vnd.mapbox-vector-tile":mime_1.default.getType(s)||d.file.mimetype);var s={Bucket:`${l.bucketName}/${l.assetCollectionName}/`+l.assetParentId,Key:e?l.assetFileName:d.file.originalname,Body:e?r.data:d.file.buffer,ContentType:e?r.headers["content-type"]:i};t.upload(s,function(e){e?o(e):a("aws")})}));l.downloadLink?(i=yield(0,axios_1.default)({method:"GET",url:l.downloadLink,responseType:"arraybuffer"})).data&&(yield s("buffer",i)):yield s("file")}else{const r=this.uploadAdmin.storage().bucket();var a,o=(a,o)=>new Promise((i,s)=>__awaiter(this,void 0,void 0,function*(){var e="buffer"===a,t={contentType:e?o.headers["content-type"]:d.file.mimetype,customMetadata:{"file-name":"buffer"===a?l.assetFileName:d.file.originalname},metadata:{firebaseStorageDownloadTokens:n}},t=r.file(`${l.assetCollectionName}/${l.assetParentId}/`+l.assetFileName).createWriteStream({metadata:t});t.end(e?o.data:d.file.buffer),t.on("error",s),t.on("finish",i)}));l.downloadLink?(a=yield(0,axios_1.default)({method:"GET",url:l.downloadLink,responseType:"arraybuffer"})).data&&(yield o("buffer",a)):yield o("file")}c.send({data:n})}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.initDelete=(o,n)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s,a=o.body.data;this.modelChecker(a)?(e=(0,QueryModel_1.checkBaseConfig)(this.config,o),t=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",o,a),e&&t?("aws"===this.storageType?(i=new this.uploadAdmin.S3,s={Bucket:`${a.bucketName}/${a.assetCollectionName}/`+a.assetParentId,Key:a.assetFileName},yield i.deleteObject(s).promise()):yield this.uploadAdmin.storage().bucket().file(`${a.assetCollectionName}/${a.assetParentId}/`+a.assetFileName).delete(),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.deleteGoogleFolderContents=o=>__awaiter(this,void 0,void 0,function*(){try{var e={prefix:o.name,autoPaginate:!1},[t]=yield this.uploadAdmin.storage().bucket().getFiles(e),i=[];for(const s of t)i.push(s.delete());for(const a of t)a.name.endsWith("/")&&i.push(this.deleteGoogleFolderContents(a));yield Promise.all(i)}catch(e){}}),this.deleteAWSFolderContent=(a,o)=>__awaiter(this,void 0,void 0,function*(){try{var e=new this.uploadAdmin.S3,t=(yield e.listObjectsV2({Bucket:a,Prefix:o}).promise()).Contents.map(e=>e.Key),i=[];for(const s of t)i.push(e.deleteObject({Bucket:a,Key:s}).promise());yield Promise.all(i)}catch(e){}}),this.initDeleteDirectory=(a,o)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s=a.body.data;this.modelChecker(s,!0)?(e=(0,QueryModel_1.checkBaseConfig)(this.config,a),t=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",a,s),e&&t?("aws"===this.storageType?yield this.deleteAWSFolderContent(""+s.bucketName,`${s.assetCollectionName}/${s.assetParentId}/`):(i=this.uploadAdmin.storage().bucket().file(`${s.assetCollectionName}/${s.assetParentId}/`),yield this.deleteGoogleFolderContents(i)),o.send({success:!0})):o.status(400).json({err:"Invalid Security Configuration"})):o.status(400).json({err:"Invalid Fields"})}catch(e){o.status(400).json({err:e})}}),this.initGet=(n,r)=>__awaiter(this,void 0,void 0,function*(){try{const{bucketName:e,assetCollectionName:t,assetParentId:i,fileName:s}=n.params;if("aws"===this.storageType){const a=new this.uploadAdmin.S3,o={Bucket:e,Key:t+`/${i}/`+s};a.headObject(o,e=>__awaiter(this,void 0,void 0,function*(){if(e&&"NotFound"===e.code)r.status(404).send({message:"File not found"});else{var t=yield a.getObject(o).promise(),i=s.split(".").pop();let e;e="mbtiles"===i?"application/vnd.mapbox-vector-tile":mime_1.default.getType(i)||t.ContentType,r.set("Content-Type",e),r.send(t.Body)}}))}}catch(e){r.status(400).json({err:e})}}),this.init=()=>{var e=this.lib.express.Router();return this.lib.cors&&e.use(this.lib.cors()),e.post("/upload",this.lib.multer.single("file"),(e,t)=>{this.initUpload(e,t,"create")}),e.post("/update",this.lib.multer.single("file"),(e,t)=>{this.initUpload(e,t,"update")}),e.post("/delete",this.initDelete),e.post("/deleteDirectory",this.initDeleteDirectory),e.get("/get/:bucketName/:assetCollectionName/:assetParentId/:fileName",this.initGet),e},this.uploadAdmin=e,this.isProd=t,this.config=i,this.lib=s,this.storageType=null!=a?a:"firebase"}}exports.UploadWrapper=UploadWrapper;
1
+ "use strict";var __awaiter=this&&this.__awaiter||function(e,n,r,l){return new(r=r||Promise)(function(i,t){function s(e){try{a(l.next(e))}catch(e){t(e)}}function o(e){try{a(l.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((l=l.apply(e,n||[])).next())})},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.UploadWrapper=void 0;const QueryModel_1=require("../model/QueryModel"),axios_1=__importDefault(require("axios")),mime_1=__importDefault(require("mime"));class UploadWrapper{constructor(e,t,i,s,o){this.uploadAdmin="",this.isProd=!1,this.config={baseConfig:[],opsConfig:{read:[],create:[],update:[],delete:[]}},this.storageType="firebase",this.modelChecker=(e,t)=>!(!e.assetCollectionName||!e.assetParentId||!t&&!e.assetFileName),this.initUpload=(d,c,u)=>__awaiter(this,void 0,void 0,function*(){try{const l=d.body.data;if(this.modelChecker(l)&&(d.file||l.downloadLink)){var e=(0,QueryModel_1.checkBaseConfig)(this.config,d),t=(0,QueryModel_1.checkOpsConfig)(this.config,u,d,l);if(e&&t){const n=this.lib.randomUUID();if("aws"===this.storageType){var i,s=(n,r)=>new Promise((o,a)=>__awaiter(this,void 0,void 0,function*(){var e="buffer"===n,t=new this.uploadAdmin.S3;let i;e||(s=d.file.originalname.split(".").pop(),i="mbtiles"===s?"application/vnd.mapbox-vector-tile":mime_1.default.getType(s)||d.file.mimetype);var s={Bucket:`${l.bucketName}/${l.assetCollectionName}/`+l.assetParentId,Key:e?l.assetFileName:d.file.originalname,Body:e?r.data:d.file.buffer,ContentType:e?r.headers["content-type"]:i};t.upload(s,function(e){e?a(e):o("aws")})}));l.downloadLink?(i=yield(0,axios_1.default)({method:"GET",url:l.downloadLink,responseType:"arraybuffer"})).data&&(yield s("buffer",i)):yield s("file")}else{const r=this.uploadAdmin.storage().bucket();var o,a=(o,a)=>new Promise((i,s)=>__awaiter(this,void 0,void 0,function*(){var e="buffer"===o,t={contentType:e?a.headers["content-type"]:d.file.mimetype,customMetadata:{"file-name":"buffer"===o?l.assetFileName:d.file.originalname},metadata:{firebaseStorageDownloadTokens:n}},t=r.file(`${l.assetCollectionName}/${l.assetParentId}/`+l.assetFileName).createWriteStream({metadata:t});t.end(e?a.data:d.file.buffer),t.on("error",s),t.on("finish",i)}));l.downloadLink?(o=yield(0,axios_1.default)({method:"GET",url:l.downloadLink,responseType:"arraybuffer"})).data&&(yield a("buffer",o)):yield a("file")}c.send({data:n})}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.initDelete=(a,n)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s,o=a.body.data;this.modelChecker(o)?(e=(0,QueryModel_1.checkBaseConfig)(this.config,a),t=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",a,o),e&&t?("aws"===this.storageType?(i=new this.uploadAdmin.S3,s={Bucket:`${o.bucketName}/${o.assetCollectionName}/`+o.assetParentId,Key:o.assetFileName},yield i.deleteObject(s).promise()):yield this.uploadAdmin.storage().bucket().file(`${o.assetCollectionName}/${o.assetParentId}/`+o.assetFileName).delete(),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.deleteGoogleFolderContents=a=>__awaiter(this,void 0,void 0,function*(){try{var e={prefix:a.name,autoPaginate:!1},[t]=yield this.uploadAdmin.storage().bucket().getFiles(e),i=[];for(const s of t)i.push(s.delete());for(const o of t)o.name.endsWith("/")&&i.push(this.deleteGoogleFolderContents(o));yield Promise.all(i)}catch(e){}}),this.deleteAWSFolderContent=(o,a)=>__awaiter(this,void 0,void 0,function*(){try{var e=new this.uploadAdmin.S3,t=(yield e.listObjectsV2({Bucket:o,Prefix:a}).promise()).Contents.map(e=>e.Key),i=[];for(const s of t)i.push(e.deleteObject({Bucket:o,Key:s}).promise());yield Promise.all(i)}catch(e){}}),this.initDeleteDirectory=(o,a)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s=o.body.data;this.modelChecker(s,!0)?(e=(0,QueryModel_1.checkBaseConfig)(this.config,o),t=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",o,s),e&&t?("aws"===this.storageType?yield this.deleteAWSFolderContent(""+s.bucketName,`${s.assetCollectionName}/${s.assetParentId}/`):(i=this.uploadAdmin.storage().bucket().file(`${s.assetCollectionName}/${s.assetParentId}/`),yield this.deleteGoogleFolderContents(i)),a.send({success:!0})):a.status(400).json({err:"Invalid Security Configuration"})):a.status(400).json({err:"Invalid Fields"})}catch(e){a.status(400).json({err:e})}}),this.initBatchDeleteDirectory=(o,a)=>__awaiter(this,void 0,void 0,function*(){try{const s=o.body.data;var e,t,i=JSON.parse(s.assetParentId);this.modelChecker(s,!0)?(e=(0,QueryModel_1.checkBaseConfig)(this.config,o),t=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",o,s),e&&t?(yield Promise.all(i.map(t=>__awaiter(this,void 0,void 0,function*(){var e;"aws"===this.storageType?yield this.deleteAWSFolderContent(""+s.bucketName,s.assetCollectionName+`/${t}/`):(e=this.uploadAdmin.storage().bucket().file(s.assetCollectionName+`/${t}/`),yield this.deleteGoogleFolderContents(e))}))),a.send({success:!0})):a.status(400).json({err:"Invalid Security Configuration"})):a.status(400).json({err:"Invalid Fields"})}catch(e){a.status(400).json({err:e})}}),this.initGet=(n,r)=>__awaiter(this,void 0,void 0,function*(){try{const{bucketName:e,assetCollectionName:t,assetParentId:i,fileName:s}=n.params;if("aws"===this.storageType){const o=new this.uploadAdmin.S3,a={Bucket:e,Key:t+`/${i}/`+s};o.headObject(a,e=>__awaiter(this,void 0,void 0,function*(){if(e&&"NotFound"===e.code)r.status(404).send({message:"File not found"});else{var t=yield o.getObject(a).promise(),i=s.split(".").pop();let e;e="mbtiles"===i?"application/vnd.mapbox-vector-tile":mime_1.default.getType(i)||t.ContentType,r.set("Content-Type",e),r.send(t.Body)}}))}}catch(e){r.status(400).json({err:e})}}),this.init=()=>{var e=this.lib.express.Router();return this.lib.cors&&e.use(this.lib.cors()),e.post("/upload",this.lib.multer.single("file"),(e,t)=>{this.initUpload(e,t,"create")}),e.post("/update",this.lib.multer.single("file"),(e,t)=>{this.initUpload(e,t,"update")}),e.post("/delete",this.initDelete),e.post("/deleteDirectory",this.initDeleteDirectory),e.post("/batchDeleteDirectory",this.initBatchDeleteDirectory),e.get("/get/:bucketName/:assetCollectionName/:assetParentId/:fileName",this.initGet),e},this.uploadAdmin=e,this.isProd=t,this.config=i,this.lib=s,this.storageType=null!=o?o:"firebase"}}exports.UploadWrapper=UploadWrapper;
@@ -0,0 +1,9 @@
1
+ export declare const compareUpdatedFields: (prevData: any, currentData: any, idPrefix: string) => {
2
+ workflowUpdateFields: {
3
+ [x: string]: any;
4
+ };
5
+ updatedFields: {
6
+ [key: string]: any;
7
+ };
8
+ };
9
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/apis/utils.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,oBAAoB,aACrB,GAAG,eACA,GAAG,YACN,MAAM;;;;;;;CAejB,CAAC"}
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.compareUpdatedFields=void 0;const compareUpdatedFields=(d,s,e)=>{const t={};return Object.keys(s).forEach(e=>{s[e]!==d[e]&&(t[e]=s[e])}),{workflowUpdateFields:Object.assign(Object.assign({},t),{[e]:s[e]}),updatedFields:t}};exports.compareUpdatedFields=compareUpdatedFields;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blixify-server",
3
- "version": "0.1.35",
3
+ "version": "0.1.37",
4
4
  "license": "MIT",
5
5
  "main": "dist/apis/index.js",
6
6
  "private": false,
@@ -37,7 +37,8 @@
37
37
  "dependencies": {
38
38
  "axios": "^1.4.0",
39
39
  "mime": "^3.0.0",
40
- "moment": "^2.29.4"
40
+ "moment": "^2.29.4",
41
+ "moment-timezone": "^0.5.43"
41
42
  },
42
43
  "devDependencies": {
43
44
  "@types/express": "^4.17.14",