@cocreate/crud-server 1.2.8 → 1.3.0

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/user.js DELETED
@@ -1,284 +0,0 @@
1
-
2
- const CoCreateBase = require("./base");
3
- const {ObjectID, Binary} = require("mongodb");
4
-
5
- class CoCreateUser 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('createUserNew', (socket, data, roomInfo) => this.createUserNew(socket, data));
14
- this.wsManager.on('createUser', (socket, data, roomInfo) => this.createUser(socket, data));
15
- this.wsManager.on('login', (socket, data, roomInfo) => this.login(socket, data, roomInfo))
16
- this.wsManager.on('usersCurrentOrg', (socket, data, roomInfo) => this.usersCurrentOrg(socket, data, roomInfo))
17
- this.wsManager.on('fetchUser', (socket, data, roomInfo) => this.fetchUser(socket, data, roomInfo))
18
- this.wsManager.on('userStatus', (socket, data, roomInfo) => this.setUserStatus(socket, data, roomInfo))
19
- }
20
- }
21
-
22
-
23
- async createUserNew(socket, data) {
24
- const self = this;
25
- if(!data) return;
26
- const newOrg_id = data.newOrg_id;
27
- if (newOrg_id != data.organization_id) {
28
- try{
29
- const collection = this.db.collection(data);
30
- const query = {
31
- "_id": new ObjectID(data["user_id"])
32
- };
33
-
34
- collection.find(query).toArray(function(error, result) {
35
- if(!error && result){
36
- const newOrgDb = self.getDB(newOrg_id).collection(data['collection']);
37
- // Create new user in config db users collection
38
- newOrgDb.insertOne({...result.ops[0], organization_id : newOrg_id}, function(error, result) {
39
- if(!error && result){
40
- const response = { ...data, document_id: result.ops[0]._id, data: result.ops[0]}
41
- self.wsManager.send(socket, 'createUserNew', response, data['organization_id']);
42
- }
43
- });
44
- }
45
- });
46
- }catch(error){
47
- console.log('createDocument error', error);
48
- }
49
- }
50
- }
51
-
52
- async createUser(socket, data) {
53
- const self = this;
54
- if(!data.data) return;
55
-
56
- try{
57
- const collection = this.getCollection(data);
58
- // Create new user in config db users collection
59
- collection.insertOne(data.data, function(error, result) {
60
- if(!error && result){
61
- const orgDB = data.orgDB;
62
- // if new orgDb Create new user in new org db users collection
63
- if (orgDB != data.organization_id) {
64
- if (orgDB) {
65
- const anotherCollection = self.getDB(orgDB).collection(data['collection']);
66
- anotherCollection.insertOne({...result.ops[0], organization_id : orgDB});
67
- }
68
- }
69
- const response = { ...data, document_id: result.ops[0]._id, data: result.ops[0]}
70
- self.wsManager.send(socket, 'createUser', response, data['organization_id']);
71
- }
72
- });
73
- }catch(error){
74
- console.log('createDocument error', error);
75
- }
76
- }
77
-
78
-
79
- /**
80
- data = {
81
- namespace: string,
82
- collection: string,
83
- loginData: object,
84
- eId: string,
85
- form_id: string,
86
-
87
- apiKey: string,
88
- organization_id: string
89
- }
90
- **/
91
- async login(socket, req_data) {
92
- const securityRes = await this.checkSecurity(req_data);
93
- const self = this;
94
- if (!securityRes.result) {
95
- self.wsManager.send(socket, 'securityError', 'error', req_data['organization_id']);
96
- return;
97
- }
98
- try {
99
- const {organization_id} = req_data
100
- const selectedDB = organization_id;
101
- const collection = self.getDB(selectedDB).collection(req_data["collection"]);
102
- const query = new Object();
103
-
104
- // query['connected_orgs'] = data['organization_id'];
105
-
106
- for (var key in req_data['loginData']) {
107
- query[key] = req_data['loginData'][key];
108
- }
109
-
110
- collection.find(query).toArray(async function(error, result) {
111
- let response = {
112
- eId: req_data['eId'],
113
- uid: req_data['uid'],
114
- form_id: req_data['form_id'],
115
- success: false,
116
- message: "Login failed",
117
- status: "failed"
118
- }
119
- if (!error && result && result.length > 0) {
120
- let token = null;
121
- if (self.wsManager.authInstance) {
122
- token = await self.wsManager.authInstance.generateToken({user_id: result[0]['_id']});
123
- }
124
-
125
- response = { ...response,
126
- success: true,
127
- id: result[0]['_id'],
128
- // collection: collection,
129
- document_id: result[0]['_id'],
130
- current_org: result[0]['current_org'],
131
- message: "Login successful",
132
- status: "success",
133
- token
134
- };
135
- }
136
- console.log('before socket', response);
137
- self.wsManager.send(socket, 'login', response, req_data['organization_id'])
138
- console.log('success socket', req_data['organization_id']);
139
- });
140
- } catch (error) {
141
- console.log('login failed', error);
142
- }
143
- }
144
-
145
- /**
146
- data = {
147
- namespace: string,
148
- collection: string,
149
- user_id: string,
150
- href: string
151
- }
152
- **/
153
- async usersCurrentOrg(socket, req_data) {
154
- try {
155
- const self = this;
156
- const {organization_id, db} = req_data
157
- const selectedDB = db || organization_id;
158
- const collection = this.getDB(selectedDB).collection(req_data["collection"]);
159
-
160
- let query = new Object();
161
-
162
- query['_id'] = new ObjectID(req_data['user_id']);
163
-
164
- collection.find(query).toArray(function(error, result) {
165
-
166
- if (!error && result && result.length > 0) {
167
-
168
- if (result.length > 0) {
169
- let org_id = result[0]['current_org'];
170
- const orgCollection = self.getDB(selectedDB).collection('organizations');
171
-
172
- orgCollection.find({"_id": new ObjectID(org_id),}).toArray(function(err, res) {
173
- if (!err && res && res.length > 0) {
174
- self.wsManager.send(socket, 'usersCurrentOrg', {
175
- id: req_data['id'],
176
- uid: req_data['uid'],
177
- success: true,
178
- user_id: result[0]['_id'],
179
- current_org: result[0]['current_org'],
180
- apiKey: res[0]['apiKey'],
181
- adminUI_id: res[0]['adminUI_id'],
182
- builderUI_id: res[0]['builderUI_id'],
183
- href: req_data['href']
184
- }, req_data['organization_id'])
185
- }
186
- });
187
- }
188
- } else {
189
- // socket.emit('loginResult', {
190
- // form_id: data['form_id'],
191
- // success: false
192
- // });
193
- }
194
- });
195
- } catch (error) {
196
-
197
- }
198
- }
199
-
200
- /**
201
- data = {
202
- namespace: string,
203
- collection: string,
204
- user_id: object,
205
-
206
- apiKey: string,
207
- organization_id: string
208
- }
209
- **/
210
- async fetchUser(socket, req_data) {
211
- const self = this;
212
- const securityRes = await this.checkSecurity(req_data);
213
- if (!securityRes.result) {
214
- this.wsManager.send(socket, 'securityError', 'error', req_data['organization_id']);
215
- return;
216
- }
217
-
218
- try {
219
- const {organization_id, db} = req_data
220
- const selectedDB = db || organization_id;
221
- const collection = self.getDB(selectedDB).collection(req_data['collection']);
222
- const user_id = req_data['user_id'];
223
- const query = {
224
- "_id": new ObjectID(user_id),
225
- }
226
-
227
- if (securityRes['organization_id']) {
228
- query['organization_id'] = securityRes['organization_id'];
229
- }
230
-
231
- collection.findOne(query, function(error, result) {
232
- if (!error) {
233
- self.wsManager.send(socket, 'fetchedUser', result, req_data['organization_id']);
234
- }
235
- })
236
- } catch (error) {
237
- console.log('fetchUser error')
238
- }
239
- }
240
-
241
- /**
242
- * status: 'on/off/idle'
243
- */
244
- async setUserStatus(socket, req_data, roomInfo) {
245
- const self = this;
246
- // const securityRes = await this.checkSecurity(data);
247
- // if (!securityRes.result) {
248
- // this.wsManager.send(socket, 'securityError', 'error');
249
- // return;
250
- // }
251
- const {info, status} = req_data;
252
-
253
- const items = info.split('/');
254
-
255
- if (items[0] !== 'users') {
256
- return;
257
- }
258
-
259
- if (!items[1]) return;
260
-
261
- try {
262
- const {organization_id, db} = req_data
263
- const selectedDB = db || organization_id;
264
- const collection = self.getDB(selectedDB).collection('users');
265
- const user_id = items[1];
266
- const query = {
267
- "_id": new ObjectID(user_id),
268
- }
269
- collection.update(query, {$set: {status: status}}, function(err, res) {
270
- if (!err) {
271
- self.wsManager.broadcast(socket, '', null, 'changedUserStatus',
272
- {
273
- user_id,
274
- status
275
- })
276
- }
277
- })
278
- } catch (error) {
279
- console.log('fetchUser error')
280
- }
281
- }
282
- }
283
-
284
- module.exports = CoCreateUser;