Exemplo n.º 1
0
def create_user(username: str, email: str, plain_password: str) -> User:
    from lib.auth import hash_password

    # TODO validate that email is valid and unique
    data = UserWithHashedPassword(
        username=username,
        email=email,
        hashed_password=hash_password(plain_password))
    data = db_insert_one(data)
    return User.parse_obj(data)
Exemplo n.º 2
0
def create_transaction(transaction: Transaction) -> Transaction:
    if not transaction.main_currency_exchange_rate and transaction.currency != MAIN_CURRENCY:
        currency_exchange_rate = get_currency_exchange_rate_for_nearest_date(
            MAIN_CURRENCY, transaction.currency, transaction.date
        )
        assert currency_exchange_rate
        transaction.main_currency_exchange_rate = currency_exchange_rate.rate

    transaction = db_insert_one(transaction)

    if transaction.type == TransactionType.TRANSFER:
        from_account = _update_related_account_balance(
            transaction.account_id, transaction.amount, increase=False
        )
        assert (
            from_account.currency == transaction.currency
        ), "Currency of account should be equal to transaction currency"
        transaction.account = from_account

        assert transaction.to_account_id
        to_account = get_account_by_id(transaction.to_account_id)
        assert to_account

        amount = transaction.amount
        assert (
            to_account.currency == transaction.to_currency
        ), "Currency of account should be equal to transaction currency"
        if to_account.currency != from_account.currency:
            assert transaction.to_amount is not None, "For different currencies to_amount should be present"
            amount = transaction.to_amount

        to_account = _update_related_account_balance(transaction.to_account_id, amount, increase=True)
        transaction.to_account = to_account
    else:
        increase = transaction.type == TransactionType.INCOME.value
        account = _update_related_account_balance(transaction.account_id, transaction.amount, increase)
        transaction.account = account

    return transaction
Exemplo n.º 3
0
def create_employer(employer: Employer) -> Employer:
    data = db_insert_one(employer)
    return data
Exemplo n.º 4
0
def create_currency_exchange_rate(currency_exchange_rate: CurrencyExchangeRate) -> CurrencyExchangeRate:
    data = db_insert_one(currency_exchange_rate)
    return data
Exemplo n.º 5
0
def create_finance_category(
        finance_category: FinanceCategory) -> FinanceCategory:
    data = db_insert_one(finance_category)
    return data