@camox/api 0.3.1 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/routes/projects.ts +119 -2
package/package.json
CHANGED
package/src/routes/projects.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ORPCError } from "@orpc/server";
|
|
2
|
-
import { and, eq } from "drizzle-orm";
|
|
2
|
+
import { and, eq, inArray, or } from "drizzle-orm";
|
|
3
3
|
import { generateKeyBetween } from "fractional-indexing";
|
|
4
4
|
import { z } from "zod";
|
|
5
5
|
|
|
@@ -8,8 +8,11 @@ import { resolveEnvironment } from "../lib/resolve-environment";
|
|
|
8
8
|
import { generateUniqueSlug } from "../lib/slug";
|
|
9
9
|
import { authed, pub } from "../orpc";
|
|
10
10
|
import {
|
|
11
|
+
aiJobs,
|
|
12
|
+
blockDefinitions,
|
|
11
13
|
blocks,
|
|
12
14
|
environments,
|
|
15
|
+
files,
|
|
13
16
|
layouts,
|
|
14
17
|
organizationTable,
|
|
15
18
|
pages,
|
|
@@ -133,9 +136,123 @@ const update = authed
|
|
|
133
136
|
const deleteFn = authed.input(z.object({ id: z.number() })).handler(async ({ context, input }) => {
|
|
134
137
|
const project = await getAuthorizedProject(context.db, input.id, context.user.id);
|
|
135
138
|
if (!project) throw new ORPCError("NOT_FOUND");
|
|
139
|
+
|
|
140
|
+
const projectId = project.id;
|
|
141
|
+
|
|
142
|
+
// Collect IDs needed for cascade deletion
|
|
143
|
+
const pageRows = await context.db
|
|
144
|
+
.select({ id: pages.id })
|
|
145
|
+
.from(pages)
|
|
146
|
+
.where(eq(pages.projectId, projectId));
|
|
147
|
+
const pageIds = pageRows.map((r) => r.id);
|
|
148
|
+
|
|
149
|
+
const layoutRows = await context.db
|
|
150
|
+
.select({ id: layouts.id })
|
|
151
|
+
.from(layouts)
|
|
152
|
+
.where(eq(layouts.projectId, projectId));
|
|
153
|
+
const layoutIds = layoutRows.map((r) => r.id);
|
|
154
|
+
|
|
155
|
+
const blockConditions = [
|
|
156
|
+
...(pageIds.length > 0 ? [inArray(blocks.pageId, pageIds)] : []),
|
|
157
|
+
...(layoutIds.length > 0 ? [inArray(blocks.layoutId, layoutIds)] : []),
|
|
158
|
+
];
|
|
159
|
+
const blockRows =
|
|
160
|
+
blockConditions.length > 0
|
|
161
|
+
? await context.db
|
|
162
|
+
.select({ id: blocks.id })
|
|
163
|
+
.from(blocks)
|
|
164
|
+
.where(or(...blockConditions))
|
|
165
|
+
: [];
|
|
166
|
+
const blockIds = blockRows.map((r) => r.id);
|
|
167
|
+
|
|
168
|
+
const repeatableItemRows =
|
|
169
|
+
blockIds.length > 0
|
|
170
|
+
? await context.db
|
|
171
|
+
.select({ id: repeatableItems.id })
|
|
172
|
+
.from(repeatableItems)
|
|
173
|
+
.where(inArray(repeatableItems.blockId, blockIds))
|
|
174
|
+
: [];
|
|
175
|
+
const repeatableItemIds = repeatableItemRows.map((r) => r.id);
|
|
176
|
+
|
|
177
|
+
const fileRows = await context.db
|
|
178
|
+
.select({ id: files.id, blobId: files.blobId })
|
|
179
|
+
.from(files)
|
|
180
|
+
.where(eq(files.projectId, projectId));
|
|
181
|
+
const fileIds = fileRows.map((r) => r.id);
|
|
182
|
+
|
|
183
|
+
// Delete AI jobs for all collected entities
|
|
184
|
+
const aiJobConditions = [
|
|
185
|
+
...(pageIds.length > 0
|
|
186
|
+
? [
|
|
187
|
+
and(
|
|
188
|
+
eq(aiJobs.entityTable, "pages"),
|
|
189
|
+
inArray(
|
|
190
|
+
aiJobs.entityId,
|
|
191
|
+
pageIds.map((id) => String(id)),
|
|
192
|
+
),
|
|
193
|
+
),
|
|
194
|
+
]
|
|
195
|
+
: []),
|
|
196
|
+
...(blockIds.length > 0
|
|
197
|
+
? [
|
|
198
|
+
and(
|
|
199
|
+
eq(aiJobs.entityTable, "blocks"),
|
|
200
|
+
inArray(
|
|
201
|
+
aiJobs.entityId,
|
|
202
|
+
blockIds.map((id) => String(id)),
|
|
203
|
+
),
|
|
204
|
+
),
|
|
205
|
+
]
|
|
206
|
+
: []),
|
|
207
|
+
...(repeatableItemIds.length > 0
|
|
208
|
+
? [
|
|
209
|
+
and(
|
|
210
|
+
eq(aiJobs.entityTable, "repeatableItems"),
|
|
211
|
+
inArray(
|
|
212
|
+
aiJobs.entityId,
|
|
213
|
+
repeatableItemIds.map((id) => String(id)),
|
|
214
|
+
),
|
|
215
|
+
),
|
|
216
|
+
]
|
|
217
|
+
: []),
|
|
218
|
+
...(fileIds.length > 0
|
|
219
|
+
? [
|
|
220
|
+
and(
|
|
221
|
+
eq(aiJobs.entityTable, "files"),
|
|
222
|
+
inArray(
|
|
223
|
+
aiJobs.entityId,
|
|
224
|
+
fileIds.map((id) => String(id)),
|
|
225
|
+
),
|
|
226
|
+
),
|
|
227
|
+
]
|
|
228
|
+
: []),
|
|
229
|
+
];
|
|
230
|
+
if (aiJobConditions.length > 0) {
|
|
231
|
+
await context.db.delete(aiJobs).where(or(...aiJobConditions));
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Delete in FK-safe order
|
|
235
|
+
if (repeatableItemIds.length > 0) {
|
|
236
|
+
await context.db.delete(repeatableItems).where(inArray(repeatableItems.id, repeatableItemIds));
|
|
237
|
+
}
|
|
238
|
+
if (blockIds.length > 0) {
|
|
239
|
+
await context.db.delete(blocks).where(inArray(blocks.id, blockIds));
|
|
240
|
+
}
|
|
241
|
+
await context.db.delete(pages).where(eq(pages.projectId, projectId));
|
|
242
|
+
|
|
243
|
+
// Delete files from R2 and database
|
|
244
|
+
if (fileRows.length > 0) {
|
|
245
|
+
await Promise.all(fileRows.map((f) => context.env.FILES_BUCKET.delete(f.blobId)));
|
|
246
|
+
await context.db.delete(files).where(eq(files.projectId, projectId));
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
await context.db.delete(layouts).where(eq(layouts.projectId, projectId));
|
|
250
|
+
await context.db.delete(blockDefinitions).where(eq(blockDefinitions.projectId, projectId));
|
|
251
|
+
await context.db.delete(environments).where(eq(environments.projectId, projectId));
|
|
252
|
+
|
|
136
253
|
const result = await context.db
|
|
137
254
|
.delete(projects)
|
|
138
|
-
.where(eq(projects.id,
|
|
255
|
+
.where(eq(projects.id, projectId))
|
|
139
256
|
.returning()
|
|
140
257
|
.get();
|
|
141
258
|
return result;
|