-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathsend-invites.ts
More file actions
173 lines (151 loc) · 4.24 KB
/
send-invites.ts
File metadata and controls
173 lines (151 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"use server";
import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
import { sendEmail } from "@cap/database/emails/config";
import { OrganizationInvite } from "@cap/database/emails/organization-invite";
import { nanoId } from "@cap/database/helpers";
import {
organizationInvites,
organizationMembers,
organizations,
users,
} from "@cap/database/schema";
import { serverEnv } from "@cap/env";
import type { Organisation } from "@cap/web-domain";
import { and, eq, inArray } from "drizzle-orm";
import { revalidatePath } from "next/cache";
export async function sendOrganizationInvites(
invitedEmails: string[],
organizationId: Organisation.OrganisationId,
) {
const user = await getCurrentUser();
if (!user) {
throw new Error("Unauthorized");
}
const [organization] = await db()
.select()
.from(organizations)
.where(eq(organizations.id, organizationId));
if (!organization) {
throw new Error("Organization not found");
}
const [ownerMembership] = await db()
.select({ id: organizationMembers.id })
.from(organizationMembers)
.where(
and(
eq(organizationMembers.organizationId, organizationId),
eq(organizationMembers.userId, user.id),
eq(organizationMembers.role, "owner"),
),
)
.limit(1);
if (!ownerMembership) {
throw new Error("Only the organization owner can send invites");
}
const MAX_INVITES = 50;
if (invitedEmails.length > MAX_INVITES) {
throw new Error(`Cannot send more than ${MAX_INVITES} invites at once`);
}
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const validEmails = Array.from(
new Set(
invitedEmails
.map((email) => email.trim().toLowerCase())
.filter((email) => emailRegex.test(email)),
),
);
if (validEmails.length === 0) {
return { success: true, failedEmails: [] as string[] };
}
const inviteRecords = await db().transaction(async (tx) => {
const [existingInvites, existingMembers] = await Promise.all([
tx
.select({ invitedEmail: organizationInvites.invitedEmail })
.from(organizationInvites)
.where(
and(
eq(organizationInvites.organizationId, organizationId),
inArray(organizationInvites.invitedEmail, validEmails),
),
),
tx
.select({ email: users.email })
.from(organizationMembers)
.innerJoin(users, eq(organizationMembers.userId, users.id))
.where(
and(
eq(organizationMembers.organizationId, organizationId),
inArray(users.email, validEmails),
),
),
]);
const existingInviteEmails = new Set(
existingInvites.map((i) => i.invitedEmail.toLowerCase()),
);
const existingMemberEmails = new Set(
existingMembers.map((m) => m.email.toLowerCase()),
);
const emailsToInvite = validEmails.filter(
(email) =>
!existingInviteEmails.has(email) && !existingMemberEmails.has(email),
);
const records = emailsToInvite.map((email) => ({
id: nanoId(),
email,
}));
if (records.length > 0) {
await tx.insert(organizationInvites).values(
records.map((r) => ({
id: r.id,
organizationId: organizationId,
invitedEmail: r.email,
invitedByUserId: user.id,
role: "member" as const,
})),
);
}
return records;
});
const emailResults = await Promise.allSettled(
inviteRecords.map((record) => {
const inviteUrl = `${serverEnv().WEB_URL}/invite/${record.id}`;
return sendEmail({
email: record.email,
subject: `Invitation to join ${organization.name} on Cap`,
react: OrganizationInvite({
email: record.email,
url: inviteUrl,
organizationName: organization.name,
}),
});
}),
);
const failedInvites = inviteRecords.filter(
(_, i) => emailResults[i]?.status === "rejected",
);
const failedEmails = failedInvites.map((r) => r.email);
if (failedInvites.length > 0) {
try {
await db()
.delete(organizationInvites)
.where(
inArray(
organizationInvites.id,
failedInvites.map((r) => r.id),
),
);
} catch (cleanupError) {
console.error(
"Failed to clean up invite records after email delivery failure:",
{
failedInviteIds: failedInvites.map((r) => r.id),
failedEmails,
error: cleanupError,
},
);
}
}
revalidatePath("/dashboard/settings/organization");
return { success: true, failedEmails };
}