Simplifies database queries and updates auth
Removes the `castle.` prefix from database table names in queries, streamlining data access. Updates authentication to use `WalletTypeInfo` dependency injection for retrieving wallet information. This improves security and aligns with LNBits' authentication patterns. Also modifies the main router's tag to uppercase.
This commit is contained in:
parent
cdd0cda001
commit
5589d813f0
3 changed files with 39 additions and 35 deletions
39
views_api.py
39
views_api.py
|
|
@ -1,7 +1,7 @@
|
|||
from http import HTTPStatus
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from lnbits.core.crud import get_user
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from lnbits.core.models import WalletTypeInfo
|
||||
from lnbits.decorators import require_admin_key, require_invoice_key
|
||||
|
||||
from .crud import (
|
||||
|
|
@ -46,7 +46,7 @@ async def api_get_accounts() -> list[Account]:
|
|||
@castle_api_router.post("/accounts", status_code=HTTPStatus.CREATED)
|
||||
async def api_create_account(
|
||||
data: CreateAccount,
|
||||
wallet_id: str = require_admin_key, # type: ignore
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||
) -> Account:
|
||||
"""Create a new account (admin only)"""
|
||||
return await create_account(data)
|
||||
|
|
@ -94,10 +94,11 @@ async def api_get_journal_entries(limit: int = 100) -> list[JournalEntry]:
|
|||
|
||||
@castle_api_router.get("/entries/user")
|
||||
async def api_get_user_entries(
|
||||
wallet_id: str = require_invoice_key, limit: int = 100 # type: ignore
|
||||
wallet: WalletTypeInfo = Depends(require_invoice_key),
|
||||
limit: int = 100,
|
||||
) -> list[JournalEntry]:
|
||||
"""Get journal entries created by the current user"""
|
||||
return await get_journal_entries_by_user(wallet_id, limit)
|
||||
return await get_journal_entries_by_user(wallet.wallet.id, limit)
|
||||
|
||||
|
||||
@castle_api_router.get("/entries/{entry_id}")
|
||||
|
|
@ -114,11 +115,11 @@ async def api_get_journal_entry(entry_id: str) -> JournalEntry:
|
|||
@castle_api_router.post("/entries", status_code=HTTPStatus.CREATED)
|
||||
async def api_create_journal_entry(
|
||||
data: CreateJournalEntry,
|
||||
wallet_id: str = require_invoice_key, # type: ignore
|
||||
wallet: WalletTypeInfo = Depends(require_invoice_key),
|
||||
) -> JournalEntry:
|
||||
"""Create a new journal entry"""
|
||||
try:
|
||||
return await create_journal_entry(data, wallet_id)
|
||||
return await create_journal_entry(data, wallet.wallet.id)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail=str(e))
|
||||
|
||||
|
|
@ -129,7 +130,7 @@ async def api_create_journal_entry(
|
|||
@castle_api_router.post("/entries/expense", status_code=HTTPStatus.CREATED)
|
||||
async def api_create_expense_entry(
|
||||
data: ExpenseEntry,
|
||||
wallet_id: str = require_invoice_key, # type: ignore
|
||||
wallet: WalletTypeInfo = Depends(require_invoice_key),
|
||||
) -> JournalEntry:
|
||||
"""
|
||||
Create an expense entry for a user.
|
||||
|
|
@ -180,13 +181,13 @@ async def api_create_expense_entry(
|
|||
],
|
||||
)
|
||||
|
||||
return await create_journal_entry(entry_data, wallet_id)
|
||||
return await create_journal_entry(entry_data, wallet.wallet.id)
|
||||
|
||||
|
||||
@castle_api_router.post("/entries/receivable", status_code=HTTPStatus.CREATED)
|
||||
async def api_create_receivable_entry(
|
||||
data: ReceivableEntry,
|
||||
wallet_id: str = require_admin_key, # type: ignore
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||
) -> JournalEntry:
|
||||
"""
|
||||
Create an accounts receivable entry (user owes castle).
|
||||
|
|
@ -228,13 +229,13 @@ async def api_create_receivable_entry(
|
|||
],
|
||||
)
|
||||
|
||||
return await create_journal_entry(entry_data, wallet_id)
|
||||
return await create_journal_entry(entry_data, wallet.wallet.id)
|
||||
|
||||
|
||||
@castle_api_router.post("/entries/revenue", status_code=HTTPStatus.CREATED)
|
||||
async def api_create_revenue_entry(
|
||||
data: RevenueEntry,
|
||||
wallet_id: str = require_admin_key, # type: ignore
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||
) -> JournalEntry:
|
||||
"""
|
||||
Create a revenue entry (castle receives payment).
|
||||
|
|
@ -281,7 +282,7 @@ async def api_create_revenue_entry(
|
|||
],
|
||||
)
|
||||
|
||||
return await create_journal_entry(entry_data, wallet_id)
|
||||
return await create_journal_entry(entry_data, wallet.wallet.id)
|
||||
|
||||
|
||||
# ===== USER BALANCE ENDPOINTS =====
|
||||
|
|
@ -289,10 +290,10 @@ async def api_create_revenue_entry(
|
|||
|
||||
@castle_api_router.get("/balance")
|
||||
async def api_get_my_balance(
|
||||
wallet_id: str = require_invoice_key, # type: ignore
|
||||
wallet: WalletTypeInfo = Depends(require_invoice_key),
|
||||
) -> UserBalance:
|
||||
"""Get current user's balance with the Castle"""
|
||||
return await get_user_balance(wallet_id)
|
||||
return await get_user_balance(wallet.wallet.id)
|
||||
|
||||
|
||||
@castle_api_router.get("/balance/{user_id}")
|
||||
|
|
@ -307,12 +308,14 @@ async def api_get_user_balance(user_id: str) -> UserBalance:
|
|||
@castle_api_router.post("/pay-balance")
|
||||
async def api_pay_balance(
|
||||
amount: int,
|
||||
wallet_id: str = require_invoice_key, # type: ignore
|
||||
wallet: WalletTypeInfo = Depends(require_invoice_key),
|
||||
) -> dict:
|
||||
"""
|
||||
Record a payment from user to castle (reduces what user owes or what castle owes user).
|
||||
This should be called after an invoice is paid.
|
||||
"""
|
||||
wallet_id = wallet.wallet.id
|
||||
|
||||
# Get user's receivable account (what user owes)
|
||||
user_receivable = await get_or_create_user_account(
|
||||
wallet_id, AccountType.ASSET, "Accounts Receivable"
|
||||
|
|
@ -361,7 +364,7 @@ async def api_pay_balance(
|
|||
async def api_pay_user(
|
||||
user_id: str,
|
||||
amount: int,
|
||||
wallet_id: str = require_admin_key, # type: ignore
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||
) -> dict:
|
||||
"""
|
||||
Record a payment from castle to user (reduces what castle owes user).
|
||||
|
|
@ -399,7 +402,7 @@ async def api_pay_user(
|
|||
],
|
||||
)
|
||||
|
||||
entry = await create_journal_entry(entry_data, wallet_id)
|
||||
entry = await create_journal_entry(entry_data, wallet.wallet.id)
|
||||
|
||||
# Get updated balance
|
||||
balance = await get_user_balance(user_id)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue