-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
564 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { handleError } from "./lib/handle-error/handle-error.ts"; | ||
import { getUser } from "./services/get-user.ts"; | ||
import { createAuthFailedResponse } from "./lib/json-response/create-auth-failed-response.ts"; | ||
import { createBadRequestResponse } from "./lib/json-response/create-bad-request-response.ts"; | ||
import { z } from "zod"; | ||
import { getDeckByIdAndAuthorId } from "./db/deck/get-deck-by-id-and-author-id.ts"; | ||
import { envSchema } from "./env/env-schema.ts"; | ||
import { createForbiddenRequestResponse } from "./lib/json-response/create-forbidden-request-response.ts"; | ||
import { createJsonResponse } from "./lib/json-response/create-json-response.ts"; | ||
import { createDeckAccessDb } from "./db/deck-access/create-deck-access-db.ts"; | ||
|
||
const requestSchema = z.object({ | ||
deckId: z.number(), | ||
durationDays: z.number().nullable(), | ||
}); | ||
|
||
const responseSchema = z.object({ | ||
share_id: z.string(), | ||
}); | ||
|
||
export type AddDeckAccessRequest = z.infer<typeof requestSchema>; | ||
export type AddDeckAccessResponse = z.infer<typeof responseSchema>; | ||
|
||
export const onRequestPost = handleError(async ({ request, env }) => { | ||
const user = await getUser(request, env); | ||
if (!user) return createAuthFailedResponse(); | ||
const input = requestSchema.safeParse(await request.json()); | ||
if (!input.success) { | ||
return createBadRequestResponse(); | ||
} | ||
|
||
const envSafe = envSchema.parse(env); | ||
const canEdit = await getDeckByIdAndAuthorId( | ||
envSafe, | ||
input.data.deckId, | ||
user, | ||
); | ||
if (!canEdit) { | ||
return createForbiddenRequestResponse(); | ||
} | ||
|
||
const createDeckAccessResult = await createDeckAccessDb( | ||
envSafe, | ||
user.id, | ||
input.data.deckId, | ||
input.data.durationDays, | ||
); | ||
|
||
return createJsonResponse<AddDeckAccessResponse>( | ||
responseSchema.parse(createDeckAccessResult), | ||
200, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { EnvSafe } from "../../env/env-schema.ts"; | ||
import { shortUniqueId } from "../../lib/short-unique-id/short-unique-id.ts"; | ||
import { DatabaseException } from "../database-exception.ts"; | ||
import { getDatabase } from "../get-database.ts"; | ||
|
||
export const createDeckAccessDb = async ( | ||
envSafe: EnvSafe, | ||
userId: number, | ||
deckId: number, | ||
durationDays: number | null, | ||
) => { | ||
const db = getDatabase(envSafe); | ||
|
||
const createDeckAccessResult = await db | ||
.from("deck_access") | ||
.insert({ | ||
deck_id: deckId, | ||
author_id: userId, | ||
share_id: shortUniqueId(), | ||
duration_days: durationDays, | ||
}) | ||
.select() | ||
.single(); | ||
|
||
if (createDeckAccessResult.error) { | ||
throw new DatabaseException(createDeckAccessResult.error); | ||
} | ||
|
||
return createDeckAccessResult.data; | ||
}; |
29 changes: 29 additions & 0 deletions
29
functions/db/deck-access/get-deck-access-by-share-id-db.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { DatabaseException } from "../database-exception.ts"; | ||
import { getDatabase } from "../get-database.ts"; | ||
import { EnvSafe } from "../../env/env-schema.ts"; | ||
import { z } from "zod"; | ||
|
||
const resultSchema = z.object({ | ||
deck_id: z.number(), | ||
author_id: z.number(), | ||
used_by: z.number().nullable(), | ||
}); | ||
|
||
export const getDeckAccessByShareIdDb = async ( | ||
envSafe: EnvSafe, | ||
shareId: string, | ||
) => { | ||
const db = getDatabase(envSafe); | ||
|
||
const oneTimeShareLinkResult = await db | ||
.from("deck_access") | ||
.select("deck_id, author_id, used_by") | ||
.eq("share_id", shareId) | ||
.single(); | ||
|
||
if (oneTimeShareLinkResult.error) { | ||
throw new DatabaseException(oneTimeShareLinkResult.error); | ||
} | ||
|
||
return resultSchema.parse(oneTimeShareLinkResult.data); | ||
}; |
25 changes: 25 additions & 0 deletions
25
functions/db/deck-access/get-last-deck-accesses-for-deck-db.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { DatabaseException } from "../database-exception.ts"; | ||
import { getDatabase } from "../get-database.ts"; | ||
import { EnvSafe } from "../../env/env-schema.ts"; | ||
|
||
export const getLastDeckAccessesForDeckDb = async ( | ||
envSafe: EnvSafe, | ||
deckId: number, | ||
) => { | ||
const db = getDatabase(envSafe); | ||
|
||
const { data, error } = await db | ||
.from("deck_access") | ||
.select( | ||
"deck_id, author_id, used_by, share_id, id, created_at, duration_days", | ||
) | ||
.eq("deck_id", deckId) | ||
.order("created_at", { ascending: false }) | ||
.limit(100); | ||
|
||
if (error) { | ||
throw new DatabaseException(error); | ||
} | ||
|
||
return data; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { getDatabase } from "../get-database.ts"; | ||
import { EnvSafe } from "../../env/env-schema.ts"; | ||
import { DatabaseException } from "../database-exception.ts"; | ||
|
||
export const startUsingDeckAccessDb = async ( | ||
envSafe: EnvSafe, | ||
userId: number, | ||
shareId: string, | ||
) => { | ||
const db = getDatabase(envSafe); | ||
|
||
const updateResult = await db | ||
.from("deck_access") | ||
.update({ | ||
used_by: userId, | ||
usage_started_at: new Date().toISOString(), | ||
}) | ||
.eq("share_id", shareId) | ||
.single(); | ||
|
||
if (updateResult.error) { | ||
throw new DatabaseException(updateResult.error); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { DatabaseException } from "../database-exception.ts"; | ||
import { deckWithCardsSchema } from "./decks-with-cards-schema.ts"; | ||
import { EnvSafe } from "../../env/env-schema.ts"; | ||
import { getDatabase } from "../get-database.ts"; | ||
|
||
export const getDeckWithCardsByShareIdDb = async ( | ||
env: EnvSafe, | ||
shareId: string, | ||
) => { | ||
const db = getDatabase(env); | ||
|
||
const { data, error } = await db | ||
.from("deck") | ||
.select("*, deck_card!deck_card_deck_id_fkey(*)") | ||
.eq("share_id", shareId) | ||
.limit(1) | ||
.single(); | ||
|
||
if (error) { | ||
throw new DatabaseException(error); | ||
} | ||
|
||
return deckWithCardsSchema.parse(data); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.