forked from ebhomengo/niki
64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"git.gocasts.ir/ebhomengo/niki/types"
|
|
"time"
|
|
)
|
|
|
|
func (s *CampaignService) MonitorCampaignProgress(ctx context.Context) {
|
|
|
|
//c := cron.New()
|
|
//
|
|
//c.AddFunc("@hourly", func() {
|
|
// s.checkAndCompleteCampaigns(context.Background())
|
|
//})
|
|
//
|
|
//c.Start()
|
|
|
|
//ticker := time.NewTicker(1 * time.Hour) // Check every hour
|
|
//defer ticker.Stop()
|
|
//
|
|
//for {
|
|
// select {
|
|
// case <-ticker.C:
|
|
// s.checkAndCompleteCampaigns(ctx)
|
|
// case <-ctx.Done():
|
|
// return
|
|
// }
|
|
//}
|
|
}
|
|
|
|
func (s *CampaignService) checkAndCompleteCampaigns(ctx context.Context) {
|
|
activeCampaigns, err := s.repoStatus.FindActiveCampaigns(ctx) // Method to fetch active campaigns
|
|
if err != nil {
|
|
// Log the error, but don't stop the monitor
|
|
return
|
|
}
|
|
|
|
for _, campaign := range activeCampaigns {
|
|
// Check if deadline has passed
|
|
if campaign.DeadlineAt != nil && campaign.DeadlineAt.Before(time.Now()) {
|
|
if campaign.Status != types.CampaignFinished {
|
|
campaign.Status = types.CampaignFinished
|
|
err := s.repoStatus.UpdateStatus(ctx, campaign.ID, types.CampaignFinished) // Method to update status
|
|
if err != nil {
|
|
// Log error for this specific campaign
|
|
}
|
|
}
|
|
continue // Move to next campaign
|
|
}
|
|
|
|
// Check if goal is met
|
|
if campaign.RaisedAmount >= campaign.GoalAmount {
|
|
if campaign.Status != types.CampaignFinished {
|
|
campaign.Status = types.CampaignFinished
|
|
err := s.repoStatus.UpdateStatus(ctx, campaign.ID, types.CampaignFinished) // Method to update status
|
|
if err != nil {
|
|
// Log error for this specific campaign
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|