aboutsummaryrefslogtreecommitdiffstats
path: root/packages/trpc/routers
diff options
context:
space:
mode:
authorMohamed Bassem <me@mbassem.com>2025-09-06 06:24:05 +0000
committerMohamed Bassem <me@mbassem.com>2025-09-06 06:25:15 +0000
commit3760d23abc4d02eb2c3823b8aa322f53914fd9b6 (patch)
tree1d720b1f1d1391317185b319d1fb1fba3f18a274 /packages/trpc/routers
parentfcfe6a53b49dc2fdff6abac876b41b52f1b0fed7 (diff)
downloadkarakeep-3760d23abc4d02eb2c3823b8aa322f53914fd9b6.tar.zst
refactor: Extract quota logic into its own class
Diffstat (limited to 'packages/trpc/routers')
-rw-r--r--packages/trpc/routers/bookmarks.ts33
1 files changed, 12 insertions, 21 deletions
diff --git a/packages/trpc/routers/bookmarks.ts b/packages/trpc/routers/bookmarks.ts
index 298f0961..db9d33fc 100644
--- a/packages/trpc/routers/bookmarks.ts
+++ b/packages/trpc/routers/bookmarks.ts
@@ -1,5 +1,5 @@
import { experimental_trpcMiddleware, TRPCError } from "@trpc/server";
-import { and, count, eq, gt, inArray, lt, or } from "drizzle-orm";
+import { and, eq, gt, inArray, lt, or } from "drizzle-orm";
import { EnqueueOptions } from "liteque";
import invariant from "tiny-invariant";
import { z } from "zod";
@@ -20,8 +20,8 @@ import {
bookmarkTexts,
customPrompts,
tagsOnBookmarks,
- users,
} from "@karakeep/db/schema";
+import { QuotaService } from "@karakeep/shared-server";
import {
deleteAsset,
SUPPORTED_BOOKMARK_ASSET_TYPES,
@@ -273,26 +273,17 @@ export const bookmarksAppRouter = router({
}
// Check user quota
- const user = await ctx.db.query.users.findFirst({
- where: eq(users.id, ctx.user.id),
- columns: {
- bookmarkQuota: true,
- },
- });
-
- if (user?.bookmarkQuota !== null && user?.bookmarkQuota !== undefined) {
- const currentBookmarkCount = await ctx.db
- .select({ count: count() })
- .from(bookmarks)
- .where(eq(bookmarks.userId, ctx.user.id));
-
- if (currentBookmarkCount[0].count >= user.bookmarkQuota) {
- throw new TRPCError({
- code: "FORBIDDEN",
- message: `Bookmark quota exceeded. You can only have ${user.bookmarkQuota} bookmarks.`,
- });
- }
+ const quotaResult = await QuotaService.canCreateBookmark(
+ ctx.db,
+ ctx.user.id,
+ );
+ if (!quotaResult.result) {
+ throw new TRPCError({
+ code: "FORBIDDEN",
+ message: quotaResult.error,
+ });
}
+
const bookmark = await ctx.db.transaction(async (tx) => {
const bookmark = (
await tx