0.1.2•Updated 7 months ago
import { DataTypeQuery } from "@infinity-beyond/classes/query.ts";
import type { LedgerEntry } from "@infinity-beyond/classes/data_types/ledger/ledger.types.ts";
export const FindConsumptionEntries = new DataTypeQuery<[
/* $1 */ key: string,
/* $2 */ required_amount: number,
], {
entries_table: string
}>(`
select
t.*
from (
select {entries_table}.*, sum(amount) over (order by timestamp asc) as running_amount
from {entries_table} where key=$1 and remaining > 0
) t
where running_amount - amount < $2
order by timestamp asc
`,
(rows: LedgerEntry[]) => rows.map(row => {
delete (row as any)['running_amount'];
return row;
})
)