@cocreate/crud-server 1.2.10 → 1.3.2

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/src/industry.js DELETED
@@ -1,324 +0,0 @@
1
-
2
- const CoCreateBase = require("./base");
3
- const {ObjectID, Binary} = require("mongodb");
4
-
5
- class CoCreateIndustry extends CoCreateBase {
6
- constructor(wsManager, db) {
7
- super(wsManager, db);
8
- this.init()
9
- }
10
-
11
- init() {
12
- if (this.wsManager) {
13
- this.wsManager.on('runIndustry', (socket, data, roomInfo) => this.runIndustry(socket, data));
14
- this.wsManager.on('createIndustry', (socket, data, roomInfo) => this.createIndustry(socket, data));
15
- this.wsManager.on('fetchInfoForBuilder', (socket, data) => this.fetchInfoForBuilder(socket, data))
16
- }
17
- }
18
-
19
- /**
20
- * Run Industry logic
21
- **/
22
- async runIndustry(socket, data) {
23
- const {industry_id, newOrg_id} = data
24
- const db = data.organization_id;
25
- let industryDocumentsCollection = this.getDB(db).collection('industry_documents');
26
-
27
-
28
- let industry = await this.getDB(db).collection('industries').findOne({_id: ObjectID(industry_id)});
29
- let error = null;
30
-
31
- if (!industry._id) {
32
- error = "Can't get industry"
33
- } else {
34
- let newOrgDocument = await this.getDB(db).collection('organizations').findOne({_id: ObjectID(newOrg_id)});
35
-
36
- if (!newOrgDocument) {
37
- error = "Can't get organization";
38
- } else {
39
- let new_subdomain = newOrgDocument && newOrgDocument.domains ? newOrgDocument.domains[0] : "";
40
-
41
- const {adminUI_id, builderUI_id, idPairs} = await this.createEmptyDocumentsFromIndustry
42
- (
43
- industryDocumentsCollection,
44
- industry_id,
45
- newOrgDocument,
46
- industry.organization_data || {},
47
- new_subdomain
48
- );
49
- await this.updateDocumentsByIndustry(idPairs, newOrg_id)
50
- this.wsManager.send(socket, 'runIndustry', {
51
- error: false,
52
- message: "successfuly",
53
- adminUI_id,
54
- builderUI_id,
55
- industry_id
56
- }, data['organization_id'])
57
- return;
58
- }
59
- }
60
- if (error) {
61
- this.wsManager.send(socket, 'runIndustry', {
62
- error: true,
63
- message: error,
64
- }, data['organization_id'])
65
- }
66
-
67
-
68
- }
69
-
70
- async createEmptyDocumentsFromIndustry(industryDocumentsCollection, industry_id, newOrg, orgData, new_subdomain) {
71
- const newOrgId = newOrg._id.toString();
72
- const newOrgApiKey = newOrg.apiKey;
73
-
74
- const {subdomain, apiKey, organization_id} = orgData;
75
-
76
-
77
- const newDB = this.getDB(newOrgId);
78
- const self = this;
79
- let adminUI_id = '';
80
- let builderUI_id = '';
81
- let idPairs = [];
82
-
83
- let documentCursor = industryDocumentsCollection.find({"industry_data.industry_id" : industry_id})
84
-
85
- while(await documentCursor.hasNext()) {
86
- let document = await documentCursor.next();
87
- const {collection, document_id, industry_id} = document.industry_data || {}
88
- if (!collection || !document_id) {
89
- continue;
90
- }
91
- const collectionInstance = newDB.collection(collection);
92
-
93
- document['organization_id'] = newOrgId;
94
-
95
- delete document['_id'];
96
- delete document['industry_data'];
97
-
98
- //. replace subdomain
99
- for (let field in document) {
100
- if (field != '_id' && field != 'organization_id') {
101
- if (subdomain && new_subdomain) {
102
- document[field] = self.replaceContent(document[field], subdomain, new_subdomain);
103
- }
104
-
105
- if (newOrgId && organization_id) {
106
- document[field] = self.replaceContent(document[field], organization_id, newOrgId);
107
- }
108
- if (newOrgApiKey && apiKey) {
109
- document[field] = self.replaceContent(document[field], apiKey, newOrgApiKey);
110
- }
111
- }
112
- }
113
-
114
- let newDocument = await collectionInstance.insertOne(document);
115
- if (newDocument) {
116
- if (newDocument['ops'][0].name == 'Admin UI') {
117
- adminUI_id = newDocument['ops'][0]['_id'];
118
- } else if (newDocument['ops'][0].name == 'Builder UI') {
119
- builderUI_id = newDocument['ops'][0]['_id'];
120
- }
121
-
122
- idPairs.push({
123
- new_id : newDocument['ops'][0]['_id'].toString(),
124
- old_id : document_id,
125
- collection_name: collection
126
- })
127
- }
128
- }
129
- // console.log(idPairs);
130
- // console.log(idPairs.length);
131
-
132
- return {
133
- adminUI_id: adminUI_id,
134
- builderUI_id: builderUI_id,
135
- idPairs: idPairs
136
- }
137
- }
138
-
139
- async updateDocumentsByIndustry(idPairs, newOrgId) {
140
- const newDB = this.getDB(newOrgId);
141
-
142
- for (let i = 0; i < idPairs.length; i++) {
143
- const {collection_name, new_id} = idPairs[i];
144
- const collection = newDB.collection(collection_name);
145
-
146
- let document = await collection.findOne({'_id': ObjectID(new_id)});
147
-
148
- for (let field in document) {
149
- if (field != '_id' && field != 'organization_id') {
150
- var newFieldValue = this.replaceId(document[field], idPairs);
151
- document[field] = newFieldValue;
152
- }
153
- }
154
-
155
- delete document['_id'];
156
- await collection.findOneAndUpdate({'_id': ObjectID(new_id)}, {$set: document});
157
- }
158
- }
159
-
160
- replaceId(fieldValue, idPairs) {
161
- const self = this;
162
- idPairs.forEach(({old_id, new_id}) => {
163
- fieldValue = self.replaceContent(fieldValue, old_id, new_id)
164
- })
165
- return fieldValue;
166
- }
167
-
168
- replaceContent(content, src, target) {
169
- const type = typeof content
170
- if (type == 'string') {
171
- content = content.replace(new RegExp(src, 'g'), target);
172
- } else if (type == "object") {
173
- for (let key in content) {
174
- if (content[key] && typeof content[key] == 'string') {
175
- content[key] = content[key].replace(new RegExp(src, 'g'), target);
176
- }
177
- }
178
- }
179
- return content
180
- }
181
-
182
- /**
183
- * Create industry
184
- **/
185
- async createIndustry(socket, data)
186
- {
187
- try {
188
- const {organization_id, db} = data;
189
- const self = this;
190
- const collection = this.getCollection(data);
191
-
192
- let orgDocument = await this.getDB(db).collection('organizations').findOne({_id: ObjectID(organization_id)});
193
- let subdomain = orgDocument && orgDocument.domains ? orgDocument.domains[0] : "";
194
-
195
- let insertData = data.data;
196
- insertData.organization_data = {
197
- subdomain: subdomain,
198
- apiKey: orgDocument.apiKey,
199
- organization_id: organization_id
200
- }
201
- //. set masterDB
202
- insertData.organization_id = db || organization_id;
203
- let insertResult = await collection.insertOne(insertData);
204
-
205
- const industry_id = insertResult.ops[0]._id + "";
206
- // const anotherCollection = self.getDB(organization_id).collection(data['collection']);
207
- // //. update organization
208
- // insertResult.ops[0].organization_id = organization_id;
209
- // anotherCollection.insertOne(insertResult.ops[0]);
210
-
211
- //. create inustryDocuments
212
- const srcDB = this.getDB(organization_id);
213
- let collections = []
214
- const exclusion_collections = ["users", "organizations", "industries", "industry_documents", "crdt-transactions", "metrics"];
215
- collections = await srcDB.listCollections().toArray()
216
- collections = collections.map(x => x.name)
217
-
218
- for (let i = 0; i < collections.length; i++) {
219
- let collection = collections[i];
220
- if (exclusion_collections.indexOf(collection) > -1) {
221
- continue;
222
- }
223
- await self.insertDocumentsToIndustry(collection, industry_id, organization_id, db);
224
- }
225
-
226
- //. update subdomain
227
- const response = {
228
- 'db': data['db'],
229
- 'organization_id': organization_id,
230
- 'industry_id': industry_id,
231
- 'data': collections,
232
- 'metadata': data['metadata'],
233
- }
234
-
235
- console.log(response)
236
- self.wsManager.send(socket, 'createIndustry', response, data['organization_id']);
237
-
238
-
239
- } catch (error) {
240
- console.log(error)
241
- }
242
- }
243
-
244
- async insertDocumentsToIndustry(collectionName, industryId, organizationId, targetDB) {
245
- try{
246
- const industryDocumentsCollection = this.getDB(targetDB).collection('industry_documents');
247
- const collection = this.getDB(organizationId).collection(collectionName);
248
-
249
- const query = {
250
- 'organization_id': organizationId
251
- }
252
-
253
- const documentCursor = collection.find(query);
254
- await documentCursor.forEach(async (document) => {
255
- var documentId = document['_id'].toString();
256
-
257
- delete document['_id'];
258
-
259
- document['industry_data'] = {
260
- document_id: documentId,
261
- industry_id: industryId,
262
- collection: collectionName,
263
- }
264
-
265
- industryDocumentsCollection.update(
266
- {
267
- "industry_data.document_id" : {$eq: documentId},
268
- "industry_data.industry_id" : {$eq: industryId},
269
- "industry_data.collection" : {$eq: collectionName},
270
- },
271
- {
272
- $set: document
273
- },
274
- {
275
- upsert: true
276
- }
277
- );
278
- })
279
- }
280
- catch (e) {
281
- console.log(e)
282
- }
283
- }
284
-
285
- //. builder
286
- async fetchInfoForBuilder(socket, data) {
287
- const self = this;
288
- if (!await this.checkSecurity(data)) {
289
- this.wsManager.send(socket, 'securityError', 'error', data['organization_id']);
290
- return;
291
- }
292
-
293
- try {
294
-
295
- var collectionList = [];
296
- var modules = [];
297
- this.db.listCollections().toArray(function(err, collInfos) {
298
-
299
- collInfos.forEach(function(colInfo) {
300
- collectionList.push(colInfo.name);
301
- })
302
-
303
- const modulesCollection = this.db.collection('modules');
304
- modulesCollection.find().toArray(function(error, result) {
305
- result.forEach(function(item) {
306
- modules.push({
307
- _id: item['_id'],
308
- name: item['name']
309
- })
310
- })
311
-
312
- self.wsManager.send(socket, 'fetchedInfoForBuilder', {
313
- collections: collectionList,
314
- modules: modules
315
- }, data['organization_id']);
316
- })
317
- });
318
- } catch (error) {
319
- console.log('fetchInfoBuilder error');
320
- }
321
- }
322
- }
323
-
324
- module.exports = CoCreateIndustry;
@@ -1,112 +0,0 @@
1
-
2
- const CoCreateBase = require("./base");
3
- const {ObjectID, Binary} = require("mongodb");
4
-
5
- class CoCreateOrganization extends CoCreateBase {
6
- constructor(wsManager, db) {
7
- super(wsManager, db);
8
- this.init()
9
- }
10
-
11
- init() {
12
- if (this.wsManager) {
13
- this.wsManager.on('createOrgNew', (socket, data, roomInfo) => this.createOrgNew(socket, data));
14
- this.wsManager.on('createOrg', (socket, data, roomInfo) => this.createOrg(socket, data));
15
- this.wsManager.on('deleteOrg', (socket, data, roomInfo) => this.deleteOrg(socket, data));
16
- }
17
- }
18
-
19
- async createOrgNew(socket, data) {
20
- const self = this;
21
- if(!data) return;
22
- const newOrg_id = data.newOrg_id;
23
- if (newOrg_id != data.organization_id) {
24
- try{
25
- const collection = this.db.collection(data['collection']);
26
- const query = {
27
- "_id": new ObjectID(newOrg_id)
28
- };
29
-
30
- collection.find(query).toArray(function(error, result) {
31
- if(!error && result){
32
- const newOrgDb = self.getDB(newOrg_id).collection(data['collection']);
33
- // Create new user in config db users collection
34
- newOrgDb.insertOne({...result.ops[0], organization_id : newOrg_id}, function(error, result) {
35
- if(!error && result){
36
- const response = { ...data, document_id: result.ops[0]._id, data: result.ops[0]}
37
- self.wsManager.send(socket, 'createOrgNew', response, data['organization_id']);
38
- }
39
- });
40
- }
41
- });
42
- }catch(error){
43
- console.log('createDocument error', error);
44
- }
45
- }
46
- }
47
-
48
- async createOrg(socket, data) {
49
- const self = this;
50
- if(!data.data) return;
51
-
52
- try{
53
- const collection = this.getCollection(data);
54
- // create new org in config db organization collection
55
- collection.insertOne({ ...data.data, organization_id: data.organization_id }, function(error, result) {
56
- if(!error && result){
57
- const orgId = result.ops[0]._id + "";
58
- const anotherCollection = self.getDB(orgId).collection(data['collection']);
59
- // Create new org db and insert organization
60
- anotherCollection.insertOne({...result.ops[0], organization_id : orgId});
61
-
62
- const response = { ...data, document_id: result.ops[0]._id, data: result.ops[0] }
63
-
64
- self.wsManager.send(socket, 'createOrg', response );
65
- if (data.room) {
66
- self.wsManager.broadcast(socket, data.namespace || data['organization_id'] , data.room, 'createDocument', response, true);
67
- } else {
68
- self.wsManager.broadcast(socket, data.namespace || data['organization_id'], null, 'createDocument', response)
69
- }
70
- }
71
- // add new org to masterDb
72
- const masterOrgDb = self.getDB(data.mdb).collection(data['collection']);
73
- masterOrgDb.insertOne({...result.ops[0], organization_id : data['mdb']});
74
-
75
- });
76
- }catch(error){
77
- console.log('createDocument error', error);
78
- }
79
- }
80
-
81
-
82
- async deleteOrg(socket, data) {
83
- const self = this;
84
- if(!data.data) return;
85
-
86
- try{
87
- const collection = this.getCollection(data);
88
- return;
89
- collection.insertOne(data.data, function(error, result) {
90
- if(!error && result){
91
- const orgId = result.ops[0]._id + "";
92
- const anotherCollection = self.getDB(orgId).collection(data['collection']);
93
- anotherCollection.insertOne(result.ops[0]);
94
-
95
- const response = { ...data, document_id: result.ops[0]._id, data: result.ops[0]}
96
-
97
- self.wsManager.send(socket, 'createOrg', response);
98
- if (data.room) {
99
- self.wsManager.broadcast(socket, data.namespace || data['organization_id'] , data.room, 'createDocument', response, true);
100
- } else {
101
- self.wsManager.broadcast(socket, data.namespace || data['organization_id'], null, 'createDocument', response)
102
- }
103
- }
104
- });
105
- }catch(error){
106
- console.log('createDocument error', error);
107
- }
108
- }
109
-
110
- }
111
-
112
- module.exports = CoCreateOrganization;
package/src/unique.js DELETED
@@ -1,54 +0,0 @@
1
- const CoCreateBase = require("./base");
2
-
3
- class CoCreateUnique extends CoCreateBase {
4
- constructor(wsManager, db) {
5
- super(wsManager, db);
6
- this.init();
7
- }
8
-
9
- init() {
10
- if (this.wsManager) {
11
- this.wsManager.on('checkUnique', (socket, data, roomInfo) => this.checkUnique(socket, data, roomInfo));
12
- }
13
- }
14
-
15
-
16
- async checkUnique(socket, req_data) {
17
- const securityRes = await this.checkSecurity(req_data);
18
- const self = this;
19
- if (!securityRes.result) {
20
- this.wsManager.send(socket, 'securityError', 'error');
21
- return;
22
- }
23
-
24
- const collection = this.db.collection(req_data["collection"]);
25
- const query = {
26
- [req_data['name']]: req_data['value']
27
- };
28
-
29
- if (securityRes['organization_id']) {
30
- query['organization_id'] = securityRes['organization_id'];
31
- }
32
-
33
- try {
34
- collection.find(query).toArray(function(error, result) {
35
- if (!error && result) {
36
- let response = {
37
- request_id: req_data['request_id'],
38
- name: req_data['name'],
39
- unique: true
40
- };
41
- if (result.length) {
42
- response.unique = false;
43
- }
44
- self.wsManager.send(socket, 'checkedUnique', response, req_data['organization_id']);
45
- }
46
- });
47
- } catch (error) {
48
- console.log(error);
49
- }
50
- }
51
-
52
- }
53
-
54
- module.exports = CoCreateUnique;