@cocreate/organizations 1.1.68 → 1.2.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,25 @@
1
+ ## [1.2.2](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.2.1...v1.2.2) (2022-03-21)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * update class sortable to attribute sortable ([8257806](https://github.com/CoCreate-app/CoCreate-organizations/commit/825780670c4a40eac352e5c83067df9dcea686bd))
7
+
8
+ ## [1.2.1](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.2.0...v1.2.1) (2022-03-06)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * removed param isExact from broadcast as it can be handled by param room ([241b0fe](https://github.com/CoCreate-app/CoCreate-organizations/commit/241b0fec1bd0251604bd43b7cc0d6273c23d4342))
14
+ * update param roomInfo to socketInfo ([fadc197](https://github.com/CoCreate-app/CoCreate-organizations/commit/fadc197cd647b0e951895f787bee6f4f2c92f6d6))
15
+
16
+ # [1.2.0](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.1.68...v1.2.0) (2022-03-03)
17
+
18
+
19
+ ### Features
20
+
21
+ * manage server and client scripts ([3084e7e](https://github.com/CoCreate-app/CoCreate-organizations/commit/3084e7e0d6096905dd3c6bdeb21d56fb43039e51))
22
+
1
23
  ## [1.1.68](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.1.67...v1.1.68) (2022-02-24)
2
24
 
3
25
 
package/demo/index.html CHANGED
@@ -71,7 +71,7 @@
71
71
  <div class="padding:25px background:gainsboro text-align:left">
72
72
 
73
73
  <!-- Industry select -->
74
- <cocreate-select class="sortable floating-label" collection="organizations" name="industry" placeholder="Industry" realtime="false" save="false">
74
+ <cocreate-select sortable class="floating-label" collection="organizations" name="industry" placeholder="Industry" realtime="false" save="false">
75
75
  <input placeholder="Search" template_id="industries">
76
76
  <ul fetch-collection="industries"
77
77
  template_id="industries"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/organizations",
3
- "version": "1.1.68",
3
+ "version": "1.2.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",
@@ -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
- 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;
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,90 @@
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, socketInfo) => this.createOrgNew(socket, data));
13
+ this.wsManager.on('createOrg', (socket, data, socketInfo) => this.createOrg(socket, data));
14
+ this.wsManager.on('deleteOrg', (socket, data, socketInfo) => 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
+ self.wsManager.broadcast(socket, data.namespace || data['organization_id'] , data.room, 'createDocument', response);
66
+ }
67
+ // add new org to masterDb
68
+ const masterOrgDb = self.dbClient.db(data.mdb).collection(data['collection']);
69
+ masterOrgDb.insertOne({...result.ops[0], organization_id : data['mdb']});
70
+
71
+ });
72
+ }catch(error){
73
+ console.log('createDocument error', error);
74
+ }
75
+ }
76
+
77
+
78
+ async deleteOrg(socket, data) {
79
+ const self = this;
80
+ if(!data.data) return;
81
+ try{
82
+ // ToDo: Delete DB and delete org and user from masterDB
83
+ }catch(error){
84
+ console.log('deleteOrg error', error);
85
+ }
86
+ }
87
+
88
+ }
89
+
90
+ module.exports = CoCreateOrganization;