Skip to content

Commit

Permalink
Fix invite
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Feb 10, 2025
1 parent f8a9052 commit 347e0cf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/web-production.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
api-key: ${{ secrets.LANGUINE_API_KEY }}
project-id: ${{ secrets.LANGUINE_PROJECT_ID }}
working-directory: apps/web
cli-version: canary
# cli-version: canary
# create-pull-request: true
dev-mode: true

7 changes: 5 additions & 2 deletions apps/web/src/app/api/invite/[inviteId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function GET(
{ params }: { params: { inviteId: string } },
) {
try {
const { inviteId } = params;
const { inviteId } = await params;
const cookieStore = await cookies();

// Check if user is logged in
Expand All @@ -26,7 +26,10 @@ export async function GET(
const storedInviteId = cookieStore.get("invite-id")?.value;
const inviteIdToUse = storedInviteId || inviteId;

const result = await acceptInvitation(inviteIdToUse);
const result = await acceptInvitation({
invitationId: inviteIdToUse,
userId: session.user.id,
});

if (!result) {
redirect("/login");
Expand Down
24 changes: 13 additions & 11 deletions apps/web/src/db/queries/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,13 @@ export const inviteMember = async ({
return invitation;
};

export const acceptInvitation = async (invitationId: string) => {
export const acceptInvitation = async ({
invitationId,
userId,
}: {
invitationId: string;
userId: string;
}) => {
const db = await connectDb();

// Get the invitation
Expand All @@ -376,10 +382,6 @@ export const acceptInvitation = async (invitationId: string) => {
throw new Error("Invitation not found");
}

if (invitation.status !== "pending") {
throw new Error("Invitation is no longer valid");
}

if (new Date() > invitation.expiresAt) {
throw new Error("Invitation has expired");
}
Expand All @@ -389,18 +391,18 @@ export const acceptInvitation = async (invitationId: string) => {
.insert(members)
.values({
organizationId: invitation.organizationId,
userId: invitation.inviterId,
userId,
role: invitation.role || "member",
})
.returning();

// Update invitation status
await db
.update(invitations)
.set({ status: "accepted" })
.where(eq(invitations.id, invitationId));
await db.delete(invitations).where(eq(invitations.id, invitationId));

return { member, invitation };
return {
member,
invitation,
};
};

export const getOrganizationStats = async (organizationId: string) => {
Expand Down

0 comments on commit 347e0cf

Please sign in to comment.