|
| 1 | +'use server'; |
| 2 | + |
| 3 | +import { auth } from '@/utils/auth'; |
| 4 | +import { sendInviteMemberEmail } from '@comp/email/lib/invite-member'; |
| 5 | +import { db } from '@db'; |
| 6 | +import { headers } from 'next/headers'; |
| 7 | + |
| 8 | +export const sendInvitationEmailToExistingMember = async ({ |
| 9 | + email, |
| 10 | + organizationId, |
| 11 | + roles, |
| 12 | +}: { |
| 13 | + email: string; |
| 14 | + organizationId: string; |
| 15 | + roles: string[]; |
| 16 | +}) => { |
| 17 | + try { |
| 18 | + const session = await auth.api.getSession({ headers: await headers() }); |
| 19 | + if (!session?.session) { |
| 20 | + throw new Error('Authentication required.'); |
| 21 | + } |
| 22 | + |
| 23 | + const currentUserId = session.session.userId; |
| 24 | + const currentUserMember = await db.member.findFirst({ |
| 25 | + where: { |
| 26 | + organizationId: organizationId, |
| 27 | + userId: currentUserId, |
| 28 | + deactivated: false, |
| 29 | + }, |
| 30 | + }); |
| 31 | + |
| 32 | + if ( |
| 33 | + !currentUserMember || |
| 34 | + (!currentUserMember.role.includes('admin') && !currentUserMember.role.includes('owner')) |
| 35 | + ) { |
| 36 | + throw new Error("You don't have permission to send invitations."); |
| 37 | + } |
| 38 | + |
| 39 | + // Get organization name |
| 40 | + const organization = await db.organization.findUnique({ |
| 41 | + where: { id: organizationId }, |
| 42 | + select: { name: true }, |
| 43 | + }); |
| 44 | + |
| 45 | + if (!organization) { |
| 46 | + throw new Error('Organization not found.'); |
| 47 | + } |
| 48 | + |
| 49 | + // Generate invitation using Better Auth |
| 50 | + // Note: This might fail if member already exists, so we'll create invitation manually |
| 51 | + const invitation = await db.invitation.create({ |
| 52 | + data: { |
| 53 | + email: email.toLowerCase(), |
| 54 | + organizationId, |
| 55 | + role: roles.length === 1 ? roles[0] : roles.join(','), |
| 56 | + status: 'pending', |
| 57 | + expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), // 7 days |
| 58 | + inviterId: currentUserId, |
| 59 | + }, |
| 60 | + }); |
| 61 | + |
| 62 | + // Generate invite link |
| 63 | + const isLocalhost = process.env.NODE_ENV === 'development'; |
| 64 | + const protocol = isLocalhost ? 'http' : 'https'; |
| 65 | + |
| 66 | + const betterAuthUrl = process.env.NEXT_PUBLIC_BETTER_AUTH_URL; |
| 67 | + const isDevEnv = betterAuthUrl?.includes('dev.trycomp.ai'); |
| 68 | + const isProdEnv = betterAuthUrl?.includes('app.trycomp.ai'); |
| 69 | + |
| 70 | + const domain = isDevEnv ? 'dev.trycomp.ai' : isProdEnv ? 'app.trycomp.ai' : 'localhost:3000'; |
| 71 | + const inviteLink = `${protocol}://${domain}/invite/${invitation.id}`; |
| 72 | + |
| 73 | + // Send the invitation email |
| 74 | + await sendInviteMemberEmail({ |
| 75 | + inviteeEmail: email.toLowerCase(), |
| 76 | + inviteLink, |
| 77 | + organizationName: organization.name, |
| 78 | + }); |
| 79 | + |
| 80 | + return { success: true }; |
| 81 | + } catch (error) { |
| 82 | + console.error('Error sending invitation email:', error); |
| 83 | + throw error; |
| 84 | + } |
| 85 | +}; |
0 commit comments