@cocreate/organizations 1.13.4 → 1.14.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,23 @@
1
+ ## [1.14.1](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.14.0...v1.14.1) (2023-05-20)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * update packages to latest version. This commit updates various packages in the dependencies section of the package.json file to their latest published versions, thereby fixing multiple bugs and improving overall performance. ([0350a07](https://github.com/CoCreate-app/CoCreate-organizations/commit/0350a07f26027496324961c70da3350f42315b5a))
7
+
8
+ # [1.14.0](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.13.4...v1.14.0) (2023-05-18)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * websocket event listener for organizational creation ([bdbec1f](https://github.com/CoCreate-app/CoCreate-organizations/commit/bdbec1f4cf3e536607765561b4a74159d1d1c353))
14
+ * createOrganization method to correctly save data to the serverDB ([c0f574f](https://github.com/CoCreate-app/CoCreate-organizations/commit/c0f574f662c777e1b663cc35bec5a0de8438a33f))
15
+
16
+
17
+ ### Features
18
+
19
+ * Create or add an exisitng organization using createOrganization() function. ([311efee](https://github.com/CoCreate-app/CoCreate-organizations/commit/311efee025bcdb4323466e38f211f746faf252bb))
20
+
1
21
  ## [1.13.4](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.13.3...v1.13.4) (2023-05-12)
2
22
 
3
23
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/organizations",
3
- "version": "1.13.4",
3
+ "version": "1.14.1",
4
4
  "description": "A simple organizations component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "organizations",
@@ -59,10 +59,10 @@
59
59
  "webpack-log": "^3.0.1"
60
60
  },
61
61
  "dependencies": {
62
- "@cocreate/actions": "^1.8.8",
63
- "@cocreate/crud-client": "^1.20.4",
64
- "@cocreate/docs": "^1.7.8",
65
- "@cocreate/form": "^1.14.6",
66
- "@cocreate/indexeddb": "^1.8.6"
62
+ "@cocreate/actions": "^1.8.11",
63
+ "@cocreate/crud-client": "^1.21.2",
64
+ "@cocreate/docs": "^1.7.11",
65
+ "@cocreate/form": "^1.14.11",
66
+ "@cocreate/indexeddb": "^1.9.3"
67
67
  }
68
68
  }
package/src/client.js CHANGED
@@ -4,51 +4,59 @@ import action from '@cocreate/actions';
4
4
  import form from '@cocreate/form';
5
5
 
6
6
  const CoCreateOrganization = {
7
-
8
- createOrg: async function(btn) {
9
- let formEl = btn.closest("form");
10
- if (!formEl) return;
11
-
12
- let organization = form.getData(formEl, 'organizations')
13
- let user = form.getData(formEl, 'users')
14
-
15
- let response
16
- let documents = await indexeddb.generateDB(organization, user)
17
-
18
- if (documents) {
19
- if(!organization || !organization.document || !organization.document[0]) return
20
- let org = organization.document[0]
21
- let organization_id = org._id
22
- let key = org.key
23
-
24
- if (crud.socket.organization !== true) {
25
- crud.socket.organization = true
26
- crud.socket.create({organization_id, key})
27
- }
28
-
29
- form.setDocumentId(formEl, organization)
30
- form.setDocumentId(formEl, user)
31
-
32
- response = await crud.socket.send('createOrg', {
33
- documents,
34
- broadcastBrowser: false,
35
- organization_id,
36
- key
37
- });
38
- }
39
-
40
- document.dispatchEvent(new CustomEvent('createdOrg', {
41
- detail: response
42
- }));
43
- }
7
+
8
+ createOrganization: async function (btn) {
9
+ let formEl = btn.closest("form");
10
+ if (!formEl) return;
11
+
12
+ let organization = form.getData(formEl, 'organizations')
13
+ let user = form.getData(formEl, 'users')
14
+
15
+ if (!organization || !organization.document)
16
+ return
17
+ if (!user || !user.document)
18
+ return
19
+
20
+ if (!organization.document._id && !user.document._id) {
21
+ let documents = await indexeddb.generateDB(organization, user)
22
+ if (!documents)
23
+ return
24
+ }
25
+
26
+ form.setDocumentId(formEl, organization)
27
+ form.setDocumentId(formEl, user)
28
+
29
+ organization = organization.document[0]
30
+ user = user.document[0]
31
+
32
+ let organization_id = organization._id
33
+ let key = organization.key
34
+
35
+ if (crud.socket.organization !== true) {
36
+ crud.socket.organization = true
37
+ crud.socket.create({ organization_id, key })
38
+ }
39
+
40
+ let response = await crud.socket.send('createOrganization', {
41
+ organization,
42
+ user,
43
+ broadcastBrowser: false,
44
+ organization_id,
45
+ key
46
+ });
47
+
48
+ document.dispatchEvent(new CustomEvent('createdOrganization', {
49
+ detail: response
50
+ }));
51
+ }
44
52
  };
45
53
 
46
54
  action.init({
47
- name: "createOrg",
48
- endEvent: "createdOrg",
49
- callback: (btn, data) => {
50
- CoCreateOrganization.createOrg(btn);
51
- },
55
+ name: "createOrganization",
56
+ endEvent: "createdOrganization",
57
+ callback: (btn) => {
58
+ CoCreateOrganization.createOrganization(btn);
59
+ }
52
60
  });
53
61
 
54
62
  export default CoCreateOrganization;
package/src/server.js CHANGED
@@ -1,38 +1,63 @@
1
1
  class CoCreateOrganization {
2
- constructor(crud) {
3
- this.wsManager = crud.wsManager
4
- this.crud = crud
5
- this.init()
6
- }
7
-
8
- init() {
9
- if (this.wsManager) {
10
- this.wsManager.on('createOrg', (socket, data) =>
11
- this.createOrg(socket, data)
12
- );
13
- }
14
- }
15
-
16
- async createOrg(socket, data) {
17
- try {
18
- const platformSocket = {
19
- config: {
20
- organization_id: process.env.organization_id,
21
- }
22
- }
23
-
24
- for(let document of data.documents) {
25
- document.database = process.env.organization_id
26
- document.organization_id = process.env.organization_id
27
- let response = await this.crud.createDocument(document)
28
- this.wsManager.broadcast(platformSocket, 'createDocument', response);
29
- }
30
- this.wsManager.send(socket, 'createOrg', data);
31
- } catch(error) {
32
- console.log('createDocument error', error);
33
- }
34
- }
35
-
2
+ constructor(crud) {
3
+ this.wsManager = crud.wsManager
4
+ this.crud = crud
5
+ this.platformSocket = {
6
+ config: {
7
+ organization_id: process.env.organization_id,
8
+ }
9
+ }
10
+ this.init()
11
+ }
12
+
13
+ init() {
14
+ if (this.wsManager) {
15
+ this.wsManager.on('createOrganization', (socket, data) =>
16
+ this.createOrganization(socket, data)
17
+ );
18
+ }
19
+ }
20
+
21
+ async createOrganization(socket, data) {
22
+ try {
23
+ if (!data.organization || !data.organization._id) return
24
+ if (!data.user || !data.user._id || !data.user.email || !data.user.password) return
25
+
26
+ const organization = {
27
+ _id: data.organization._id,
28
+ name: data.organization.name || 'untitled',
29
+ hosts: data.organization.hosts || [],
30
+ owner: data.user._id
31
+ }
32
+
33
+ // TODO: check if user exist and check if credentials match
34
+ const user = {
35
+ _id: data.user._id,
36
+ name: data.user.name || 'Admin',
37
+ email: data.user.email,
38
+ password: data.user.password,
39
+ currentOrg: data.organization._id,
40
+ connectedOrgs: [data.organization._id]
41
+ }
42
+
43
+ const Data = {}
44
+ Data.database = process.env.organization_id
45
+ Data.organization_id = process.env.organization_id
46
+
47
+ if (organization) {
48
+ const response = await this.crud.createDocument({ ...Data, collection: 'organizations', document: organization })
49
+ this.wsManager.broadcast(this.platformSocket, 'createDocument', response);
50
+ }
51
+ if (user) {
52
+ const response = await this.crud.createDocument({ ...Data, collection: 'users', document: user })
53
+ this.wsManager.broadcast(this.platformSocket, 'createDocument', response);
54
+ }
55
+
56
+ this.wsManager.send(socket, 'createOrganization', data);
57
+ } catch (error) {
58
+ console.log('createDocument error', error);
59
+ }
60
+ }
36
61
  }
37
62
 
38
63
  module.exports = CoCreateOrganization;