-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore_transaction.py
More file actions
55 lines (42 loc) · 1.53 KB
/
store_transaction.py
File metadata and controls
55 lines (42 loc) · 1.53 KB
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
"""
Module 2: Store Transaction - SOLUTION
Store complete transaction as a JSON document in Redis.
This provides the source of truth for all transaction data.
"""
from typing import Dict, List, Optional
def process_transaction(redis_client, tx_data: Dict[str, str]) -> None:
"""
Store transaction as JSON document.
"""
tx_id = tx_data.get('transactionId')
transaction = {
'transactionId': tx_id,
'customerId': tx_data.get('customerId'),
'amount': float(tx_data.get('amount', 0)),
'merchant': tx_data.get('merchant'),
'category': tx_data.get('category'),
'timestamp': int(tx_data.get('timestamp', 0)),
'location': tx_data.get('location'),
'cardLast4': tx_data.get('cardLast4'),
}
redis_client.json().set(f"transaction:{tx_id}", "$", transaction)
def get_transaction(redis_client, tx_id: str) -> Optional[Dict]:
"""
Retrieve a single transaction by ID.
"""
result = redis_client.json().get(f"transaction:{tx_id}", "$")
return result[0] if result else None
def get_transactions_by_ids(redis_client, tx_ids: List[str]) -> List[Dict]:
"""
Retrieve multiple transactions by IDs using JSON.MGET.
Single Redis call for all documents.
"""
if not tx_ids:
return []
keys = [f"transaction:{tx_id}" for tx_id in tx_ids]
results = redis_client.json().mget(keys, "$")
transactions = []
for result in results:
if result and result[0]:
transactions.append(result[0])
return transactions