From 3aa349f0df3ff56d1917680e234194bb228eb379 Mon Sep 17 00:00:00 2001 From: Gorbachev Egor <7gorbachevm@gmail.com> Date: Fri, 5 Jan 2024 19:14:47 +0700 Subject: [PATCH] f --- .../get-deck-access-by-share-id-db.ts | 3 ++- functions/get-shared-deck.test.ts | 22 +++++++++++++++++++ functions/get-shared-deck.ts | 21 +++++++++--------- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/functions/db/deck-access/get-deck-access-by-share-id-db.ts b/functions/db/deck-access/get-deck-access-by-share-id-db.ts index 38e60cd8..758a0729 100644 --- a/functions/db/deck-access/get-deck-access-by-share-id-db.ts +++ b/functions/db/deck-access/get-deck-access-by-share-id-db.ts @@ -7,6 +7,7 @@ const resultSchema = z.object({ deck_id: z.number(), author_id: z.number(), used_by: z.number().nullable(), + processed_at: z.string().nullable(), }); type GetDeckAccessByShareIdDbResultType = z.infer; @@ -19,7 +20,7 @@ export const getDeckAccessByShareIdDb = async ( const oneTimeShareLinkResult = await db .from("deck_access") - .select("deck_id, author_id, used_by") + .select("deck_id, author_id, used_by, processed_at") .eq("share_id", shareId) .maybeSingle(); diff --git a/functions/get-shared-deck.test.ts b/functions/get-shared-deck.test.ts index 36363b80..70337dd5 100644 --- a/functions/get-shared-deck.test.ts +++ b/functions/get-shared-deck.test.ts @@ -37,6 +37,11 @@ vi.mock("./db/deck-access/start-using-deck-access-db.ts", () => ({ startUsingDeckAccessDb: async () => startUsingDeckAccessDbMock(), })); +const addDeckToMineDbMock = vi.hoisted(() => vi.fn()); +vi.mock("./db/deck/add-deck-to-mine-db.ts", () => ({ + addDeckToMineDb: async () => addDeckToMineDbMock(), +})); + const mockDeckOfUser1: DeckWithCardsDbType = { id: 1, name: "name", @@ -135,5 +140,22 @@ describe("get shared deck", () => { expect(getDeckWithCardsByIdMock).toBeCalled(); expect(startUsingDeckAccessDbMock).toBeCalled(); + expect(addDeckToMineDbMock).toBeCalled(); + }); + + test("one time access found but it is already processed", async () => { + getDeckAccessByShareIdMock.mockReturnValue({ + author_id: 2, + used_by: null, + deck_id: 2, + processed_at: new Date().toISOString(), + }); + getDeckWithCardsByIdMock.mockReturnValue(mockDeckOfUser2); + + await getSharedDeckRequest( + createMockRequest(`https://example.com?share_id=SHARE_ID1`), + ); + + expect(createBadRequestResponseMock).toBeCalled(); }); }); diff --git a/functions/get-shared-deck.ts b/functions/get-shared-deck.ts index c3c21e0b..294848ba 100644 --- a/functions/get-shared-deck.ts +++ b/functions/get-shared-deck.ts @@ -31,27 +31,28 @@ export const onRequest = handleError(async ({ env, request }) => { const envSafe = envSchema.parse(env); - const oneTimeShareLinkResultData = await getDeckAccessByShareIdDb( - envSafe, - shareId, - ); + const deckAccessResult = await getDeckAccessByShareIdDb(envSafe, shareId); - if (oneTimeShareLinkResultData) { - if (oneTimeShareLinkResultData.author_id !== user.id) { - if (oneTimeShareLinkResultData.used_by) { - if (oneTimeShareLinkResultData.used_by !== user.id) { + if (deckAccessResult) { + if (deckAccessResult.processed_at) { + return createBadRequestResponse("The link has expired"); + } + + if (deckAccessResult.author_id !== user.id) { + if (deckAccessResult.used_by) { + if (deckAccessResult.used_by !== user.id) { return createBadRequestResponse("The link has already been used"); } } else { await startUsingDeckAccessDb(envSafe, user.id, shareId); await addDeckToMineDb(envSafe, { user_id: user.id, - deck_id: oneTimeShareLinkResultData.deck_id, + deck_id: deckAccessResult.deck_id, }); } } - const deckId = oneTimeShareLinkResultData.deck_id; + const deckId = deckAccessResult.deck_id; const stableShareLinkResult = await getDeckWithCardsById(envSafe, deckId); return createJsonResponse({