blixify-server 0.1.46 → 0.1.47
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,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,
|
|
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,mBAoPlC;IAEF,IAAI,YA+EF;CACH"}
|
package/dist/apis/fbWrapper.js
CHANGED
|
@@ -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=(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=(D,Y)=>__awaiter(this,void 0,void 0,function*(){var e,t,i,s,a;try{var d=(0,QueryModel_1.checkBaseConfig)(this.config,D);if(d){var o=D.body.aggregate,n=null!=(e=D.body.query)?e:[];if(o){const C=null!=(t=o.queryId)?t:"";var r=null!=(i=o.dateId)?i:"baseUpdatedAt",l=null!=(s=o.range)?s:[],c=[];for(const j of l){let e=this.fbAdmin.firestore().collection(this.collection);var u=j.split("-"),[h,f=(0,moment_1.default)().format("DD/MM/YYYY HH:mm:ss")]=u,y=2===u.length,b=(0,moment_1.default)(h,"DD/MM/YYYY").isValid(),m=(0,moment_1.default)(f,"DD/MM/YYYY").isValid(),p=(0,moment_1.default)(h,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss"),v=(0,moment_1.default)(f,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss");if(!b||!m)return void Y.status(400).json({err:"Invalid Aggregate Range Configuration"});var g=y?j:"Begining until "+(0,moment_1.default)(p).format("DD/MM/YYYY");e=y?e.where(r,">=",(0,moment_1.default)(p).toDate()).where(r,"<=",(0,moment_1.default)(v).toDate()):e.where(r,"<",(0,moment_1.default)(p).toDate());let t=0;switch(o.type){case"count":var _=yield e.count().get();t=_.data().count;break;case"value":var A=yield(e=(e=e.limit(1)).orderBy(r,"desc")).get();A&&A.forEach(e=>{e=e.data(),e=JSON.parse(JSON.stringify(e));t=e[C]})}c.push({_id:g,value:t})}Y.send({data:c})}else{let o=this.fbAdmin.firestore().collection(this.collection),r="";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&&(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)}}),D.body.stopLimit||(o=o.limit(null!=(a=D.body.limit)?a:10)),D.body.sort){var w=D.body.sort;if(r&&w.sortId!==r)return void Y.status(400).json({err:"Invalid Search Sort Configuration"});r||(o=o.orderBy(w.sortId,"asc"===w.type?"asc":"desc"))}var M,k=yield(o=D.body.cursor?o.startAfter(D.body.cursor):o).get();let t=!0;const I=[];if(k&&k.forEach(e=>{e=e.data();I.push(e),(0,QueryModel_1.checkOpsConfig)(this.config,"read",D,e)||(t=!1)}),d&&t){let t="";if(I.length===D.body.limit){let e="id";D.body.sort&&(M=D.body.sort,e=M.sortId),t=I[D.body.limit-1][e]}Y.send({data:I,count:t})}else Y.status(400).json({err:"Invalid Security Configuration"})}}else Y.status(400).json({err:"Invalid Security Configuration"})}catch(e){Y.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{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"),utils_1=require("./utils");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=(l,c,u)=>__awaiter(this,void 0,void 0,function*(){try{const r=this.fbAdmin.firestore().batch(),o=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 o.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=o.doc(t.id);r.set(e,Object.assign({},t))}))),yield r.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=(o,a,d)=>__awaiter(this,void 0,void 0,function*(){var e,t;try{if(this.modelChecker(o.body.data)){var i=(0,QueryModel_1.checkBaseConfig)(this.config,o),s=(0,QueryModel_1.checkOpsConfig)(this.config,"create",o,o.body.data);if(i&&s){var r=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 r.where(o.body.unique,"==",null==(t=o.body.data)?void 0:t[o.body.unique]).get()).empty)return void a.status(400).json({err:`Unique:${o.body.data[o.body.unique]} exists`});d&&(yield d(o.body.data)),yield r.doc(o.body.data.id).set(o.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=(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=(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,r=this.parseModel(null!=(e=b.body.data)?e:[]),o=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=[];o.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()}),r&&(t=Object.assign(Object.assign({},r),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 r=s.data(),o=(0,QueryModel_1.checkBaseConfig)(this.config,f),a=(0,QueryModel_1.checkOpsConfig)(this.config,"update",f,r);if(o&&a){if(f.body.unique&&null!=r&&r[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)(r,f.body.data);f.body.sensitive?(0,moment_1.default)(f.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(1e3*r.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 r=[];e&&e.forEach(e=>{e=e.data();r.push(e),(0,QueryModel_1.checkOpsConfig)(this.config,"delete",c,e)||(t=!1)}),i&&t?(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(),u.send({success:c.body.id})):u.status(400).json({err:"Invalid Security Configuration"})}else{let r=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&&(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)}});var o=yield r.get(),a=(0,QueryModel_1.checkBaseConfig)(this.config,c);let t=!0;const l=[];o&&o.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?(o.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=(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,Y)=>__awaiter(this,void 0,void 0,function*(){var e,t,i,s,a;try{var d=(0,QueryModel_1.checkBaseConfig)(this.config,D);if(d){var o=D.body.aggregate,n=null!=(e=D.body.query)?e:[];if(o){const C=null!=(t=o.queryId)?t:"";var l=null!=(i=o.dateId)?i:"baseUpdatedAt",r=null!=(s=o.range)?s:[],c=[];for(const j of r){let r=this.fbAdmin.firestore().collection(this.collection);n&&0<n.length&&n.map(e=>{var t,i=null!=(t=e.queryId)?t:"",s=e.value;switch(e.type){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)}});var u=j.split("-"),[h,f=(0,moment_1.default)().format("DD/MM/YYYY HH:mm:ss")]=u,y=2===u.length,b=(0,moment_1.default)(h,"DD/MM/YYYY").isValid(),m=(0,moment_1.default)(f,"DD/MM/YYYY").isValid(),p=(0,moment_1.default)(h,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss"),v=(0,moment_1.default)(f,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss");if(!b||!m)return void Y.status(400).json({err:"Invalid Aggregate Range Configuration"});var g=y?j:"Begining until "+(0,moment_1.default)(p).format("DD/MM/YYYY");r=y?r.where(l,">=",(0,moment_1.default)(p).toDate()).where(l,"<=",(0,moment_1.default)(v).toDate()):r.where(l,"<",(0,moment_1.default)(p).toDate());let t=0;switch(o.type){case"count":var _=yield r.count().get();t=_.data().count;break;case"value":var w=yield(r=(r=r.limit(1)).orderBy(l,"desc")).get();w&&w.forEach(e=>{e=e.data(),e=JSON.parse(JSON.stringify(e));t=e[C]})}c.push({_id:g,value:t})}Y.send({data:c})}else{let r=this.fbAdmin.firestore().collection(this.collection),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.body.stopLimit||(r=r.limit(null!=(a=D.body.limit)?a:10)),D.body.sort){var A=D.body.sort;if(o&&A.sortId!==o)return void Y.status(400).json({err:"Invalid Search Sort Configuration"});o||(r=r.orderBy(A.sortId,"asc"===A.type?"asc":"desc"))}var k,M=yield(r=D.body.cursor?r.startAfter(D.body.cursor):r).get();let t=!0;const I=[];if(M&&M.forEach(e=>{e=e.data();I.push(e),(0,QueryModel_1.checkOpsConfig)(this.config,"read",D,e)||(t=!1)}),d&&t){let t="";if(I.length===D.body.limit){let e="id";D.body.sort&&(k=D.body.sort,e=k.sortId),t=I[D.body.limit-1][e]}Y.send({data:I,count:t})}else Y.status(400).json({err:"Invalid Security Configuration"})}}else Y.status(400).json({err:"Invalid Security Configuration"})}catch(e){Y.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;
|