@cocreate/organizations 1.4.0 → 1.5.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,11 @@
1
+ # [1.5.0](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.4.0...v1.5.0) (2022-05-29)
2
+
3
+
4
+ ### Features
5
+
6
+ * deleteDocument broadcasted after orgDeleted ([be1ecee](https://github.com/CoCreate-app/CoCreate-organizations/commit/be1ecee732ce71a3877dfbb06df9b19c0148eb16))
7
+ * deleteOrgs action to select and delete many organizations ([0abaa20](https://github.com/CoCreate-app/CoCreate-organizations/commit/0abaa2061ac7575c51314f5011aa6031022b6d7f))
8
+
1
9
  # [1.4.0](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.3.2...v1.4.0) (2022-05-28)
2
10
 
3
11
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/organizations",
3
- "version": "1.4.0",
3
+ "version": "1.5.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
@@ -30,14 +30,11 @@ const CoCreateOrganization = {
30
30
  }
31
31
  data[name] = value;
32
32
  });
33
- const room = config.organization_id;
34
33
 
35
34
  crud.send('createOrg', {
36
- apiKey: config.apiKey,
37
- organization_id: config.organization_id,
38
35
  collection: 'organizations',
39
36
  data: data
40
- }, room);
37
+ });
41
38
  },
42
39
 
43
40
  setDocumentId: function(collection, id) {
@@ -53,9 +50,63 @@ const CoCreateOrganization = {
53
50
  });
54
51
  }
55
52
  },
53
+
54
+ deleteOrg: async function(btn) {
55
+ const { collection, document_id } = crud.getAttr(btn);
56
+ const organization_id = document_id;
57
+
58
+ if(crud.checkAttrValue(collection) && crud.checkAttrValue(document_id)) {
59
+
60
+ crud.send('deleteOrg', {
61
+ collection,
62
+ document_id,
63
+ data: {
64
+ organization_id,
65
+ },
66
+ 'metadata': 'deleteOrg-action'
67
+ }).then(response => {
68
+
69
+ // ToDo: replace with custom event
70
+ document.dispatchEvent(new CustomEvent('deletedOrg', {
71
+ detail: {}
72
+ }));
73
+ })
74
+ }
75
+ },
76
+
77
+ deleteOrgs: async function(btn) {
78
+ const collection = btn.getAttribute('collection');
79
+ if(crud.checkAttrValue(collection)) {
80
+ const dataTemplateid = btn.getAttribute('template_id');
81
+ if(!dataTemplateid) return;
82
+
83
+ const selectedEls = document.querySelectorAll(`.selected[templateid="${dataTemplateid}"]`);
84
+
85
+ selectedEls.forEach((el) => {
86
+ const document_id = el.getAttribute('document_id');
87
+ const organization_id = document_id;
88
+
89
+ if(crud.checkAttrValue(document_id)) {
90
+ crud.send('deleteOrg', {
91
+ collection,
92
+ document_id,
93
+ data: {
94
+ organization_id,
95
+ },
96
+ 'metadata': 'deleteOrgs-action'
97
+ })
98
+ }
99
+ });
100
+
101
+ document.dispatchEvent(new CustomEvent('deletedOrgs', {
102
+ detail: {}
103
+ }));
104
+ }
105
+ }
56
106
 
57
107
  };
58
108
 
109
+
59
110
  action.init({
60
111
  name: "createOrg",
61
112
  endEvent: "createdOrg",
@@ -64,6 +115,22 @@ action.init({
64
115
  },
65
116
  });
66
117
 
118
+ action.init({
119
+ name: "deleteOrg",
120
+ endEvent: "deletedOrg",
121
+ callback: (btn, data) => {
122
+ CoCreateOrganization.deleteOrg(btn);
123
+ },
124
+ });
125
+
126
+ action.init({
127
+ name: "deleteOrgs",
128
+ endEvent: "deletedOrgs",
129
+ callback: (btn, data) => {
130
+ CoCreateOrganization.deleteOrgs(btn);
131
+ },
132
+ });
133
+
67
134
  CoCreateOrganization.init();
68
135
 
69
136
  export default CoCreateOrganization;
package/src/server.js CHANGED
@@ -9,8 +9,12 @@ class CoCreateOrganization {
9
9
 
10
10
  init() {
11
11
  if (this.wsManager) {
12
- this.wsManager.on('createOrg', (socket, data, socketInfo) => this.createOrg(socket, data, socketInfo));
13
- this.wsManager.on('deleteOrg', (socket, data, socketInfo) => this.deleteOrg(socket, data, socketInfo));
12
+ this.wsManager.on('createOrg', (socket, data, socketInfo) =>
13
+ this.createOrg(socket, data, socketInfo)
14
+ );
15
+ this.wsManager.on('deleteOrg', (socket, data, socketInfo) =>
16
+ this.deleteOrg(socket, data, socketInfo)
17
+ );
14
18
  }
15
19
  }
16
20
 
@@ -54,16 +58,14 @@ class CoCreateOrganization {
54
58
  if(!data.data) return;
55
59
  const organization_id = data.data.organization_id
56
60
  if(!organization_id || organization_id == process.env.organization_id) return;
57
-
61
+ if(data.organization_id != process.env.organization_id) return;
58
62
  try{
59
63
  const db = this.dbClient.db(organization_id);
60
64
  db.dropDatabase().then(response => {
61
65
  if (response === true){
62
- console.log('deleteOrg response', response)
63
- }
66
+ process.emit('deleteOrg', organization_id)
64
67
 
65
- // delete org from platformDB
66
- // if (organization_id != process.env.organization_id) {
68
+ // delete org from platformDB
67
69
  const platformDB = self.dbClient.db(process.env.organization_id).collection(data['collection']);
68
70
  const query = {
69
71
  "_id": new ObjectId(organization_id)
@@ -72,12 +74,13 @@ class CoCreateOrganization {
72
74
  platformDB.deleteOne(query, function(error, result) {
73
75
  if (!error) {
74
76
  let response = { ...data }
75
- self.broadcast(socket, 'deleteDocument', response, socketInfo)
77
+ self.wsManager.send(socket, 'deleteOrg', response, socketInfo);
78
+ self.wsManager.broadcast(socket, response.namespace || response['organization_id'], response.room, 'deleteDocument', response, socketInfo);
76
79
  } else {
77
80
  self.wsManager.send(socket, 'ServerError', error, socketInfo);
78
81
  }
79
82
  })
80
- // }
83
+ }
81
84
  })
82
85
  }catch(error){
83
86
  console.log('deleteOrg error', error);