blixify-server 0.1.42 → 0.1.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"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,gCAoItC;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC,gCA6EtC;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
+ {"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,gCAoItC;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC,gCAkFtC;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{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))["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)),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;
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=(l,c,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(l.body.data)){let t=!0;if(l.body.data.map(e=>{this.modelChecker(e)||(t=!1)}),t){var e=l.body.data,i=(0,QueryModel_1.checkBaseConfig)(this.config,l);let t=!0;const a=[],d=[];if(e.map(e=>{a.push(e.id),(0,QueryModel_1.checkOpsConfig)(this.config,"update",l,e)||(t=!1),l.body.unique&&null!=e&&e[l.body.unique]&&d.push(null==e?void 0:e[l.body.unique])}),i&&t){if(0<d.length){var s=yield r.find(l.body.unique,"in",d).get();const n=[];if(s.empty||s.forEach(e=>{e=e.data();n.push(e)}),0<n.length)return void c.status(400).json({err:`Unique:${n.map(e=>null==e?void 0:e[l.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(),c.send({success:a})}else c.status(400).json({err:"Invalid Security Configuration"})}else c.status(400).json({err:"Invalid Model Structure"})}else c.status(400).json({err:"Invalid Fields"})}catch(e){c.status(400).json({err:e})}}),this.initCreate=(r,a,d)=>__awaiter(this,void 0,void 0,function*(){var e,t;try{if(this.modelChecker(r.body.data)){var i=(0,QueryModel_1.checkBaseConfig)(this.config,r),s=(0,QueryModel_1.checkOpsConfig)(this.config,"create",r,r.body.data);if(i&&s){var o=this.fbAdmin.firestore().collection(this.collection);if(r.body.data.baseUpdatedAt=(0,moment_1.default)().toDate(),r.body.unique&&null!=(e=r.body.data)&&e[r.body.unique])if(!(yield o.where(r.body.unique,"==",null==(t=r.body.data)?void 0:t[r.body.unique]).get()).empty)return void a.status(400).json({err:`Unique:${r.body.data[r.body.unique]} exists`});d&&(yield d(r.body.data)),yield o.doc(r.body.data.id).set(r.body.data),a.send({success:!0})}else a.status(400).json({err:"Invalid Security Configuration"})}else a.status(400).json({err:"Invalid Model Structure"})}catch(e){a.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 l=a?this.fbAdmin.firestore.Filter.or(...h):this.fbAdmin.firestore.Filter.and(...h),c=yield n.where(l).get(),u=(0,QueryModel_1.checkBaseConfig)(this.config,b);let i=!0;const f=[],y=[];if(c&&c.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))["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=(f,y,b)=>__awaiter(this,void 0,void 0,function*(){var e;try{var t=this.modelChecker(f.body.data)&&f.body.id;if(t){var i=this.fbAdmin.firestore().collection(this.collection),s=yield i.doc(t).get();if(s.exists){var o=s.data(),r=(0,QueryModel_1.checkBaseConfig)(this.config,f),a=(0,QueryModel_1.checkOpsConfig)(this.config,"update",f,o);if(r&&a){if(f.body.unique&&null!=o&&o[f.body.unique]){const h="id"===f.body.unique?f.body.id:null==(e=f.body.data)?void 0:e[f.body.unique];var d=yield i.where(f.body.unique,"==",h).get();d.empty||d.forEach(e=>{e=e.data();f.body.id!==e.id&&y.status(400).json({err:`${null!==h&&void 0!==h?h:"Unique Fields Data"} exists`})})}var n,l,{workflowUpdateFields:c,updatedFields:u}=(0,utils_1.compareUpdatedFields)(o,f.body.data);f.body.sensitive?(0,moment_1.default)(f.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(1e3*o.baseUpdatedAt.seconds))?(b&&(yield b(c)),n=this.parseModel(u),yield i.doc(t).update(Object.assign({},n)),y.send({success:!0})):y.status(400).json({err:"Refresh Sensitive Model"}):(b&&(yield b(c)),l=this.parseModel(u),yield i.doc(t).update(Object.assign({},l)),y.send({success:!0}))}else y.status(400).json({err:"Invalid Security Configuration"})}else y.status(400).json({err:"Invalid Security Configuration"})}else y.status(400).json({err:"Invalid Model Structure"})}catch(e){y.status(400).json({err:e})}}),this.initBatchDelete=(c,u,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 o=[];e&&e.forEach(e=>{e=e.data();o.push(e),(0,QueryModel_1.checkOpsConfig)(this.config,"delete",c,e)||(t=!1)}),i&&t?(h&&(yield h(o)),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(),u.send({success:c.body.id})):u.status(400).json({err:"Invalid Security Configuration"})}else{let o=this.fbAdmin.firestore().collection(this.collection);var s=c.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,c);let t=!0;const l=[];r&&r.forEach(e=>{e=e.data();n.push(e.id),l.push(e),(0,QueryModel_1.checkOpsConfig)(this.config,"delete",c,e)||(t=!1)}),h&&(yield h(l)),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,l=null!=(e=q.body.query)?e:[];let r="";if(l&&0<l.length&&l.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 c=null!=(i=n.dateId)?i:"baseUpdatedAt",u=null!=(s=n.range)?s:[],h=[];for(const D of u){var f=D.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(),v=(0,moment_1.default)(b,"DD/MM/YYYY").isValid(),g=(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||!v)return void F.status(400).json({err:"Invalid Aggregate Range Configuration"});var A=m?D:"Begining until "+(0,moment_1.default)(y).format("DD/MM/YYYY");o=m?o.where(c,">=",(0,moment_1.default)(g).toDate()).where(c,"<=",(0,moment_1.default)(_).toDate()):o.where(c,"<",(0,moment_1.default)(g).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(c,"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 Y=[];if(j&&j.forEach(e=>{e=e.data();Y.push(e),(0,QueryModel_1.checkOpsConfig)(this.config,"read",q,e)||(t=!1)}),d&&t){let t="";if(Y.length===q.body.limit){let e="id";q.body.sort&&(C=q.body.sort,e=C.sortId),t=Y[q.body.limit-1][e]}F.send({data:Y,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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blixify-server",
3
- "version": "0.1.42",
3
+ "version": "0.1.43",
4
4
  "license": "MIT",
5
5
  "main": "dist/apis/index.js",
6
6
  "private": false,