@@ -147,12 +147,19 @@ export async function getUsersInvites({ email }: { email: string }) {
147
147
} ) ;
148
148
}
149
149
150
- 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
+ } ) {
151
157
return await prisma . $transaction ( async ( tx ) => {
152
158
// 1. Delete the invite and get the invite details
153
159
const invite = await tx . orgMemberInvite . delete ( {
154
160
where : {
155
161
id : inviteId ,
162
+ email : user . email ,
156
163
} ,
157
164
include : {
158
165
organization : {
@@ -167,7 +174,7 @@ export async function acceptInvite({ userId, inviteId }: { userId: string; invit
167
174
const member = await tx . orgMember . create ( {
168
175
data : {
169
176
organizationId : invite . organizationId ,
170
- userId,
177
+ userId : user . id ,
171
178
role : invite . role ,
172
179
} ,
173
180
} ) ;
@@ -187,47 +194,49 @@ export async function acceptInvite({ userId, inviteId }: { userId: string; invit
187
194
// 4. Check for other invites
188
195
const remainingInvites = await tx . orgMemberInvite . findMany ( {
189
196
where : {
190
- email : invite . email ,
197
+ email : user . email ,
191
198
} ,
192
199
} ) ;
193
200
194
201
return { remainingInvites, organization : invite . organization } ;
195
202
} ) ;
196
203
}
197
204
198
- 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
+ } ) {
199
212
return await prisma . $transaction ( async ( tx ) => {
200
213
//1. delete invite
201
214
const declinedInvite = await prisma . orgMemberInvite . delete ( {
202
215
where : {
203
216
id : inviteId ,
217
+ email : user . email ,
204
218
} ,
205
219
include : {
206
220
organization : true ,
207
221
} ,
208
222
} ) ;
209
223
210
- //2. get email
211
- const user = await prisma . user . findUnique ( {
212
- where : { id : userId } ,
213
- select : { email : true } ,
214
- } ) ;
215
-
216
- //3. check for other invites
224
+ //2. check for other invites
217
225
const remainingInvites = await prisma . orgMemberInvite . findMany ( {
218
226
where : {
219
- email : user ! . email ,
227
+ email : user . email ,
220
228
} ,
221
229
} ) ;
222
230
223
231
return { remainingInvites, organization : declinedInvite . organization } ;
224
232
} ) ;
225
233
}
226
234
227
- export async function resendInvite ( { inviteId } : { inviteId : string } ) {
235
+ export async function resendInvite ( { inviteId, userId } : { inviteId : string ; userId : string } ) {
228
236
return await prisma . orgMemberInvite . update ( {
229
237
where : {
230
238
id : inviteId ,
239
+ inviterId : userId ,
231
240
} ,
232
241
data : {
233
242
updatedAt : new Date ( ) ,
@@ -241,26 +250,27 @@ export async function resendInvite({ inviteId }: { inviteId: string }) {
241
250
242
251
export async function revokeInvite ( {
243
252
userId,
244
- slug ,
253
+ orgSlug ,
245
254
inviteId,
246
255
} : {
247
256
userId : string ;
248
- slug : string ;
257
+ orgSlug : string ;
249
258
inviteId : string ;
250
259
} ) {
251
- const org = await prisma . organization . findFirst ( {
252
- where : { slug, members : { some : { userId } } } ,
253
- } ) ;
254
-
255
- if ( ! org ) {
256
- throw new Error ( "User does not have access to this organization" ) ;
257
- }
258
- const invite = await prisma . orgMemberInvite . delete ( {
260
+ const invite = await prisma . orgMemberInvite . findFirst ( {
259
261
where : {
260
262
id : inviteId ,
261
- organizationId : org . id ,
263
+ organization : {
264
+ slug : orgSlug ,
265
+ members : {
266
+ some : {
267
+ userId,
268
+ } ,
269
+ } ,
270
+ } ,
262
271
} ,
263
272
select : {
273
+ id : true ,
264
274
email : true ,
265
275
organization : true ,
266
276
} ,
@@ -270,5 +280,11 @@ export async function revokeInvite({
270
280
throw new Error ( "Invite not found" ) ;
271
281
}
272
282
283
+ await prisma . orgMemberInvite . delete ( {
284
+ where : {
285
+ id : invite . id ,
286
+ } ,
287
+ } ) ;
288
+
273
289
return { email : invite . email , organization : invite . organization } ;
274
290
}
0 commit comments