-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathcreate-space.ts
More file actions
210 lines (185 loc) · 5.35 KB
/
create-space.ts
File metadata and controls
210 lines (185 loc) · 5.35 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"use server";
import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
import { nanoId, nanoIdLength } from "@cap/database/helpers";
import { spaceMembers, spaces, users } from "@cap/database/schema";
import { serverEnv } from "@cap/env";
import { S3Buckets } from "@cap/web-backend";
import { Space } from "@cap/web-domain";
import { and, eq, inArray } from "drizzle-orm";
import { Effect, Option } from "effect";
import { revalidatePath } from "next/cache";
import { v4 as uuidv4 } from "uuid";
import { runPromise } from "@/lib/server";
interface CreateSpaceResponse {
success: boolean;
spaceId?: string;
name?: string;
iconUrl?: string | null;
error?: string;
}
export async function createSpace(
formData: FormData,
): Promise<CreateSpaceResponse> {
try {
const user = await getCurrentUser();
if (!user || !user.activeOrganizationId) {
return {
success: false,
error: "User not logged in or no active organization",
};
}
const name = formData.get("name") as string;
if (!name) {
return {
success: false,
error: "Space name is required",
};
}
// Check for duplicate space name in the same organization
const existingSpace = await db()
.select({ id: spaces.id })
.from(spaces)
.where(
and(
eq(spaces.organizationId, user.activeOrganizationId),
eq(spaces.name, name),
),
)
.limit(1);
if (existingSpace.length > 0) {
return {
success: false,
error: "A space with this name already exists.",
};
}
// Generate the space ID early so we can use it in the file path
const spaceId = Space.SpaceId.make(nanoId());
const iconFile = formData.get("icon") as File | null;
let iconUrl = null;
if (iconFile) {
// Validate file type
if (!iconFile.type.startsWith("image/")) {
return {
success: false,
error: "File must be an image",
};
}
// Validate file size (limit to 2MB)
if (iconFile.size > 2 * 1024 * 1024) {
return {
success: false,
error: "File size must be less than 2MB",
};
}
try {
// Create a unique file key
const fileExtension = iconFile.name.split(".").pop();
const fileKey = `organizations/${
user.activeOrganizationId
}/spaces/${spaceId}/icon-${Date.now()}.${fileExtension}`;
await Effect.gen(function* () {
const [bucket] = yield* S3Buckets.getBucketAccess(Option.none());
yield* bucket.putObject(
fileKey,
yield* Effect.promise(() => iconFile.bytes()),
{ contentType: iconFile.type },
);
// Construct the icon URL
if (serverEnv().CAP_AWS_BUCKET_URL) {
// If a custom bucket URL is defined, use it
iconUrl = `${serverEnv().CAP_AWS_BUCKET_URL}/${fileKey}`;
} else if (serverEnv().CAP_AWS_ENDPOINT) {
// For custom endpoints like MinIO
iconUrl = `${serverEnv().CAP_AWS_ENDPOINT}/${bucket.bucketName}/${fileKey}`;
} else {
// Default AWS S3 URL format
iconUrl = `https://${bucket.bucketName}.s3.${
serverEnv().CAP_AWS_REGION || "us-east-1"
}.amazonaws.com/${fileKey}`;
}
}).pipe(runPromise);
} catch (error) {
console.error("Error uploading space icon:", error);
return {
success: false,
error: "Failed to upload space icon",
};
}
}
await db()
.insert(spaces)
.values({
id: spaceId,
name,
organizationId: user.activeOrganizationId,
createdById: user.id,
iconUrl,
description: iconUrl ? `Space with custom icon: ${iconUrl}` : null,
createdAt: new Date(),
updatedAt: new Date(),
});
// --- Member Management Logic ---
// Collect member emails from formData
const members: string[] = [];
for (const entry of formData.getAll("members[]")) {
if (typeof entry === "string" && entry.length > 0) {
members.push(entry);
}
}
// Always add the creator as Owner (if not already in the list)
const memberEmailsSet = new Set(members.map((e) => e.toLowerCase()));
const creatorEmail = user.email.toLowerCase();
if (!memberEmailsSet.has(creatorEmail)) {
members.push(user.email);
}
// Look up user IDs for each email
if (members.length > 0) {
// Fetch all users with these emails
const usersFound = await db()
.select({ id: users.id, email: users.email })
.from(users)
.where(inArray(users.email, members));
// Map email to userId
const emailToUserId = Object.fromEntries(
usersFound.map((u) => [u.email.toLowerCase(), u.id]),
);
// Prepare spaceMembers insertions
const spaceMembersToInsert = members
.map((email) => {
const userId = emailToUserId[email.toLowerCase()];
if (!userId) return null;
// Creator is always Owner, others are Member
const role =
email.toLowerCase() === creatorEmail
? ("Admin" as const)
: ("member" as const);
return {
id: uuidv4().substring(0, nanoIdLength),
spaceId,
userId,
role,
createdAt: new Date(),
updatedAt: new Date(),
};
})
.filter((v): v is NonNullable<typeof v> => Boolean(v));
if (spaceMembersToInsert.length > 0) {
await db().insert(spaceMembers).values(spaceMembersToInsert);
}
}
revalidatePath("/dashboard");
return {
success: true,
spaceId,
name,
iconUrl,
};
} catch (error) {
console.error("Error creating space:", error);
return {
success: false,
error: "Failed to create space",
};
}
}