@cocreate/organizations 1.1.68 → 1.2.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 +7 -0
- package/package.json +3 -2
- package/src/client.js +101 -0
- package/src/index.js +14 -101
- package/src/server.js +94 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [1.2.0](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.1.68...v1.2.0) (2022-03-03)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* manage server and client scripts ([3084e7e](https://github.com/CoCreate-app/CoCreate-organizations/commit/3084e7e0d6096905dd3c6bdeb21d56fb43039e51))
|
|
7
|
+
|
|
1
8
|
## [1.1.68](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.1.67...v1.1.68) (2022-02-24)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/organizations",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.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",
|
|
@@ -64,6 +64,7 @@
|
|
|
64
64
|
"@cocreate/actions": "^1.3.33",
|
|
65
65
|
"@cocreate/crud-client": "^1.4.47",
|
|
66
66
|
"@cocreate/docs": "^1.2.69",
|
|
67
|
-
"@cocreate/elements": "^1.6.11"
|
|
67
|
+
"@cocreate/elements": "^1.6.11",
|
|
68
|
+
"mongodb": "^4.4.0"
|
|
68
69
|
}
|
|
69
70
|
}
|
package/src/client.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import crud from '@cocreate/crud-client';
|
|
2
|
+
// import input from '@cocreate/elements'
|
|
3
|
+
import action from '@cocreate/actions';
|
|
4
|
+
|
|
5
|
+
const CoCreateOrganization = {
|
|
6
|
+
// masterDB: '5ae0cfac6fb8c4e656fdaf92', // '5ae0cfac6fb8c4e656fdaf92' /** masterDB **/,
|
|
7
|
+
init: function() {
|
|
8
|
+
const self = this;
|
|
9
|
+
crud.listen('createOrgNew', function(data) {
|
|
10
|
+
document.dispatchEvent(new CustomEvent('createdOrg', {
|
|
11
|
+
detail: data
|
|
12
|
+
}));
|
|
13
|
+
});
|
|
14
|
+
crud.listen('createOrg', function(data) {
|
|
15
|
+
self.setDocumentId('organizations', data.document_id);
|
|
16
|
+
document.dispatchEvent(new CustomEvent('createdOrg', {
|
|
17
|
+
detail: data
|
|
18
|
+
}));
|
|
19
|
+
});
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
createOrgNew: function(btn) {
|
|
23
|
+
let form = btn.closest("form");
|
|
24
|
+
if (!form) return;
|
|
25
|
+
let newOrg_id = form.querySelector("input[collection='organizations'][name='_id']");
|
|
26
|
+
|
|
27
|
+
const room = config.organization_Id;
|
|
28
|
+
|
|
29
|
+
crud.send('createOrgNew', {
|
|
30
|
+
apiKey: config.apiKey,
|
|
31
|
+
organization_id: config.organization_Id,
|
|
32
|
+
collection: 'organizations',
|
|
33
|
+
newOrg_id: newOrg_id,
|
|
34
|
+
}, room);
|
|
35
|
+
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
createOrg: function(btn) {
|
|
39
|
+
let form = btn.closest("form");
|
|
40
|
+
if (!form) return;
|
|
41
|
+
|
|
42
|
+
let elements = form.querySelectorAll("[collection='organizations'][name]");
|
|
43
|
+
|
|
44
|
+
let data = {};
|
|
45
|
+
//. get form data
|
|
46
|
+
elements.forEach(el => {
|
|
47
|
+
let name = el.getAttribute('name');
|
|
48
|
+
let value = el.getValue(el) || el.getAttribute('value');
|
|
49
|
+
if (!name || !value) return;
|
|
50
|
+
if (el.getAttribute('data-type') == 'array') {
|
|
51
|
+
value = [value];
|
|
52
|
+
}
|
|
53
|
+
data[name] = value;
|
|
54
|
+
});
|
|
55
|
+
const room = config.organization_Id;
|
|
56
|
+
|
|
57
|
+
crud.send('createOrg', {
|
|
58
|
+
apiKey: config.apiKey,
|
|
59
|
+
organization_id: config.organization_Id,
|
|
60
|
+
// orgDb: newOrg,
|
|
61
|
+
mdb: '5ae0cfac6fb8c4e656fdaf92',
|
|
62
|
+
collection: 'organizations',
|
|
63
|
+
data: data
|
|
64
|
+
}, room);
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
setDocumentId: function(collection, id) {
|
|
68
|
+
let orgIdElements = document.querySelectorAll(`[collection='${collection}']`);
|
|
69
|
+
if (orgIdElements && orgIdElements.length > 0) {
|
|
70
|
+
orgIdElements.forEach((el) => {
|
|
71
|
+
if (!el.getAttribute('document_id')) {
|
|
72
|
+
el.setAttribute('document_id', id);
|
|
73
|
+
}
|
|
74
|
+
if (el.getAttribute('name') == "_id") {
|
|
75
|
+
el.value = id;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
action.init({
|
|
84
|
+
name: "createOrgNew",
|
|
85
|
+
endEvent: "createdOrg",
|
|
86
|
+
callback: (btn, data) => {
|
|
87
|
+
CoCreateOrganization.createOrgNew(btn);
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
action.init({
|
|
92
|
+
name: "createOrg",
|
|
93
|
+
endEvent: "createdOrg",
|
|
94
|
+
callback: (btn, data) => {
|
|
95
|
+
CoCreateOrganization.createOrg(btn);
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
CoCreateOrganization.init();
|
|
100
|
+
|
|
101
|
+
export default CoCreateOrganization;
|
package/src/index.js
CHANGED
|
@@ -1,101 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
self.setDocumentId('organizations', data.document_id);
|
|
16
|
-
document.dispatchEvent(new CustomEvent('createdOrg', {
|
|
17
|
-
detail: data
|
|
18
|
-
}));
|
|
19
|
-
});
|
|
20
|
-
},
|
|
21
|
-
|
|
22
|
-
createOrgNew: function(btn) {
|
|
23
|
-
let form = btn.closest("form");
|
|
24
|
-
if (!form) return;
|
|
25
|
-
let newOrg_id = form.querySelector("input[collection='organizations'][name='_id']");
|
|
26
|
-
|
|
27
|
-
const room = config.organization_Id;
|
|
28
|
-
|
|
29
|
-
crud.send('createOrgNew', {
|
|
30
|
-
apiKey: config.apiKey,
|
|
31
|
-
organization_id: config.organization_Id,
|
|
32
|
-
collection: 'organizations',
|
|
33
|
-
newOrg_id: newOrg_id,
|
|
34
|
-
}, room);
|
|
35
|
-
|
|
36
|
-
},
|
|
37
|
-
|
|
38
|
-
createOrg: function(btn) {
|
|
39
|
-
let form = btn.closest("form");
|
|
40
|
-
if (!form) return;
|
|
41
|
-
|
|
42
|
-
let elements = form.querySelectorAll("[collection='organizations'][name]");
|
|
43
|
-
|
|
44
|
-
let data = {};
|
|
45
|
-
//. get form data
|
|
46
|
-
elements.forEach(el => {
|
|
47
|
-
let name = el.getAttribute('name');
|
|
48
|
-
let value = el.getValue(el) || el.getAttribute('value');
|
|
49
|
-
if (!name || !value) return;
|
|
50
|
-
if (el.getAttribute('data-type') == 'array') {
|
|
51
|
-
value = [value];
|
|
52
|
-
}
|
|
53
|
-
data[name] = value;
|
|
54
|
-
});
|
|
55
|
-
const room = config.organization_Id;
|
|
56
|
-
|
|
57
|
-
crud.send('createOrg', {
|
|
58
|
-
apiKey: config.apiKey,
|
|
59
|
-
organization_id: config.organization_Id,
|
|
60
|
-
// orgDb: newOrg,
|
|
61
|
-
mdb: '5ae0cfac6fb8c4e656fdaf92',
|
|
62
|
-
collection: 'organizations',
|
|
63
|
-
data: data
|
|
64
|
-
}, room);
|
|
65
|
-
},
|
|
66
|
-
|
|
67
|
-
setDocumentId: function(collection, id) {
|
|
68
|
-
let orgIdElements = document.querySelectorAll(`[collection='${collection}']`);
|
|
69
|
-
if (orgIdElements && orgIdElements.length > 0) {
|
|
70
|
-
orgIdElements.forEach((el) => {
|
|
71
|
-
if (!el.getAttribute('document_id')) {
|
|
72
|
-
el.setAttribute('document_id', id);
|
|
73
|
-
}
|
|
74
|
-
if (el.getAttribute('name') == "_id") {
|
|
75
|
-
el.value = id;
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
},
|
|
80
|
-
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
action.init({
|
|
84
|
-
name: "createOrgNew",
|
|
85
|
-
endEvent: "createdOrg",
|
|
86
|
-
callback: (btn, data) => {
|
|
87
|
-
CoCreateOrganization.createOrgNew(btn);
|
|
88
|
-
},
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
action.init({
|
|
92
|
-
name: "createOrg",
|
|
93
|
-
endEvent: "createdOrg",
|
|
94
|
-
callback: (btn, data) => {
|
|
95
|
-
CoCreateOrganization.createOrg(btn);
|
|
96
|
-
},
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
CoCreateOrganization.init();
|
|
100
|
-
|
|
101
|
-
export default CoCreateOrganization;
|
|
1
|
+
(function (root, factory) {
|
|
2
|
+
if (typeof define === 'function' && define.amd) {
|
|
3
|
+
define(["./client"], function(CoCreateOrganization) {
|
|
4
|
+
return factory(CoCreateOrganization)
|
|
5
|
+
});
|
|
6
|
+
} else if (typeof module === 'object' && module.exports) {
|
|
7
|
+
const CoCreateOrganization = require("./server.js")
|
|
8
|
+
module.exports = factory(CoCreateOrganization);
|
|
9
|
+
} else {
|
|
10
|
+
root.returnExports = factory(root["./client.js"]);
|
|
11
|
+
}
|
|
12
|
+
}(typeof self !== 'undefined' ? self : this, function (CoCreateOrganization) {
|
|
13
|
+
return CoCreateOrganization;
|
|
14
|
+
}));
|
package/src/server.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
const {ObjectID} = require("mongodb");
|
|
2
|
+
|
|
3
|
+
class CoCreateOrganization {
|
|
4
|
+
constructor(wsManager, dbClient) {
|
|
5
|
+
this.wsManager = wsManager
|
|
6
|
+
this.dbClient = dbClient
|
|
7
|
+
this.init()
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
init() {
|
|
11
|
+
if (this.wsManager) {
|
|
12
|
+
this.wsManager.on('createOrgNew', (socket, data, roomInfo) => this.createOrgNew(socket, data));
|
|
13
|
+
this.wsManager.on('createOrg', (socket, data, roomInfo) => this.createOrg(socket, data));
|
|
14
|
+
this.wsManager.on('deleteOrg', (socket, data, roomInfo) => this.deleteOrg(socket, data));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async createOrgNew(socket, data) {
|
|
19
|
+
const self = this;
|
|
20
|
+
if(!data) return;
|
|
21
|
+
const newOrg_id = data.newOrg_id;
|
|
22
|
+
if (newOrg_id != data.organization_id) {
|
|
23
|
+
try{
|
|
24
|
+
const db = this.dbClient.db(req_data['organization_id']);
|
|
25
|
+
const collection = db.collection(req_data["collection"]);
|
|
26
|
+
const query = {
|
|
27
|
+
"_id": new ObjectID(newOrg_id)
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
collection.find(query).toArray(function(error, result) {
|
|
31
|
+
if(!error && result){
|
|
32
|
+
const newOrgDb = self.dbClient.db(newOrg_id).collection(data['collection']);
|
|
33
|
+
// Create new user in config db users collection
|
|
34
|
+
newOrgDb.insertOne({...result.ops[0], organization_id : newOrg_id}, function(error, result) {
|
|
35
|
+
if(!error && result){
|
|
36
|
+
const response = { ...data, document_id: result.ops[0]._id, data: result.ops[0]}
|
|
37
|
+
self.wsManager.send(socket, 'createOrgNew', response, data['organization_id']);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}catch(error){
|
|
43
|
+
console.log('createDocument error', error);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async createOrg(socket, data) {
|
|
49
|
+
const self = this;
|
|
50
|
+
if(!data.data) return;
|
|
51
|
+
|
|
52
|
+
try{
|
|
53
|
+
const collection = this.dbClient.db(data.organization_id).collection(data.collection);
|
|
54
|
+
// create new org in config db organization collection
|
|
55
|
+
collection.insertOne({ ...data.data, organization_id: data.organization_id }, function(error, result) {
|
|
56
|
+
if(!error && result){
|
|
57
|
+
const orgId = result.ops[0]._id + "";
|
|
58
|
+
const anotherCollection = self.dbClient.db(orgId).collection(data['collection']);
|
|
59
|
+
// Create new org db and insert organization
|
|
60
|
+
anotherCollection.insertOne({...result.ops[0], organization_id : orgId});
|
|
61
|
+
|
|
62
|
+
const response = { ...data, document_id: result.ops[0]._id, data: result.ops[0] }
|
|
63
|
+
|
|
64
|
+
self.wsManager.send(socket, 'createOrg', response );
|
|
65
|
+
if (data.room) {
|
|
66
|
+
self.wsManager.broadcast(socket, data.namespace || data['organization_id'] , data.room, 'createDocument', response, true);
|
|
67
|
+
} else {
|
|
68
|
+
self.wsManager.broadcast(socket, data.namespace || data['organization_id'], null, 'createDocument', response)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// add new org to masterDb
|
|
72
|
+
const masterOrgDb = self.dbClient.db(data.mdb).collection(data['collection']);
|
|
73
|
+
masterOrgDb.insertOne({...result.ops[0], organization_id : data['mdb']});
|
|
74
|
+
|
|
75
|
+
});
|
|
76
|
+
}catch(error){
|
|
77
|
+
console.log('createDocument error', error);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
async deleteOrg(socket, data) {
|
|
83
|
+
const self = this;
|
|
84
|
+
if(!data.data) return;
|
|
85
|
+
try{
|
|
86
|
+
// ToDo: Delete DB and delete org and user from masterDB
|
|
87
|
+
}catch(error){
|
|
88
|
+
console.log('deleteOrg error', error);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
module.exports = CoCreateOrganization;
|