From d147e04d1b52f1359507a6f9d88e0227cf6bb250 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 22 Jan 2026 18:48:07 -0800 Subject: read payments from archive --- lib/db.ts | 37 +++++++++++++++++++++++++++++++++++++ lib/utils.ts | 10 +++++----- 2 files changed, 42 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/db.ts b/lib/db.ts index 56921adb..12e89757 100644 --- a/lib/db.ts +++ b/lib/db.ts @@ -152,6 +152,43 @@ export const sa = (k, v) => { archive.set(k, JSON.stringify(v)); }; +// Get with fallback to archive +export const gf = async (k) => { + let v = await db.get(k); + if (v === null) v = await archive.get(k); + try { + return JSON.parse(v); + } catch (e) { + return v; + } +}; + +// lRange with fallback to archive for missing items +export const lRangeWithArchive = async (k, start, end) => { + const items = await db.lRange(k, start, end); + return items || []; +}; + +// Get payment/invoice with archive fallback - for individual lookups +export const getWithArchive = async (prefix, id) => { + let v = await db.get(`${prefix}:${id}`); + if (v === null) v = await archive.get(`${prefix}:${id}`); + if (v === null) return null; + try { + const parsed = JSON.parse(v); + // If it's a reference to another key, follow it + if (typeof parsed === "string") { + let ref = await db.get(`${prefix}:${parsed}`); + if (ref === null) ref = await archive.get(`${prefix}:${parsed}`); + if (ref === null) return null; + return JSON.parse(ref); + } + return parsed; + } catch (e) { + return v; + } +}; + const retries = {}; export const t = async (k, f) => { try { diff --git a/lib/utils.ts b/lib/utils.ts index 018dc92e..add09782 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -1,5 +1,5 @@ import config from "$config"; -import { g } from "$lib/db"; +import { g, gf } from "$lib/db"; import locales from "$lib/locales/index"; const { URL } = process.env; @@ -57,15 +57,15 @@ export const getUser = async (username, fields = undefined) => { }; export const getInvoice = async (hash) => { - let iid = await g(`invoice:${hash}`); + let iid = await gf(`invoice:${hash}`); if (iid?.id) iid = iid.id; else if (iid?.hash) iid = iid.hash; - return await g(`invoice:${iid}`); + return await gf(`invoice:${iid}`); }; export const getPayment = async (id) => { - let p = await g(`payment:${id}`); - if (typeof p === "string") p = await g(`payment:${p}`); + let p = await gf(`payment:${id}`); + if (typeof p === "string") p = await gf(`payment:${p}`); return p; }; -- cgit v1.2.3