@cocreate/organizations 1.13.3 → 1.14.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/CHANGELOG.md CHANGED
@@ -1,3 +1,23 @@
1
+ # [1.14.0](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.13.4...v1.14.0) (2023-05-18)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * websocket event listener for organizational creation ([bdbec1f](https://github.com/CoCreate-app/CoCreate-organizations/commit/bdbec1f4cf3e536607765561b4a74159d1d1c353))
7
+ * createOrganization method to correctly save data to the serverDB ([c0f574f](https://github.com/CoCreate-app/CoCreate-organizations/commit/c0f574f662c777e1b663cc35bec5a0de8438a33f))
8
+
9
+
10
+ ### Features
11
+
12
+ * Create or add an exisitng organization using createOrganization() function. ([311efee](https://github.com/CoCreate-app/CoCreate-organizations/commit/311efee025bcdb4323466e38f211f746faf252bb))
13
+
14
+ ## [1.13.4](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.13.3...v1.13.4) (2023-05-12)
15
+
16
+
17
+ ### Bug Fixes
18
+
19
+ * socket.status renamed to socket.organization ([6eb9919](https://github.com/CoCreate-app/CoCreate-organizations/commit/6eb9919f1b41da5dc1891f3801c6dcf93301cdb5))
20
+
1
21
  ## [1.13.3](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.13.2...v1.13.3) (2023-05-11)
2
22
 
3
23
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/organizations",
3
- "version": "1.13.3",
3
+ "version": "1.14.0",
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",
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.status) {
25
- crud.socket.status = 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,62 @@
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
+ const user = {
34
+ _id: data.user._id,
35
+ name: data.user.name || 'Admin',
36
+ email: data.user.email,
37
+ password: data.user.password,
38
+ currentOrg: data.organization._id,
39
+ connectedOrgs: [data.organization._id]
40
+ }
41
+
42
+ const Data = {}
43
+ Data.database = process.env.organization_id
44
+ Data.organization_id = process.env.organization_id
45
+
46
+ if (organization) {
47
+ const response = await this.crud.createDocument({ ...Data, collection: 'organizations', document: organization })
48
+ this.wsManager.broadcast(this.platformSocket, 'createDocument', response);
49
+ }
50
+ if (user) {
51
+ const response = await this.crud.createDocument({ ...Data, collection: 'users', document: user })
52
+ this.wsManager.broadcast(this.platformSocket, 'createDocument', response);
53
+ }
54
+
55
+ this.wsManager.send(socket, 'createOrganization', data);
56
+ } catch (error) {
57
+ console.log('createDocument error', error);
58
+ }
59
+ }
36
60
  }
37
61
 
38
62
  module.exports = CoCreateOrganization;