.機能追加、DBバグ修正
This commit is contained in:
@@ -31,18 +31,22 @@ func NewAssignmentService() *AssignmentService {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *AssignmentService) Create(userID uint, title, description, subject, priority string, dueDate time.Time) (*models.Assignment, error) {
|
||||
func (s *AssignmentService) Create(userID uint, title, description, subject, priority string, dueDate time.Time, reminderEnabled bool, reminderAt *time.Time, urgentReminderEnabled bool) (*models.Assignment, error) {
|
||||
if priority == "" {
|
||||
priority = "medium"
|
||||
}
|
||||
assignment := &models.Assignment{
|
||||
UserID: userID,
|
||||
Title: title,
|
||||
Description: description,
|
||||
Subject: subject,
|
||||
Priority: priority,
|
||||
DueDate: dueDate,
|
||||
IsCompleted: false,
|
||||
UserID: userID,
|
||||
Title: title,
|
||||
Description: description,
|
||||
Subject: subject,
|
||||
Priority: priority,
|
||||
DueDate: dueDate,
|
||||
IsCompleted: false,
|
||||
ReminderEnabled: reminderEnabled,
|
||||
ReminderAt: reminderAt,
|
||||
ReminderSent: false,
|
||||
UrgentReminderEnabled: urgentReminderEnabled,
|
||||
}
|
||||
|
||||
if err := s.assignmentRepo.Create(assignment); err != nil {
|
||||
@@ -191,7 +195,7 @@ func (s *AssignmentService) SearchAssignments(userID uint, query, priority, filt
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *AssignmentService) Update(userID, assignmentID uint, title, description, subject, priority string, dueDate time.Time) (*models.Assignment, error) {
|
||||
func (s *AssignmentService) Update(userID, assignmentID uint, title, description, subject, priority string, dueDate time.Time, reminderEnabled bool, reminderAt *time.Time, urgentReminderEnabled bool) (*models.Assignment, error) {
|
||||
assignment, err := s.GetByID(userID, assignmentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -202,6 +206,13 @@ func (s *AssignmentService) Update(userID, assignmentID uint, title, description
|
||||
assignment.Subject = subject
|
||||
assignment.Priority = priority
|
||||
assignment.DueDate = dueDate
|
||||
assignment.ReminderEnabled = reminderEnabled
|
||||
assignment.ReminderAt = reminderAt
|
||||
assignment.UrgentReminderEnabled = urgentReminderEnabled
|
||||
// Reset reminder sent flag if reminder settings changed
|
||||
if reminderEnabled && reminderAt != nil {
|
||||
assignment.ReminderSent = false
|
||||
}
|
||||
|
||||
if err := s.assignmentRepo.Update(assignment); err != nil {
|
||||
return nil, err
|
||||
@@ -267,3 +278,133 @@ func (s *AssignmentService) GetDashboardStats(userID uint) (*DashboardStats, err
|
||||
Subjects: subjects,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// StatisticsFilter holds filter parameters for statistics
|
||||
type StatisticsFilter struct {
|
||||
Subject string
|
||||
From *time.Time
|
||||
To *time.Time
|
||||
IncludeArchived bool
|
||||
}
|
||||
|
||||
// SubjectStats holds statistics for a subject
|
||||
type SubjectStats struct {
|
||||
Subject string `json:"subject"`
|
||||
Total int64 `json:"total"`
|
||||
Completed int64 `json:"completed"`
|
||||
Pending int64 `json:"pending"`
|
||||
Overdue int64 `json:"overdue"`
|
||||
OnTimeCompletionRate float64 `json:"on_time_completion_rate"`
|
||||
IsArchived bool `json:"is_archived,omitempty"`
|
||||
}
|
||||
|
||||
// StatisticsSummary holds overall statistics
|
||||
type StatisticsSummary struct {
|
||||
TotalAssignments int64 `json:"total_assignments"`
|
||||
CompletedAssignments int64 `json:"completed_assignments"`
|
||||
PendingAssignments int64 `json:"pending_assignments"`
|
||||
OverdueAssignments int64 `json:"overdue_assignments"`
|
||||
OnTimeCompletionRate float64 `json:"on_time_completion_rate"`
|
||||
Filter *FilterInfo `json:"filter,omitempty"`
|
||||
Subjects []SubjectStats `json:"subjects,omitempty"`
|
||||
}
|
||||
|
||||
// FilterInfo shows applied filters in response
|
||||
type FilterInfo struct {
|
||||
Subject *string `json:"subject"`
|
||||
From *string `json:"from"`
|
||||
To *string `json:"to"`
|
||||
IncludeArchived bool `json:"include_archived"`
|
||||
}
|
||||
|
||||
// GetStatistics returns statistics for a user with optional filters
|
||||
func (s *AssignmentService) GetStatistics(userID uint, filter StatisticsFilter) (*StatisticsSummary, error) {
|
||||
// Convert filter to repository filter
|
||||
repoFilter := repository.StatisticsFilter{
|
||||
Subject: filter.Subject,
|
||||
From: filter.From,
|
||||
To: filter.To,
|
||||
IncludeArchived: filter.IncludeArchived,
|
||||
}
|
||||
|
||||
// Get overall statistics
|
||||
stats, err := s.assignmentRepo.GetStatistics(userID, repoFilter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
summary := &StatisticsSummary{
|
||||
TotalAssignments: stats.Total,
|
||||
CompletedAssignments: stats.Completed,
|
||||
PendingAssignments: stats.Pending,
|
||||
OverdueAssignments: stats.Overdue,
|
||||
OnTimeCompletionRate: stats.OnTimeCompletionRate,
|
||||
}
|
||||
|
||||
// Build filter info
|
||||
filterInfo := &FilterInfo{}
|
||||
hasFilter := false
|
||||
if filter.Subject != "" {
|
||||
filterInfo.Subject = &filter.Subject
|
||||
hasFilter = true
|
||||
}
|
||||
if filter.From != nil {
|
||||
fromStr := filter.From.Format("2006-01-02")
|
||||
filterInfo.From = &fromStr
|
||||
hasFilter = true
|
||||
}
|
||||
if filter.To != nil {
|
||||
toStr := filter.To.Format("2006-01-02")
|
||||
filterInfo.To = &toStr
|
||||
hasFilter = true
|
||||
}
|
||||
filterInfo.IncludeArchived = filter.IncludeArchived
|
||||
if filter.IncludeArchived {
|
||||
hasFilter = true
|
||||
}
|
||||
if hasFilter {
|
||||
summary.Filter = filterInfo
|
||||
}
|
||||
|
||||
// If no specific subject filter, get per-subject statistics
|
||||
if filter.Subject == "" {
|
||||
subjectStats, err := s.assignmentRepo.GetStatisticsBySubjects(userID, repoFilter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, ss := range subjectStats {
|
||||
summary.Subjects = append(summary.Subjects, SubjectStats{
|
||||
Subject: ss.Subject,
|
||||
Total: ss.Total,
|
||||
Completed: ss.Completed,
|
||||
Pending: ss.Pending,
|
||||
Overdue: ss.Overdue,
|
||||
OnTimeCompletionRate: ss.OnTimeCompletionRate,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return summary, nil
|
||||
}
|
||||
|
||||
// ArchiveSubject archives all assignments for a subject
|
||||
func (s *AssignmentService) ArchiveSubject(userID uint, subject string) error {
|
||||
return s.assignmentRepo.ArchiveBySubject(userID, subject)
|
||||
}
|
||||
|
||||
// UnarchiveSubject unarchives all assignments for a subject
|
||||
func (s *AssignmentService) UnarchiveSubject(userID uint, subject string) error {
|
||||
return s.assignmentRepo.UnarchiveBySubject(userID, subject)
|
||||
}
|
||||
|
||||
// GetSubjectsWithArchived returns subjects optionally including archived
|
||||
func (s *AssignmentService) GetSubjectsWithArchived(userID uint, includeArchived bool) ([]string, error) {
|
||||
return s.assignmentRepo.GetSubjectsByUserIDWithArchived(userID, includeArchived)
|
||||
}
|
||||
|
||||
// GetArchivedSubjects returns archived subjects only
|
||||
func (s *AssignmentService) GetArchivedSubjects(userID uint) ([]string, error) {
|
||||
return s.assignmentRepo.GetArchivedSubjects(userID)
|
||||
}
|
||||
|
||||
|
||||
330
internal/service/notification_service.go
Normal file
330
internal/service/notification_service.go
Normal file
@@ -0,0 +1,330 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"homework-manager/internal/database"
|
||||
"homework-manager/internal/models"
|
||||
)
|
||||
|
||||
// NotificationService handles Telegram and LINE notifications
|
||||
type NotificationService struct {
|
||||
telegramBotToken string
|
||||
}
|
||||
|
||||
// NewNotificationService creates a new notification service
|
||||
func NewNotificationService(telegramBotToken string) *NotificationService {
|
||||
return &NotificationService{
|
||||
telegramBotToken: telegramBotToken,
|
||||
}
|
||||
}
|
||||
|
||||
// GetUserSettings retrieves notification settings for a user
|
||||
func (s *NotificationService) GetUserSettings(userID uint) (*models.UserNotificationSettings, error) {
|
||||
var settings models.UserNotificationSettings
|
||||
result := database.GetDB().Where("user_id = ?", userID).First(&settings)
|
||||
if result.Error != nil {
|
||||
// If not found, return a new empty settings object
|
||||
if result.RowsAffected == 0 {
|
||||
return &models.UserNotificationSettings{
|
||||
UserID: userID,
|
||||
}, nil
|
||||
}
|
||||
return nil, result.Error
|
||||
}
|
||||
return &settings, nil
|
||||
}
|
||||
|
||||
// UpdateUserSettings updates notification settings for a user
|
||||
func (s *NotificationService) UpdateUserSettings(userID uint, settings *models.UserNotificationSettings) error {
|
||||
settings.UserID = userID
|
||||
|
||||
var existing models.UserNotificationSettings
|
||||
result := database.GetDB().Where("user_id = ?", userID).First(&existing)
|
||||
|
||||
if result.RowsAffected == 0 {
|
||||
// Create new
|
||||
return database.GetDB().Create(settings).Error
|
||||
}
|
||||
|
||||
// Update existing
|
||||
settings.ID = existing.ID
|
||||
return database.GetDB().Save(settings).Error
|
||||
}
|
||||
|
||||
// SendTelegramNotification sends a message via Telegram Bot API
|
||||
func (s *NotificationService) SendTelegramNotification(chatID, message string) error {
|
||||
if s.telegramBotToken == "" {
|
||||
return fmt.Errorf("telegram bot token is not configured")
|
||||
}
|
||||
if chatID == "" {
|
||||
return fmt.Errorf("telegram chat ID is empty")
|
||||
}
|
||||
|
||||
apiURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", s.telegramBotToken)
|
||||
|
||||
payload := map[string]string{
|
||||
"chat_id": chatID,
|
||||
"text": message,
|
||||
"parse_mode": "HTML",
|
||||
}
|
||||
|
||||
jsonPayload, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := http.Post(apiURL, "application/json", bytes.NewBuffer(jsonPayload))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("telegram API returned status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendLineNotification sends a message via LINE Notify API
|
||||
func (s *NotificationService) SendLineNotification(token, message string) error {
|
||||
if token == "" {
|
||||
return fmt.Errorf("LINE Notify token is empty")
|
||||
}
|
||||
|
||||
apiURL := "https://notify-api.line.me/api/notify"
|
||||
|
||||
data := url.Values{}
|
||||
data.Set("message", message)
|
||||
|
||||
req, err := http.NewRequest("POST", apiURL, strings.NewReader(data.Encode()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("LINE Notify API returned status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendAssignmentReminder sends a reminder notification for an assignment
|
||||
func (s *NotificationService) SendAssignmentReminder(userID uint, assignment *models.Assignment) error {
|
||||
settings, err := s.GetUserSettings(userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
message := fmt.Sprintf(
|
||||
"📚 課題リマインダー\n\n【%s】\n科目: %s\n期限: %s\n\n%s",
|
||||
assignment.Title,
|
||||
assignment.Subject,
|
||||
assignment.DueDate.Format("2006/01/02 15:04"),
|
||||
assignment.Description,
|
||||
)
|
||||
|
||||
var errors []string
|
||||
|
||||
// Send to Telegram if enabled
|
||||
if settings.TelegramEnabled && settings.TelegramChatID != "" {
|
||||
if err := s.SendTelegramNotification(settings.TelegramChatID, message); err != nil {
|
||||
errors = append(errors, fmt.Sprintf("Telegram: %v", err))
|
||||
}
|
||||
}
|
||||
|
||||
// Send to LINE if enabled
|
||||
if settings.LineEnabled && settings.LineNotifyToken != "" {
|
||||
if err := s.SendLineNotification(settings.LineNotifyToken, message); err != nil {
|
||||
errors = append(errors, fmt.Sprintf("LINE: %v", err))
|
||||
}
|
||||
}
|
||||
|
||||
if len(errors) > 0 {
|
||||
return fmt.Errorf("notification errors: %s", strings.Join(errors, "; "))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendUrgentReminder sends an urgent reminder notification for an assignment
|
||||
func (s *NotificationService) SendUrgentReminder(userID uint, assignment *models.Assignment) error {
|
||||
settings, err := s.GetUserSettings(userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
timeRemaining := time.Until(assignment.DueDate)
|
||||
var timeStr string
|
||||
if timeRemaining < 0 {
|
||||
timeStr = "期限切れ!"
|
||||
} else if timeRemaining < time.Hour {
|
||||
timeStr = fmt.Sprintf("あと%d分", int(timeRemaining.Minutes()))
|
||||
} else {
|
||||
timeStr = fmt.Sprintf("あと%d時間%d分", int(timeRemaining.Hours()), int(timeRemaining.Minutes())%60)
|
||||
}
|
||||
|
||||
priorityEmoji := "📌"
|
||||
switch assignment.Priority {
|
||||
case "high":
|
||||
priorityEmoji = "🚨"
|
||||
case "medium":
|
||||
priorityEmoji = "⚠️"
|
||||
case "low":
|
||||
priorityEmoji = "📌"
|
||||
}
|
||||
|
||||
message := fmt.Sprintf(
|
||||
"%s 督促通知!\n\n【%s】\n科目: %s\n期限: %s (%s)\n\n完了したらアプリで完了ボタンを押してください!",
|
||||
priorityEmoji,
|
||||
assignment.Title,
|
||||
assignment.Subject,
|
||||
assignment.DueDate.Format("2006/01/02 15:04"),
|
||||
timeStr,
|
||||
)
|
||||
|
||||
var errors []string
|
||||
|
||||
// Send to Telegram if enabled
|
||||
if settings.TelegramEnabled && settings.TelegramChatID != "" {
|
||||
if err := s.SendTelegramNotification(settings.TelegramChatID, message); err != nil {
|
||||
errors = append(errors, fmt.Sprintf("Telegram: %v", err))
|
||||
}
|
||||
}
|
||||
|
||||
// Send to LINE if enabled
|
||||
if settings.LineEnabled && settings.LineNotifyToken != "" {
|
||||
if err := s.SendLineNotification(settings.LineNotifyToken, message); err != nil {
|
||||
errors = append(errors, fmt.Sprintf("LINE: %v", err))
|
||||
}
|
||||
}
|
||||
|
||||
if len(errors) > 0 {
|
||||
return fmt.Errorf("notification errors: %s", strings.Join(errors, "; "))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getUrgentReminderInterval returns the reminder interval based on priority
|
||||
// high=10min, medium=30min, low=60min
|
||||
func getUrgentReminderInterval(priority string) time.Duration {
|
||||
switch priority {
|
||||
case "high":
|
||||
return 10 * time.Minute
|
||||
case "medium":
|
||||
return 30 * time.Minute
|
||||
case "low":
|
||||
return 60 * time.Minute
|
||||
default:
|
||||
return 30 * time.Minute
|
||||
}
|
||||
}
|
||||
|
||||
// ProcessPendingReminders checks and sends pending one-time reminders
|
||||
func (s *NotificationService) ProcessPendingReminders() {
|
||||
now := time.Now()
|
||||
|
||||
var assignments []models.Assignment
|
||||
result := database.GetDB().Where(
|
||||
"reminder_enabled = ? AND reminder_sent = ? AND reminder_at <= ? AND is_completed = ?",
|
||||
true, false, now, false,
|
||||
).Find(&assignments)
|
||||
|
||||
if result.Error != nil {
|
||||
log.Printf("Error fetching pending reminders: %v", result.Error)
|
||||
return
|
||||
}
|
||||
|
||||
for _, assignment := range assignments {
|
||||
if err := s.SendAssignmentReminder(assignment.UserID, &assignment); err != nil {
|
||||
log.Printf("Error sending reminder for assignment %d: %v", assignment.ID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Mark as sent
|
||||
database.GetDB().Model(&assignment).Update("reminder_sent", true)
|
||||
log.Printf("Sent reminder for assignment %d to user %d", assignment.ID, assignment.UserID)
|
||||
}
|
||||
}
|
||||
|
||||
// ProcessUrgentReminders checks and sends urgent (repeating) reminders
|
||||
// Starts 3 hours before deadline, repeats at interval based on priority
|
||||
func (s *NotificationService) ProcessUrgentReminders() {
|
||||
now := time.Now()
|
||||
urgentStartTime := 3 * time.Hour // Start 3 hours before deadline
|
||||
|
||||
var assignments []models.Assignment
|
||||
result := database.GetDB().Where(
|
||||
"urgent_reminder_enabled = ? AND is_completed = ? AND due_date > ?",
|
||||
true, false, now,
|
||||
).Find(&assignments)
|
||||
|
||||
if result.Error != nil {
|
||||
log.Printf("Error fetching urgent reminders: %v", result.Error)
|
||||
return
|
||||
}
|
||||
|
||||
for _, assignment := range assignments {
|
||||
timeUntilDue := assignment.DueDate.Sub(now)
|
||||
|
||||
// Only send if within 3 hours of deadline
|
||||
if timeUntilDue > urgentStartTime {
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if enough time has passed since last urgent reminder
|
||||
interval := getUrgentReminderInterval(assignment.Priority)
|
||||
|
||||
if assignment.LastUrgentReminderSent != nil {
|
||||
timeSinceLastReminder := now.Sub(*assignment.LastUrgentReminderSent)
|
||||
if timeSinceLastReminder < interval {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// Send urgent reminder
|
||||
if err := s.SendUrgentReminder(assignment.UserID, &assignment); err != nil {
|
||||
log.Printf("Error sending urgent reminder for assignment %d: %v", assignment.ID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Update last sent time
|
||||
database.GetDB().Model(&assignment).Update("last_urgent_reminder_sent", now)
|
||||
log.Printf("Sent urgent reminder for assignment %d (priority: %s) to user %d",
|
||||
assignment.ID, assignment.Priority, assignment.UserID)
|
||||
}
|
||||
}
|
||||
|
||||
// StartReminderScheduler starts a background goroutine to process reminders
|
||||
func (s *NotificationService) StartReminderScheduler() {
|
||||
go func() {
|
||||
ticker := time.NewTicker(1 * time.Minute)
|
||||
defer ticker.Stop()
|
||||
|
||||
for range ticker.C {
|
||||
s.ProcessPendingReminders()
|
||||
s.ProcessUrgentReminders()
|
||||
}
|
||||
}()
|
||||
log.Println("Reminder scheduler started (one-time + urgent reminders)")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user