-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathworkspaceResolver.js
190 lines (166 loc) · 5.16 KB
/
workspaceResolver.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
const { ObjectId } = require('mongoose').Types
const { ApiError } = require('../helpers/errors')
const Workspace = require('../models/workspace')
const Article = require('../models/article')
const Corpus = require('../models/corpus')
async function workspace (_, { workspaceId }, { user }) {
if (user?.admin === true) {
const workspace = await Workspace.findById(workspaceId)
if (!workspace) {
throw new ApiError('NOT_FOUND', `Unable to find workspace with id ${workspaceId}`)
}
return workspace
}
const workspace = await Workspace.findOne({
$and: [
{ _id: workspaceId },
{ 'members.user': user?._id }
]
})
if (!workspace) {
throw new ApiError('NOT_FOUND', `Unable to find workspace with id ${workspaceId} for user with id ${user?._id}`)
}
return workspace
}
class WorkspaceArticle {
constructor (workspace, article) {
this.workspace = workspace
this.article = article
}
async remove () {
if (this.article) {
this.workspace.articles.pull({ _id: this.article._id })
return this.workspace.save()
}
return this.workspace
}
}
class WorkspaceMember {
constructor (workspace, member) {
this.workspace = workspace
this.member = member
}
async remove () {
if (this.member) {
this.workspace.members.pull({ _id: this.member._id })
return this.workspace.save()
}
return this.workspace
}
}
module.exports = {
Mutation: {
async createWorkspace (_, args, { user }) {
const { createWorkspaceInput } = args
if (!user) {
throw new ApiError('UNAUTHENTICATED', 'Unable to create a workspace as an unauthenticated user')
}
// any user can create a workspace
const newWorkspace = new Workspace({
name: createWorkspaceInput.name,
color: createWorkspaceInput.color,
description: createWorkspaceInput.description,
members: [{ user: user._id }],
articles: [],
creator: user._id,
})
return newWorkspace.save()
},
/**
*
*/
workspace
},
Query: {
/**
*
*/
workspace,
/**
*
*/
async workspaces (_root, _args, { user }) {
if (user?.admin === true) {
return Workspace.find()
}
return Workspace.find({ 'members.user': user?._id }).sort([['updatedAt', -1]])
},
},
WorkspaceArticle: {
async article (workspaceArticle, { articleId }) {
const article = workspace.articles.find((a) => String(a._id) === articleId)
return new WorkspaceArticle(workspace, article)
},
},
Workspace: {
async article (workspace, { articleId }) {
const article = workspace.articles.find((a) => String(a._id) === articleId)
return new WorkspaceArticle(workspace, article)
},
/**
*
* @param workspace
* @param _args
* @param {{ loaders: { articles } }} context
* @returns {Promise<*>}
*/
async articles (workspace, _args, context) {
const articles = (await Promise.all(workspace.articles.map((articleId) => context.loaders.articles.load(articleId))))
.filter((a) => a !== undefined) // remove deleted articles
articles.sort((a, b) => a.createdAt > b.createdAt ? -1 : 1)
return Article.complete(articles, context.loaders)
},
async corpus(workspace) {
return Corpus
.find({ 'workspace': workspace._id })
.populate([{ path: 'creator' }])
.sort([['updatedAt', -1]])
},
async member (workspace, { userId }) {
const member = workspace.members.find(m => String(m.user) === userId)
return new WorkspaceMember(workspace, member)
},
async members (workspace, { limit }) {
await workspace.populate({ path: 'members', populate: 'user', limit }).execPopulate()
return workspace.members.map((m) => m.user)
},
async stats (workspace) {
return {
articlesCount: workspace.articles.length,
membersCount: workspace.members.length,
}
},
async creator (workspace, _args, context) {
return await context.loaders.users.load(workspace.creator)
},
// mutations
async leave (workspace, args, { user }) {
if (!user) {
throw new ApiError('UNAUTHENTICATED', 'Unable to leave a workspace as an unauthenticated user')
}
// TODO: remove workspace if there's no member left!
return Workspace.findOneAndUpdate(
{_id: ObjectId(workspace._id) },
{ $pull: { members: { user: ObjectId(user.id) } } },
{ lean: true }
)
},
async addArticle (workspace, { articleId }) {
const articleAlreadyAdded = workspace.articles.find((id) => String(id) === articleId)
if (articleAlreadyAdded) {
return workspace
}
workspace.articles.push({ _id: articleId })
return workspace.save()
},
async inviteMember (workspace, { userId, role }) {
// question: should we check that the authenticated user "knows" the member?
const memberAlreadyInvited = workspace.members.find((id) => String(id) === userId)
if (memberAlreadyInvited) {
return workspace
}
workspace.members.push({ user: userId, role })
return workspace.save()
},
}
}