Stores and retrieves entry line metadata as JSON

Updates journal entry creation to store entry line metadata as a JSON string in the database.

Updates entry line retrieval to parse the JSON string back into a metadata object.

This change allows storing more complex data structures in the metadata field.
This commit is contained in:
padreug 2025-10-22 13:45:59 +02:00
parent e95230c0f4
commit 15da11a606
2 changed files with 64 additions and 13 deletions

71
crud.py
View file

@ -1,3 +1,4 @@
import json
from datetime import datetime
from typing import Optional
@ -123,7 +124,21 @@ async def create_journal_entry(
lines=[],
)
await db.insert("journal_entries", journal_entry)
# Insert journal entry without the lines field (lines are stored in entry_lines table)
await db.execute(
"""
INSERT INTO journal_entries (id, description, entry_date, created_by, created_at, reference)
VALUES (:id, :description, :entry_date, :created_by, :created_at, :reference)
""",
{
"id": journal_entry.id,
"description": journal_entry.description,
"entry_date": journal_entry.entry_date,
"created_by": journal_entry.created_by,
"created_at": journal_entry.created_at,
"reference": journal_entry.reference,
},
)
# Create entry lines
lines = []
@ -136,8 +151,24 @@ async def create_journal_entry(
debit=line_data.debit,
credit=line_data.credit,
description=line_data.description,
metadata=line_data.metadata,
)
# Insert with metadata as JSON string
await db.execute(
"""
INSERT INTO entry_lines (id, journal_entry_id, account_id, debit, credit, description, metadata)
VALUES (:id, :journal_entry_id, :account_id, :debit, :credit, :description, :metadata)
""",
{
"id": line.id,
"journal_entry_id": line.journal_entry_id,
"account_id": line.account_id,
"debit": line.debit,
"credit": line.credit,
"description": line.description,
"metadata": json.dumps(line.metadata),
},
)
await db.insert("entry_lines", line)
lines.append(line)
journal_entry.lines = lines
@ -158,12 +189,28 @@ async def get_journal_entry(entry_id: str) -> Optional[JournalEntry]:
async def get_entry_lines(journal_entry_id: str) -> list[EntryLine]:
return await db.fetchall(
rows = await db.fetchall(
"SELECT * FROM entry_lines WHERE journal_entry_id = :id",
{"id": journal_entry_id},
EntryLine,
)
lines = []
for row in rows:
# Parse metadata from JSON string
metadata = json.loads(row.metadata) if row.metadata else {}
line = EntryLine(
id=row.id,
journal_entry_id=row.journal_entry_id,
account_id=row.account_id,
debit=row.debit,
credit=row.credit,
description=row.description,
metadata=metadata,
)
lines.append(line)
return lines
async def get_all_journal_entries(limit: int = 100) -> list[JournalEntry]:
entries = await db.fetchall(
@ -270,7 +317,7 @@ async def get_account_transactions(
account_id: str, limit: int = 100
) -> list[tuple[JournalEntry, EntryLine]]:
"""Get all transactions affecting a specific account"""
lines = await db.fetchall(
rows = await db.fetchall(
"""
SELECT * FROM entry_lines
WHERE account_id = :id
@ -278,11 +325,21 @@ async def get_account_transactions(
LIMIT :limit
""",
{"id": account_id, "limit": limit},
EntryLine,
)
transactions = []
for line in lines:
for row in rows:
# Parse metadata from JSON string
metadata = json.loads(row.metadata) if row.metadata else {}
line = EntryLine(
id=row.id,
journal_entry_id=row.journal_entry_id,
account_id=row.account_id,
debit=row.debit,
credit=row.credit,
description=row.description,
metadata=metadata,
)
entry = await get_journal_entry(line.journal_entry_id)
if entry:
transactions.append((entry, line))

View file

@ -39,12 +39,10 @@ window.app = Vue.createApp({
return 'Amount (sats) *'
},
currencyOptions() {
console.log('Computing currencyOptions, this.currencies:', this.currencies)
const options = [{label: 'Satoshis (default)', value: null}]
this.currencies.forEach(curr => {
options.push({label: curr, value: curr})
})
console.log('Currency options:', options)
return options
}
},
@ -92,12 +90,8 @@ window.app = Vue.createApp({
'/castle/api/v1/currencies',
this.g.user.wallets[0].inkey
)
console.log('Currencies API response:', response)
console.log('Currencies data:', response.data)
this.currencies = response.data
console.log('this.currencies:', this.currencies)
} catch (error) {
console.error('Error loading currencies:', error)
LNbits.utils.notifyApiError(error)
}
},