blixify-server 0.1.65 → 0.1.67
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.
- package/dist/apis/fbWrapper.d.ts.map +1 -1
- package/dist/apis/fbWrapper.js +820 -1
- package/dist/apis/index.js +13 -1
- package/dist/apis/mondayWrapper.js +387 -14
- package/dist/apis/mongoWrapper.js +943 -1
- package/dist/apis/security.js +80 -1
- package/dist/apis/uploadWrapper.js +396 -1
- package/dist/apis/utils.js +21 -1
- package/dist/model/QueryModel.js +118 -1
- package/dist/model/SecurityConfig.js +2 -1
- package/package.json +2 -2
package/dist/apis/fbWrapper.js
CHANGED
|
@@ -1 +1,820 @@
|
|
|
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.push(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;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.FirebaseWrapper = void 0;
|
|
16
|
+
const firestore_1 = require("firebase-admin/firestore");
|
|
17
|
+
const moment_1 = __importDefault(require("moment"));
|
|
18
|
+
const QueryModel_1 = require("../model/QueryModel");
|
|
19
|
+
const utils_1 = require("./utils");
|
|
20
|
+
/**
|
|
21
|
+
* @Wrapper
|
|
22
|
+
*
|
|
23
|
+
*/
|
|
24
|
+
class FirebaseWrapper {
|
|
25
|
+
constructor(fbAdmin, collection, isProd, config, modelChecker, lib) {
|
|
26
|
+
this.fbAdmin = "";
|
|
27
|
+
this.collection = "";
|
|
28
|
+
this.isProd = false;
|
|
29
|
+
this.config = {
|
|
30
|
+
baseConfig: [],
|
|
31
|
+
opsConfig: {
|
|
32
|
+
read: [],
|
|
33
|
+
create: [],
|
|
34
|
+
update: [],
|
|
35
|
+
delete: [],
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
// eslint-disable-next-line
|
|
39
|
+
this.modelChecker = (obj) => {
|
|
40
|
+
return false;
|
|
41
|
+
};
|
|
42
|
+
this.parseModel = (data) => {
|
|
43
|
+
delete data["id"];
|
|
44
|
+
data["baseUpdatedAt"] = (0, moment_1.default)().toDate();
|
|
45
|
+
return data;
|
|
46
|
+
};
|
|
47
|
+
this.initBatchCreate = (req, res, workflow) => __awaiter(this, void 0, void 0, function* () {
|
|
48
|
+
try {
|
|
49
|
+
const batch = this.fbAdmin.firestore().batch();
|
|
50
|
+
const fbCollection = this.fbAdmin.firestore().collection(this.collection);
|
|
51
|
+
if (Array.isArray(req.body.data)) {
|
|
52
|
+
let valid = true;
|
|
53
|
+
req.body.data.map((eachData) => {
|
|
54
|
+
if (!this.modelChecker(eachData)) {
|
|
55
|
+
valid = false;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
if (valid) {
|
|
59
|
+
const dataList = req.body.data;
|
|
60
|
+
const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
|
|
61
|
+
let validOpsConfig = true;
|
|
62
|
+
const ids = [];
|
|
63
|
+
const uniqueDataListField = [];
|
|
64
|
+
dataList.map((eachData) => {
|
|
65
|
+
ids.push(eachData.id);
|
|
66
|
+
if (!(0, QueryModel_1.checkOpsConfig)(this.config, "update", req, eachData)) {
|
|
67
|
+
validOpsConfig = false;
|
|
68
|
+
}
|
|
69
|
+
if (req.body.unique && (eachData === null || eachData === void 0 ? void 0 : eachData[req.body.unique]))
|
|
70
|
+
uniqueDataListField.push(eachData === null || eachData === void 0 ? void 0 : eachData[req.body.unique]);
|
|
71
|
+
});
|
|
72
|
+
if (!validBaseConfig || !validOpsConfig) {
|
|
73
|
+
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (uniqueDataListField.length > 0) {
|
|
77
|
+
const existsDataSnapshot = yield fbCollection
|
|
78
|
+
.find(req.body.unique, "in", uniqueDataListField)
|
|
79
|
+
.get();
|
|
80
|
+
const existsDataList = [];
|
|
81
|
+
if (!existsDataSnapshot.empty) {
|
|
82
|
+
existsDataSnapshot.forEach((eachDataDoc) => {
|
|
83
|
+
const eachData = eachDataDoc.data();
|
|
84
|
+
existsDataList.push(eachData);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if (existsDataList.length > 0) {
|
|
88
|
+
res.status(400).json({
|
|
89
|
+
err: `Unique:${existsDataList.map((eachData) => eachData === null || eachData === void 0 ? void 0 : eachData[req.body.unique])} exists`,
|
|
90
|
+
});
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (workflow)
|
|
95
|
+
yield workflow(dataList);
|
|
96
|
+
yield Promise.all(dataList.map((eachData) => __awaiter(this, void 0, void 0, function* () {
|
|
97
|
+
eachData["baseUpdatedAt"] = (0, moment_1.default)().toDate();
|
|
98
|
+
const batchRef = fbCollection.doc(eachData["id"]);
|
|
99
|
+
batch.set(batchRef, Object.assign({}, eachData));
|
|
100
|
+
})));
|
|
101
|
+
yield batch.commit();
|
|
102
|
+
res.send({ success: ids });
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
res.status(400).json({ err: "Invalid Model Structure" });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
res.status(400).json({ err: "Invalid Fields" });
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
catch (err) {
|
|
113
|
+
res.status(400).json({ err: err });
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
this.initCreate = (req, res, workflow) => __awaiter(this, void 0, void 0, function* () {
|
|
117
|
+
var _a, _b;
|
|
118
|
+
try {
|
|
119
|
+
const valid = this.modelChecker(req.body.data);
|
|
120
|
+
if (valid) {
|
|
121
|
+
//INFO : Security Checker
|
|
122
|
+
const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
|
|
123
|
+
const validOpsConfig = (0, QueryModel_1.checkOpsConfig)(this.config, "create", req, req.body.data);
|
|
124
|
+
if (!validBaseConfig || !validOpsConfig) {
|
|
125
|
+
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const fbCollection = this.fbAdmin
|
|
129
|
+
.firestore()
|
|
130
|
+
.collection(this.collection);
|
|
131
|
+
req.body.data["baseUpdatedAt"] = (0, moment_1.default)().toDate();
|
|
132
|
+
if (req.body.unique && ((_a = req.body.data) === null || _a === void 0 ? void 0 : _a[req.body.unique])) {
|
|
133
|
+
const querySnapshot = yield fbCollection
|
|
134
|
+
.where(req.body.unique, "==", (_b = req.body.data) === null || _b === void 0 ? void 0 : _b[req.body.unique])
|
|
135
|
+
.get();
|
|
136
|
+
if (!querySnapshot.empty) {
|
|
137
|
+
res
|
|
138
|
+
.status(400)
|
|
139
|
+
.json({ err: `Unique:${req.body.data[req.body.unique]} exists` });
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (workflow)
|
|
144
|
+
yield workflow(req.body.data);
|
|
145
|
+
yield fbCollection.doc(req.body.data["id"]).set(req.body.data);
|
|
146
|
+
res.send({ success: true });
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
res.status(400).json({ err: "Invalid Model Structure" });
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
catch (err) {
|
|
153
|
+
res.status(400).json({ err: err });
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
this.initGet = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
157
|
+
try {
|
|
158
|
+
const valid = req.body.id;
|
|
159
|
+
if (valid) {
|
|
160
|
+
//INFO : Security Checker
|
|
161
|
+
const dataQuery = yield this.fbAdmin
|
|
162
|
+
.firestore()
|
|
163
|
+
.collection(this.collection)
|
|
164
|
+
.doc(valid)
|
|
165
|
+
.get();
|
|
166
|
+
if (!dataQuery.exists) {
|
|
167
|
+
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
const fbData = dataQuery.data();
|
|
171
|
+
const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
|
|
172
|
+
const validOpsConfig = (0, QueryModel_1.checkOpsConfig)(this.config, "read", req, fbData);
|
|
173
|
+
if (!validBaseConfig || !validOpsConfig) {
|
|
174
|
+
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
res.send({ data: fbData });
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
res.status(400).json({ err: "Invalid Fields" });
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
catch (err) {
|
|
184
|
+
res.status(400).json({ err: err });
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
this.initBatchUpdate = (req, res, workflow) => __awaiter(this, void 0, void 0, function* () {
|
|
188
|
+
var _c, _d;
|
|
189
|
+
const batch = this.fbAdmin.firestore().batch();
|
|
190
|
+
try {
|
|
191
|
+
const valid = this.modelChecker(req.body.data) && req.body.query;
|
|
192
|
+
const data = this.parseModel((_c = req.body.data) !== null && _c !== void 0 ? _c : []);
|
|
193
|
+
const queryList = (_d = req.body.query) !== null && _d !== void 0 ? _d : [];
|
|
194
|
+
const isOr = req.body.isOr || false; // Check if it's an OR query
|
|
195
|
+
const unsetData = req.body.unsetData;
|
|
196
|
+
if (valid) {
|
|
197
|
+
const fbCollection = this.fbAdmin
|
|
198
|
+
.firestore()
|
|
199
|
+
.collection(this.collection);
|
|
200
|
+
//INFO : Step 1 - Query
|
|
201
|
+
const conditions = [];
|
|
202
|
+
queryList.forEach((eachQuery) => {
|
|
203
|
+
var _a;
|
|
204
|
+
const queryId = (_a = eachQuery.queryId) !== null && _a !== void 0 ? _a : "";
|
|
205
|
+
const value = eachQuery.value;
|
|
206
|
+
switch (eachQuery.type) {
|
|
207
|
+
case "=":
|
|
208
|
+
conditions.push(this.fbAdmin.firestore.Filter.where(queryId, "==", value));
|
|
209
|
+
break;
|
|
210
|
+
case "!=":
|
|
211
|
+
conditions.push(this.fbAdmin.firestore.Filter.where(queryId, "!=", value));
|
|
212
|
+
break;
|
|
213
|
+
case ">":
|
|
214
|
+
conditions.push(this.fbAdmin.firestore.Filter.where(queryId, ">", value));
|
|
215
|
+
break;
|
|
216
|
+
case "<":
|
|
217
|
+
conditions.push(this.fbAdmin.firestore.Filter.where(queryId, "<", value));
|
|
218
|
+
break;
|
|
219
|
+
case "><":
|
|
220
|
+
if (Array.isArray(value) && value.length >= 2) {
|
|
221
|
+
conditions.push(this.fbAdmin.firestore.Filter.where(queryId, ">=", value[0]), this.fbAdmin.firestore.Filter.where(queryId, "<=", value[1]));
|
|
222
|
+
}
|
|
223
|
+
break;
|
|
224
|
+
case "in":
|
|
225
|
+
conditions.push(this.fbAdmin.firestore.Filter.where(queryId, "in", value));
|
|
226
|
+
break;
|
|
227
|
+
default:
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
// Construct the final query condition based on isOr
|
|
232
|
+
const finalQueryCondition = isOr
|
|
233
|
+
? this.fbAdmin.firestore.Filter.or(...conditions)
|
|
234
|
+
: this.fbAdmin.firestore.Filter.and(...conditions);
|
|
235
|
+
const fbSnapshot = yield fbCollection.where(finalQueryCondition).get();
|
|
236
|
+
const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
|
|
237
|
+
//INFO : Security Checker
|
|
238
|
+
let validOpsConfig = true;
|
|
239
|
+
const workflowUpdateFieldsList = [];
|
|
240
|
+
const filterDataIds = [];
|
|
241
|
+
if (fbSnapshot) {
|
|
242
|
+
fbSnapshot.forEach((eachDataDoc) => {
|
|
243
|
+
const fbData = eachDataDoc.data();
|
|
244
|
+
filterDataIds.push(fbData.id);
|
|
245
|
+
if (req.body.sensitive) {
|
|
246
|
+
if (!(0, moment_1.default)(req.body.data.baseUpdatedAt).isAfter((0, moment_1.default)(fbData.baseUpdatedAt.seconds * 1000))) {
|
|
247
|
+
res.status(400).json({ err: "Refresh Sensitive Model" });
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
if (!(0, QueryModel_1.checkOpsConfig)(this.config, "update", req, fbData)) {
|
|
251
|
+
validOpsConfig = false;
|
|
252
|
+
}
|
|
253
|
+
const { workflowUpdateFields } = (0, utils_1.compareUpdatedFields)(eachDataDoc, req.body.data);
|
|
254
|
+
workflowUpdateFieldsList.push(workflowUpdateFields);
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
if (!validBaseConfig || !validOpsConfig) {
|
|
258
|
+
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
let condition = {};
|
|
262
|
+
if (unsetData) {
|
|
263
|
+
unsetData.forEach((eachField) => {
|
|
264
|
+
condition[eachField] = this.fbAdmin.firestore.FieldValue.delete();
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
if (data) {
|
|
268
|
+
condition = Object.assign(Object.assign({}, data), condition);
|
|
269
|
+
}
|
|
270
|
+
if (workflow)
|
|
271
|
+
yield workflow(workflowUpdateFieldsList);
|
|
272
|
+
yield Promise.all(filterDataIds.map((eachId) => {
|
|
273
|
+
const batchRef = this.fbAdmin
|
|
274
|
+
.firestore()
|
|
275
|
+
.collection(this.collection)
|
|
276
|
+
.doc(eachId);
|
|
277
|
+
batch.update(batchRef, Object.assign({}, condition));
|
|
278
|
+
}));
|
|
279
|
+
yield batch.commit();
|
|
280
|
+
res.send({ success: filterDataIds });
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
catch (err) {
|
|
284
|
+
res.status(400).json({ err: err });
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
this.initUpdate = (req, res, workflow) => __awaiter(this, void 0, void 0, function* () {
|
|
288
|
+
var _e;
|
|
289
|
+
try {
|
|
290
|
+
const valid = this.modelChecker(req.body.data) && req.body.id;
|
|
291
|
+
if (valid) {
|
|
292
|
+
const fbCollection = this.fbAdmin
|
|
293
|
+
.firestore()
|
|
294
|
+
.collection(this.collection);
|
|
295
|
+
const dataQuery = yield fbCollection.doc(valid).get();
|
|
296
|
+
if (!dataQuery.exists) {
|
|
297
|
+
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
const fbData = dataQuery.data();
|
|
301
|
+
//INFO : Security Checker
|
|
302
|
+
const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
|
|
303
|
+
const validOpsConfig = (0, QueryModel_1.checkOpsConfig)(this.config, "update", req, fbData);
|
|
304
|
+
if (!validBaseConfig || !validOpsConfig) {
|
|
305
|
+
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
if (req.body.unique && (fbData === null || fbData === void 0 ? void 0 : fbData[req.body.unique])) {
|
|
309
|
+
const fields = req.body.unique === "id"
|
|
310
|
+
? req.body.id
|
|
311
|
+
: (_e = req.body.data) === null || _e === void 0 ? void 0 : _e[req.body.unique];
|
|
312
|
+
const fbSnapshot = yield fbCollection
|
|
313
|
+
.where(req.body.unique, "==", fields)
|
|
314
|
+
.get();
|
|
315
|
+
if (!fbSnapshot.empty) {
|
|
316
|
+
fbSnapshot.forEach((eachDataDoc) => {
|
|
317
|
+
const eachData = eachDataDoc.data();
|
|
318
|
+
if (req.body.id !== eachData.id) {
|
|
319
|
+
res.status(400).json({
|
|
320
|
+
err: `${fields !== null && fields !== void 0 ? fields : "Unique Fields Data"} exists`,
|
|
321
|
+
});
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
const { workflowUpdateFields, updatedFields } = (0, utils_1.compareUpdatedFields)(fbData, req.body.data);
|
|
328
|
+
if (req.body.sensitive) {
|
|
329
|
+
if ((0, moment_1.default)(req.body.data.baseUpdatedAt).isAfter((0, moment_1.default)(fbData.baseUpdatedAt.seconds * 1000))) {
|
|
330
|
+
if (workflow)
|
|
331
|
+
yield workflow(workflowUpdateFields);
|
|
332
|
+
const data = this.parseModel(updatedFields);
|
|
333
|
+
yield fbCollection.doc(valid).update(Object.assign({}, data));
|
|
334
|
+
res.send({ success: true });
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
res.status(400).json({ err: "Refresh Sensitive Model" });
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
if (workflow)
|
|
342
|
+
yield workflow(workflowUpdateFields);
|
|
343
|
+
const data = this.parseModel(updatedFields);
|
|
344
|
+
yield fbCollection.doc(valid).update(Object.assign({}, data));
|
|
345
|
+
res.send({ success: true });
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
else {
|
|
349
|
+
res.status(400).json({ err: "Invalid Model Structure" });
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
catch (err) {
|
|
353
|
+
res.status(400).json({ err: err });
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
this.initBatchDelete = (req, res, workflow) => __awaiter(this, void 0, void 0, function* () {
|
|
357
|
+
try {
|
|
358
|
+
const batch = this.fbAdmin.firestore().batch();
|
|
359
|
+
if (Array.isArray(req.body.id)) {
|
|
360
|
+
const fbCollection = this.fbAdmin
|
|
361
|
+
.firestore()
|
|
362
|
+
.collection(this.collection)
|
|
363
|
+
.where("id", "in", req.body.id);
|
|
364
|
+
const fbSnapshot = yield fbCollection.get();
|
|
365
|
+
const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
|
|
366
|
+
//INFO : Security Checker
|
|
367
|
+
let validOpsConfig = true;
|
|
368
|
+
const dataList = [];
|
|
369
|
+
if (fbSnapshot) {
|
|
370
|
+
fbSnapshot.forEach((eachDataDoc) => {
|
|
371
|
+
const eachData = eachDataDoc.data();
|
|
372
|
+
dataList.push(eachData);
|
|
373
|
+
if (!(0, QueryModel_1.checkOpsConfig)(this.config, "delete", req, eachData)) {
|
|
374
|
+
validOpsConfig = false;
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
if (!validBaseConfig || !validOpsConfig) {
|
|
379
|
+
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
if (workflow)
|
|
383
|
+
yield workflow(dataList);
|
|
384
|
+
yield Promise.all(req.body.id.map((eachId) => __awaiter(this, void 0, void 0, function* () {
|
|
385
|
+
const batchRef = yield this.fbAdmin
|
|
386
|
+
.firestore()
|
|
387
|
+
.collection(this.collection)
|
|
388
|
+
.doc(eachId);
|
|
389
|
+
batch.delete(batchRef);
|
|
390
|
+
})));
|
|
391
|
+
yield batch.commit();
|
|
392
|
+
res.send({ success: req.body.id });
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
let fbCollection = this.fbAdmin.firestore().collection(this.collection);
|
|
396
|
+
const queryList = req.body.query;
|
|
397
|
+
const ids = [];
|
|
398
|
+
if (queryList && queryList.length > 0) {
|
|
399
|
+
queryList.map((eachQuery) => {
|
|
400
|
+
var _a;
|
|
401
|
+
const queryId = (_a = eachQuery.queryId) !== null && _a !== void 0 ? _a : "";
|
|
402
|
+
const value = eachQuery.value;
|
|
403
|
+
switch (eachQuery.type) {
|
|
404
|
+
case "search":
|
|
405
|
+
if (eachQuery.searchIds && eachQuery.searchIds.length > 0) {
|
|
406
|
+
fbCollection = fbCollection
|
|
407
|
+
.orderBy(eachQuery.searchIds[0])
|
|
408
|
+
.startAt(value)
|
|
409
|
+
.endAt(value + "\uf8ff");
|
|
410
|
+
}
|
|
411
|
+
break;
|
|
412
|
+
case "=":
|
|
413
|
+
fbCollection = fbCollection.where(queryId, "==", value);
|
|
414
|
+
break;
|
|
415
|
+
case "!=":
|
|
416
|
+
fbCollection = fbCollection.where(queryId, "!=", value);
|
|
417
|
+
break;
|
|
418
|
+
case ">":
|
|
419
|
+
fbCollection = fbCollection.where(queryId, ">", value);
|
|
420
|
+
break;
|
|
421
|
+
case "<":
|
|
422
|
+
fbCollection = fbCollection.where(queryId, "<", value);
|
|
423
|
+
break;
|
|
424
|
+
case "><":
|
|
425
|
+
if (Array.isArray(value) && value.length >= 2) {
|
|
426
|
+
fbCollection = fbCollection
|
|
427
|
+
.where(queryId, ">=", value[0])
|
|
428
|
+
.where(queryId, "<=", value[1]);
|
|
429
|
+
}
|
|
430
|
+
break;
|
|
431
|
+
case "in":
|
|
432
|
+
fbCollection = fbCollection.where(queryId, "in", value);
|
|
433
|
+
break;
|
|
434
|
+
default:
|
|
435
|
+
break;
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
const fbSnapshot = yield fbCollection.get();
|
|
440
|
+
const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
|
|
441
|
+
//INFO : Security Checker
|
|
442
|
+
let validOpsConfig = true;
|
|
443
|
+
const dataList = [];
|
|
444
|
+
if (fbSnapshot) {
|
|
445
|
+
fbSnapshot.forEach((eachDataDoc) => {
|
|
446
|
+
const eachData = eachDataDoc.data();
|
|
447
|
+
ids.push(eachData.id);
|
|
448
|
+
dataList.push(eachData);
|
|
449
|
+
if (!(0, QueryModel_1.checkOpsConfig)(this.config, "delete", req, eachData)) {
|
|
450
|
+
validOpsConfig = false;
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
if (workflow)
|
|
455
|
+
yield workflow(dataList);
|
|
456
|
+
if (!validBaseConfig || !validOpsConfig) {
|
|
457
|
+
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
fbSnapshot.forEach((doc) => {
|
|
461
|
+
batch.delete(doc.ref);
|
|
462
|
+
});
|
|
463
|
+
yield batch.commit();
|
|
464
|
+
res.send({ success: ids });
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
catch (err) {
|
|
468
|
+
res.status(400).json({ err: err });
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
this.initDelete = (req, res, workflow) => __awaiter(this, void 0, void 0, function* () {
|
|
472
|
+
try {
|
|
473
|
+
const valid = req.body.id;
|
|
474
|
+
if (valid) {
|
|
475
|
+
const dataQuery = yield this.fbAdmin
|
|
476
|
+
.firestore()
|
|
477
|
+
.collection(this.collection)
|
|
478
|
+
.doc(valid)
|
|
479
|
+
.get();
|
|
480
|
+
if (!dataQuery.exists) {
|
|
481
|
+
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
const fbData = dataQuery.data();
|
|
485
|
+
//INFO : Security Checker
|
|
486
|
+
const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
|
|
487
|
+
const validOpsConfig = (0, QueryModel_1.checkOpsConfig)(this.config, "delete", req, fbData);
|
|
488
|
+
if (!validBaseConfig || !validOpsConfig) {
|
|
489
|
+
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
if (workflow)
|
|
493
|
+
yield workflow(fbData);
|
|
494
|
+
yield this.fbAdmin
|
|
495
|
+
.firestore()
|
|
496
|
+
.collection(this.collection)
|
|
497
|
+
.doc(valid)
|
|
498
|
+
.delete();
|
|
499
|
+
res.send({ success: true });
|
|
500
|
+
}
|
|
501
|
+
else {
|
|
502
|
+
res.status(400).json({ err: "Invalid Fields" });
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
catch (err) {
|
|
506
|
+
res.status(400).json({ err: err });
|
|
507
|
+
}
|
|
508
|
+
});
|
|
509
|
+
this.initList = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
510
|
+
var _f, _g, _h, _j, _k;
|
|
511
|
+
try {
|
|
512
|
+
//INFO : Security Checker
|
|
513
|
+
const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
|
|
514
|
+
if (validBaseConfig) {
|
|
515
|
+
const aggregate = req.body.aggregate;
|
|
516
|
+
const queryList = (_f = req.body.query) !== null && _f !== void 0 ? _f : [];
|
|
517
|
+
if (aggregate) {
|
|
518
|
+
const queryId = (_g = aggregate.queryId) !== null && _g !== void 0 ? _g : "";
|
|
519
|
+
const dateId = (_h = aggregate.dateId) !== null && _h !== void 0 ? _h : "baseUpdatedAt";
|
|
520
|
+
const dateRanges = (_j = aggregate.range) !== null && _j !== void 0 ? _j : [];
|
|
521
|
+
const dataList = [];
|
|
522
|
+
for (const dateRange of dateRanges) {
|
|
523
|
+
//INFO : Step 1 - Query
|
|
524
|
+
let fbCollection = this.fbAdmin
|
|
525
|
+
.firestore()
|
|
526
|
+
.collection(this.collection);
|
|
527
|
+
if (queryList && queryList.length > 0) {
|
|
528
|
+
queryList.map((eachQuery) => {
|
|
529
|
+
var _a;
|
|
530
|
+
const queryId = (_a = eachQuery.queryId) !== null && _a !== void 0 ? _a : "";
|
|
531
|
+
const value = eachQuery.value;
|
|
532
|
+
switch (eachQuery.type) {
|
|
533
|
+
case "=":
|
|
534
|
+
fbCollection = fbCollection.where(queryId, "==", value);
|
|
535
|
+
break;
|
|
536
|
+
case "!=":
|
|
537
|
+
fbCollection = fbCollection.where(queryId, "!=", value);
|
|
538
|
+
break;
|
|
539
|
+
case ">":
|
|
540
|
+
fbCollection = fbCollection.where(queryId, ">", value);
|
|
541
|
+
break;
|
|
542
|
+
case "<":
|
|
543
|
+
fbCollection = fbCollection.where(queryId, "<", value);
|
|
544
|
+
break;
|
|
545
|
+
case "><":
|
|
546
|
+
if (Array.isArray(value) && value.length >= 2) {
|
|
547
|
+
fbCollection = fbCollection
|
|
548
|
+
.where(queryId, ">=", value[0])
|
|
549
|
+
.where(queryId, "<=", value[1]);
|
|
550
|
+
}
|
|
551
|
+
break;
|
|
552
|
+
case "in":
|
|
553
|
+
fbCollection = fbCollection.where(queryId, "in", value);
|
|
554
|
+
break;
|
|
555
|
+
default:
|
|
556
|
+
break;
|
|
557
|
+
}
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
const date = dateRange.split("-");
|
|
561
|
+
const [startDate, endDate = (0, moment_1.default)().format("DD/MM/YYYY HH:mm:ss"),] = date;
|
|
562
|
+
const isDuration = date.length === 2;
|
|
563
|
+
const isValidStartDate = (0, moment_1.default)(startDate, "DD/MM/YYYY").isValid();
|
|
564
|
+
const isValidEndDate = (0, moment_1.default)(endDate, "DD/MM/YYYY").isValid();
|
|
565
|
+
const isoStartDate = (0, moment_1.default)(startDate, "DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss");
|
|
566
|
+
const isoEndDate = (0, moment_1.default)(endDate, "DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss");
|
|
567
|
+
if (!isValidStartDate || !isValidEndDate) {
|
|
568
|
+
res
|
|
569
|
+
.status(400)
|
|
570
|
+
.json({ err: "Invalid Aggregate Range Configuration" });
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
const id = isDuration
|
|
574
|
+
? dateRange
|
|
575
|
+
: `Begining until ${(0, moment_1.default)(isoStartDate).format("DD/MM/YYYY")}`;
|
|
576
|
+
if (isDuration) {
|
|
577
|
+
fbCollection = fbCollection
|
|
578
|
+
.where(dateId, ">=", (0, moment_1.default)(isoStartDate).toDate())
|
|
579
|
+
.where(dateId, "<=", (0, moment_1.default)(isoEndDate).toDate());
|
|
580
|
+
}
|
|
581
|
+
else {
|
|
582
|
+
fbCollection = fbCollection.where(dateId, "<", (0, moment_1.default)(isoStartDate).toDate());
|
|
583
|
+
}
|
|
584
|
+
let value = 0;
|
|
585
|
+
switch (aggregate.type) {
|
|
586
|
+
case "count":
|
|
587
|
+
const countFbSnapshot = yield fbCollection.count().get();
|
|
588
|
+
value = countFbSnapshot.data().count;
|
|
589
|
+
break;
|
|
590
|
+
case "sum":
|
|
591
|
+
const sumAggregateQuery = fbCollection.aggregate({
|
|
592
|
+
sumValue: firestore_1.AggregateField.sum(queryId),
|
|
593
|
+
});
|
|
594
|
+
const sumFbSnapshot = yield sumAggregateQuery.get();
|
|
595
|
+
value = sumFbSnapshot.data().sumValue;
|
|
596
|
+
break;
|
|
597
|
+
case "avg":
|
|
598
|
+
const avgAggregateQuery = fbCollection.aggregate({
|
|
599
|
+
avgValue: firestore_1.AggregateField.average(queryId),
|
|
600
|
+
});
|
|
601
|
+
const avgFbSnapshot = yield avgAggregateQuery.get();
|
|
602
|
+
value = avgFbSnapshot.data().avgValue;
|
|
603
|
+
break;
|
|
604
|
+
case "value":
|
|
605
|
+
fbCollection = fbCollection.limit(1);
|
|
606
|
+
fbCollection = fbCollection.orderBy(dateId, "desc");
|
|
607
|
+
const fbSnapshot = yield fbCollection.get();
|
|
608
|
+
if (fbSnapshot) {
|
|
609
|
+
fbSnapshot.forEach((eachDataDoc) => {
|
|
610
|
+
const eachData = eachDataDoc.data();
|
|
611
|
+
const clonedEachData = JSON.parse(JSON.stringify(eachData));
|
|
612
|
+
value = clonedEachData[queryId];
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
break;
|
|
616
|
+
default:
|
|
617
|
+
break;
|
|
618
|
+
}
|
|
619
|
+
dataList.push({
|
|
620
|
+
_id: id,
|
|
621
|
+
value: value,
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
res.send({
|
|
625
|
+
data: dataList,
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
else {
|
|
629
|
+
//INFO : Step 1 - Query
|
|
630
|
+
let fbCollection = this.fbAdmin
|
|
631
|
+
.firestore()
|
|
632
|
+
.collection(this.collection);
|
|
633
|
+
let searchQueryExists = "";
|
|
634
|
+
if (queryList && queryList.length > 0) {
|
|
635
|
+
queryList.map((eachQuery) => {
|
|
636
|
+
var _a;
|
|
637
|
+
const queryId = (_a = eachQuery.queryId) !== null && _a !== void 0 ? _a : "";
|
|
638
|
+
const value = eachQuery.value;
|
|
639
|
+
switch (eachQuery.type) {
|
|
640
|
+
case "search":
|
|
641
|
+
if (eachQuery.searchIds && eachQuery.searchIds.length > 0) {
|
|
642
|
+
searchQueryExists = eachQuery.searchIds[0];
|
|
643
|
+
fbCollection = fbCollection
|
|
644
|
+
.orderBy(eachQuery.searchIds[0])
|
|
645
|
+
.startAt(value)
|
|
646
|
+
.endAt(value + "\uf8ff");
|
|
647
|
+
}
|
|
648
|
+
break;
|
|
649
|
+
case "=":
|
|
650
|
+
fbCollection = fbCollection.where(queryId, "==", value);
|
|
651
|
+
break;
|
|
652
|
+
case "!=":
|
|
653
|
+
fbCollection = fbCollection.where(queryId, "!=", value);
|
|
654
|
+
break;
|
|
655
|
+
case ">":
|
|
656
|
+
fbCollection = fbCollection.where(queryId, ">", value);
|
|
657
|
+
break;
|
|
658
|
+
case "<":
|
|
659
|
+
fbCollection = fbCollection.where(queryId, "<", value);
|
|
660
|
+
break;
|
|
661
|
+
case "><":
|
|
662
|
+
if (Array.isArray(value) && value.length >= 2) {
|
|
663
|
+
fbCollection = fbCollection
|
|
664
|
+
.where(queryId, ">=", value[0])
|
|
665
|
+
.where(queryId, "<=", value[1]);
|
|
666
|
+
}
|
|
667
|
+
break;
|
|
668
|
+
case "in":
|
|
669
|
+
fbCollection = fbCollection.where(queryId, "in", value);
|
|
670
|
+
break;
|
|
671
|
+
default:
|
|
672
|
+
break;
|
|
673
|
+
}
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
//INFO : Step 2 - Limit
|
|
677
|
+
if (!req.body.stopLimit) {
|
|
678
|
+
fbCollection = fbCollection.limit((_k = req.body.limit) !== null && _k !== void 0 ? _k : 10);
|
|
679
|
+
}
|
|
680
|
+
//INFO : Step 3 - Sort
|
|
681
|
+
if (req.body.sort) {
|
|
682
|
+
const sortReq = req.body.sort;
|
|
683
|
+
if (searchQueryExists && sortReq.sortId !== searchQueryExists) {
|
|
684
|
+
res
|
|
685
|
+
.status(400)
|
|
686
|
+
.json({ err: "Invalid Search Sort Configuration" });
|
|
687
|
+
return;
|
|
688
|
+
}
|
|
689
|
+
if (!searchQueryExists) {
|
|
690
|
+
fbCollection = fbCollection.orderBy(sortReq.sortId, sortReq.type === "asc" ? "asc" : "desc");
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
//INFO : Step 4 - Pagination
|
|
694
|
+
if (req.body.cursor) {
|
|
695
|
+
fbCollection = fbCollection.startAfter(req.body.cursor);
|
|
696
|
+
}
|
|
697
|
+
const fbSnapshot = yield fbCollection.get();
|
|
698
|
+
//INFO : Security Checker
|
|
699
|
+
let validOpsConfig = true;
|
|
700
|
+
const dataList = [];
|
|
701
|
+
if (fbSnapshot) {
|
|
702
|
+
fbSnapshot.forEach((eachDataDoc) => {
|
|
703
|
+
const eachData = eachDataDoc.data();
|
|
704
|
+
dataList.push(eachData);
|
|
705
|
+
if (!(0, QueryModel_1.checkOpsConfig)(this.config, "read", req, eachData)) {
|
|
706
|
+
validOpsConfig = false;
|
|
707
|
+
}
|
|
708
|
+
});
|
|
709
|
+
}
|
|
710
|
+
if (!validBaseConfig || !validOpsConfig) {
|
|
711
|
+
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
712
|
+
return;
|
|
713
|
+
}
|
|
714
|
+
let cursor = "";
|
|
715
|
+
if (dataList.length === req.body.limit) {
|
|
716
|
+
let sortCol = "id";
|
|
717
|
+
if (req.body.sort) {
|
|
718
|
+
const sortReq = req.body.sort;
|
|
719
|
+
sortCol = sortReq.sortId;
|
|
720
|
+
}
|
|
721
|
+
cursor = dataList[req.body.limit - 1][sortCol];
|
|
722
|
+
}
|
|
723
|
+
res.send({
|
|
724
|
+
data: dataList,
|
|
725
|
+
count: cursor,
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
else {
|
|
730
|
+
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
catch (err) {
|
|
734
|
+
if (err.message)
|
|
735
|
+
res.status(400).json({ err: err.message });
|
|
736
|
+
else
|
|
737
|
+
res.status(400).json({ err: err });
|
|
738
|
+
}
|
|
739
|
+
});
|
|
740
|
+
this.init = () => {
|
|
741
|
+
const router = this.lib.express.Router();
|
|
742
|
+
/**
|
|
743
|
+
* @Input :
|
|
744
|
+
* data - Model
|
|
745
|
+
* unique - string (Optional)
|
|
746
|
+
*/
|
|
747
|
+
router.post("/create", (req, res) => {
|
|
748
|
+
this.initCreate(req, res);
|
|
749
|
+
});
|
|
750
|
+
/**
|
|
751
|
+
* @Input :
|
|
752
|
+
* data - Model
|
|
753
|
+
* unique - string (Optional)
|
|
754
|
+
*/
|
|
755
|
+
router.post("/batchCreate", (req, res) => {
|
|
756
|
+
this.initBatchCreate(req, res);
|
|
757
|
+
});
|
|
758
|
+
/**
|
|
759
|
+
* @Input :
|
|
760
|
+
* id - string
|
|
761
|
+
*/
|
|
762
|
+
router.post("/get", (req, res) => {
|
|
763
|
+
this.initGet(req, res);
|
|
764
|
+
});
|
|
765
|
+
/**
|
|
766
|
+
* @Input :
|
|
767
|
+
* data - Model
|
|
768
|
+
* id - string
|
|
769
|
+
* unique - string (Optional)
|
|
770
|
+
*/
|
|
771
|
+
router.post("/update", (req, res) => {
|
|
772
|
+
this.initUpdate(req, res);
|
|
773
|
+
});
|
|
774
|
+
/**
|
|
775
|
+
* @Input :
|
|
776
|
+
* data - Model
|
|
777
|
+
* query - Query[]
|
|
778
|
+
* unsetData - string[] (Unset Attribute Id) (Optional)
|
|
779
|
+
* isOr - boolean (Optional)
|
|
780
|
+
*/
|
|
781
|
+
router.post("/batchUpdate", (req, res) => {
|
|
782
|
+
this.initBatchUpdate(req, res);
|
|
783
|
+
});
|
|
784
|
+
/**
|
|
785
|
+
* @Input :
|
|
786
|
+
* id - string
|
|
787
|
+
*/
|
|
788
|
+
router.post("/delete", (req, res) => {
|
|
789
|
+
this.initDelete(req, res);
|
|
790
|
+
});
|
|
791
|
+
/**
|
|
792
|
+
* @Input :
|
|
793
|
+
* id - string[]
|
|
794
|
+
*/
|
|
795
|
+
router.post("/batchDelete", (req, res) => {
|
|
796
|
+
this.initBatchDelete(req, res);
|
|
797
|
+
});
|
|
798
|
+
/**
|
|
799
|
+
* @Input
|
|
800
|
+
* query - Query interface (Optional)
|
|
801
|
+
* cursor - any (Optional)
|
|
802
|
+
* limit - number (Optional)
|
|
803
|
+
* sort - Sort interface (Optional)
|
|
804
|
+
* stopLimit - boolean to retreive all data (Optional)
|
|
805
|
+
*
|
|
806
|
+
*/
|
|
807
|
+
router.post("/list", (req, res) => {
|
|
808
|
+
this.initList(req, res);
|
|
809
|
+
});
|
|
810
|
+
return router;
|
|
811
|
+
};
|
|
812
|
+
this.fbAdmin = fbAdmin;
|
|
813
|
+
this.collection = collection;
|
|
814
|
+
this.isProd = isProd;
|
|
815
|
+
this.config = config;
|
|
816
|
+
this.modelChecker = modelChecker;
|
|
817
|
+
this.lib = lib;
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
exports.FirebaseWrapper = FirebaseWrapper;
|