diff --git a/functions/lib/short-unique-id/short-unique-id.ts b/functions/lib/short-unique-id/short-unique-id.ts new file mode 100644 index 00000000..be1c0426 --- /dev/null +++ b/functions/lib/short-unique-id/short-unique-id.ts @@ -0,0 +1,5 @@ +import ShortUniqueId from "short-unique-id"; + +export const shortUniqueId = () => { + return new ShortUniqueId({ length: 10 }).rnd(); +}; diff --git a/functions/share-deck.ts b/functions/share-deck.ts index ba99ed0b..4af89882 100644 --- a/functions/share-deck.ts +++ b/functions/share-deck.ts @@ -8,6 +8,7 @@ import { envSchema } from "./env/env-schema.ts"; import { getDatabase } from "./db/get-database.ts"; import { DatabaseException } from "./db/database-exception.ts"; import { createJsonResponse } from "./lib/json-response/create-json-response.ts"; +import { shortUniqueId } from "./lib/short-unique-id/short-unique-id.ts"; const requestSchema = z.object({ deckId: z.number(), @@ -49,7 +50,7 @@ export const onRequestPost = handleError(async ({ env, request }) => { ); } - const shareId = new ShortUniqueId({ length: 10 }).rnd(); + const shareId = shortUniqueId(); const { error } = await db .from("deck") diff --git a/functions/upsert-deck.ts b/functions/upsert-deck.ts index c4f0b663..5608518a 100644 --- a/functions/upsert-deck.ts +++ b/functions/upsert-deck.ts @@ -11,6 +11,7 @@ import { deckSchema } from "./db/deck/decks-with-cards-schema.ts"; import { addDeckToMineDb } from "./db/deck/add-deck-to-mine-db.ts"; import { createForbiddenRequestResponse } from "./lib/json-response/create-forbidden-request-response.ts"; import { canEditDeck } from "./db/deck/can-edit-deck.ts"; +import { shortUniqueId } from "./lib/short-unique-id/short-unique-id.ts"; const requestSchema = z.object({ id: z.number().nullable().optional(), @@ -53,6 +54,7 @@ export const onRequestPost = handleError(async ({ request, env }) => { .upsert({ id: input.data.id ? input.data.id : undefined, author_id: user.id, + share_id: input.data.id ? undefined : shortUniqueId(), name: input.data.title, description: input.data.description, is_public: false,