refactor: parse zap request with zod

This commit is contained in:
P. Reis 2024-06-17 21:07:27 -03:00
parent bf017195a6
commit 8b67a05792

View file

@ -548,11 +548,11 @@ const zappedByController: AppController = async (c) => {
const store = await Storages.db(); const store = await Storages.db();
const amountSchema = z.coerce.number().int().nonnegative().catch(0); const amountSchema = z.coerce.number().int().nonnegative().catch(0);
const events: DittoEvent[] = (await store.query([{ kinds: [9735], '#e': [id], limit: 100 }])).map((event) => { const events = (await store.query([{ kinds: [9735], '#e': [id], limit: 100 }])).map((event) => {
const zapRequestString = event.tags.find(([name]) => name === 'description')?.[1]; const zapRequestString = event.tags.find(([name]) => name === 'description')?.[1];
if (!zapRequestString) return; if (!zapRequestString) return;
try { try {
const zapRequest = JSON.parse(zapRequestString); const zapRequest = n.json().pipe(n.event()).parse(zapRequestString);
const amount = zapRequest?.tags.find(([name]: any) => name === 'amount')?.[1]; const amount = zapRequest?.tags.find(([name]: any) => name === 'amount')?.[1];
if (!amount) { if (!amount) {
const amount = getAmount(event?.tags.find(([name]) => name === 'bolt11')?.[1]); const amount = getAmount(event?.tags.find(([name]) => name === 'bolt11')?.[1]);
@ -563,7 +563,7 @@ const zappedByController: AppController = async (c) => {
} catch { } catch {
return; return;
} }
}).filter(Boolean); }).filter(Boolean) as DittoEvent[];
await hydrateEvents({ events, store }); await hydrateEvents({ events, store });