Browse Source

add tentative destroy action w commented out test, needs work

Kieran Gibb 2 years ago
parent
commit
b2b417a9fd
No known key found for this signature in database
2 changed files with 39 additions and 9 deletions
  1. 23
    9
      index.js
  2. 16
    0
      test/index.test.js

+ 23
- 9
index.js View File

@@ -41,12 +41,10 @@ class GroupStore {
41 41
       return uniq(Object.values(this.groups))
42 42
     } catch (err) {
43 43
       debug(err)
44
-      return err
44
+      return []
45 45
     }
46 46
   }
47 47
 
48
-  // expects address to be a buffer...
49
-  // so we must sanitize params before using store
50 48
   async where (params = {}) {
51 49
     try {
52 50
       var response = await this.all()
@@ -62,7 +60,7 @@ class GroupStore {
62 60
       return response
63 61
     } catch (err) {
64 62
       debug(err)
65
-      return err
63
+      return false
66 64
     }
67 65
   }
68 66
 
@@ -72,7 +70,7 @@ class GroupStore {
72 70
       return response[0]
73 71
     } catch (err) {
74 72
       debug(err)
75
-      return err
73
+      return false
76 74
     }
77 75
   }
78 76
 
@@ -81,7 +79,6 @@ class GroupStore {
81 79
       var group = await this.findBy(params)
82 80
       if (group) throw new Error('group already exists')
83 81
 
84
-      var { name, address, encryptionKey } = params
85 82
       var group = this._buildGroup(params)
86 83
 
87 84
       await group.ready()
@@ -92,12 +89,30 @@ class GroupStore {
92 89
       return group
93 90
     } catch (error) {
94 91
       debug(error)
95
-      return error
92
+      return false
96 93
     }
97 94
   }
98 95
 
99
-  async destroy (id) {
96
+  // TODO: this doesn't work currently, throws a level db error
97
+  async destroy (params = {}) {
98
+    try {
99
+      var group = await this.findBy(params)
100
+      if (!group) throw new Error('no group exists')
100 101
 
102
+      this.config.groups.delete(group.address)
103
+      // TODO: delete group data
104
+
105
+      if (this.config.save()) {
106
+        delete this.groups[group.address.toString('hex')]
107
+        return true
108
+      } else {
109
+        this.config.groups.set(group.address, group)
110
+        throw new Error('failed to save config file')
111
+      }
112
+    } catch (error) {
113
+      debug(error)
114
+      return false
115
+    }
101 116
   }
102 117
 
103 118
   // ------------------------------------------------------------------
@@ -153,7 +168,6 @@ class GroupStore {
153 168
 
154 169
     return true
155 170
   }
156
-
157 171
 }
158 172
 
159 173
 // _validate (id) {

+ 16
- 0
test/index.test.js View File

@@ -90,4 +90,20 @@ describe('group store: basic', (context) => {
90 90
     assert.ok(group && group.address, 'creates a new group')
91 91
     cleanup(storage, next)
92 92
   })
93
+
94
+  // context('destroy()', async function (assert, next) {
95
+  //   let storage = tmp(),
96
+  //     config = Config(storage),
97
+  //     params = crypto.keySet()
98
+
99
+  //   config.groups.set(params.address, params)
100
+  //   const store = Store(Decrypted, storage, { config })
101
+
102
+  //   var groups = await store.all()
103
+  //   console.log(groups.map((group) => group.address.toString('hex')))
104
+  //   var success = await store.destroy(params)
105
+
106
+  //   assert.ok(success, 'deleted the group')
107
+  //   cleanup(storage, next)
108
+  // })
93 109
 })