@@ -120,17 +120,10 @@ export async function inviteMembers({
120120 } ) ;
121121}
122122
123- export async function getInviteFromToken ( { token, userId } : { token : string ; userId : string } ) {
123+ export async function getInviteFromToken ( { token } : { token : string } ) {
124124 return await prisma . orgMemberInvite . findFirst ( {
125125 where : {
126126 token,
127- organization : {
128- members : {
129- some : {
130- userId,
131- } ,
132- } ,
133- } ,
134127 } ,
135128 include : {
136129 organization : true ,
@@ -154,19 +147,19 @@ export async function getUsersInvites({ email }: { email: string }) {
154147 } ) ;
155148}
156149
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+ } ) {
158157 return await prisma . $transaction ( async ( tx ) => {
159158 // 1. Delete the invite and get the invite details
160159 const invite = await tx . orgMemberInvite . delete ( {
161160 where : {
162161 id : inviteId ,
163- organization : {
164- members : {
165- some : {
166- userId,
167- } ,
168- } ,
169- } ,
162+ email : user . email ,
170163 } ,
171164 include : {
172165 organization : {
@@ -181,7 +174,7 @@ export async function acceptInvite({ userId, inviteId }: { userId: string; invit
181174 const member = await tx . orgMember . create ( {
182175 data : {
183176 organizationId : invite . organizationId ,
184- userId,
177+ userId : user . id ,
185178 role : invite . role ,
186179 } ,
187180 } ) ;
@@ -201,57 +194,37 @@ export async function acceptInvite({ userId, inviteId }: { userId: string; invit
201194 // 4. Check for other invites
202195 const remainingInvites = await tx . orgMemberInvite . findMany ( {
203196 where : {
204- email : invite . email ,
205- organization : {
206- members : {
207- some : {
208- userId,
209- } ,
210- } ,
211- } ,
197+ email : user . email ,
212198 } ,
213199 } ) ;
214200
215201 return { remainingInvites, organization : invite . organization } ;
216202 } ) ;
217203}
218204
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+ } ) {
220212 return await prisma . $transaction ( async ( tx ) => {
221213 //1. delete invite
222214 const declinedInvite = await prisma . orgMemberInvite . delete ( {
223215 where : {
224216 id : inviteId ,
225- organization : {
226- members : {
227- some : {
228- userId,
229- } ,
230- } ,
231- } ,
217+ email : user . email ,
232218 } ,
233219 include : {
234220 organization : true ,
235221 } ,
236222 } ) ;
237223
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
245225 const remainingInvites = await prisma . orgMemberInvite . findMany ( {
246226 where : {
247- email : user ! . email ,
248- organization : {
249- members : {
250- some : {
251- userId,
252- } ,
253- } ,
254- } ,
227+ email : user . email ,
255228 } ,
256229 } ) ;
257230
@@ -284,7 +257,7 @@ export async function revokeInvite({
284257 orgSlug : string ;
285258 inviteId : string ;
286259} ) {
287- const invite = await prisma . orgMemberInvite . delete ( {
260+ const invite = await prisma . orgMemberInvite . findFirst ( {
288261 where : {
289262 id : inviteId ,
290263 organization : {
@@ -297,6 +270,7 @@ export async function revokeInvite({
297270 } ,
298271 } ,
299272 select : {
273+ id : true ,
300274 email : true ,
301275 organization : true ,
302276 } ,
@@ -306,5 +280,11 @@ export async function revokeInvite({
306280 throw new Error ( "Invite not found" ) ;
307281 }
308282
283+ await prisma . orgMemberInvite . delete ( {
284+ where : {
285+ id : invite . id ,
286+ } ,
287+ } ) ;
288+
309289 return { email : invite . email , organization : invite . organization } ;
310290}
0 commit comments