@cocreate/organizations 1.21.0 → 1.21.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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [1.21.2](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.21.1...v1.21.2) (2023-10-09)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * bump cocreate dependency versions ([3a4cbf6](https://github.com/CoCreate-app/CoCreate-organizations/commit/3a4cbf6ca1cfeec0565cb8d9267e6c2464e55bfe))
7
+
8
+ ## [1.21.1](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.21.0...v1.21.1) (2023-10-09)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * update wsManager.broadcast to wsManager.send() ([f955cbf](https://github.com/CoCreate-app/CoCreate-organizations/commit/f955cbf1c56834e48e926aa79fc04308fd24efa4))
14
+
1
15
  # [1.21.0](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.20.0...v1.21.0) (2023-09-19)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/organizations",
3
- "version": "1.21.0",
3
+ "version": "1.21.2",
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,7 +59,8 @@
59
59
  },
60
60
  "dependencies": {
61
61
  "@cocreate/actions": "^1.11.2",
62
- "@cocreate/crud-client": "^1.26.0",
63
- "@cocreate/elements": "^1.21.1"
62
+ "@cocreate/config": "^1.6.0",
63
+ "@cocreate/crud-client": "^1.28.0",
64
+ "@cocreate/elements": "^1.23.0"
64
65
  }
65
66
  }
package/src/client.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import Crud from '@cocreate/crud-client';
2
2
  import Action from '@cocreate/actions';
3
3
  import Elements from '@cocreate/elements';
4
-
4
+ import Config from '@cocreate/config';
5
5
 
6
6
  async function generateDB(organization = { object: {} }, user = { object: {} }) {
7
7
  const organization_id = organization.object._id || Crud.ObjectId();
@@ -97,6 +97,83 @@ async function generateDB(organization = { object: {} }, user = { object: {} })
97
97
  }
98
98
  }
99
99
 
100
+ async function get() {
101
+ let organization_id = await getOrganizationFromServiceWorker()
102
+ if (!organization_id) {
103
+ let data = await indexeddb.send({ method: 'read.database' })
104
+ for (let database of data.database) {
105
+ let name = database.database.name
106
+ if (name.match(/^[0-9a-fA-F]{24}$/)) {
107
+ organization_id = name
108
+ }
109
+ }
110
+ }
111
+ if (!organization_id)
112
+ organization_id = await createOrganization()
113
+
114
+ if (organization_id)
115
+ Config.set('organization_id', organization_id)
116
+
117
+ return organization_id
118
+
119
+ }
120
+
121
+ async function getOrganizationFromServiceWorker() {
122
+ return new Promise((resolve, reject) => {
123
+ if (!navigator.serviceWorker)
124
+ return resolve()
125
+
126
+ const handleMessage = (event) => {
127
+ if (event.data.action === 'getOrganization') {
128
+ navigator.serviceWorker.removeEventListener('message', handleMessage); // Remove the event listener
129
+ resolve(event.data.organization_id);
130
+ }
131
+ };
132
+
133
+ navigator.serviceWorker.addEventListener('message', handleMessage);
134
+
135
+ // Send message to Service Worker
136
+ const msg = new MessageChannel();
137
+ navigator.serviceWorker.ready
138
+ .then(() => {
139
+ navigator.serviceWorker.controller.postMessage('getOrganization', [msg.port1]);
140
+ })
141
+ .catch(reject);
142
+ });
143
+ }
144
+
145
+ async function createOrganization() {
146
+ let createOrganization = document.querySelector('[actions*="createOrganization"]')
147
+
148
+ if (Crud.socket.organization == 'canceled' || Crud.socket.organization == 'pending') return
149
+
150
+ if (!createOrganization && confirm("An organization_id could not be found, if you already have an organization_id add it to this html and refresh the page.\n\nOr click 'OK' create a new organization") == true) {
151
+ Crud.socket.organization = 'pending'
152
+ if (indexeddb) {
153
+ try {
154
+ // const Organization = await import('@cocreate/organizations')
155
+
156
+ let org = { object: {} }
157
+ let { organization, apikey, user } = await generateDB(org)
158
+ if (organization && apikey && user) {
159
+ Crud.socket.apikey = apikey
160
+ Crud.socket.user_id = user._id
161
+ Config.set('organization_id', organization._id)
162
+ Config.set('apikey', apikey)
163
+ Config.set('user_id', user._id)
164
+ Crud.socket.organization = true
165
+ return organization._id
166
+ }
167
+ } catch (error) {
168
+ console.error('Failed to load the script:', error);
169
+ }
170
+ }
171
+ } else {
172
+ Crud.socket.organization = 'canceled'
173
+ }
174
+ }
175
+
176
+
100
177
  async function create(btn) {
101
178
  let formEl = btn.closest("form");
102
179
  if (!formEl) return;
@@ -149,4 +226,4 @@ Action.init({
149
226
  }
150
227
  });
151
228
 
152
- export default { generateDB, create };
229
+ export default { generateDB, create, get };
package/src/server.js CHANGED
@@ -57,16 +57,16 @@ class CoCreateOrganization {
57
57
 
58
58
  if (organization) {
59
59
  const response = await this.crud.send({ ...Data, method: 'create.object', array: 'organizations', object: organization })
60
- this.wsManager.broadcast(this.platformSocket, response);
60
+ this.wsManager.send(this.platformSocket, response);
61
61
  }
62
62
  if (user) {
63
63
  const response = await this.crud.send({ ...Data, method: 'create.object', array: 'users', object: user })
64
- this.wsManager.broadcast(this.platformSocket, response);
64
+ this.wsManager.send(this.platformSocket, response);
65
65
  }
66
66
 
67
67
  if (userKey) {
68
68
  const response = await this.crud.send({ ...Data, method: 'create.object', array: 'keys', object: userKey })
69
- this.wsManager.broadcast(this.platformSocket, response);
69
+ this.wsManager.send(this.platformSocket, response);
70
70
  }
71
71
 
72
72
  this.wsManager.send(data);