diff options
| author | Dhravya Shah <[email protected]> | 2024-07-25 17:35:15 -0500 |
|---|---|---|
| committer | Dhravya Shah <[email protected]> | 2024-07-25 17:35:15 -0500 |
| commit | c57719446ae95c2bbd432d7b2b6648a23b35c351 (patch) | |
| tree | c6f7aca777c7f4748cc6dc335fe56fba8725af02 /apps/web/app/api/store/route.ts | |
| parent | add try catch in api/add for better error handling (diff) | |
| parent | ughh, regenerated migrations. my bad. (diff) | |
| download | supermemory-kush/experimental-thread.tar.xz supermemory-kush/experimental-thread.zip | |
solve merge conflictskush/experimental-thread
Diffstat (limited to 'apps/web/app/api/store/route.ts')
| -rw-r--r-- | apps/web/app/api/store/route.ts | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/apps/web/app/api/store/route.ts b/apps/web/app/api/store/route.ts index f9ab7c01..992c2a0e 100644 --- a/apps/web/app/api/store/route.ts +++ b/apps/web/app/api/store/route.ts @@ -4,7 +4,7 @@ import { ensureAuth } from "../ensureAuth"; import { z } from "zod"; import { db } from "@/server/db"; import { contentToSpace, space, storedContent } from "@/server/db/schema"; -import { and, eq, inArray } from "drizzle-orm"; +import { and, eq, gt, inArray, sql } from "drizzle-orm"; import { LIMITS } from "@/lib/constants"; import { limit } from "@/app/actions/doers"; @@ -22,6 +22,29 @@ const createMemoryFromAPI = async (input: { }; } + // Get number of items saved in the last 2 hours + const last2Hours = new Date(Date.now() - 2 * 60 * 60 * 1000); + + const numberOfItemsSavedInLast2Hours = await db + .select({ + count: sql<number>`count(*)`.mapWith(Number), + }) + .from(storedContent) + .where( + and( + gt(storedContent.savedAt, last2Hours), + eq(storedContent.userId, input.userId), + ), + ); + + if (numberOfItemsSavedInLast2Hours[0]!.count >= 20) { + return { + success: false, + data: 0, + error: `You have exceeded the limit`, + }; + } + const vectorSaveResponse = await fetch( `${process.env.BACKEND_BASE_URL}/api/add`, { |