-
Notifications
You must be signed in to change notification settings - Fork 1
/
Group.js
248 lines (235 loc) · 5.52 KB
/
Group.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
const {
gql
} = require('@apollo/client/core')
const Actor = require('./Actor')
const FIND_BY_ID = gql`
query find_group_by_id($groupId: uuid!) {
actor_by_pk(id: $groupId){
id
name
publicKey: public_key,
users {
user {
id
address
publicKey: public_key
},
roleId: role_id
}
}
}
`
const CREATE_GROUP = gql`
mutation create_group($name: String!, $publicKey: String!, $securityData: String!) {
create_group(req:
{
name: $name,
public_key: $publicKey,
security_data: $securityData
}
){
id
}
}
`
const UPSERT_MEMBER = gql`
mutation upsert_member($groupId: uuid!, $userId: uuid!, $roleId: Int!){
insert_group_user(objects:[
{
group_id: $groupId,
user_id: $userId,
role_id: $roleId
}
],
on_conflict:{
constraint:group_user_pkey,
update_columns:[role_id]
}){
affected_rows
}
}
`
const DELETE_MEMBER = gql`
mutation delete_member($groupId: uuid!, $userId: uuid!) {
delete_group_user_by_pk(group_id: $groupId, user_id: $userId){
group_id
user_id
role_id
}
}
`
class Group extends Actor {
constructor ({
gql,
crypto,
user
}) {
super({ gql })
this._crypto = crypto
this._user = user
}
/**
* @desc Finds a group by id
*
* @param {string} groupId
* @return {Object|null} with the following structure
* {
* "id": "551030f9-6054-4ffa-b02d-a275398ec50d",
* "name": "group2",
* "publicKey": "pubKey2",
* "users": [
* {
* "user": {
* "id": "3e4d4c1e-c0b1-433f-be0a-56cb662ce284",
* "address": "address",
* "publicKey": "publicKey20"
* }
* },
* {
* "user": {
* "id": "e43c5a55-46a7-41fb-ae64-b04480d8ab74",
* "address": "5Dnk6vQhAVDY9ysZr8jrqWJENDWYHaF3zorFA4dr9Mtbei77",
* "publicKey": "pubKey21"
* }
* }
* ]
* }
*/
async findById (groupId) {
const { actor_by_pk: group } = await this.query({
query: FIND_BY_ID,
variables: {
groupId
}
})
return group
}
/**
* @desc Finds a group by id
*
* @param {string} groupId
* @return {Object|null} with the following structure
* {
* "id": "551030f9-6054-4ffa-b02d-a275398ec50d",
* "name": "group2",
* "publicKey": "pubKey2",
* "users": [
* {
* "user": {
* "id": "3e4d4c1e-c0b1-433f-be0a-56cb662ce284",
* "address": "address",
* "publicKey": "publicKey20"
* }
* },
* {
* "user": {
* "id": "e43c5a55-46a7-41fb-ae64-b04480d8ab74",
* "address": "5Dnk6vQhAVDY9ysZr8jrqWJENDWYHaF3zorFA4dr9Mtbei77",
* "publicKey": "pubKey21"
* }
* }
* ]
* }
* @throws {Error} If group does not exist
*/
async getById (groupId) {
const group = await this.findById(groupId)
if (!group) {
throw new Error(`Group: ${groupId} does not exist`)
}
return group
}
/**
* @desc Creates a group
*
* @param {string} name
* @return {string} id of the newly created group
*/
async createGroup ({ name }) {
const {
publicKey,
privateKey: securityData
} = this._crypto.generateKeyPair()
const {
create_group: {
id
}
} = await this.mutate({
mutation: CREATE_GROUP,
variables: {
name,
publicKey,
securityData
}
})
return id
}
/**
* @desc Insert a group member or update the role for an existing member, one
* of userId or userAddress has to be specified
*
* @param {string} [userId] the user id to add as member or update role
* @param {string} [userAddress] the user address to add as member or update role
* @param {string} [groupId]
* @param {string} [roleId] the id of the role to assign the user
* @throws {Error} if the user is not a group admin
*/
async upsertMember ({
userId = null,
userAddress = null,
groupId,
roleId
}) {
userId = await this._user.getUserId({
userId,
userAddress
})
try {
const response = await this.mutate({
mutation: UPSERT_MEMBER,
variables: {
groupId,
userId,
roleId
}
}, { evict: { actorId: groupId } })
return response
} catch (error) {
if (error.message.includes('check constraint of an insert/update permission has failed')) {
throw new Error(`User does not have permission to manage members on group:${groupId}`)
}
throw error
}
}
/**
* @desc Delete a group member one
* of userId or userAddress has to be specified
*
* @param {string} [userId] the user id to add as member or update role
* @param {string} [userAddress] the user address to add as member or update role
* @param {string} [groupId]
* @throws {Error} if the user is not a group admin
*/
async deleteMember ({
userId = null,
userAddress = null,
groupId
}) {
userId = await this._user.getUserId({
userId,
userAddress
})
const { delete_group_user_by_pk: groupUser } = await this.mutate({
mutation: DELETE_MEMBER,
variables: {
groupId,
userId
}
}, { evict: { actorId: groupId } })
if (!groupUser) {
throw new Error(`User does not have permission to delete members from group: ${groupId}`)
}
return groupUser
}
}
module.exports = Group