from __future__ import annotations from datetime import date, timedelta from services.db.mysql_service import DatabaseService class ReminderRepository: """td_reminders 테이블을 통한 알림 저장소.""" def __init__(self, db: DatabaseService): self._db = db def add(self, user_id: str, remind_date: date, message: str) -> int: return self._db.execute_write( "INSERT INTO td_reminders (user_id, remind_date, message) VALUES (%s, %s, %s)", (user_id, remind_date.isoformat(), message), ) def get_upcoming(self, user_id: str, days_ahead: int = 30) -> list[dict]: today = date.today() limit = today + timedelta(days=days_ahead) return self._db.execute( """SELECT id, remind_date, message FROM td_reminders WHERE user_id = %s AND remind_date >= %s AND remind_date <= %s ORDER BY remind_date""", (user_id, today.isoformat(), limit.isoformat()), ) def get_due(self, today: date) -> list[dict]: """D-0(당일), D-1(내일), D-7(7일 후) 미발송 알림 반환.""" d1 = today + timedelta(days=1) d7 = today + timedelta(days=7) return self._db.execute( """SELECT *, CASE WHEN remind_date = %s THEN 'd0' WHEN remind_date = %s THEN 'd1' WHEN remind_date = %s THEN 'd7' END AS notify_type FROM td_reminders WHERE (remind_date = %s AND sent_d0 = 0) OR (remind_date = %s AND sent_d1 = 0) OR (remind_date = %s AND sent_d7 = 0)""", ( today.isoformat(), d1.isoformat(), d7.isoformat(), today.isoformat(), d1.isoformat(), d7.isoformat(), ), ) def mark_sent(self, reminder_id: int, notify_type: str) -> None: col = {"d0": "sent_d0", "d1": "sent_d1", "d7": "sent_d7"}.get(notify_type) if col: self._db.execute_write( f"UPDATE td_reminders SET {col} = 1 WHERE id = %s", (reminder_id,), )