From b557617e15bfca23ce744745746e2cdb6d8df085 Mon Sep 17 00:00:00 2001 From: pixel Date: Sat, 20 Jan 2024 05:06:59 +0100 Subject: [PATCH] more backend functions for getting and creating checks --- src/backendlib.ts | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/backendlib.ts b/src/backendlib.ts index 1167668..ffce740 100644 --- a/src/backendlib.ts +++ b/src/backendlib.ts @@ -1,7 +1,7 @@ import { createClient, type User, AuthError, type UserMetadata, type PostgrestError } from "@supabase/supabase-js"; import type { AstroCookies } from "astro"; import NodeCache from "node-cache"; -import type { Check, Template, TemplateRevision, check, check_revision, kinkcheck, template, template_revision } from "./base"; +import type { Check, Template, TemplateRevision, check, check_data, check_revision, template, template_revision } from "./base"; type ValOrErr = [V, null] | [null, E]; @@ -28,7 +28,7 @@ export async function getUser({ cookies }: { cookies: AstroCookies }): Promise & { id: string }): profileCache.set(data.id, data); } +// TODO: think about whether we really need this export async function getProfileByName(username: string): Promise> { // TODO: consider just having a second cache for usernames const cached = profileCache.keys().filter((k) => profileCache.get(k)?.username === username); @@ -166,8 +167,31 @@ export async function getTemplateRevision({ id, version }: { id: string, version return [{ ...template, ...revision }, null]; } +// FIXME: this completely fucks up visibility const checkCache = new NodeCache({ stdTTL: 3600 }); +// TODO: all kinds of caching (we just need to redo like all the caching lmao) +export async function getAllChecksByUser(user: string): Promise> { + const { data: checks, error } = await supabase + .from("checks") + .select<"*", check>() + .eq("user_id", user); + if (error) return [null, error]; + + const results = await Promise.all(checks.map(check => supabase + .from("check_revisions") + .select<"*", check_revision>() + .eq("user_id", user) + .eq("template", check.template))); + const err = results.find(({ error }) => error)?.error; + if (err) return [null, err]; + + const revs = results.map(({ data }) => data!); + const full_checks: Check[] = checks.map((check, i) => ({ ...check, revisions: revs[i] })); + + return [full_checks, null]; +} + export async function getCheck({ user, template }: { user: string, template: string }): Promise> { const key = `${user}/${template}`; const cached = checkCache.get(key); @@ -199,6 +223,25 @@ export async function getCheck({ user, template }: { user: string, template: str return [t, null]; } +export async function getOwnCheck({ cookies }: { cookies: AstroCookies }, template: string): + Promise> { + const [user, error] = await getUser({ cookies }); + if (!user) return [null, error]; + return getCheck({ user: user.id, template }); +} + +export function getLatestCheckRevision(check: Check): check & check_revision | null { + if (!check.revisions.length) return null; + return { + ...check, + ...check.revisions.toSorted(({ modified: a }, { modified: b }) => a < b ? 1 : a > b ? -1 : 0)[0] + }; +} + +export function createCheckMeta(check: check): PromiseLike { + return supabase.from("checks").insert(check).then(({ error }) => error); +} + export async function createCheckRevision({ user, template, version, data }: { user: string, template: string, version: string, data: check_data }): Promise {