-
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.
* Deck one time access
- Loading branch information
Showing
63 changed files
with
1,315 additions
and
155 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; | ||
}; |
34 changes: 34 additions & 0 deletions
34
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,34 @@ | ||
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(), | ||
processed_at: z.string().nullable(), | ||
}); | ||
|
||
type GetDeckAccessByShareIdDbResultType = z.infer<typeof resultSchema>; | ||
|
||
export const getDeckAccessByShareIdDb = async ( | ||
envSafe: EnvSafe, | ||
shareId: string, | ||
): Promise<GetDeckAccessByShareIdDbResultType | null> => { | ||
const db = getDatabase(envSafe); | ||
|
||
const oneTimeShareLinkResult = await db | ||
.from("deck_access") | ||
.select("deck_id, author_id, used_by, processed_at") | ||
.eq("share_id", shareId) | ||
.maybeSingle(); | ||
|
||
if (oneTimeShareLinkResult.error) { | ||
throw new DatabaseException(oneTimeShareLinkResult.error); | ||
} | ||
|
||
return oneTimeShareLinkResult.data | ||
? resultSchema.parse(oneTimeShareLinkResult.data) | ||
: null; | ||
}; |
46 changes: 46 additions & 0 deletions
46
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,46 @@ | ||
import { DatabaseException } from "../database-exception.ts"; | ||
import { getDatabase } from "../get-database.ts"; | ||
import { EnvSafe } from "../../env/env-schema.ts"; | ||
import { z } from "zod"; | ||
|
||
const responseSchema = z.array( | ||
z.object({ | ||
used_by: z.number().nullable(), | ||
user: z | ||
.object({ | ||
id: z.number(), | ||
username: z.string().nullable(), | ||
first_name: z.string().nullable(), | ||
last_name: z.string().nullable(), | ||
}) | ||
.nullable(), | ||
share_id: z.string(), | ||
id: z.number(), | ||
created_at: z.string(), | ||
duration_days: z.number().nullable(), | ||
}), | ||
); | ||
|
||
export type DeckAccessesForDeckTypeDb = z.infer<typeof responseSchema>; | ||
|
||
export const getLastDeckAccessesForDeckDb = async ( | ||
envSafe: EnvSafe, | ||
deckId: number, | ||
): Promise<DeckAccessesForDeckTypeDb> => { | ||
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, user:used_by (id, username, first_name, last_name)", | ||
) | ||
.eq("deck_id", deckId) | ||
.order("created_at", { ascending: false }) | ||
.limit(100); | ||
|
||
if (error) { | ||
throw new DatabaseException(error); | ||
} | ||
|
||
return responseSchema.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
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); | ||
} | ||
}; |
Oops, something went wrong.