story-research-zapwall/lib/purchaseQueries.ts
2026-01-09 02:11:12 +01:00

32 lines
1.3 KiB
TypeScript

import type { Purchase } from '@/types/nostr'
import { getCachedObjectById } from './helpers/queryHelpers'
import { objectCache } from './objectCache'
export async function getPurchaseById(purchaseId: string, _timeoutMs: number = 5000): Promise<Purchase | null> {
return getCachedObjectById<Purchase>('purchase', purchaseId)
}
export async function getPurchasesForArticle(articleId: string, _timeoutMs: number = 5000): Promise<Purchase[]> {
// Read only from IndexedDB cache
const allPurchases = await objectCache.getAll('purchase')
const purchases = allPurchases as Purchase[]
// Filter by articleId
const articlePurchases = purchases.filter((purchase) => purchase.articleId === articleId)
// Sort by creation date descending
return articlePurchases.sort((a, b) => b.createdAt - a.createdAt)
}
export async function getPurchasesByPayer(payerPubkey: string, _timeoutMs: number = 5000): Promise<Purchase[]> {
// Read only from IndexedDB cache
const allPurchases = await objectCache.getAll('purchase')
const purchases = allPurchases as Purchase[]
// Filter by payerPubkey
const payerPurchases = purchases.filter((purchase) => purchase.payerPubkey === payerPubkey)
// Sort by creation date descending
return payerPurchases.sort((a, b) => b.createdAt - a.createdAt)
}