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
|
import { TRPCError } from "@trpc/server";
import { and, count, eq } from "drizzle-orm";
import { z } from "zod";
import {
bookmarkLinks,
bookmarks,
importSessionBookmarks,
importSessions,
} from "@karakeep/db/schema";
import {
zCreateImportSessionRequestSchema,
ZImportSession,
ZImportSessionWithStats,
} from "@karakeep/shared/types/importSessions";
import type { AuthedContext } from "../index";
export class ImportSession {
protected constructor(
protected ctx: AuthedContext,
public session: ZImportSession,
) {}
static async fromId(
ctx: AuthedContext,
importSessionId: string,
): Promise<ImportSession> {
const session = await ctx.db.query.importSessions.findFirst({
where: and(
eq(importSessions.id, importSessionId),
eq(importSessions.userId, ctx.user.id),
),
});
if (!session) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Import session not found",
});
}
return new ImportSession(ctx, session);
}
static async create(
ctx: AuthedContext,
input: z.infer<typeof zCreateImportSessionRequestSchema>,
): Promise<ImportSession> {
const [session] = await ctx.db
.insert(importSessions)
.values({
name: input.name,
userId: ctx.user.id,
rootListId: input.rootListId,
})
.returning();
return new ImportSession(ctx, session);
}
static async getAll(ctx: AuthedContext): Promise<ImportSession[]> {
const sessions = await ctx.db.query.importSessions.findMany({
where: eq(importSessions.userId, ctx.user.id),
orderBy: (importSessions, { desc }) => [desc(importSessions.createdAt)],
limit: 50,
});
return sessions.map((session) => new ImportSession(ctx, session));
}
static async getAllWithStats(
ctx: AuthedContext,
): Promise<ZImportSessionWithStats[]> {
const sessions = await this.getAll(ctx);
return await Promise.all(
sessions.map(async (session) => {
return await session.getWithStats();
}),
);
}
async attachBookmark(bookmarkId: string): Promise<void> {
await this.ctx.db.insert(importSessionBookmarks).values({
importSessionId: this.session.id,
bookmarkId,
});
}
async getWithStats(): Promise<ZImportSessionWithStats> {
// Get bookmark counts by status
const statusCounts = await this.ctx.db
.select({
crawlStatus: bookmarkLinks.crawlStatus,
taggingStatus: bookmarks.taggingStatus,
count: count(),
})
.from(importSessionBookmarks)
.innerJoin(
importSessions,
eq(importSessions.id, importSessionBookmarks.importSessionId),
)
.leftJoin(bookmarks, eq(bookmarks.id, importSessionBookmarks.bookmarkId))
.leftJoin(
bookmarkLinks,
eq(bookmarkLinks.id, importSessionBookmarks.bookmarkId),
)
.where(
and(
eq(importSessionBookmarks.importSessionId, this.session.id),
eq(importSessions.userId, this.ctx.user.id),
),
)
.groupBy(bookmarkLinks.crawlStatus, bookmarks.taggingStatus);
const stats = {
totalBookmarks: 0,
completedBookmarks: 0,
failedBookmarks: 0,
pendingBookmarks: 0,
processingBookmarks: 0,
};
statusCounts.forEach((statusCount) => {
const { crawlStatus, taggingStatus, count } = statusCount;
stats.totalBookmarks += count;
const isCrawlFailure = crawlStatus === "failure";
const isTagFailure = taggingStatus === "failure";
if (isCrawlFailure || isTagFailure) {
stats.failedBookmarks += count;
return;
}
const isCrawlPending = crawlStatus === "pending";
const isTagPending = taggingStatus === "pending";
if (isCrawlPending || isTagPending) {
stats.pendingBookmarks += count;
return;
}
const isCrawlSuccessfulOrNotRequired =
crawlStatus === "success" || crawlStatus === null;
const isTagSuccessfulOrUnknown =
taggingStatus === "success" || taggingStatus === null;
if (isCrawlSuccessfulOrNotRequired && isTagSuccessfulOrUnknown) {
stats.completedBookmarks += count;
} else {
// Fallback to pending to avoid leaving imports unclassified
stats.pendingBookmarks += count;
}
});
return {
...this.session,
status: stats.pendingBookmarks > 0 ? "in_progress" : "completed",
...stats,
};
}
async delete(): Promise<void> {
// Delete the session (cascade will handle the bookmarks)
const result = await this.ctx.db
.delete(importSessions)
.where(
and(
eq(importSessions.id, this.session.id),
eq(importSessions.userId, this.ctx.user.id),
),
);
if (result.changes === 0) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Import session not found",
});
}
}
}
|