aboutsummaryrefslogtreecommitdiffstats
path: root/packages/trpc/lib
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/lib
parentfcfe6a53b49dc2fdff6abac876b41b52f1b0fed7 (diff)
downloadkarakeep-3760d23abc4d02eb2c3823b8aa322f53914fd9b6.tar.zst
refactor: Extract quota logic into its own class
Diffstat (limited to 'packages/trpc/lib')
-rw-r--r--packages/trpc/lib/storageQuota.ts57
1 files changed, 0 insertions, 57 deletions
diff --git a/packages/trpc/lib/storageQuota.ts b/packages/trpc/lib/storageQuota.ts
deleted file mode 100644
index 49b96af8..00000000
--- a/packages/trpc/lib/storageQuota.ts
+++ /dev/null
@@ -1,57 +0,0 @@
-import { eq, sum } from "drizzle-orm";
-
-import type { DB, KarakeepDBTransaction } from "@karakeep/db";
-import { assets, users } from "@karakeep/db/schema";
-import { QuotaApproved } from "@karakeep/shared/storageQuota";
-
-export class StorageQuotaError extends Error {
- constructor(
- public readonly currentUsage: number,
- public readonly quota: number,
- public readonly requestedSize: number,
- ) {
- super(
- `Storage quota exceeded. Current usage: ${Math.round(currentUsage / 1024 / 1024)}MB, Quota: ${Math.round(quota / 1024 / 1024)}MB, Requested: ${Math.round(requestedSize / 1024 / 1024)}MB`,
- );
- this.name = "StorageQuotaError";
- }
-}
-
-export async function checkStorageQuota(
- db: DB | KarakeepDBTransaction,
- userId: string,
- requestedSize: number,
-): Promise<QuotaApproved> {
- const user = await db.query.users.findFirst({
- where: eq(users.id, userId),
- columns: {
- storageQuota: true,
- },
- });
-
- if (user?.storageQuota === null || user?.storageQuota === undefined) {
- // No quota limit - approve the request
- return QuotaApproved._create(userId, requestedSize);
- }
-
- const currentUsage = await getCurrentStorageUsage(db, userId);
-
- if (currentUsage + requestedSize > user.storageQuota) {
- throw new StorageQuotaError(currentUsage, user.storageQuota, requestedSize);
- }
-
- // Quota check passed - return approval token
- return QuotaApproved._create(userId, requestedSize);
-}
-
-export async function getCurrentStorageUsage(
- db: DB | KarakeepDBTransaction,
- userId: string,
-): Promise<number> {
- const currentUsageResult = await db
- .select({ totalSize: sum(assets.size) })
- .from(assets)
- .where(eq(assets.userId, userId));
-
- return Number(currentUsageResult[0]?.totalSize ?? 0);
-}