@cocreate/users 1.9.13 → 1.10.1

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/CHANGELOG.md CHANGED
@@ -1,3 +1,29 @@
1
+ ## [1.10.1](https://github.com/CoCreate-app/CoCreate-users/compare/v1.10.0...v1.10.1) (2022-11-21)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * bump d@cocreate ependencies ([ee0e697](https://github.com/CoCreate-app/CoCreate-users/commit/ee0e6973ba07012fba07db4ba0d97e31df1be136))
7
+
8
+ # [1.10.0](https://github.com/CoCreate-app/CoCreate-users/compare/v1.9.13...v1.10.0) (2022-11-21)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * bump cdn to 1.28.7 ([a80ebcc](https://github.com/CoCreate-app/CoCreate-users/commit/a80ebcc52c8f82e878241f0dae933039163c5207))
14
+ * bump cdn to 1.28.8 ([a66d8bd](https://github.com/CoCreate-app/CoCreate-users/commit/a66d8bd75045a968755362c05b323768ebee6e16))
15
+ * renamed data.data to data.document ([6aa2257](https://github.com/CoCreate-app/CoCreate-users/commit/6aa2257f3c755fa8cf112f89fe43c8270aa6c729))
16
+ * renamed getSocket to getSockets ([56fd7c9](https://github.com/CoCreate-app/CoCreate-users/commit/56fd7c90f68785e9f654e052bae9a7df5335e2f7))
17
+ * typo data | request ([2ef1f2f](https://github.com/CoCreate-app/CoCreate-users/commit/2ef1f2fb485d3e42159479e5d517476ea3785689))
18
+ * update crud functions to receive an array of objects as the response ([117db56](https://github.com/CoCreate-app/CoCreate-users/commit/117db56540891728946b183c561df56ad5aa23c0))
19
+ * update status to userStatus ([febb7ac](https://github.com/CoCreate-app/CoCreate-users/commit/febb7acb7fa13db900896db069195796a25064b2))
20
+
21
+
22
+ ### Features
23
+
24
+ * Add @cocreate/element-prototype ([9c2e8be](https://github.com/CoCreate-app/CoCreate-users/commit/9c2e8be1f7516877ee36074b0dcd1d9a03e3bd4a))
25
+ * updated to use new crud which has the abilty to use multiple databases ([88db3d3](https://github.com/CoCreate-app/CoCreate-users/commit/88db3d328bdc82d97c8e265bfb2e39c10d7383a5))
26
+
1
27
  ## [1.9.13](https://github.com/CoCreate-app/CoCreate-users/compare/v1.9.12...v1.9.13) (2022-10-02)
2
28
 
3
29
 
package/demo/login.html CHANGED
@@ -83,7 +83,7 @@
83
83
  }
84
84
  </script>
85
85
 
86
- <script src="https://cdn.cocreate.app/1.28.5/CoCreate.min.js"></script>
86
+ <script src="https://cdn.cocreate.app/1.28.8/CoCreate.min.js"></script>
87
87
  <!-- <script src="../../../CoCreateJS/dist/CoCreate.js"></script> -->
88
88
 
89
89
  </body>
package/demo/logout.html CHANGED
@@ -32,7 +32,7 @@
32
32
  </div>
33
33
  </div>
34
34
 
35
- <script src="https://cdn.cocreate.app/1.28.5/CoCreate.min.js"></script>
35
+ <script src="https://cdn.cocreate.app/1.28.8/CoCreate.min.js"></script>
36
36
  <!-- <script src="../../../CoCreateJS/dist/CoCreate.js"></script> -->
37
37
 
38
38
  </body>
package/demo/signup.html CHANGED
@@ -103,7 +103,7 @@
103
103
  host: 'server.cocreate.app' // Points to socket server. If not defined it will use the url in web browser by default
104
104
  }
105
105
  </script>
106
- <script src="https://cdn.cocreate.app/1.28.5/CoCreate.min.js"></script>
106
+ <script src="https://cdn.cocreate.app/1.28.8/CoCreate.min.js"></script>
107
107
  <!-- <script src="../../../CoCreateJS/dist/CoCreate.js"></script> -->
108
108
 
109
109
  </body>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/users",
3
- "version": "1.9.13",
3
+ "version": "1.10.1",
4
4
  "description": "A simple users component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "users",
@@ -61,11 +61,11 @@
61
61
  "webpack-log": "^3.0.1"
62
62
  },
63
63
  "dependencies": {
64
- "@cocreate/actions": "^1.5.12",
65
- "@cocreate/crud-client": "^1.12.12",
66
- "@cocreate/docs": "^1.3.18",
67
- "@cocreate/elements": "^1.12.11",
68
- "@cocreate/render": "^1.15.10",
64
+ "@cocreate/actions": "^1.5.13",
65
+ "@cocreate/crud-client": "^1.13.0",
66
+ "@cocreate/docs": "^1.3.20",
67
+ "@cocreate/element-prototype": "^1.0.0",
68
+ "@cocreate/render": "^1.16.1",
69
69
  "mongodb": "^4.4.0"
70
70
  }
71
71
  }
package/src/client.js CHANGED
@@ -1,7 +1,14 @@
1
1
  /*globals CustomEvent, btoa*/
2
- import crud from '@cocreate/crud-client';
2
+ import CRUD from '@cocreate/crud-client';
3
3
  import action from '@cocreate/actions';
4
4
  import render from '@cocreate/render';
5
+ import '@cocreate/element-prototype';
6
+
7
+ let crud
8
+ if(CRUD && CRUD.default)
9
+ crud = CRUD.default
10
+ else
11
+ crud = CRUD
5
12
 
6
13
  const CONST_PERMISSION_CLASS = 'checkPermission';
7
14
 
@@ -16,7 +23,7 @@ const CoCreateUser = {
16
23
  initSocket: function() {
17
24
  const self = this;
18
25
  crud.listen('createUser', function(data) {
19
- self.setDocumentId('users', data.document_id);
26
+ self.setDocumentId('users', data.document[0]._id);
20
27
  document.dispatchEvent(new CustomEvent('createUser', {
21
28
  detail: data
22
29
  }));
@@ -46,37 +53,31 @@ const CoCreateUser = {
46
53
 
47
54
  let request = {
48
55
  collection,
56
+ document: {
57
+ lastLogin: new Date().toISOString(),
58
+ current_org: crud.socket.config.organization_id
59
+ },
49
60
  filter: {
50
61
  query
51
62
  }
52
63
  }
53
64
 
54
- const socket = crud.socket.getSocket()
55
- if (!socket || !socket.connected || window && !window.navigator.onLine) {
56
-
57
- // ToDo: can use updateDocument with filter query
58
- crud.readDocuments(request).then((data) => {
59
- data.data = data.data[0]
60
- data.data['lastLogin'] = new Date().toISOString()
61
- data.data['current_org'] = data.organization_id
62
- crud.updateDocument(data).then((response) => {
63
- response['success'] = false
64
- response['status'] = "Login failed"
65
- if (response.data) {
66
- response['success'] = true
67
- response['status'] = "success"
68
- this.loginResponse(response)
69
- } else {
70
- this.loginResponse(response)
71
- }
72
- })
65
+ const socket = crud.socket.getSockets()
66
+ if (!socket[0] || !socket[0].connected || window && !window.navigator.onLine) {
67
+ crud.updateDocument(request).then((response) => {
68
+ response['success'] = false
69
+ response['status'] = "Login failed"
70
+ if (response.document) {
71
+ response['success'] = true
72
+ response['status'] = "success"
73
+ this.loginResponse(response)
74
+ } else {
75
+ this.loginResponse(response)
76
+ }
73
77
  })
74
78
  } else {
75
79
  // ToDo: can be depreciated if we have another means of token generation
76
- crud.send('login', {
77
- "collection": collection,
78
- "loginData": query
79
- });
80
+ crud.socket.send('login', request);
80
81
  }
81
82
  },
82
83
 
@@ -87,7 +88,7 @@ const CoCreateUser = {
87
88
  window.localStorage.setItem('organization_id', crud.socket.config.organization_id);
88
89
  window.localStorage.setItem("apiKey", crud.socket.config.apiKey);
89
90
  window.localStorage.setItem("host", crud.socket.config.host);
90
- window.localStorage.setItem('user_id', data.data['_id']);
91
+ window.localStorage.setItem('user_id', data.document[0]['_id']);
91
92
  window.localStorage.setItem("token", token);
92
93
  document.cookie = `token=${token};path=/`;
93
94
  message = "Succesful Login";
@@ -142,13 +143,12 @@ const CoCreateUser = {
142
143
  // ToDo: can get selected value from event/element, readDocument not required.
143
144
  crud.readDocument({
144
145
  collection: collection || 'users',
145
- document_id: user_id,
146
- data: {
146
+ document: {
147
147
  _id: user_id
148
148
  },
149
149
  }).then((data) => {
150
150
  window.localStorage.setItem('apiKey', data['apiKey']);
151
- window.localStorage.setItem('organization_id', data.data['current_org']);
151
+ window.localStorage.setItem('organization_id', data.document[0]['current_org']);
152
152
  window.localStorage.setItem('host', crud.socket.config.host);
153
153
 
154
154
  document.dispatchEvent(new CustomEvent('logIn'));
@@ -231,7 +231,7 @@ const CoCreateUser = {
231
231
  let statusEls = document.querySelectorAll(`[user-status][document_id='${data['user_id']}']`);
232
232
 
233
233
  statusEls.forEach((el) => {
234
- el.setAttribute('user-status', data['status']);
234
+ el.setAttribute('user-status', data['userStatus']);
235
235
  });
236
236
  },
237
237
 
@@ -262,7 +262,7 @@ const CoCreateUser = {
262
262
  else
263
263
  org_id = crud.socket.config.organization_id;
264
264
 
265
- let data = {data: {}};
265
+ let data = {document: {}};
266
266
  //. get form data
267
267
  elements.forEach(el => {
268
268
  let name = el.getAttribute('name');
@@ -272,20 +272,20 @@ const CoCreateUser = {
272
272
  if (el.getAttribute('data-type') == 'array') {
273
273
  value = [value];
274
274
  }
275
- data.data[name] = value;
275
+ data.document[name] = value;
276
276
  });
277
277
  data['collection'] = 'users'
278
- data.data['current_org'] = org_id;
279
- data.data['connected_orgs'] = [org_id];
278
+ data.document['current_org'] = org_id;
279
+ data.document['connected_orgs'] = [org_id];
280
280
 
281
- const socket = crud.socket.getSocket()
282
- if (!socket || !socket.connected || window && !window.navigator.onLine) {
281
+ const socket = crud.socket.getSockets()
282
+ if (!socket[0] || !socket[0].connected || window && !window.navigator.onLine) {
283
283
  // ToDo: can use updateDocument with filter query
284
284
  crud.createDocument(data).then((response) => {
285
285
  self.setDocumentId('users', response.document_id);
286
286
  data.database = org_id
287
287
  data.document_id = response.document_id
288
- data.data['_id'] = response.document_id
288
+ data.document['_id'] = response.document_id
289
289
  data.organization_id = org_id
290
290
  crud.createDocument(request).then((response) => {
291
291
 
@@ -297,9 +297,9 @@ const CoCreateUser = {
297
297
  })
298
298
  } else {
299
299
  // ToDo: creates user in platformdb
300
- crud.send('createUser', {
300
+ crud.socket.send('createUser', {
301
301
  collection: 'users',
302
- data: data,
302
+ ...data,
303
303
  orgDB: org_id
304
304
  });
305
305
  }
package/src/server.js CHANGED
@@ -1,9 +1,7 @@
1
- const {ObjectId} = require("mongodb");
2
-
3
1
  class CoCreateUser {
4
- constructor(wsManager, dbClient) {
2
+ constructor(wsManager, crud) {
5
3
  this.wsManager = wsManager
6
- this.dbClient = dbClient
4
+ this.crud = crud
7
5
  this.init()
8
6
  }
9
7
 
@@ -17,31 +15,32 @@ class CoCreateUser {
17
15
 
18
16
  async createUser(socket, data) {
19
17
  const self = this;
20
- if(!data.data) return;
18
+ if (!data.document) return;
21
19
 
22
- try{
23
- const collection = this.dbClient.db(data['organization_id']).collection(data['collection']);
20
+ try {
24
21
  // Create new user in config db users collection
25
- collection.insertOne(data.data, function(error, result) {
26
- if(!error && result){
22
+ this.crud.createDocument(data).then((data) => {
23
+ if (data.document[0] && data.document[0]._id) {
27
24
  const orgDB = data.orgDB;
28
- data.data['_id'] = result.insertedId;
25
+
29
26
  // if new orgDb Create new user in new org db users collection
30
27
  if (orgDB && orgDB != data.organization_id) {
31
- const anotherCollection = self.dbClient.db(orgDB).collection(data['collection']);
32
- anotherCollection.insertOne({...data.data, organization_id : orgDB});
28
+ let Data = {...data, organization_id: orgDB}
29
+ self.crud.createDocument(Data)
33
30
  }
34
- const response = { ...data, document_id: `${result.insertedId}`}
35
- self.wsManager.send(socket, 'createUser', response);
31
+
32
+ self.wsManager.send(socket, 'createUser', data);
36
33
 
37
34
  // add new user to platformDB
38
35
  if (data.organization_id != process.env.organization_id) {
39
- const platformDB = self.dbClient.db(process.env.organization_id).collection(data['collection']);
40
- platformDB.insertOne({...data.data, organization_id: process.env.organization_id});
36
+ let Data = {...data, organization_id: process.env.organization_id}
37
+ self.crud.createDocument(Data)
41
38
  }
42
39
  }
43
- });
44
- }catch(error){
40
+
41
+ // self.wsManager.broadcast(socket, 'updateUserStatus', data)
42
+ })
43
+ } catch(error) {
45
44
  console.log('createDocument error', error);
46
45
  }
47
46
  }
@@ -61,55 +60,43 @@ class CoCreateUser {
61
60
  async login(socket, data) {
62
61
  const self = this;
63
62
  try {
64
- const {organization_id} = data
65
- const selectedDB = organization_id;
66
- const collection = self.dbClient.db(selectedDB).collection(data["collection"]);
67
- const query = new Object();
68
-
69
- for (let item of data['loginData']) {
70
- query[item.name] = item.value;
71
- }
72
-
73
- collection.findOneAndUpdate(query, {$set: {lastLogin: new Date()}}, async function(error, result) {
63
+ this.crud.updateDocument(data).then(async (data) => {
74
64
  let response = {
65
+ ...data,
75
66
  success: false,
76
67
  message: "Login failed",
77
- status: "failed",
78
- uid: data['uid']
68
+ status: "failed"
79
69
  }
80
- if (!error && result && result.value) {
70
+
71
+ if (data.document[0] && data.document[0]._id) {
81
72
  let token = null;
82
73
  if (self.wsManager.authInstance) {
83
- token = await self.wsManager.authInstance.generateToken({user_id: `${result.value['_id']}`});
74
+ token = await self.wsManager.authInstance.generateToken({user_id: data.document[0]._id});
84
75
  }
85
76
 
86
77
  if (token && token != 'null')
87
78
  response = { ...response,
88
79
  success: true,
89
- collection: data["collection"],
90
- document_id: result.value['_id'],
91
- data: {
92
- _id: result.value['_id']
93
- },
94
- current_org: result.value['current_org'],
95
80
  message: "Login successful",
96
81
  status: "success",
82
+ userStatus: 'on',
97
83
  token
98
84
  };
99
85
 
100
- let user_id = response.document_id;
101
- const query = {
102
- "_id": new ObjectId(user_id),
103
- }
104
86
 
105
87
  if (data.organization_id != process.env.organization_id) {
106
- const platformDB = self.dbClient.db(process.env.organization_id).collection(data['collection']);
107
- platformDB.updateOne(query, {$set: {lastLogin: new Date()}})
108
- }
88
+ let Data = {organization_id: process.env.organization_id}
89
+ Data.document['_id'] = data.document[0]._id
90
+ Data.document['lastLogin'] = data.document[0].lastLogin
91
+ Data.document['organization_id'] = process.env.organization_id
92
+ crud.updateDocument(Data)
93
+ }
94
+
109
95
  }
110
96
  self.wsManager.send(socket, 'login', response)
111
- console.log(`${response.message} user_id: ${result.value['_id']}`)
112
- });
97
+ self.wsManager.broadcast(socket, 'updateUserStatus', data)
98
+ })
99
+
113
100
  } catch (error) {
114
101
  console.log('login failed', error);
115
102
  }
@@ -121,22 +108,17 @@ class CoCreateUser {
121
108
  */
122
109
  async userStatus(socket, data) {
123
110
  const self = this;
124
-
125
111
  try {
126
- const {organization_id, db} = data
127
- const selectedDB = db || organization_id;
128
- const collection = self.dbClient.db(selectedDB).collection('users');
129
- // const user_id = items[2];
130
- const query = {
131
- "_id": new ObjectId(data.user_id),
112
+ data.collection = 'users'
113
+ data['document'] = {
114
+ _id: data.user_id,
115
+ userStatus: data.userStatus
132
116
  }
133
- collection.updateOne(query, {$set: {status: data.status}}, function(err, res) {
134
- if (!err) {
135
- self.wsManager.broadcast(socket, 'updateUserStatus', data)
136
- } else {
137
- console.log('err', err)
138
- }
117
+
118
+ this.crud.updateDocument(data).then((data) => {
119
+ self.wsManager.broadcast(socket, 'updateUserStatus', data)
139
120
  })
121
+
140
122
  } catch (error) {
141
123
  console.log('userStatus error')
142
124
  }