Skip to content

Commit 02d3e02

Browse files
committed
Fix invite flow changes
1 parent 61b7219 commit 02d3e02

File tree

3 files changed

+34
-54
lines changed

3 files changed

+34
-54
lines changed

apps/webapp/app/models/member.server.ts

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

apps/webapp/app/routes/invite-accept.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
2424
});
2525
}
2626

27-
const invite = await getInviteFromToken({ token, userId: user.id });
27+
const invite = await getInviteFromToken({ token });
2828
if (!invite) {
2929
return redirectWithErrorMessage(
3030
"/",

apps/webapp/app/routes/invites.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { conform, useForm } from "@conform-to/react";
22
import { parse } from "@conform-to/zod";
3-
import { ActionFunction, LoaderFunctionArgs, json, redirect } from "@remix-run/node";
3+
import { type ActionFunction, type LoaderFunctionArgs, json, redirect } from "@remix-run/node";
44
import { Form, useActionData } from "@remix-run/react";
55
import { typedjson, useTypedLoaderData } from "remix-typedjson";
66
import { z } from "zod";
@@ -36,7 +36,7 @@ const schema = z.object({
3636
});
3737

3838
export const action: ActionFunction = async ({ request }) => {
39-
const userId = await requireUserId(request);
39+
const user = await requireUser(request);
4040

4141
const formData = await request.formData();
4242
const submission = parse(formData, { schema });
@@ -49,7 +49,7 @@ export const action: ActionFunction = async ({ request }) => {
4949
if (submission.intent === "accept") {
5050
const { remainingInvites, organization } = await acceptInvite({
5151
inviteId: submission.value.inviteId,
52-
userId,
52+
user: { id: user.id, email: user.email },
5353
});
5454

5555
if (remainingInvites.length === 0) {
@@ -64,7 +64,7 @@ export const action: ActionFunction = async ({ request }) => {
6464
} else if (submission.intent === "decline") {
6565
const { remainingInvites, organization } = await declineInvite({
6666
inviteId: submission.value.inviteId,
67-
userId,
67+
user: { id: user.id, email: user.email },
6868
});
6969
if (remainingInvites.length === 0) {
7070
return redirectWithSuccessMessage(

0 commit comments

Comments
 (0)