Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to update boltcards with v1.0.0 #46

Open
Amperstrand opened this issue Dec 16, 2024 · 1 comment
Open

Unable to update boltcards with v1.0.0 #46

Amperstrand opened this issue Dec 16, 2024 · 1 comment

Comments

@Amperstrand
Copy link

File "/app/.venv/lib/python3.10/site-packages/fastapi/routing.py", line 212, in run_endpoint_function
  return await dependant.call(**values)
File "/app/"/app/data/extensions/"/extensions/boltcards/views_api.py", line 88, in api_card_update
  card = await update_card(card_id, **data.dict())
TypeError: update_card() got an unexpected keyword argument 'card_name'
2024-12-16 11:00:34.21 | ERROR | lnbits.exceptions:exception_handler:65 | Exception: update_card() got an unexpected keyword argument 'card_name'

card = await update_card(card_id, **data.dict())

Testing LNbits 1.0.0 and having trouble updating cards. Leaving this here to look into further

@Amperstrand
Copy link
Author

workaround:

views_api.py:

async def api_card_update(
    data: CreateCardData,
    card_id: str,
    wallet: WalletTypeInfo = Depends(require_admin_key),
) -> Card:
    existing_card = await get_card(card_id)
    if not existing_card:
        raise HTTPException(detail="Card does not exist.", status_code=404)

    if existing_card.wallet != wallet.wallet.id:
        raise HTTPException(detail="Not your card.", status_code=403)

    # Allowed fields to update
    allowed_fields = {"card_name", "daily_limit", "tx_limit", "counter"}

    existing_data = existing_card.dict()
    incoming_data = data.dict(exclude_unset=True)
    filtered_updates = {f: v for f, v in incoming_data.items() if f in allowed_fields}
    updated_data = {**existing_data, **filtered_updates}

    # new_data now includes all fields that Card requires
    new_data = CreateCardData(**updated_data)

    updated_card = await update_card(card_id, new_data)
    assert updated_card, "update_card should always return a card"
    return updated_card

crud.py

async def update_card(card_id: str, data: CreateCardData) -> Card:
    # Fetch the existing card
    existing_card = await get_card(card_id)
    if not existing_card:
        raise HTTPException(status_code=404, detail="Card not found")

    # Convert existing card to dict to preserve all fields including wallet, external_id, otp, time
    card_dict = existing_card.dict()

    # Extract updates from data (only the fields you want to allow updating)
    allowed_fields = {"card_name", "daily_limit", "tx_limit", "counter"}
    updates = data.dict(exclude_unset=True)
    for field in allowed_fields:
        if field in updates:
            card_dict[field] = updates[field]

    # Rebuild a new Card object with unchanged fields preserved
    card = Card(**card_dict)

    # Now perform the update in the database
    await db.update("boltcards.cards", card)
    return card

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant