|
| 1 | +import { db } from '@sim/db' |
| 2 | +import { credentialSet, credentialSetInvitation, member, organization, user } from '@sim/db/schema' |
| 3 | +import { createLogger } from '@sim/logger' |
| 4 | +import { and, eq } from 'drizzle-orm' |
| 5 | +import { type NextRequest, NextResponse } from 'next/server' |
| 6 | +import { getEmailSubject, renderPollingGroupInvitationEmail } from '@/components/emails' |
| 7 | +import { getSession } from '@/lib/auth' |
| 8 | +import { getBaseUrl } from '@/lib/core/utils/urls' |
| 9 | +import { sendEmail } from '@/lib/messaging/email/mailer' |
| 10 | + |
| 11 | +const logger = createLogger('CredentialSetInviteResend') |
| 12 | + |
| 13 | +async function getCredentialSetWithAccess(credentialSetId: string, userId: string) { |
| 14 | + const [set] = await db |
| 15 | + .select({ |
| 16 | + id: credentialSet.id, |
| 17 | + organizationId: credentialSet.organizationId, |
| 18 | + name: credentialSet.name, |
| 19 | + providerId: credentialSet.providerId, |
| 20 | + }) |
| 21 | + .from(credentialSet) |
| 22 | + .where(eq(credentialSet.id, credentialSetId)) |
| 23 | + .limit(1) |
| 24 | + |
| 25 | + if (!set) return null |
| 26 | + |
| 27 | + const [membership] = await db |
| 28 | + .select({ role: member.role }) |
| 29 | + .from(member) |
| 30 | + .where(and(eq(member.userId, userId), eq(member.organizationId, set.organizationId))) |
| 31 | + .limit(1) |
| 32 | + |
| 33 | + if (!membership) return null |
| 34 | + |
| 35 | + return { set, role: membership.role } |
| 36 | +} |
| 37 | + |
| 38 | +export async function POST( |
| 39 | + req: NextRequest, |
| 40 | + { params }: { params: Promise<{ id: string; invitationId: string }> } |
| 41 | +) { |
| 42 | + const session = await getSession() |
| 43 | + |
| 44 | + if (!session?.user?.id) { |
| 45 | + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) |
| 46 | + } |
| 47 | + |
| 48 | + const { id, invitationId } = await params |
| 49 | + |
| 50 | + try { |
| 51 | + const result = await getCredentialSetWithAccess(id, session.user.id) |
| 52 | + |
| 53 | + if (!result) { |
| 54 | + return NextResponse.json({ error: 'Credential set not found' }, { status: 404 }) |
| 55 | + } |
| 56 | + |
| 57 | + if (result.role !== 'admin' && result.role !== 'owner') { |
| 58 | + return NextResponse.json({ error: 'Admin or owner permissions required' }, { status: 403 }) |
| 59 | + } |
| 60 | + |
| 61 | + const [invitation] = await db |
| 62 | + .select() |
| 63 | + .from(credentialSetInvitation) |
| 64 | + .where( |
| 65 | + and( |
| 66 | + eq(credentialSetInvitation.id, invitationId), |
| 67 | + eq(credentialSetInvitation.credentialSetId, id) |
| 68 | + ) |
| 69 | + ) |
| 70 | + .limit(1) |
| 71 | + |
| 72 | + if (!invitation) { |
| 73 | + return NextResponse.json({ error: 'Invitation not found' }, { status: 404 }) |
| 74 | + } |
| 75 | + |
| 76 | + if (invitation.status !== 'pending') { |
| 77 | + return NextResponse.json({ error: 'Only pending invitations can be resent' }, { status: 400 }) |
| 78 | + } |
| 79 | + |
| 80 | + // Update expiration |
| 81 | + const newExpiresAt = new Date() |
| 82 | + newExpiresAt.setDate(newExpiresAt.getDate() + 7) |
| 83 | + |
| 84 | + await db |
| 85 | + .update(credentialSetInvitation) |
| 86 | + .set({ expiresAt: newExpiresAt }) |
| 87 | + .where(eq(credentialSetInvitation.id, invitationId)) |
| 88 | + |
| 89 | + const inviteUrl = `${getBaseUrl()}/credential-account/${invitation.token}` |
| 90 | + |
| 91 | + // Send email if email address exists |
| 92 | + if (invitation.email) { |
| 93 | + try { |
| 94 | + const [inviter] = await db |
| 95 | + .select({ name: user.name }) |
| 96 | + .from(user) |
| 97 | + .where(eq(user.id, session.user.id)) |
| 98 | + .limit(1) |
| 99 | + |
| 100 | + const [org] = await db |
| 101 | + .select({ name: organization.name }) |
| 102 | + .from(organization) |
| 103 | + .where(eq(organization.id, result.set.organizationId)) |
| 104 | + .limit(1) |
| 105 | + |
| 106 | + const provider = (result.set.providerId as 'google-email' | 'outlook') || 'google-email' |
| 107 | + const emailHtml = await renderPollingGroupInvitationEmail({ |
| 108 | + inviterName: inviter?.name || 'A team member', |
| 109 | + organizationName: org?.name || 'your organization', |
| 110 | + pollingGroupName: result.set.name, |
| 111 | + provider, |
| 112 | + inviteLink: inviteUrl, |
| 113 | + }) |
| 114 | + |
| 115 | + const emailResult = await sendEmail({ |
| 116 | + to: invitation.email, |
| 117 | + subject: getEmailSubject('polling-group-invitation'), |
| 118 | + html: emailHtml, |
| 119 | + emailType: 'transactional', |
| 120 | + }) |
| 121 | + |
| 122 | + if (!emailResult.success) { |
| 123 | + logger.warn('Failed to resend invitation email', { |
| 124 | + email: invitation.email, |
| 125 | + error: emailResult.message, |
| 126 | + }) |
| 127 | + return NextResponse.json({ error: 'Failed to send email' }, { status: 500 }) |
| 128 | + } |
| 129 | + } catch (emailError) { |
| 130 | + logger.error('Error sending invitation email', emailError) |
| 131 | + return NextResponse.json({ error: 'Failed to send email' }, { status: 500 }) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + logger.info('Resent credential set invitation', { |
| 136 | + credentialSetId: id, |
| 137 | + invitationId, |
| 138 | + userId: session.user.id, |
| 139 | + }) |
| 140 | + |
| 141 | + return NextResponse.json({ success: true }) |
| 142 | + } catch (error) { |
| 143 | + logger.error('Error resending invitation', error) |
| 144 | + return NextResponse.json({ error: 'Failed to resend invitation' }, { status: 500 }) |
| 145 | + } |
| 146 | +} |
0 commit comments