-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMutation.js
218 lines (217 loc) · 6.72 KB
/
Mutation.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
import bcrypt from 'bcryptjs'
import authorizeUser from '../utils/authorizeUser'
import generateToken from '../utils/generateToken'
import hashPassword from '../utils/hashPassword'
const Mutation = {
async createUser(parents, { data }, { prisma }, info) {
if (data.passwordConfirm !== data.password)
throw Error('Passwords do not match!')
data.password = await hashPassword(data.password)
data.photo = 'default.jpg'
data.header = 'header.jpg'
data.active = true
delete data.passwordConfirm
const user = await prisma.mutation.createUser({ data })
return {
user,
token: generateToken(user.id),
expiresOn: String(new Date(Date.now() + 90 * 24 * 60 * 60 * 1000)),
}
},
async loginUser(parents, { email, password }, { prisma }, info) {
const user = await prisma.query.user({ where: { email } })
if (!user) throw Error('The email is incorrect')
const passwordCorrect = await bcrypt.compare(password, user.password)
if (!user || !passwordCorrect)
throw Error('The email and the password do not match')
return {
user,
token: generateToken(user.id),
expiresOn: String(new Date(Date.now() + 90 * 24 * 60 * 60 * 1000)),
}
},
async logoutUser(parents, args, { prisma, request }, info) {
const id = authorizeUser(request)
const user = await prisma.query.users({ where: { id } }, info)
return {
user,
token: null,
expiresOn: null,
}
},
async updateUser(parents, { data, password }, { prisma, request }, info) {
const id = authorizeUser(request)
const user = await prisma.query.user({ where: { id } })
const passwordCorrect = await bcrypt.compare(password, user.password)
if (!user || !passwordCorrect) throw Error('Wrong password')
return prisma.mutation.updateUser({ where: { id }, data }, info)
},
deleteUser(parents, args, { prisma, request }, info) {
const id = authorizeUser(request)
return prisma.mutation.updateUser(
{
where: { id },
data: {
active: false,
createdPosts: {
updateMany: {
where: { published: true },
data: { published: false, wasPublished: true },
},
},
},
},
info
)
},
async activateUser(parents, { email, password }, { prisma, request }, info) {
const id = authorizeUser(request)
let user = await prisma.query.user({ where: { email } })
const passwordCorrect = await bcrypt.compare(password, user.password)
if (!user || !passwordCorrect)
throw Error('The email and the password do not match')
return prisma.mutation.updateUser(
{
where: { id },
data: {
active: true,
createdPosts: {
updateMany: {
where: { wasPublished: true },
data: { published: true, wasPublished: false },
},
},
},
},
info
)
},
setNotificationToken(
parents,
{ notificationToken },
{ prisma, request },
info
) {
const id = authorizeUser(request)
return prisma.mutation.updateUser(
{ where: { id }, data: { notificationToken } },
info
)
},
createNote(parents, { data }, { prisma, request }, info) {
const id = authorizeUser(request)
data.wasPublished = false
return prisma.mutation.createNote(
{ data: { ...data, creator: { connect: { id } } } },
info
)
},
async deleteNote(parents, args, { request, prisma }, info) {
const id = authorizeUser(request)
const postExists = await prisma.exists.Note({
id: args.id,
creator: {
id,
},
})
if (!postExists) throw Error('This note does not exists')
return prisma.mutation.deleteNote({ where: { id } }, info)
},
async updateNote(parents, { id, data }, { prisma, request }, info) {
const userId = authorizeUser(request)
const postExists = await prisma.exists.Note({ id })
if (!postExists) throw Error('This post does not exists')
const userAuthorized =
(await prisma.exists.Note({ id, creator: { id: userId } })) ||
(await prisma.exists.Note({ id, collaborator_some: { id: userId } }))
if (!userAuthorized)
throw Error("You don't have permissions to edit this note")
return prisma.mutation.updateNote({ where: { id }, data }, info)
},
async toggleLikes(parents, args, { prisma, request }, info) {
const id = authorizeUser(request)
const postExists = await prisma.exists.Note({
id: args.id,
likes_some: { id },
})
if (postExists)
return prisma.mutation.updateNote(
{ where: { id: args.id }, data: { likes: { disconnect: { id } } } },
info
)
return prisma.mutation.updateNote(
{ where: { id: args.id }, data: { likes: { connect: { id } } } },
info
)
},
async sendRequest(
parents,
{ data: { noteId, type } },
{ prisma, request },
info
) {
const userId = authorizeUser(request)
if (
!(await prisma.exists.Request({
note: { id: noteId },
user: { id: userId },
type,
}))
) {
return prisma.mutation.createRequest(
{
data: {
type,
accepted: false,
user: { connect: { id: userId } },
note: { connect: { id: noteId } },
},
},
info
)
}
},
async respondRequest(
parents,
{ data: { response = null, id } },
{ prisma, request },
info
) {
authorizeUser(request)
const { type, user, note } = await prisma.query.request(
{ where: { id } },
`{ user { id } note { id } type }`
)
if (response) {
if (type === 'EDIT') {
return prisma.mutation.updateRequest(
{
where: { id },
data: {
accepted: response,
note: { update: { collaborator: { connect: { id: user.id } } } },
user: {
update: { collaboratedPosts: { connect: { id: note.id } } },
},
},
},
info
)
} else if (type === 'VIEW') {
return prisma.mutation.updateRequest(
{
where: { id },
data: {
accepted: response,
note: { update: { consumer: { connect: { id: user.id } } } },
user: { update: { consumedPosts: { connect: { id: note.id } } } },
},
},
info
)
}
}
return prisma.mutation.deleteRequest({ where: { id } }, info)
},
}
export default Mutation