@@ -120,17 +120,10 @@ export async function inviteMembers({
120
120
} ) ;
121
121
}
122
122
123
- export async function getInviteFromToken ( { token, userId } : { token : string ; userId : string } ) {
123
+ export async function getInviteFromToken ( { token } : { token : string } ) {
124
124
return await prisma . orgMemberInvite . findFirst ( {
125
125
where : {
126
126
token,
127
- organization : {
128
- members : {
129
- some : {
130
- userId,
131
- } ,
132
- } ,
133
- } ,
134
127
} ,
135
128
include : {
136
129
organization : true ,
@@ -154,19 +147,19 @@ export async function getUsersInvites({ email }: { email: string }) {
154
147
} ) ;
155
148
}
156
149
157
- export async function acceptInvite ( { userId, inviteId } : { userId : string ; inviteId : string } ) {
150
+ export async function acceptInvite ( {
151
+ user,
152
+ inviteId,
153
+ } : {
154
+ user : { id : string ; email : string } ;
155
+ inviteId : string ;
156
+ } ) {
158
157
return await prisma . $transaction ( async ( tx ) => {
159
158
// 1. Delete the invite and get the invite details
160
159
const invite = await tx . orgMemberInvite . delete ( {
161
160
where : {
162
161
id : inviteId ,
163
- organization : {
164
- members : {
165
- some : {
166
- userId,
167
- } ,
168
- } ,
169
- } ,
162
+ email : user . email ,
170
163
} ,
171
164
include : {
172
165
organization : {
@@ -181,7 +174,7 @@ export async function acceptInvite({ userId, inviteId }: { userId: string; invit
181
174
const member = await tx . orgMember . create ( {
182
175
data : {
183
176
organizationId : invite . organizationId ,
184
- userId,
177
+ userId : user . id ,
185
178
role : invite . role ,
186
179
} ,
187
180
} ) ;
@@ -201,57 +194,37 @@ export async function acceptInvite({ userId, inviteId }: { userId: string; invit
201
194
// 4. Check for other invites
202
195
const remainingInvites = await tx . orgMemberInvite . findMany ( {
203
196
where : {
204
- email : invite . email ,
205
- organization : {
206
- members : {
207
- some : {
208
- userId,
209
- } ,
210
- } ,
211
- } ,
197
+ email : user . email ,
212
198
} ,
213
199
} ) ;
214
200
215
201
return { remainingInvites, organization : invite . organization } ;
216
202
} ) ;
217
203
}
218
204
219
- export async function declineInvite ( { userId, inviteId } : { userId : string ; inviteId : string } ) {
205
+ export async function declineInvite ( {
206
+ user,
207
+ inviteId,
208
+ } : {
209
+ user : { id : string ; email : string } ;
210
+ inviteId : string ;
211
+ } ) {
220
212
return await prisma . $transaction ( async ( tx ) => {
221
213
//1. delete invite
222
214
const declinedInvite = await prisma . orgMemberInvite . delete ( {
223
215
where : {
224
216
id : inviteId ,
225
- organization : {
226
- members : {
227
- some : {
228
- userId,
229
- } ,
230
- } ,
231
- } ,
217
+ email : user . email ,
232
218
} ,
233
219
include : {
234
220
organization : true ,
235
221
} ,
236
222
} ) ;
237
223
238
- //2. get email
239
- const user = await prisma . user . findUnique ( {
240
- where : { id : userId } ,
241
- select : { email : true } ,
242
- } ) ;
243
-
244
- //3. check for other invites
224
+ //2. check for other invites
245
225
const remainingInvites = await prisma . orgMemberInvite . findMany ( {
246
226
where : {
247
- email : user ! . email ,
248
- organization : {
249
- members : {
250
- some : {
251
- userId,
252
- } ,
253
- } ,
254
- } ,
227
+ email : user . email ,
255
228
} ,
256
229
} ) ;
257
230
@@ -284,7 +257,7 @@ export async function revokeInvite({
284
257
orgSlug : string ;
285
258
inviteId : string ;
286
259
} ) {
287
- const invite = await prisma . orgMemberInvite . delete ( {
260
+ const invite = await prisma . orgMemberInvite . findFirst ( {
288
261
where : {
289
262
id : inviteId ,
290
263
organization : {
@@ -297,6 +270,7 @@ export async function revokeInvite({
297
270
} ,
298
271
} ,
299
272
select : {
273
+ id : true ,
300
274
email : true ,
301
275
organization : true ,
302
276
} ,
@@ -306,5 +280,11 @@ export async function revokeInvite({
306
280
throw new Error ( "Invite not found" ) ;
307
281
}
308
282
283
+ await prisma . orgMemberInvite . delete ( {
284
+ where : {
285
+ id : invite . id ,
286
+ } ,
287
+ } ) ;
288
+
309
289
return { email : invite . email , organization : invite . organization } ;
310
290
}
0 commit comments