@cocreate/organizations 1.28.3 → 1.28.5

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.
Files changed (3) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/package.json +5 -13
  3. package/src/server.js +102 -68
package/CHANGELOG.md CHANGED
@@ -1,3 +1,18 @@
1
+ ## [1.28.5](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.28.4...v1.28.5) (2025-09-03)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * improve organization creation logic and error handling ([bfed09d](https://github.com/CoCreate-app/CoCreate-organizations/commit/bfed09df186993dccef4164674ac430fe4aec17b))
7
+ * streamline organization creation validation and error handling ([4e94111](https://github.com/CoCreate-app/CoCreate-organizations/commit/4e94111529c50be01f96424e5428c14b0d0e4b36))
8
+
9
+ ## [1.28.4](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.28.3...v1.28.4) (2025-05-01)
10
+
11
+
12
+ ### Bug Fixes
13
+
14
+ * update [@cocreate](https://github.com/cocreate) dependencies ([4c71cd3](https://github.com/CoCreate-app/CoCreate-organizations/commit/4c71cd3eee8dff2dc3fe6aca5e64eb5e87696339))
15
+
1
16
  ## [1.28.3](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.28.2...v1.28.3) (2025-04-30)
2
17
 
3
18
 
package/package.json CHANGED
@@ -1,18 +1,10 @@
1
1
  {
2
2
  "name": "@cocreate/organizations",
3
- "version": "1.28.3",
3
+ "version": "1.28.5",
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",
7
- "cocreate",
8
- "low-code-framework",
9
- "no-code-framework",
10
- "cocreatejs",
11
- "cocreatejs-component",
12
- "cocreate-framework",
13
- "no-code",
14
7
  "low-code",
15
- "collaborative-framework",
16
8
  "realtime",
17
9
  "realtime-framework",
18
10
  "collaboration",
@@ -54,9 +46,9 @@
54
46
  "webpack-log": "^3.0.1"
55
47
  },
56
48
  "dependencies": {
57
- "@cocreate/actions": "^1.21.0",
58
- "@cocreate/config": "^1.13.0",
59
- "@cocreate/crud-client": "^1.34.1",
60
- "@cocreate/elements": "^1.42.0"
49
+ "@cocreate/actions": "^1.21.1",
50
+ "@cocreate/config": "^1.13.1",
51
+ "@cocreate/crud-client": "^1.34.2",
52
+ "@cocreate/elements": "^1.42.1"
61
53
  }
62
54
  }
package/src/server.js CHANGED
@@ -1,80 +1,114 @@
1
1
  class CoCreateOrganization {
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
- }
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
12
 
13
- init() {
14
- if (this.wsManager) {
15
- this.wsManager.on('createOrganization', (data) =>
16
- this.createOrganization(data)
17
- );
18
- }
19
- }
13
+ init() {
14
+ if (this.wsManager) {
15
+ this.wsManager.on("createOrganization", (data) =>
16
+ this.createOrganization(data)
17
+ );
18
+ }
19
+ }
20
20
 
21
- async createOrganization(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
21
+ async createOrganization(data) {
22
+ try {
23
+ const { organization, user, key } = data;
25
24
 
26
- const organization = {
27
- _id: data.organization._id,
28
- name: data.organization.name || 'untitled',
29
- host: data.organization.host || [],
30
- owner: data.user._id,
31
- balance: 10, // TODO: set balance to 0 and create a transcation with type credit to add the $10
32
- dataTransfered: 0
33
- }
25
+ if (
26
+ !organization ||
27
+ !organization._id ||
28
+ !organization.host ||
29
+ !organization.host[0] ||
30
+ !organization.host[0].name
31
+ ) {
32
+ this.errorHandler(
33
+ data,
34
+ "invalid_organization: missing required organization fields"
35
+ );
36
+ }
34
37
 
35
- // TODO: check if user exist and confirm credentials
36
- const user = {
37
- _id: data.user._id,
38
- firstname: data.user.firtname || 'Admin',
39
- lastname: data.user.lastname || ''
40
- }
38
+ if (!user || !user._id || !user.email || !user.password) {
39
+ this.errorHandler(
40
+ data,
41
+ "invalid_user: missing required user fields"
42
+ );
43
+ }
41
44
 
42
- const userKey = {
43
- type: "user",
44
- key: user._id,
45
- roles: ['user'],
46
- email: user.object.email,
47
- password: user.object.password || btoa('0000'),
48
- user: {
49
- array: 'users'
50
- }
51
- }
45
+ if (!key || !Array.isArray(key) || key.length !== 3) {
46
+ this.errorHandler(
47
+ data,
48
+ "invalid_key: An array of 3 keys is required with type key, user and role."
49
+ );
50
+ }
52
51
 
53
- const Data = {}
54
- Data.method = 'object.create'
55
- Data.host = data.host
56
- Data.database = process.env.organization_id
57
- Data.organization_id = process.env.organization_id
52
+ // If there are validation errors, include them in the response and send immediately
53
+ if (data.success === false) {
54
+ return this.wsManager.send(data);
55
+ }
58
56
 
59
- if (organization) {
60
- const response = await this.crud.send({ ...Data, array: 'organizations', object: organization })
61
- this.wsManager.send(this.platformSocket, response);
62
- }
63
- if (user) {
64
- const response = await this.crud.send({ ...Data, array: 'users', object: user })
65
- this.wsManager.send(this.platformSocket, response);
66
- }
57
+ const Data = {};
58
+ Data.method = "object.create";
59
+ Data.host = organization.host[0].name;
60
+ Data.database = organization._id;
61
+ Data.organization_id = organization._id;
67
62
 
68
- if (userKey) {
69
- const response = await this.crud.send({ ...Data, array: 'keys', object: userKey })
70
- this.wsManager.send(this.platformSocket, response);
71
- }
63
+ if (organization) {
64
+ const response = await this.crud.send({
65
+ ...Data,
66
+ array: "organizations",
67
+ object: organization
68
+ });
69
+ if (response.error) {
70
+ this.errorHandler(data, response.error);
71
+ }
72
+ }
73
+ if (user) {
74
+ const response = await this.crud.send({
75
+ ...Data,
76
+ array: "users",
77
+ object: user
78
+ });
79
+ if (response.error) {
80
+ this.errorHandler(data, response.error);
81
+ }
82
+ }
72
83
 
73
- this.wsManager.send(data);
74
- } catch (error) {
75
- console.log('createObject error', error);
76
- }
77
- }
84
+ if (key) {
85
+ const response = await this.crud.send({
86
+ ...Data,
87
+ array: "keys",
88
+ object: key
89
+ });
90
+ if (response.error) {
91
+ this.errorHandler(data, response.error);
92
+ }
93
+ }
94
+
95
+ if (data.success !== false) {
96
+ data.success = true;
97
+ }
98
+
99
+ this.wsManager.send(data);
100
+ } catch (error) {
101
+ if (data.socket) {
102
+ this.errorHandler(data, error);
103
+ this.wsManager.send(data);
104
+ }
105
+ }
106
+ }
107
+
108
+ errorHandler(data, error) {
109
+ data.success = false;
110
+ this.crud.errorHandler(data, error);
111
+ }
78
112
  }
79
113
 
80
- module.exports = CoCreateOrganization;
114
+ module.exports = CoCreateOrganization;