blixify-server 0.1.48 → 0.1.50
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":"mondayWrapper.d.ts","sourceRoot":"","sources":["../../src/apis/mondayWrapper.ts"],"names":[],"mappings":"AAEA,OAAO,cAAc,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAE5C,eAAO,MAAM,cAAc,UAClB,MAAM,SACN,GAAG,cACE,GAAG,qDAchB,CAAC;AAEF;;;;GAIG;AACH,qBAAa,aAAa;IACxB,WAAW,SAAM;IACjB,OAAO,SAAM;IACb,OAAO,SAAM;IACb,MAAM,EAAE,cAAc,CAQpB;IACF,GAAG,EAAE,UAAU,CAAC;IAEhB,YAAY,QAAS,GAAG,aAEtB;IACF,SAAS;cAAe,MAAM,EAAE;;;;MAK9B;gBAGA,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,cAAc,EACtB,YAAY,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,OAAO,KAAK,OAAO,EACrD,SAAS,EAAE,MAAM;QAAE,IAAI,EAAE,MAAM,EAAE,CAAC;QAAC,KAAK,EAAE;YAAE,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAAA;SAAE,CAAA;KAAE,EACpE,GAAG,EAAE,UAAU;IAWjB,UAAU,eAAgB;QACxB,EAAE,EAAE,MAAM,CAAC;QACX,MAAM,MAAM,CAAC;QACb,aAAa,EAAE;YAAE,EAAE,EAAE,MAAM,CAAC;YAAC,IAAI,EAAE,MAAM,CAAC;YAAC,KAAK,EAAE,MAAM,CAAA;SAAE,EAAE,CAAC;KAC9D,SAaC;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC;;mBA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AO,GAAG;;mBA+CjC;IAEF,QAAQ,QAAe,GAAG,OAAO,GAAG;;;mBA+GlC;IAEF,UAAU,QAAe,GAAG,OAAO,GAAG;;mBAsFpC;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC;;mBA8DtC;IAEF,IAAI,YA8CF;CACH"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var __awaiter=this&&this.__awaiter||function(e,d,r,n){return new(r=r||Promise)(function(i,t){function
|
|
1
|
+
"use strict";var __awaiter=this&&this.__awaiter||function(e,d,r,n){return new(r=r||Promise)(function(i,t){function a(e){try{o(n.next(e))}catch(e){t(e)}}function s(e){try{o(n.throw(e))}catch(e){t(e)}}function o(e){var t;e.done?i(e.value):((t=e.value)instanceof r?t:new r(function(e){e(t)})).then(a,s)}o((n=n.apply(e,d||[])).next())})},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.MondayWrapper=exports.queryMondayApi=void 0;const axios_1=__importDefault(require("axios")),QueryModel_1=require("../model/QueryModel"),queryMondayApi=(e,t,i)=>__awaiter(void 0,void 0,void 0,function*(){return yield axios_1.default.post("https://api.monday.com/v2",{query:t,variables:i},{headers:{"Content-Type":"application/json",Authorization:null!=e?e:"","API-version":"2023-10"}})});exports.queryMondayApi=queryMondayApi;class MondayWrapper{constructor(e,t,i,a,s,o,d){this.mondayToken="",this.boardId="",this.groupId="",this.config={baseConfig:[],opsConfig:{read:[],create:[],update:[],delete:[]}},this.modelChecker=e=>!1,this.modelType=()=>({read:[],write:{}}),this.parseModel=e=>{const i={id:e.id,name:e.name};return e.column_values.map(t=>{if("subitems"!==t.id){let e=!0;this.modelType().read.includes(t.id)&&(e=!1),i[t.id]=e?t.text:t.value}}),i},this.initCreate=(r,n,u)=>__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),a=(0,QueryModel_1.checkOpsConfig)(this.config,"create",r,r.body.data);if(i&&a){var s=r.body.data,o=(u&&(yield u(s)),{boardId:this.boardId,groupId:this.groupId,itemName:null!=(e=r.body.data.name)?e:"Item",columnValues:JSON.stringify(s)}),d={success:(yield(0,exports.queryMondayApi)(this.mondayToken,`
|
|
2
2
|
mutation ($boardId: ID!, $groupId: String!, $itemName: String!, $columnValues: JSON!) {
|
|
3
3
|
create_item (
|
|
4
4
|
board_id: $boardId,
|
|
@@ -24,9 +24,8 @@
|
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
-
}`,i=(yield(0,exports.queryMondayApi)(this.mondayToken,t)).data.data.boards[0].items_page.items;if(0<i.length){var s
|
|
28
|
-
|
|
29
|
-
items_page(${e}) {
|
|
27
|
+
}`,i=(yield(0,exports.queryMondayApi)(this.mondayToken,t)).data.data.boards[0].items_page.items;if(0<i.length){var a,s=this.parseModel(i[0]),o=(0,QueryModel_1.checkBaseConfig)(this.config,r),d=(0,QueryModel_1.checkOpsConfig)(this.config,"read",r,s);if(o&&d)return a={data:s},null!=(e=r.body)&&e.stopRes?a:void n.send(a);n.status(400).json({err:"Invalid Security Configuration"})}else n.send({data:null})}else n.status(400).json({err:"Invalid Fields"})}catch(e){n.status(400).json({err:e})}}),this.initList=(y,h)=>__awaiter(this,void 0,void 0,function*(){var d;try{var r=(0,QueryModel_1.checkBaseConfig)(this.config,y);if(r){var n=y.body.query;let e=[];var u,l;let t="",i="",a="",s=(y.body.limit&&(i="limit: "+y.body.limit),y.body.cursor&&(a=`cursor: "${String(y.body.cursor)}"`),""),o=(s=i&&a?i+", "+a:i||a,t=a?`query {
|
|
28
|
+
next_items_page(${s}) {
|
|
30
29
|
cursor
|
|
31
30
|
items {
|
|
32
31
|
id
|
|
@@ -38,23 +37,22 @@
|
|
|
38
37
|
}
|
|
39
38
|
}
|
|
40
39
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
40
|
+
}`:n&&0<n.length?(l=n[0],`query { items_page_by_column_values (${i} board_id: ${this.boardId}, columns: [{column_id: "${l.queryId}", column_values: ["${l.value}"]}]) { cursor items {id name column_values {id text value} }}}`):`query {
|
|
41
|
+
boards(ids:${this.boardId}){
|
|
42
|
+
items_page(${i}) {
|
|
43
|
+
cursor
|
|
44
|
+
items {
|
|
45
|
+
id
|
|
46
|
+
name
|
|
47
|
+
column_values {
|
|
48
|
+
id
|
|
49
|
+
text
|
|
50
|
+
value
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}`;var l=yield(0,exports.queryMondayApi)(this.mondayToken,i);t=l.data.data.boards[0].items_page.items}let o=!0;const y=[];if(t.map(e=>{e=this.parseModel(e);y.push(e),(0,QueryModel_1.checkOpsConfig)(this.config,"read",m,e)||(o=!1)}),e&&o){let e=null;y.length===m.body.limit&&(e=!0);var c={data:y,count:e};if(null!=(d=m.body)&&d.stopRes)return c;h.send(c)}else h.status(400).json({err:"Invalid Security Configuration"})}else h.status(400).json({err:"Invalid Fields"})}catch(e){h.status(400).json({err:e})}}),this.initUpdate=(u,l)=>__awaiter(this,void 0,void 0,function*(){var e;try{if(this.modelChecker(u.body.data)&&u.body.id){var t=`query {
|
|
55
|
+
}`,u=yield(0,exports.queryMondayApi)(this.mondayToken,t),e=(a?u.data.data.next_items_page:n&&0<n.length?u.data.data.items_page_by_column_values:u.data.data.boards[0].items_page).items,!0);const m=[];if(e.map(e=>{e=this.parseModel(e);m.push(e),(0,QueryModel_1.checkOpsConfig)(this.config,"read",y,e)||(o=!1)}),r&&o){let e=null;m.length===y.body.limit&&(e=(a?u.data.data.next_items_page:n&&0<n.length?u.data.data.items_page_by_column_values:u.data.data.boards[0].items_page).cursor);var c={data:m,count:e};if(null!=(d=y.body)&&d.stopRes)return c;h.send(c)}else h.status(400).json({err:"Invalid Security Configuration"})}else h.status(400).json({err:"Invalid Fields"})}catch(e){h.status(400).json({err:e})}}),this.initUpdate=(u,l)=>__awaiter(this,void 0,void 0,function*(){var e;try{if(this.modelChecker(u.body.data)&&u.body.id){var t=`query {
|
|
58
56
|
boards(ids: ${this.boardId}) {
|
|
59
57
|
items_page (limit: 1, query_params: {ids:${u.body.id}}) {
|
|
60
58
|
cursor
|
|
@@ -69,7 +67,7 @@
|
|
|
69
67
|
}
|
|
70
68
|
}
|
|
71
69
|
}
|
|
72
|
-
}`,i=(yield(0,exports.queryMondayApi)(this.mondayToken,t)).data.data.boards[0].items_page.items;if(i.length<=0)throw"Error";var
|
|
70
|
+
}`,i=(yield(0,exports.queryMondayApi)(this.mondayToken,t)).data.data.boards[0].items_page.items;if(i.length<=0)throw"Error";var a=this.parseModel(i[0]),s=(0,QueryModel_1.checkBaseConfig)(this.config,u),o=(0,QueryModel_1.checkOpsConfig)(this.config,"update",u,a);if(s&&o){const r=u.body.data,n={boardId:this.boardId,itemId:u.body.id,columnValues:{}};Object.keys(r).map(t=>{if("id"!==t){var i=this.modelType().write;let e="";(e=i[t]?i[t]:e)?n.columnValues[t]={[e]:r[t]}:n.columnValues[t]=r[t]}}),n.columnValues=JSON.stringify(n.columnValues),yield(0,exports.queryMondayApi)(this.mondayToken,`
|
|
73
71
|
mutation ($boardId: ID!, $itemId: ID!, $columnValues: JSON!) {
|
|
74
72
|
change_multiple_column_values(
|
|
75
73
|
item_id: $itemId,
|
|
@@ -94,10 +92,10 @@
|
|
|
94
92
|
}
|
|
95
93
|
}
|
|
96
94
|
}
|
|
97
|
-
}`,i=(yield(0,exports.queryMondayApi)(this.mondayToken,t)).data.data.boards[0].items_page.items;if(i.length<=0)throw"Error";var
|
|
95
|
+
}`,i=(yield(0,exports.queryMondayApi)(this.mondayToken,t)).data.data.boards[0].items_page.items;if(i.length<=0)throw"Error";var a=this.parseModel(i[0]),s=(0,QueryModel_1.checkBaseConfig)(this.config,n),o=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",n,a);if(s&&o){l&&(yield l(a));var d={itemId:n.body.id},r=(yield(0,exports.queryMondayApi)(this.mondayToken,`
|
|
98
96
|
mutation ($itemId: ID!){
|
|
99
97
|
delete_item (item_id: $itemId) {
|
|
100
98
|
id
|
|
101
99
|
}
|
|
102
100
|
}
|
|
103
|
-
`,d),{success:!0});if(null!=(e=n.body)&&e.stopRes)return r;u.send(r)}else u.status(400).json({err:"Invalid Security Configuration"})}else u.status(400).json({err:"Invalid Model Structure"})}catch(e){u.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("/get",(e,t)=>{this.initGet(e,t)}),e.post("/list",(e,t)=>{this.initList(e,t)}),e.post("/update",(e,t)=>{this.initUpdate(e,t)}),e.post("/delete",(e,t)=>{this.initDelete(e,t)}),e},this.mondayToken=e,this.boardId=t,this.groupId=i,this.config=
|
|
101
|
+
`,d),{success:!0});if(null!=(e=n.body)&&e.stopRes)return r;u.send(r)}else u.status(400).json({err:"Invalid Security Configuration"})}else u.status(400).json({err:"Invalid Model Structure"})}catch(e){u.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("/get",(e,t)=>{this.initGet(e,t)}),e.post("/list",(e,t)=>{this.initList(e,t)}),e.post("/update",(e,t)=>{this.initUpdate(e,t)}),e.post("/delete",(e,t)=>{this.initDelete(e,t)}),e},this.mondayToken=e,this.boardId=t,this.groupId=i,this.config=a,this.modelChecker=s,this.modelType=o,this.lib=d}}exports.MondayWrapper=MondayWrapper;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mongoWrapper.d.ts","sourceRoot":"","sources":["../../src/apis/mongoWrapper.ts"],"names":[],"mappings":"AAQA,OAAO,cAAc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awaiter=this&&this.__awaiter||function(e,n,r,d){return new(r=r||Promise)(function(i,t){function s(e){try{a(d.next(e))}catch(e){t(e)}}function o(e){try{a(d.throw(e))}catch(e){t(e)}}function a(e){var t;e.done?i(e.value):((t=e.value)instanceof r?t:new r(function(e){e(t)})).then(s,o)}a((d=d.apply(e,n||[])).next())})},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.MongoWrapper=void 0;const moment_timezone_1=__importDefault(require("moment-timezone")),QueryModel_1=require("../model/QueryModel"),utils_1=require("./utils");class MongoWrapper{constructor(e,t,i,s,o,a,n,r){this.mongoDB="",this.collection="",this.isProd=!1,this.config={baseConfig:[],opsConfig:{read:[],create:[],update:[],delete:[]}},this.tableId="",this.modelChecker=e=>!1,this.parseModel=e=>(delete e._id,e.baseUpdatedAt=(0,moment_timezone_1.default)().toDate(),e),this.initBatchCreate=(u,c,h)=>__awaiter(this,void 0,void 0,function*(){var e;try{if(Array.isArray(u.body.data)){let t=!0;u.body.data.map(e=>{this.modelChecker(e)||(t=!1)}),t||c.status(400).json({err:"Invalid Model Structure"});var s=(0,QueryModel_1.checkBaseConfig)(this.config,u);let i=!0;const d=[];if(yield Promise.all(u.body.data.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"update",u,e)||(i=!1),u.body.unique&&null!=e&&e[u.body.unique]&&d.push(e[u.body.unique])})),s&&i){var o=this.mongoDB.db(this.tableId).collection(this.collection);if(0<d.length){var a=yield(yield o.find({[u.body.unique]:{$in:d}})).toArray();if(0<a.length)return void c.status(400).json({err:`Unique:${a.map(e=>e[u.body.unique])} exists`})}const l=[];var n=u.body.data.map(e=>(l.push(e._id),Object.assign(Object.assign({},e),{baseUpdatedAt:(0,moment_timezone_1.default)().toDate()}))),r=(h&&(yield h(n)),yield o.insertMany(n),{success:l});if(null!=(e=u.body)&&e.stopRes)return r;c.send(r)}else c.status(400).json({err:"Invalid Security Configuration"})}else c.status(400).json({err:"Invalid Fields"})}catch(e){c.status(400).json({err:e})}}),this.initCreate=(n,r,d)=>__awaiter(this,void 0,void 0,function*(){var e,t;try{if(this.modelChecker(n.body.data)){var i=(0,QueryModel_1.checkBaseConfig)(this.config,n),s=(0,QueryModel_1.checkOpsConfig)(this.config,"create",n,n.body.data);if(i&&s){var o=this.mongoDB.db(this.tableId).collection(this.collection);if(n.body.data.baseUpdatedAt=(0,moment_timezone_1.default)().toDate(),n.body.unique&&null!=(e=n.body.data)&&e[n.body.unique])if(yield o.findOne({[n.body.unique]:n.body.data[n.body.unique]}))return void r.status(400).json({err:`Unique:${n.body.data[n.body.unique]} exists`});d&&(yield d(n.body.data)),yield o.insertOne(n.body.data);var a={success:!0};if(null!=(t=n.body)&&t.stopRes)return a;r.send(a)}else r.status(400).json({err:"Invalid Security Configuration"})}else r.status(400).json({err:"Invalid Model Structure"})}catch(e){r.status(400).json({err:e})}}),this.initGet=(n,r)=>__awaiter(this,void 0,void 0,function*(){var e;try{var t=n.body.id;if(t){var i,s=yield this.mongoDB.db(this.tableId).collection(this.collection).findOne({_id:t}),o=(0,QueryModel_1.checkBaseConfig)(this.config,n),a=(0,QueryModel_1.checkOpsConfig)(this.config,"read",n,s);if(o&&a)return i={data:s},null!=(e=n.body)&&e.stopRes?i:void r.send(i);r.status(400).json({err:"Invalid Security Configuration"})}else r.status(400).json({err:"Invalid Fields"})}catch(e){r.status(400).json({err:e})}}),this.initBatchUpdate=(p,m,v)=>__awaiter(this,void 0,void 0,function*(){var e,i,s;try{if(this.modelChecker(p.body.data)&&p.body.query){var o=p.body.unsetData,a=null!=(e=p.body.query)?e:[];const y=p.body.isOr?"$or":"$and",b={};var n=this.parseModel(null!=(i=p.body.data)?i:[]),r=(a&&0<a.length&&(b[y]=[],a.some(e=>"search"===e.type)&&(b.$or=[]),a.map(e=>{var t,i=null!=(t=e.queryId)?t:"";const s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&e.searchIds.map(e=>{b.$or.push({[e]:{$regex:s,$options:"i"}})});break;case"=":b[y].push({[i]:s});break;case"!=":b[y].push({[i]:{$ne:s}});break;case">":b[y].push({[i]:{$gt:s}});break;case"<":b[y].push({[i]:{$lt:s}});break;case"><":Array.isArray(s)&&2<=s.length&&(b[y].push({[i]:{$lt:s}}),b[y]={[i]:{$gte:s[0],$lte:s[1]}});break;case"in":b[y].push({[i]:{$in:s}})}})),(0,QueryModel_1.checkBaseConfig)(this.config,p));let t=!0;var d=this.mongoDB.db(this.tableId).collection(this.collection),l=yield(yield d.find(Object.assign({},b))).toArray();const f=[];if(yield Promise.all(l.map(e=>{p.body.sensitive&&!(0,moment_timezone_1.default)(p.body.data.baseUpdatedAt).isAfter((0,moment_timezone_1.default)(e.baseUpdatedAt))&&m.status(400).json({err:"Refresh Sensitive Model"}),(0,QueryModel_1.checkOpsConfig)(this.config,"update",p,e)||(t=!1);e=(0,utils_1.compareUpdatedFields)(e,p.body.data).workflowUpdateFields;f.concat(e)})),r&&t){const g={};var u={},c=(o&&(o.forEach(e=>{g[e]=""}),u.$unset=Object.assign({},g)),n&&(u.$set=Object.assign({},n)),v&&(yield v(f)),yield d.updateMany(b,u),f.map(e=>e._id)),h={success:c};if(null!=(s=p.body)&&s.stopRes)return h;m.send(h)}else m.status(400).json({err:"Invalid Security Configuration"})}}catch(e){m.status(400).json({err:e})}}),this.initUpdate=(y,b,f)=>__awaiter(this,void 0,void 0,function*(){var e,t,i;try{var s=this.modelChecker(y.body.data)&&y.body.id;if(s){var o=this.mongoDB.db(this.tableId).collection(this.collection),a=yield o.findOne({_id:s}),n=(this.debug&&this.debug(y.body,a),(0,QueryModel_1.checkBaseConfig)(this.config,y)),r=(0,QueryModel_1.checkOpsConfig)(this.config,"update",y,a);if(n&&r){if(y.body.unique&&null!=(e=y.body.data)&&e[y.body.unique]){var d=yield o.findOne({[y.body.unique]:y.body.data[y.body.unique]});if(d&&y.body.id!==d._id)return void b.status(400).json({err:y.body.data[y.body.unique]+" exists"})}var{workflowUpdateFields:l,updatedFields:u}=(0,utils_1.compareUpdatedFields)(a,y.body.data),c={success:!0},h=(f&&(yield f(l)),this.parseModel(u));if(y.body.sensitive)if((0,moment_timezone_1.default)(y.body.data.baseUpdatedAt).isAfter((0,moment_timezone_1.default)(a.baseUpdatedAt))){if(yield o.updateOne({_id:s},{$set:Object.assign({},h)}),null!=(t=y.body)&&t.stopRes)return c;b.send(c)}else b.status(400).json({err:"Refresh Sensitive Model"});else{if(yield o.updateOne({_id:s},{$set:Object.assign({},h)}),null!=(i=y.body)&&i.stopRes)return c;b.send(c)}}else b.status(400).json({err:"Invalid Security Configuration"})}else b.status(400).json({err:"Invalid Model Structure"})}catch(e){this.debug&&this.debug(null,null,e),b.status(400).json({err:e})}}),this.initBatchDelete=(f,g,p)=>__awaiter(this,void 0,void 0,function*(){var e,i;try{if(Array.isArray(f.body.id)){var s=this.mongoDB.db(this.tableId).collection(this.collection),o=yield s.find({_id:{$in:f.body.id}}),a=(0,QueryModel_1.checkBaseConfig)(this.config,f),n=yield o.toArray();let t=!0;if(0<n.length&&(yield Promise.all(n.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"delete",f,e)||(t=!1)}))),a&&t){p&&(yield p(n)),yield s.deleteMany({_id:{$in:f.body.id}});var r={success:f.body.id};if(null!=(e=f.body)&&e.stopRes)return r;g.send(r)}else g.status(400).json({err:"Invalid Security Configuration"})}else{var d=f.body.query;const y={};d&&0<d.length&&d.map(e=>{var t,i=null!=(t=e.queryId)?t:"";const s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&(y.$or=[],e.searchIds.map(e=>{y.$or.push({[e]:{$regex:s,$options:"i"}})}));break;case"=":y[i]=s;break;case"!=":y[i]={$ne:s};break;case">":y[i]={$gt:s};break;case"<":y[i]={$lt:s};break;case"><":Array.isArray(s)&&2<=s.length&&(y[i]={$gte:s[0],$lte:s[1]});break;case"in":y[i]={$in:s}}});var l=this.mongoDB.db(this.tableId).collection(this.collection),u=yield l.find(y).toArray(),c=(0,QueryModel_1.checkBaseConfig)(this.config,f);let t=!0;const b=[];if(0<u.length&&u.map(e=>{b.push(e._id),(0,QueryModel_1.checkOpsConfig)(this.config,"delete",f,e)||(t=!1)}),p&&(yield p(u)),c&&t){yield l.deleteMany(y);var h={success:b};if(null!=(i=f.body)&&i.stopRes)return h;g.send(h)}else g.status(400).json({err:"Invalid Security Configuration"})}}catch(e){g.status(400).json({err:e})}}),this.initDelete=(r,d,l)=>__awaiter(this,void 0,void 0,function*(){var e;try{var t=r.body.id;if(t){var i=this.mongoDB.db(this.tableId).collection(this.collection),s=yield i.findOne({_id:t}),o=(0,QueryModel_1.checkBaseConfig)(this.config,r),a=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",r,s);if(o&&a){l&&(yield l(s)),yield i.findOneAndDelete({_id:t});var n={success:!0};if(null!=(e=r.body)&&e.stopRes)return n;d.send(n)}else d.status(400).json({err:"Invalid Security Configuration"})}else d.status(400).json({err:"Invalid Fields"})}catch(e){d.status(400).json({err:e})}}),this.initList=(U,z)=>__awaiter(this,void 0,void 0,function*(){var e,t,i,s,o,a,n,r,d;try{var l=(0,QueryModel_1.checkBaseConfig)(this.config,U);if(l){var u=U.body.aggregate,c=null!=(e=U.body.query)?e:[];const w={};if(c&&0<c.length&&c.map(e=>{var t,i=null!=(t=e.queryId)?t:"";const s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&(w.$or=[],e.searchIds.map(e=>{w.$or.push({[e]:{$regex:s,$options:"i"}})}));break;case"=":w[i]=s;break;case"!=":w[i]={$ne:s};break;case">":w[i]={$gt:s};break;case"<":w[i]={$lt:s};break;case"><":Array.isArray(s)&&2<=s.length&&(w[i]={$gte:s[0],$lte:s[1]});break;case"in":w[i]={$in:s}}}),u){var h=[],y=null!=(t=u.queryId)?t:"",b=null!=(i=u.dateId)?i:"baseUpdatedAt";for(const Q of null!=(s=u.range)?s:[]){Object.assign({},w);var f=Q.split("-"),[g,p=(0,moment_timezone_1.default)().format("DD/MM/YYYY HH:mm:ss")]=f,m=2===f.length,v=(0,moment_timezone_1.default)(g,"DD/MM/YYYY").isValid(),_=(0,moment_timezone_1.default)(p,"DD/MM/YYYY").isValid(),$=(0,moment_timezone_1.default)(g,"DD/MM/YYYY HH:mm:ss").tz("Asia/Kuala_lumpur").toDate(),M=(0,moment_timezone_1.default)(p,"DD/MM/YYYY HH:mm:ss").tz("Asia/Kuala_lumpur").toDate();if(!v||!_)return void z.status(400).json({err:"Invalid Aggregate Range Configuration"});var k=m?{$match:Object.assign(Object.assign({},w),{[b]:{$gte:$,$lte:M}})}:{$match:Object.assign(Object.assign({},w),{[b]:{$lte:$}})},I=[];let e=!1;var j=m?Q:"Begining until "+(0,moment_timezone_1.default)(g,"DD/MM/YYYY HH:mm:ss").format("DD/MM/YYYY");switch(u.type){case"avg":I.push(k,{$group:{_id:j,value:{$avg:"$"+y}}});break;case"sum":I.push(k,{$group:{_id:j,value:{$sum:"$"+y}}});break;case"count":e=!0}if(0===I.length&&!e)return void z.status(400).json({err:"Invalid Aggregate Configuration"});var C=this.mongoDB.db(this.tableId).collection(this.collection);let t=0;t=e?yield C.find(k.$match).count({}):null!=(a=null==(o=(yield C.aggregate(I).toArray())[0])?void 0:o.value)?a:0,h.push({_id:j,value:t})}var D={data:h};if(null!=(n=U.body)&&n.stopRes)return D;z.send(D)}else{let e=this.mongoDB.db(this.tableId).collection(this.collection).find(w);var O,A,q,B=yield e.count({}),Y=(U.body.cursor&&(e=e.skip(U.body.cursor)),U.body.stopLimit||(e=e.limit(null!=(r=U.body.limit)?r:10)),U.body.sort&&(A={[(O=U.body.sort).sortId]:"asc"===O.type?1:-1},e=e.sort(A)),yield e.toArray());let t=!0;if(0<Y.length&&Y.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"read",U,e)||(t=!1)}),l&&t)return q={data:Y,count:B},null!=(d=U.body)&&d.stopRes?q:void z.send(q);z.status(400).json({err:"Invalid Security Configuration"})}}else z.status(400).json({err:"Invalid Security Configuration"})}catch(e){z.status(400).json({err:e})}}),this.init=()=>{var e=this.lib.express.Router();return e.post("/create",(e,t)=>{this.initCreate(e,t)}),e.post("/batchCreate",(e,t)=>{this.initBatchCreate(e,t)}),e.post("/get",(e,t)=>{this.initGet(e,t)}),e.post("/update",(e,t)=>{this.initUpdate(e,t)}),e.post("/batchUpdate",(e,t)=>{this.initBatchUpdate(e,t)}),e.post("/delete",(e,t)=>{this.initDelete(e,t)}),e.post("/batchDelete",(e,t)=>{this.initBatchDelete(e,t)}),e.post("/list",(e,t)=>{this.initList(e,t)}),e},this.mongoDB=e,this.collection=t,this.isProd=i,this.config=s,this.modelChecker=o,this.lib=a;e=this.isProd?"prod":"dev";this.tableId=n?n+"-"+e:e,this.debug=r}}exports.MongoWrapper=MongoWrapper;
|
|
1
|
+
"use strict";var __awaiter=this&&this.__awaiter||function(e,n,r,d){return new(r=r||Promise)(function(i,t){function s(e){try{a(d.next(e))}catch(e){t(e)}}function o(e){try{a(d.throw(e))}catch(e){t(e)}}function a(e){var t;e.done?i(e.value):((t=e.value)instanceof r?t:new r(function(e){e(t)})).then(s,o)}a((d=d.apply(e,n||[])).next())})},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.MongoWrapper=void 0;const moment_timezone_1=__importDefault(require("moment-timezone")),QueryModel_1=require("../model/QueryModel"),utils_1=require("./utils");class MongoWrapper{constructor(e,t,i,s,o,a,n,r){this.mongoDB="",this.collection="",this.isProd=!1,this.config={baseConfig:[],opsConfig:{read:[],create:[],update:[],delete:[]}},this.tableId="",this.modelChecker=e=>!1,this.parseModel=e=>(delete e._id,e.baseUpdatedAt=(0,moment_timezone_1.default)().toDate(),e),this.initBatchCreate=(u,c,h)=>__awaiter(this,void 0,void 0,function*(){var e;try{if(Array.isArray(u.body.data)){let t=!0;u.body.data.map(e=>{this.modelChecker(e)||(t=!1)}),t||c.status(400).json({err:"Invalid Model Structure"});var s=(0,QueryModel_1.checkBaseConfig)(this.config,u);let i=!0;const d=[];if(yield Promise.all(u.body.data.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"update",u,e)||(i=!1),u.body.unique&&null!=e&&e[u.body.unique]&&d.push(e[u.body.unique])})),s&&i){var o=this.mongoDB.db(this.tableId).collection(this.collection);if(0<d.length){var a=yield(yield o.find({[u.body.unique]:{$in:d}})).toArray();if(0<a.length)return void c.status(400).json({err:`Unique:${a.map(e=>e[u.body.unique])} exists`})}const l=[];var n=u.body.data.map(e=>(l.push(e._id),Object.assign(Object.assign({},e),{baseUpdatedAt:(0,moment_timezone_1.default)().toDate()}))),r=(h&&(yield h(n)),yield o.insertMany(n),{success:l});if(null!=(e=u.body)&&e.stopRes)return r;c.send(r)}else c.status(400).json({err:"Invalid Security Configuration"})}else c.status(400).json({err:"Invalid Fields"})}catch(e){c.status(400).json({err:e})}}),this.initCreate=(n,r,d)=>__awaiter(this,void 0,void 0,function*(){var e,t;try{if(this.modelChecker(n.body.data)){var i=(0,QueryModel_1.checkBaseConfig)(this.config,n),s=(0,QueryModel_1.checkOpsConfig)(this.config,"create",n,n.body.data);if(i&&s){var o=this.mongoDB.db(this.tableId).collection(this.collection);if(n.body.data.baseUpdatedAt=(0,moment_timezone_1.default)().toDate(),n.body.unique&&null!=(e=n.body.data)&&e[n.body.unique])if(yield o.findOne({[n.body.unique]:n.body.data[n.body.unique]}))return void r.status(400).json({err:`Unique:${n.body.data[n.body.unique]} exists`});d&&(yield d(n.body.data)),yield o.insertOne(n.body.data);var a={success:!0};if(null!=(t=n.body)&&t.stopRes)return a;r.send(a)}else r.status(400).json({err:"Invalid Security Configuration"})}else r.status(400).json({err:"Invalid Model Structure"})}catch(e){r.status(400).json({err:e})}}),this.initGet=(n,r)=>__awaiter(this,void 0,void 0,function*(){var e;try{var t=n.body.id;if(t){var i,s=yield this.mongoDB.db(this.tableId).collection(this.collection).findOne({_id:t}),o=(0,QueryModel_1.checkBaseConfig)(this.config,n),a=(0,QueryModel_1.checkOpsConfig)(this.config,"read",n,s);if(o&&a)return i={data:s},null!=(e=n.body)&&e.stopRes?i:void r.send(i);r.status(400).json({err:"Invalid Security Configuration"})}else r.status(400).json({err:"Invalid Fields"})}catch(e){r.status(400).json({err:e})}}),this.initBatchUpdate=(p,m,v)=>__awaiter(this,void 0,void 0,function*(){var e,i,s;try{if(this.modelChecker(p.body.data)&&p.body.query||p.body.unsetData){var o=p.body.unsetData,a=null!=(e=p.body.query)?e:[];const y=p.body.isOr?"$or":"$and",b={};var n=this.parseModel(null!=(i=p.body.data)?i:[]),r=(a&&0<a.length&&(b[y]=[],a.some(e=>"search"===e.type)&&(b.$or=[]),a.map(e=>{var t,i=null!=(t=e.queryId)?t:"";const s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&e.searchIds.map(e=>{b.$or.push({[e]:{$regex:s,$options:"i"}})});break;case"=":b[y].push({[i]:s});break;case"!=":b[y].push({[i]:{$ne:s}});break;case">":b[y].push({[i]:{$gt:s}});break;case"<":b[y].push({[i]:{$lt:s}});break;case"><":Array.isArray(s)&&2<=s.length&&(b[y].push({[i]:{$lt:s}}),b[y]={[i]:{$gte:s[0],$lte:s[1]}});break;case"in":b[y].push({[i]:{$in:s}})}})),(0,QueryModel_1.checkBaseConfig)(this.config,p));let t=!0;var d=this.mongoDB.db(this.tableId).collection(this.collection),l=yield(yield d.find(Object.assign({},b))).toArray();const f=[];if(yield Promise.all(l.map(e=>{p.body.sensitive&&!(0,moment_timezone_1.default)(p.body.data.baseUpdatedAt).isAfter((0,moment_timezone_1.default)(e.baseUpdatedAt))&&m.status(400).json({err:"Refresh Sensitive Model"}),(0,QueryModel_1.checkOpsConfig)(this.config,"update",p,e)||(t=!1);e=(0,utils_1.compareUpdatedFields)(e,p.body.data).workflowUpdateFields;f.concat(e)})),r&&t){const g={};var u={},c=(o&&(o.forEach(e=>{g[e]=""}),u.$unset=Object.assign({},g)),n&&(u.$set=Object.assign({},n)),v&&(yield v(f)),yield d.updateMany(b,u),f.map(e=>e._id)),h={success:c};if(null!=(s=p.body)&&s.stopRes)return h;m.send(h)}else m.status(400).json({err:"Invalid Security Configuration"})}}catch(e){m.status(400).json({err:e})}}),this.initUpdate=(y,b,f)=>__awaiter(this,void 0,void 0,function*(){var e,t,i;try{var s=this.modelChecker(y.body.data)&&y.body.id;if(s){var o=this.mongoDB.db(this.tableId).collection(this.collection),a=yield o.findOne({_id:s}),n=(this.debug&&this.debug(y.body,a),(0,QueryModel_1.checkBaseConfig)(this.config,y)),r=(0,QueryModel_1.checkOpsConfig)(this.config,"update",y,a);if(n&&r){if(y.body.unique&&null!=(e=y.body.data)&&e[y.body.unique]){var d=yield o.findOne({[y.body.unique]:y.body.data[y.body.unique]});if(d&&y.body.id!==d._id)return void b.status(400).json({err:y.body.data[y.body.unique]+" exists"})}var{workflowUpdateFields:l,updatedFields:u}=(0,utils_1.compareUpdatedFields)(a,y.body.data),c={success:!0},h=(f&&(yield f(l)),this.parseModel(u));if(y.body.sensitive)if((0,moment_timezone_1.default)(y.body.data.baseUpdatedAt).isAfter((0,moment_timezone_1.default)(a.baseUpdatedAt))){if(yield o.updateOne({_id:s},{$set:Object.assign({},h)}),null!=(t=y.body)&&t.stopRes)return c;b.send(c)}else b.status(400).json({err:"Refresh Sensitive Model"});else{if(yield o.updateOne({_id:s},{$set:Object.assign({},h)}),null!=(i=y.body)&&i.stopRes)return c;b.send(c)}}else b.status(400).json({err:"Invalid Security Configuration"})}else b.status(400).json({err:"Invalid Model Structure"})}catch(e){this.debug&&this.debug(null,null,e),b.status(400).json({err:e})}}),this.initBatchDelete=(f,g,p)=>__awaiter(this,void 0,void 0,function*(){var e,i;try{if(Array.isArray(f.body.id)){var s=this.mongoDB.db(this.tableId).collection(this.collection),o=yield s.find({_id:{$in:f.body.id}}),a=(0,QueryModel_1.checkBaseConfig)(this.config,f),n=yield o.toArray();let t=!0;if(0<n.length&&(yield Promise.all(n.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"delete",f,e)||(t=!1)}))),a&&t){p&&(yield p(n)),yield s.deleteMany({_id:{$in:f.body.id}});var r={success:f.body.id};if(null!=(e=f.body)&&e.stopRes)return r;g.send(r)}else g.status(400).json({err:"Invalid Security Configuration"})}else{var d=f.body.query;const y={};d&&0<d.length&&d.map(e=>{var t,i=null!=(t=e.queryId)?t:"";const s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&(y.$or=[],e.searchIds.map(e=>{y.$or.push({[e]:{$regex:s,$options:"i"}})}));break;case"=":y[i]=s;break;case"!=":y[i]={$ne:s};break;case">":y[i]={$gt:s};break;case"<":y[i]={$lt:s};break;case"><":Array.isArray(s)&&2<=s.length&&(y[i]={$gte:s[0],$lte:s[1]});break;case"in":y[i]={$in:s}}});var l=this.mongoDB.db(this.tableId).collection(this.collection),u=yield l.find(y).toArray(),c=(0,QueryModel_1.checkBaseConfig)(this.config,f);let t=!0;const b=[];if(0<u.length&&u.map(e=>{b.push(e._id),(0,QueryModel_1.checkOpsConfig)(this.config,"delete",f,e)||(t=!1)}),p&&(yield p(u)),c&&t){yield l.deleteMany(y);var h={success:b};if(null!=(i=f.body)&&i.stopRes)return h;g.send(h)}else g.status(400).json({err:"Invalid Security Configuration"})}}catch(e){g.status(400).json({err:e})}}),this.initDelete=(r,d,l)=>__awaiter(this,void 0,void 0,function*(){var e;try{var t=r.body.id;if(t){var i=this.mongoDB.db(this.tableId).collection(this.collection),s=yield i.findOne({_id:t}),o=(0,QueryModel_1.checkBaseConfig)(this.config,r),a=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",r,s);if(o&&a){l&&(yield l(s)),yield i.findOneAndDelete({_id:t});var n={success:!0};if(null!=(e=r.body)&&e.stopRes)return n;d.send(n)}else d.status(400).json({err:"Invalid Security Configuration"})}else d.status(400).json({err:"Invalid Fields"})}catch(e){d.status(400).json({err:e})}}),this.initList=(U,z)=>__awaiter(this,void 0,void 0,function*(){var e,t,i,s,o,a,n,r,d;try{var l=(0,QueryModel_1.checkBaseConfig)(this.config,U);if(l){var u=U.body.aggregate,c=null!=(e=U.body.query)?e:[];const w={};if(c&&0<c.length&&c.map(e=>{var t,i=null!=(t=e.queryId)?t:"";const s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&(w.$or=[],e.searchIds.map(e=>{w.$or.push({[e]:{$regex:s,$options:"i"}})}));break;case"=":w[i]=s;break;case"!=":w[i]={$ne:s};break;case">":w[i]={$gt:s};break;case"<":w[i]={$lt:s};break;case"><":Array.isArray(s)&&2<=s.length&&(w[i]={$gte:s[0],$lte:s[1]});break;case"in":w[i]={$in:s}}}),u){var h=[],y=null!=(t=u.queryId)?t:"",b=null!=(i=u.dateId)?i:"baseUpdatedAt";for(const Q of null!=(s=u.range)?s:[]){Object.assign({},w);var f=Q.split("-"),[g,p=(0,moment_timezone_1.default)().format("DD/MM/YYYY HH:mm:ss")]=f,m=2===f.length,v=(0,moment_timezone_1.default)(g,"DD/MM/YYYY").isValid(),_=(0,moment_timezone_1.default)(p,"DD/MM/YYYY").isValid(),$=(0,moment_timezone_1.default)(g,"DD/MM/YYYY HH:mm:ss").tz("Asia/Kuala_lumpur").toDate(),M=(0,moment_timezone_1.default)(p,"DD/MM/YYYY HH:mm:ss").tz("Asia/Kuala_lumpur").toDate();if(!v||!_)return void z.status(400).json({err:"Invalid Aggregate Range Configuration"});var k=m?{$match:Object.assign(Object.assign({},w),{[b]:{$gte:$,$lte:M}})}:{$match:Object.assign(Object.assign({},w),{[b]:{$lte:$}})},I=[];let e=!1;var j=m?Q:"Begining until "+(0,moment_timezone_1.default)(g,"DD/MM/YYYY HH:mm:ss").format("DD/MM/YYYY");switch(u.type){case"avg":I.push(k,{$group:{_id:j,value:{$avg:"$"+y}}});break;case"sum":I.push(k,{$group:{_id:j,value:{$sum:"$"+y}}});break;case"count":e=!0}if(0===I.length&&!e)return void z.status(400).json({err:"Invalid Aggregate Configuration"});var C=this.mongoDB.db(this.tableId).collection(this.collection);let t=0;t=e?yield C.find(k.$match).count({}):null!=(a=null==(o=(yield C.aggregate(I).toArray())[0])?void 0:o.value)?a:0,h.push({_id:j,value:t})}var D={data:h};if(null!=(n=U.body)&&n.stopRes)return D;z.send(D)}else{let e=this.mongoDB.db(this.tableId).collection(this.collection).find(w);var O,A,q,B=yield e.count({}),Y=(U.body.cursor&&(e=e.skip(U.body.cursor)),U.body.stopLimit||(e=e.limit(null!=(r=U.body.limit)?r:10)),U.body.sort&&(A={[(O=U.body.sort).sortId]:"asc"===O.type?1:-1},e=e.sort(A)),yield e.toArray());let t=!0;if(0<Y.length&&Y.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"read",U,e)||(t=!1)}),l&&t)return q={data:Y,count:B},null!=(d=U.body)&&d.stopRes?q:void z.send(q);z.status(400).json({err:"Invalid Security Configuration"})}}else z.status(400).json({err:"Invalid Security Configuration"})}catch(e){z.status(400).json({err:e})}}),this.init=()=>{var e=this.lib.express.Router();return e.post("/create",(e,t)=>{this.initCreate(e,t)}),e.post("/batchCreate",(e,t)=>{this.initBatchCreate(e,t)}),e.post("/get",(e,t)=>{this.initGet(e,t)}),e.post("/update",(e,t)=>{this.initUpdate(e,t)}),e.post("/batchUpdate",(e,t)=>{this.initBatchUpdate(e,t)}),e.post("/delete",(e,t)=>{this.initDelete(e,t)}),e.post("/batchDelete",(e,t)=>{this.initBatchDelete(e,t)}),e.post("/list",(e,t)=>{this.initList(e,t)}),e},this.mongoDB=e,this.collection=t,this.isProd=i,this.config=s,this.modelChecker=o,this.lib=a;e=this.isProd?"prod":"dev";this.tableId=n?n+"-"+e:e,this.debug=r}}exports.MongoWrapper=MongoWrapper;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var __awaiter=this&&this.__awaiter||function(e,n,r,l){return new(r=r||Promise)(function(i,t){function s(e){try{a(l.next(e))}catch(e){t(e)}}function o(e){try{a(l.throw(e))}catch(e){t(e)}}function a(e){var t;e.done?i(e.value):((t=e.value)instanceof r?t:new r(function(e){e(t)})).then(s,o)}a((l=l.apply(e,n||[])).next())})},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.UploadWrapper=void 0;const QueryModel_1=require("../model/QueryModel"),axios_1=__importDefault(require("axios")),mime_1=__importDefault(require("mime"));class UploadWrapper{constructor(e,t,i,s,o){this.uploadAdmin="",this.isProd=!1,this.config={baseConfig:[],opsConfig:{read:[],create:[],update:[],delete:[]}},this.storageType="firebase",this.modelChecker=(e,t)=>!(!e.assetCollectionName||!e.assetParentId||!t&&!e.assetFileName),this.initUpload=(d,c,u)=>__awaiter(this,void 0,void 0,function*(){try{const l=d.body.data;if(this.modelChecker(l)&&(d.file||l.downloadLink)){var e=(0,QueryModel_1.checkBaseConfig)(this.config,d),t=(0,QueryModel_1.checkOpsConfig)(this.config,u,d,l);if(e&&t){const n=this.lib.randomUUID();if("aws"===this.storageType){var i,s=(n,r)=>new Promise((o,a)=>__awaiter(this,void 0,void 0,function*(){var e="buffer"===n,t=new this.uploadAdmin.S3;let i;e||(s=d.file.originalname.split(".").pop(),i="mbtiles"===s?"application/vnd.mapbox-vector-tile":mime_1.default.getType(s)||d.file.mimetype);var s={Bucket:`${l.bucketName}/${l.assetCollectionName}/`+l.assetParentId,Key:
|
|
1
|
+
"use strict";var __awaiter=this&&this.__awaiter||function(e,n,r,l){return new(r=r||Promise)(function(i,t){function s(e){try{a(l.next(e))}catch(e){t(e)}}function o(e){try{a(l.throw(e))}catch(e){t(e)}}function a(e){var t;e.done?i(e.value):((t=e.value)instanceof r?t:new r(function(e){e(t)})).then(s,o)}a((l=l.apply(e,n||[])).next())})},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.UploadWrapper=void 0;const QueryModel_1=require("../model/QueryModel"),axios_1=__importDefault(require("axios")),mime_1=__importDefault(require("mime"));class UploadWrapper{constructor(e,t,i,s,o){this.uploadAdmin="",this.isProd=!1,this.config={baseConfig:[],opsConfig:{read:[],create:[],update:[],delete:[]}},this.storageType="firebase",this.modelChecker=(e,t)=>!(!e.assetCollectionName||!e.assetParentId||!t&&!e.assetFileName),this.initUpload=(d,c,u)=>__awaiter(this,void 0,void 0,function*(){try{const l=d.body.data;if(this.modelChecker(l)&&(d.file||l.downloadLink)){var e=(0,QueryModel_1.checkBaseConfig)(this.config,d),t=(0,QueryModel_1.checkOpsConfig)(this.config,u,d,l);if(e&&t){const n=this.lib.randomUUID();if("aws"===this.storageType){var i,s=(n,r)=>new Promise((o,a)=>__awaiter(this,void 0,void 0,function*(){var e="buffer"===n,t=new this.uploadAdmin.S3;let i;e||(s=d.file.originalname.split(".").pop(),i="mbtiles"===s?"application/vnd.mapbox-vector-tile":mime_1.default.getType(s)||d.file.mimetype);var s={Bucket:`${l.bucketName}/${l.assetCollectionName}/`+l.assetParentId,Key:l.assetFileName,Body:e?r.data:d.file.buffer,ContentType:e?r.headers["content-type"]:i};t.upload(s,function(e){e?a(e):o("aws")})}));l.downloadLink?(i=yield(0,axios_1.default)({method:"GET",url:l.downloadLink,responseType:"arraybuffer"})).data&&(yield s("buffer",i)):yield s("file")}else{const r=this.uploadAdmin.storage().bucket();var o,a=(o,a)=>new Promise((i,s)=>__awaiter(this,void 0,void 0,function*(){var e="buffer"===o,t={contentType:e?a.headers["content-type"]:d.file.mimetype,customMetadata:{"file-name":"buffer"===o?l.assetFileName:d.file.originalname},metadata:{firebaseStorageDownloadTokens:n}},t=r.file(`${l.assetCollectionName}/${l.assetParentId}/`+l.assetFileName).createWriteStream({metadata:t});t.end(e?a.data:d.file.buffer),t.on("error",s),t.on("finish",i)}));l.downloadLink?(o=yield(0,axios_1.default)({method:"GET",url:l.downloadLink,responseType:"arraybuffer"})).data&&(yield a("buffer",o)):yield a("file")}c.send({data:n})}else c.status(400).json({err:"Invalid Security Configuration"})}else c.status(400).json({err:"Invalid Fields"})}catch(e){c.status(400).json({err:e})}}),this.initDelete=(a,n)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s,o=a.body.data;this.modelChecker(o)?(e=(0,QueryModel_1.checkBaseConfig)(this.config,a),t=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",a,o),e&&t?("aws"===this.storageType?(i=new this.uploadAdmin.S3,s={Bucket:`${o.bucketName}/${o.assetCollectionName}/`+o.assetParentId,Key:o.assetFileName},yield i.deleteObject(s).promise()):yield this.uploadAdmin.storage().bucket().file(`${o.assetCollectionName}/${o.assetParentId}/`+o.assetFileName).delete(),n.send({success:!0})):n.status(400).json({err:"Invalid Security Configuration"})):n.status(400).json({err:"Invalid Fields"})}catch(e){n.status(400).json({err:e})}}),this.deleteGoogleFolderContents=a=>__awaiter(this,void 0,void 0,function*(){try{var e={prefix:a.name,autoPaginate:!1},[t]=yield this.uploadAdmin.storage().bucket().getFiles(e),i=[];for(const s of t)i.push(s.delete());for(const o of t)o.name.endsWith("/")&&i.push(this.deleteGoogleFolderContents(o));yield Promise.all(i)}catch(e){}}),this.deleteAWSFolderContent=(o,a)=>__awaiter(this,void 0,void 0,function*(){try{var e=new this.uploadAdmin.S3,t=(yield e.listObjectsV2({Bucket:o,Prefix:a}).promise()).Contents.map(e=>e.Key),i=[];for(const s of t)i.push(e.deleteObject({Bucket:o,Key:s}).promise());yield Promise.all(i)}catch(e){}}),this.initDeleteDirectory=(o,a)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s=o.body.data;this.modelChecker(s,!0)?(e=(0,QueryModel_1.checkBaseConfig)(this.config,o),t=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",o,s),e&&t?("aws"===this.storageType?yield this.deleteAWSFolderContent(""+s.bucketName,`${s.assetCollectionName}/${s.assetParentId}/`):(i=this.uploadAdmin.storage().bucket().file(`${s.assetCollectionName}/${s.assetParentId}/`),yield this.deleteGoogleFolderContents(i)),a.send({success:!0})):a.status(400).json({err:"Invalid Security Configuration"})):a.status(400).json({err:"Invalid Fields"})}catch(e){a.status(400).json({err:e})}}),this.initBatchDeleteDirectory=(o,a)=>__awaiter(this,void 0,void 0,function*(){try{const s=o.body.data;var e,t,i=JSON.parse(s.assetParentId);this.modelChecker(s,!0)?(e=(0,QueryModel_1.checkBaseConfig)(this.config,o),t=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",o,s),e&&t?(yield Promise.all(i.map(t=>__awaiter(this,void 0,void 0,function*(){var e;"aws"===this.storageType?yield this.deleteAWSFolderContent(""+s.bucketName,s.assetCollectionName+`/${t}/`):(e=this.uploadAdmin.storage().bucket().file(s.assetCollectionName+`/${t}/`),yield this.deleteGoogleFolderContents(e))}))),a.send({success:!0})):a.status(400).json({err:"Invalid Security Configuration"})):a.status(400).json({err:"Invalid Fields"})}catch(e){a.status(400).json({err:e})}}),this.initGet=(n,r)=>__awaiter(this,void 0,void 0,function*(){try{const{bucketName:e,assetCollectionName:t,assetParentId:i,fileName:s}=n.params;if("aws"===this.storageType){const o=new this.uploadAdmin.S3,a={Bucket:e,Key:t+`/${i}/`+s};o.headObject(a,e=>__awaiter(this,void 0,void 0,function*(){if(e&&"NotFound"===e.code)r.status(404).send({message:"File not found"});else{var t=yield o.getObject(a).promise(),i=s.split(".").pop();let e;e="mbtiles"===i?"application/vnd.mapbox-vector-tile":mime_1.default.getType(i)||t.ContentType,r.set("Content-Type",e),r.send(t.Body)}}))}}catch(e){r.status(400).json({err:e})}}),this.init=()=>{var e=this.lib.express.Router();return this.lib.cors&&e.use(this.lib.cors()),e.post("/upload",this.lib.multer.single("file"),(e,t)=>{this.initUpload(e,t,"create")}),e.post("/update",this.lib.multer.single("file"),(e,t)=>{this.initUpload(e,t,"update")}),e.post("/delete",this.initDelete),e.post("/deleteDirectory",this.initDeleteDirectory),e.post("/batchDeleteDirectory",this.initBatchDeleteDirectory),e.get("/get/:bucketName/:assetCollectionName/:assetParentId/:fileName",this.initGet),e},this.uploadAdmin=e,this.isProd=t,this.config=i,this.lib=s,this.storageType=null!=o?o:"firebase"}}exports.UploadWrapper=UploadWrapper;
|