1- import { prisma } from "~/db.server" ;
1+ import { type Prisma , prisma } from "~/db.server" ;
22import { createEnvironment } from "./organization.server" ;
3+ import { customAlphabet } from "nanoid" ;
4+
5+ const tokenValueLength = 40 ;
6+ const tokenGenerator = customAlphabet ( "123456789abcdefghijkmnopqrstuvwxyz" , tokenValueLength ) ;
37
48export async function getTeamMembersAndInvites ( {
59 userId,
@@ -95,14 +99,19 @@ export async function inviteMembers({
9599 throw new Error ( "User does not have access to this organization" ) ;
96100 }
97101
98- const created = await prisma . orgMemberInvite . createMany ( {
99- data : emails . map ( ( email ) => ( {
100- email,
101- organizationId : org . id ,
102- inviterId : userId ,
103- role : "MEMBER" ,
104- } ) ) ,
105- skipDuplicates : true ,
102+ const invites = [ ...new Set ( emails ) ] . map (
103+ ( email ) =>
104+ ( {
105+ email,
106+ token : tokenGenerator ( ) ,
107+ organizationId : org . id ,
108+ inviterId : userId ,
109+ role : "MEMBER" ,
110+ } satisfies Prisma . OrgMemberInviteCreateManyInput )
111+ ) ;
112+
113+ await prisma . orgMemberInvite . createMany ( {
114+ data : invites ,
106115 } ) ;
107116
108117 return await prisma . orgMemberInvite . findMany ( {
@@ -147,12 +156,19 @@ export async function getUsersInvites({ email }: { email: string }) {
147156 } ) ;
148157}
149158
150- export async function acceptInvite ( { userId, inviteId } : { userId : string ; inviteId : string } ) {
159+ export async function acceptInvite ( {
160+ user,
161+ inviteId,
162+ } : {
163+ user : { id : string ; email : string } ;
164+ inviteId : string ;
165+ } ) {
151166 return await prisma . $transaction ( async ( tx ) => {
152167 // 1. Delete the invite and get the invite details
153168 const invite = await tx . orgMemberInvite . delete ( {
154169 where : {
155170 id : inviteId ,
171+ email : user . email ,
156172 } ,
157173 include : {
158174 organization : {
@@ -167,7 +183,7 @@ export async function acceptInvite({ userId, inviteId }: { userId: string; invit
167183 const member = await tx . orgMember . create ( {
168184 data : {
169185 organizationId : invite . organizationId ,
170- userId,
186+ userId : user . id ,
171187 role : invite . role ,
172188 } ,
173189 } ) ;
@@ -187,47 +203,49 @@ export async function acceptInvite({ userId, inviteId }: { userId: string; invit
187203 // 4. Check for other invites
188204 const remainingInvites = await tx . orgMemberInvite . findMany ( {
189205 where : {
190- email : invite . email ,
206+ email : user . email ,
191207 } ,
192208 } ) ;
193209
194210 return { remainingInvites, organization : invite . organization } ;
195211 } ) ;
196212}
197213
198- export async function declineInvite ( { userId, inviteId } : { userId : string ; inviteId : string } ) {
214+ export async function declineInvite ( {
215+ user,
216+ inviteId,
217+ } : {
218+ user : { id : string ; email : string } ;
219+ inviteId : string ;
220+ } ) {
199221 return await prisma . $transaction ( async ( tx ) => {
200222 //1. delete invite
201223 const declinedInvite = await prisma . orgMemberInvite . delete ( {
202224 where : {
203225 id : inviteId ,
226+ email : user . email ,
204227 } ,
205228 include : {
206229 organization : true ,
207230 } ,
208231 } ) ;
209232
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
233+ //2. check for other invites
217234 const remainingInvites = await prisma . orgMemberInvite . findMany ( {
218235 where : {
219- email : user ! . email ,
236+ email : user . email ,
220237 } ,
221238 } ) ;
222239
223240 return { remainingInvites, organization : declinedInvite . organization } ;
224241 } ) ;
225242}
226243
227- export async function resendInvite ( { inviteId } : { inviteId : string } ) {
244+ export async function resendInvite ( { inviteId, userId } : { inviteId : string ; userId : string } ) {
228245 return await prisma . orgMemberInvite . update ( {
229246 where : {
230247 id : inviteId ,
248+ inviterId : userId ,
231249 } ,
232250 data : {
233251 updatedAt : new Date ( ) ,
@@ -241,26 +259,27 @@ export async function resendInvite({ inviteId }: { inviteId: string }) {
241259
242260export async function revokeInvite ( {
243261 userId,
244- slug ,
262+ orgSlug ,
245263 inviteId,
246264} : {
247265 userId : string ;
248- slug : string ;
266+ orgSlug : string ;
249267 inviteId : string ;
250268} ) {
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 ( {
269+ const invite = await prisma . orgMemberInvite . findFirst ( {
259270 where : {
260271 id : inviteId ,
261- organizationId : org . id ,
272+ organization : {
273+ slug : orgSlug ,
274+ members : {
275+ some : {
276+ userId,
277+ } ,
278+ } ,
279+ } ,
262280 } ,
263281 select : {
282+ id : true ,
264283 email : true ,
265284 organization : true ,
266285 } ,
@@ -270,5 +289,11 @@ export async function revokeInvite({
270289 throw new Error ( "Invite not found" ) ;
271290 }
272291
292+ await prisma . orgMemberInvite . delete ( {
293+ where : {
294+ id : invite . id ,
295+ } ,
296+ } ) ;
297+
273298 return { email : invite . email , organization : invite . organization } ;
274299}
0 commit comments