@cocreate/organizations 1.28.1 → 1.28.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 +7 -0
- package/package.json +1 -1
- package/src/client.js +250 -224
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
## [1.28.2](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.28.1...v1.28.2) (2025-04-11)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* dispatch end event on action element rather than document ([dd5ea2a](https://github.com/CoCreate-app/CoCreate-organizations/commit/dd5ea2a43c00418448189369c39f72f83f0206b6))
|
|
7
|
+
|
|
1
8
|
## [1.28.1](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.28.0...v1.28.1) (2024-12-22)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -1,253 +1,279 @@
|
|
|
1
|
-
import Crud from
|
|
2
|
-
import Action from
|
|
3
|
-
import Elements from
|
|
4
|
-
import Config from
|
|
5
|
-
import Indexeddb from
|
|
6
|
-
import uuid from
|
|
7
|
-
|
|
8
|
-
async function generateDB(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
1
|
+
import Crud from "@cocreate/crud-client";
|
|
2
|
+
import Action from "@cocreate/actions";
|
|
3
|
+
import Elements from "@cocreate/elements";
|
|
4
|
+
import Config from "@cocreate/config";
|
|
5
|
+
import Indexeddb from "@cocreate/indexeddb";
|
|
6
|
+
import uuid from "@cocreate/uuid";
|
|
7
|
+
|
|
8
|
+
async function generateDB(
|
|
9
|
+
organization = { object: {} },
|
|
10
|
+
user = { object: {} }
|
|
11
|
+
) {
|
|
12
|
+
const organization_id =
|
|
13
|
+
organization.object._id || Crud.ObjectId().toString();
|
|
14
|
+
const apikey = organization.object.key || uuid.generate();
|
|
15
|
+
const user_id = user.object._id || Crud.ObjectId().toString();
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
// Create organization
|
|
19
|
+
organization.method = "object.create";
|
|
20
|
+
organization.storage = "indexeddb";
|
|
21
|
+
organization.database = organization_id;
|
|
22
|
+
organization.array = "organizations";
|
|
23
|
+
organization.object._id = organization_id;
|
|
24
|
+
organization.object.name = organization.object.name || "untitiled";
|
|
25
|
+
organization.organization_id = organization_id;
|
|
26
|
+
Indexeddb.send(organization);
|
|
27
|
+
|
|
28
|
+
// Create user
|
|
29
|
+
user.method = "object.create";
|
|
30
|
+
user.storage = "indexeddb";
|
|
31
|
+
user.database = organization_id;
|
|
32
|
+
user.array = "users";
|
|
33
|
+
user.object._id = user_id;
|
|
34
|
+
user.object.firstname = user.object.firstname || "untitiled";
|
|
35
|
+
user.object.lastname = user.object.lastname || "untitiled";
|
|
36
|
+
user.organization_id = organization_id;
|
|
37
|
+
Indexeddb.send(user);
|
|
38
|
+
|
|
39
|
+
// Create default key
|
|
40
|
+
let key = {
|
|
41
|
+
method: "object.create",
|
|
42
|
+
storage: "indexeddb",
|
|
43
|
+
database: organization_id,
|
|
44
|
+
array: "keys",
|
|
45
|
+
object: {
|
|
46
|
+
_id: Crud.ObjectId().toString(),
|
|
47
|
+
type: "key",
|
|
48
|
+
key: apikey,
|
|
49
|
+
actions: {
|
|
50
|
+
signIn: true,
|
|
51
|
+
signUp: true
|
|
52
|
+
},
|
|
53
|
+
default: true
|
|
54
|
+
},
|
|
55
|
+
organization_id
|
|
56
|
+
};
|
|
57
|
+
Indexeddb.send(key);
|
|
58
|
+
|
|
59
|
+
// Create role
|
|
60
|
+
let role_id = Crud.ObjectId().toString();
|
|
61
|
+
let role = {
|
|
62
|
+
method: "object.create",
|
|
63
|
+
storage: "indexeddb",
|
|
64
|
+
database: organization_id,
|
|
65
|
+
array: "keys",
|
|
66
|
+
object: {
|
|
67
|
+
_id: role_id,
|
|
68
|
+
type: "role",
|
|
69
|
+
name: "admin",
|
|
70
|
+
admin: "true"
|
|
71
|
+
},
|
|
72
|
+
organization_id
|
|
73
|
+
};
|
|
74
|
+
Indexeddb.send(role);
|
|
75
|
+
|
|
76
|
+
// Create user key
|
|
77
|
+
let userKey = {
|
|
78
|
+
method: "object.create",
|
|
79
|
+
storage: "indexeddb",
|
|
80
|
+
database: organization_id,
|
|
81
|
+
array: "keys",
|
|
82
|
+
object: {
|
|
83
|
+
_id: Crud.ObjectId().toString(),
|
|
84
|
+
type: "user",
|
|
85
|
+
key: user_id,
|
|
86
|
+
array: "users", // could be any array
|
|
87
|
+
roles: [role_id],
|
|
88
|
+
email: user.object.email,
|
|
89
|
+
password: user.object.password || btoa("0000")
|
|
90
|
+
},
|
|
91
|
+
organization_id
|
|
92
|
+
};
|
|
93
|
+
Indexeddb.send(userKey);
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
organization: organization.object,
|
|
97
|
+
apikey,
|
|
98
|
+
user: user.object,
|
|
99
|
+
role: role.object,
|
|
100
|
+
userKey: userKey.object
|
|
101
|
+
};
|
|
102
|
+
} catch (error) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
96
105
|
}
|
|
97
106
|
|
|
98
107
|
async function get() {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
return organization_id
|
|
122
|
-
|
|
108
|
+
let organization_id = await getOrganizationFromServiceWorker();
|
|
109
|
+
if (!organization_id) {
|
|
110
|
+
let data = await Indexeddb.send({ method: "database.read" });
|
|
111
|
+
for (let database of data.database) {
|
|
112
|
+
let name = database.name;
|
|
113
|
+
if (name.match(/^[0-9a-fA-F]{24}$/)) {
|
|
114
|
+
organization_id = name;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (!organization_id) {
|
|
120
|
+
let file = await fetch("/");
|
|
121
|
+
organization_id = file.headers.get("organization");
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (!organization_id) organization_id = await createOrganization();
|
|
125
|
+
|
|
126
|
+
if (organization_id) Config.set("organization_id", organization_id);
|
|
127
|
+
|
|
128
|
+
return organization_id;
|
|
123
129
|
}
|
|
124
130
|
|
|
125
131
|
async function getOrganizationFromServiceWorker() {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
132
|
+
return new Promise((resolve, reject) => {
|
|
133
|
+
if (!navigator.serviceWorker) return resolve();
|
|
134
|
+
|
|
135
|
+
const handleMessage = (event) => {
|
|
136
|
+
if (event.data.action === "getOrganization") {
|
|
137
|
+
navigator.serviceWorker.removeEventListener(
|
|
138
|
+
"message",
|
|
139
|
+
handleMessage
|
|
140
|
+
); // Remove the event listener
|
|
141
|
+
resolve(event.data.organization_id);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
navigator.serviceWorker.addEventListener("message", handleMessage);
|
|
146
|
+
|
|
147
|
+
// Send message to Service Worker
|
|
148
|
+
const msg = new MessageChannel();
|
|
149
|
+
|
|
150
|
+
navigator.serviceWorker.ready
|
|
151
|
+
.then((registration) => {
|
|
152
|
+
if (navigator.serviceWorker.controller) {
|
|
153
|
+
// If there's an active controller, send the message
|
|
154
|
+
navigator.serviceWorker.controller.postMessage(
|
|
155
|
+
{ action: "getOrganization" },
|
|
156
|
+
[msg.port1]
|
|
157
|
+
);
|
|
158
|
+
} else {
|
|
159
|
+
// Listen for a new service worker to start controlling the page
|
|
160
|
+
navigator.serviceWorker.addEventListener(
|
|
161
|
+
"controllerchange",
|
|
162
|
+
() => {
|
|
163
|
+
if (navigator.serviceWorker.controller) {
|
|
164
|
+
navigator.serviceWorker.controller.postMessage(
|
|
165
|
+
{ action: "getOrganization" },
|
|
166
|
+
[msg.port1]
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
})
|
|
173
|
+
.catch((error) => {
|
|
174
|
+
console.error(reject);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
158
177
|
}
|
|
159
178
|
|
|
160
179
|
let organizationPromise = null;
|
|
161
180
|
async function createOrganizationPromise() {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
181
|
+
let createOrganization = document.querySelector(
|
|
182
|
+
'[actions*="createOrganization"]'
|
|
183
|
+
);
|
|
184
|
+
if (createOrganization) return (Crud.socket.organization = "canceled");
|
|
185
|
+
|
|
186
|
+
if (
|
|
187
|
+
Crud.socket.organization == "canceled" ||
|
|
188
|
+
Crud.socket.organization == "pending"
|
|
189
|
+
)
|
|
190
|
+
return;
|
|
191
|
+
|
|
192
|
+
const organization_id = prompt(
|
|
193
|
+
"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"
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
if (organization_id) return organization_id;
|
|
197
|
+
if (organization_id === null)
|
|
198
|
+
return (Crud.socket.organization = "canceled");
|
|
199
|
+
|
|
200
|
+
Crud.socket.organization = "pending";
|
|
201
|
+
if (Indexeddb) {
|
|
202
|
+
try {
|
|
203
|
+
let org = { object: {} };
|
|
204
|
+
if (organization_id) org.object._id = organization_id;
|
|
205
|
+
let { organization, apikey, user } = await generateDB(org);
|
|
206
|
+
if (organization && apikey && user) {
|
|
207
|
+
Crud.socket.apikey = apikey;
|
|
208
|
+
Crud.socket.user_id = user._id;
|
|
209
|
+
Config.set("organization_id", organization._id);
|
|
210
|
+
Config.set("apikey", apikey);
|
|
211
|
+
Config.set("user_id", user._id);
|
|
212
|
+
Crud.socket.organization = true;
|
|
213
|
+
return organization._id;
|
|
214
|
+
}
|
|
215
|
+
} catch (error) {
|
|
216
|
+
console.error("Failed to load the script:", error);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
195
219
|
}
|
|
196
220
|
|
|
197
|
-
|
|
198
221
|
async function createOrganization() {
|
|
199
|
-
|
|
222
|
+
return (
|
|
223
|
+
organizationPromise ||
|
|
224
|
+
(organizationPromise = createOrganizationPromise())
|
|
225
|
+
);
|
|
200
226
|
}
|
|
201
227
|
|
|
202
|
-
async function create(
|
|
203
|
-
|
|
228
|
+
async function create(action) {
|
|
229
|
+
let form = action.form;
|
|
230
|
+
if (!form) return;
|
|
204
231
|
|
|
205
|
-
|
|
206
|
-
|
|
232
|
+
let organization = Elements.getData(form, "organizations");
|
|
233
|
+
let user = Elements.getData(form, "users");
|
|
207
234
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
if (!user || !user.object)
|
|
211
|
-
return
|
|
235
|
+
if (!organization || !organization.object) return;
|
|
236
|
+
if (!user || !user.object) return;
|
|
212
237
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
}
|
|
238
|
+
if (!organization.object._id && !user.object._id) {
|
|
239
|
+
let objects = await generateDB(organization, user);
|
|
240
|
+
if (!objects) return;
|
|
241
|
+
}
|
|
218
242
|
|
|
219
|
-
|
|
220
|
-
|
|
243
|
+
Elements.setTypeValue(form, organization);
|
|
244
|
+
Elements.setTypeValue(form, user);
|
|
221
245
|
|
|
222
|
-
|
|
223
|
-
|
|
246
|
+
organization = organization.object[0];
|
|
247
|
+
user = user.object[0];
|
|
224
248
|
|
|
225
|
-
|
|
249
|
+
let organization_id = organization._id;
|
|
226
250
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
251
|
+
if (Crud.socket.organization !== true) {
|
|
252
|
+
Crud.socket.organization = true;
|
|
253
|
+
Crud.socket.create({ organization_id });
|
|
254
|
+
}
|
|
231
255
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
256
|
+
let response = await Crud.socket.send({
|
|
257
|
+
method: "createOrganization",
|
|
258
|
+
organization,
|
|
259
|
+
user,
|
|
260
|
+
broadcastBrowser: false,
|
|
261
|
+
organization_id
|
|
262
|
+
});
|
|
239
263
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
264
|
+
action.element.dispatchEvent(
|
|
265
|
+
new CustomEvent("createdOrganization", {
|
|
266
|
+
detail: response
|
|
267
|
+
})
|
|
268
|
+
);
|
|
243
269
|
}
|
|
244
270
|
|
|
245
271
|
Action.init({
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
272
|
+
name: "createOrganization",
|
|
273
|
+
endEvent: "createdOrganization",
|
|
274
|
+
callback: (action) => {
|
|
275
|
+
create(action);
|
|
276
|
+
}
|
|
251
277
|
});
|
|
252
278
|
|
|
253
|
-
export default { generateDB, create, get };
|
|
279
|
+
export default { generateDB, create, get };
|