-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
94 lines (83 loc) · 3.01 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from dataclasses import dataclass
from datetime import datetime
from typing import List, Optional
import boto3
from boto3.dynamodb.conditions import Key
from configuration import Configuration
from payment import Payment, PersistedPayment
@dataclass(frozen=True)
class Balance:
creditor: str
debtor: str
amount: str
class Database:
def __init__(self, configuration: Configuration):
self._configuration = configuration
dynamodb = boto3.resource("dynamodb")
self._table = dynamodb.Table(
f"table-sw-{'-'.join(configuration.get_usernames()).lower()}-payments"
)
def add_payment(self, payment: Payment, timestamp: Optional[str] = None):
self._table.put_item(
Item={
"wallet": payment.wallet,
"timestamp": (
datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S")
if not timestamp
else timestamp
),
"payer": payment.payer,
"amount": payment.amount,
"note": payment.note,
}
)
def get_payments(self, wallet: str = None) -> List[PersistedPayment]:
payments: List[PersistedPayment] = []
if wallet:
response = self._table.query(
KeyConditionExpression=Key("wallet").eq(wallet)
)
else:
response = self._table.scan()
sorted_response = sorted(
response.get("Items", []),
key=lambda x: datetime.strptime(x.get("timestamp"), "%Y-%m-%d %H:%M:%S"),
)
for item in sorted_response:
payments.append(
PersistedPayment(
item.get("payer"),
item.get("amount"),
item.get("wallet"),
self._configuration.get_wallet_symbol(item.get("wallet")),
item.get("note"),
item.get("timestamp"),
)
)
return payments
def get_balance(self, wallet: str) -> Balance:
user1, balance1 = self._configuration.get_usernames()[0], 0
for payment in self.get_payments(wallet):
balance1 = (
balance1 + float(payment.amount)
if payment.payer == user1
else balance1 - float(payment.amount)
)
if balance1 == 0:
return Balance(
creditor=user1,
debtor=self._configuration.get_other_username(user1),
amount="0",
)
elif balance1 > 0:
return Balance(
creditor=user1,
debtor=self._configuration.get_other_username(user1),
amount=str(round(balance1, 2)).rstrip("0").rstrip("."),
)
else:
return Balance(
creditor=self._configuration.get_other_username(user1),
debtor=user1,
amount=str(round(-balance1, 2)).rstrip("0").rstrip("."),
)