from tinydb import Query
from db.db import digest_action_tokens_db
from models.digest_action_token import DigestActionToken


def insert_token(token: DigestActionToken) -> None:
    digest_action_tokens_db.insert(token.to_dict())


def get_token_by_id(token_id: str) -> DigestActionToken | None:
    Q = Query()
    result = digest_action_tokens_db.get(Q.id == token_id)
    return DigestActionToken.from_dict(result) if result else None


def mark_token_used(token_id: str) -> None:
    Q = Query()
    digest_action_tokens_db.update({'used': True}, Q.id == token_id)
