2025-05-09 16:31:14 +01:00
|
|
|
from datetime import datetime, timedelta
|
2025-07-15 15:44:19 +01:00
|
|
|
from typing import List, Dict, Any
|
|
|
|
|
|
|
|
def filter_accounts_next_30_days(accounts: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
|
|
|
"""Filters accounts expiring within the next 30 days.
|
2025-05-09 16:31:14 +01:00
|
|
|
|
|
|
|
Args:
|
2025-07-15 15:44:19 +01:00
|
|
|
accounts: A list of account dictionaries, each with an 'expiaryDate'
|
|
|
|
(epoch timestamp).
|
|
|
|
|
2025-05-09 16:31:14 +01:00
|
|
|
Returns:
|
2025-07-15 15:44:19 +01:00
|
|
|
A list of accounts expiring within the next 30 days, with added
|
|
|
|
'expiaryDate_rendered' and 'days_to_expiry' keys.
|
2025-05-09 16:31:14 +01:00
|
|
|
"""
|
|
|
|
now = datetime.now()
|
|
|
|
thirty_days_later = now + timedelta(days=30)
|
|
|
|
now_timestamp = int(now.timestamp())
|
|
|
|
thirty_days_later_timestamp = int(thirty_days_later.timestamp())
|
2025-07-15 14:02:52 +01:00
|
|
|
|
|
|
|
result = []
|
|
|
|
today = now.date()
|
|
|
|
|
|
|
|
for account in accounts:
|
|
|
|
if now_timestamp <= account['expiaryDate'] < thirty_days_later_timestamp:
|
|
|
|
expiry_date = datetime.fromtimestamp(account['expiaryDate'])
|
|
|
|
account['expiaryDate_rendered'] = expiry_date.strftime('%d-%m-%Y')
|
|
|
|
expiry_date_date = expiry_date.date()
|
|
|
|
account['days_to_expiry'] = (expiry_date_date - today).days
|
|
|
|
result.append(account)
|
|
|
|
return result
|
2025-05-09 16:31:14 +01:00
|
|
|
|
2025-07-15 15:44:19 +01:00
|
|
|
def filter_accounts_expired(accounts: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
|
|
|
"""Filters accounts that have already expired.
|
|
|
|
|
2025-05-09 16:31:14 +01:00
|
|
|
Args:
|
2025-07-15 15:44:19 +01:00
|
|
|
accounts: A list of account dictionaries, each with an 'expiaryDate'
|
|
|
|
(epoch timestamp).
|
|
|
|
|
2025-05-09 16:31:14 +01:00
|
|
|
Returns:
|
2025-07-15 15:44:19 +01:00
|
|
|
A list of expired accounts with an added 'expiaryDate_rendered' key.
|
2025-05-09 16:31:14 +01:00
|
|
|
"""
|
|
|
|
current_timestamp = int(datetime.now().timestamp())
|
|
|
|
|
2025-07-15 14:02:52 +01:00
|
|
|
expired_accounts = []
|
|
|
|
for account in accounts:
|
|
|
|
if account['expiaryDate'] < current_timestamp:
|
|
|
|
expiry_date = datetime.fromtimestamp(account['expiaryDate'])
|
|
|
|
account['expiaryDate_rendered'] = expiry_date.strftime('%d-%m-%Y')
|
|
|
|
expired_accounts.append(account)
|
2025-05-09 16:31:14 +01:00
|
|
|
|
|
|
|
return expired_accounts
|