Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
kubk committed Jan 5, 2024
1 parent 0381ef3 commit 3aa349f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
3 changes: 2 additions & 1 deletion functions/db/deck-access/get-deck-access-by-share-id-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof resultSchema>;
Expand All @@ -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();

Expand Down
22 changes: 22 additions & 0 deletions functions/get-shared-deck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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();
});
});
21 changes: 11 additions & 10 deletions functions/get-shared-deck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<GetSharedDeckResponse>({
Expand Down

0 comments on commit 3aa349f

Please sign in to comment.