@cocreate/organizations 1.21.5 → 1.22.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,18 @@
1
+ # [1.22.0](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.21.6...v1.22.0) (2023-10-16)
2
+
3
+
4
+ ### Features
5
+
6
+ * get() retuns promise to limit request to one ([67d59b3](https://github.com/CoCreate-app/CoCreate-organizations/commit/67d59b38bbaa6e80727a1f2327e0ef60896565d2))
7
+
8
+ ## [1.21.6](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.21.5...v1.21.6) (2023-10-14)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * typo ([c88342b](https://github.com/CoCreate-app/CoCreate-organizations/commit/c88342b5128fc472dbe0a39dc3d7ea98141c8236))
14
+ * database.database.name ([88fde84](https://github.com/CoCreate-app/CoCreate-organizations/commit/88fde8463c1d6d5160528065b951da0c38664d2b))
15
+
1
16
  ## [1.21.5](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.21.4...v1.21.5) (2023-10-09)
2
17
 
3
18
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/organizations",
3
- "version": "1.21.5",
3
+ "version": "1.22.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
@@ -2,16 +2,14 @@ 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
+ import Indexeddb from '@cocreate/indexeddb';
6
+ import uuid from '@cocreate/uuid';
5
7
 
6
8
  async function generateDB(organization = { object: {} }, user = { object: {} }) {
7
9
  const organization_id = organization.object._id || Crud.ObjectId();
8
10
  const apikey = organization.object.key || uuid.generate();
9
11
  const user_id = user.object._id || Crud.ObjectId();
10
12
 
11
- let hasApiKey = await Crud.send({ method: 'read.object', database: organization_id, array: 'keys', organization_id })
12
- if (hasApiKey && hasApiKey.object && hasApiKey.object[0])
13
- return
14
-
15
13
  try {
16
14
  // Create organization
17
15
  organization.method = 'create.object'
@@ -21,7 +19,7 @@ async function generateDB(organization = { object: {} }, user = { object: {} })
21
19
  organization.object._id = organization_id
22
20
  organization.object.name = organization.object.name || 'untitiled'
23
21
  organization.organization_id = organization_id
24
- Crud.send(organization);
22
+ Indexeddb.send(organization);
25
23
 
26
24
  // Create user
27
25
  user.method = 'create.object'
@@ -32,7 +30,7 @@ async function generateDB(organization = { object: {} }, user = { object: {} })
32
30
  user.object.firstname = user.object.firstname || 'untitiled'
33
31
  user.object.lastname = user.object.lastname || 'untitiled'
34
32
  user.organization_id = organization_id
35
- Crud.send(user);
33
+ Indexeddb.send(user);
36
34
 
37
35
  // Create default key
38
36
  let key = {
@@ -52,7 +50,7 @@ async function generateDB(organization = { object: {} }, user = { object: {} })
52
50
  },
53
51
  organization_id
54
52
  }
55
- Crud.send(key);
53
+ Indexeddb.send(key);
56
54
 
57
55
  // Create role
58
56
  let role_id = Crud.ObjectId();
@@ -69,7 +67,7 @@ async function generateDB(organization = { object: {} }, user = { object: {} })
69
67
  },
70
68
  organization_id
71
69
  };
72
- Crud.send(role);
70
+ Indexeddb.send(role);
73
71
 
74
72
  // Create user key
75
73
  let userKey = {
@@ -88,7 +86,7 @@ async function generateDB(organization = { object: {} }, user = { object: {} })
88
86
  },
89
87
  organization_id
90
88
  };
91
- Crud.send(userKey);
89
+ Indexeddb.send(userKey);
92
90
 
93
91
  return { organization: organization.object, apikey, user: user.object, role: role.object, userKey: userKey.object }
94
92
 
@@ -100,9 +98,9 @@ async function generateDB(organization = { object: {} }, user = { object: {} })
100
98
  async function get() {
101
99
  let organization_id = await getOrganizationFromServiceWorker()
102
100
  if (!organization_id) {
103
- let data = await indexeddb.send({ method: 'read.database' })
101
+ let data = await Indexeddb.send({ method: 'read.database' })
104
102
  for (let database of data.database) {
105
- let name = database.database.name
103
+ let name = database.name
106
104
  if (name.match(/^[0-9a-fA-F]{24}$/)) {
107
105
  organization_id = name
108
106
  }
@@ -142,37 +140,44 @@ async function getOrganizationFromServiceWorker() {
142
140
  });
143
141
  }
144
142
 
145
- async function createOrganization() {
143
+ let organizationPromise = null;
144
+ async function createOrganizationPromise() {
146
145
  let createOrganization = document.querySelector('[actions*="createOrganization"]')
146
+ if (createOrganization)
147
+ return Crud.socket.organization = 'canceled'
147
148
 
148
149
  if (Crud.socket.organization == 'canceled' || Crud.socket.organization == 'pending') return
149
150
 
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);
151
+ const organization_id = prompt("An organization_id could not be found, if you already have an organization_id input it now.\n\nOr leave blank and click 'OK' to create a new organization");
152
+
153
+ if (organization_id === null)
154
+ return Crud.socket.organization = 'canceled'
155
+
156
+ Crud.socket.organization = 'pending'
157
+ if (Indexeddb) {
158
+ try {
159
+ let org = { object: {} }
160
+ if (organization_id)
161
+ org.object._id = organization_id
162
+ let { organization, apikey, user } = await generateDB(org)
163
+ if (organization && apikey && user) {
164
+ Crud.socket.apikey = apikey
165
+ Crud.socket.user_id = user._id
166
+ Config.set('organization_id', organization._id)
167
+ Config.set('apikey', apikey)
168
+ Config.set('user_id', user._id)
169
+ Crud.socket.organization = true
170
+ return organization._id
169
171
  }
172
+ } catch (error) {
173
+ console.error('Failed to load the script:', error);
170
174
  }
171
- } else {
172
- Crud.socket.organization = 'canceled'
173
175
  }
174
176
  }
175
177
 
178
+ async function createOrganization() {
179
+ return organizationPromise || (organizationPromise = createOrganizationPromise());
180
+ }
176
181
 
177
182
  async function create(btn) {
178
183
  let formEl = btn.closest("form");
@@ -222,7 +227,7 @@ Action.init({
222
227
  name: "createOrganization",
223
228
  endEvent: "createdOrganization",
224
229
  callback: (action) => {
225
- CoCreateOrganization.create(action.element);
230
+ create(action.element);
226
231
  }
227
232
  });
228
233