昨天提到關於不同種類型的食材,會設定合理的即期通知門檻,下列是我們列出的狀態。
NORMAL
SOON
TODAY
EXPIRED
UNKNOWN
GET /api/alerts/expiration
SQLite
↓
ExpirationService
↓
找到需要提醒的 InventoryItem
↓
NotificationService
↓
Email / Telegram / Discord / LINE / Meshtastic
↓
使用者
NORMAL
→ 不通知
SOON
→ 通知
TODAY
→ 通知
EXPIRED
→ 通知
UNKNOWN
→ 不通知
輸出訊息之前,會需要湊出內容,先思考哪些欄位是必須用來判斷和顯示的。
Notification
├── id
├── inventory_item_id
├── channel
├── type
├── message
├── remaining_days
├── status
├── created_at
└── sent_at
避免所有內容都塞在 routers,先產生下列檔案
app/
├── routers/
├── services/
│ ├── expiration_service.py
│ └── notification_service.py
│
└── repositories/
└── notification_repository.py
notification_service.pyclass NotificationService:
def send_expiration_alert(
self,
item_name: str,
remaining_days: int,
):
message = self.build_expiration_message(
item_name,
remaining_days,
)
self.send(message)
def build_expiration_message(
self,
item_name: str,
remaining_days: int,
) -> str:
if remaining_days < 0:
return f"{item_name} 已過期"
if remaining_days == 0:
return f"{item_name} 今天到期"
return (
f"{item_name} 距到期還有 "
f"{remaining_days} 天"
)
def send(self, message: str):
raise NotImplementedError