diff --git a/cmd/publisher.go b/cmd/publisher.go index c258f5e..3ec6d62 100644 --- a/cmd/publisher.go +++ b/cmd/publisher.go @@ -2,7 +2,6 @@ package cmd import ( "encoding/json" - "log" "time" "github.com/idprm/go-football-alert/internal/domain/entity" @@ -395,14 +394,11 @@ var publisherSMSAlerteCmd = &cobra.Command{ for { timeNow := time.Now().Format("15:04") - log.Println(timeNow) scheduleRepo := repository.NewScheduleRepository(db) scheduleService := services.NewScheduleService(scheduleRepo) if scheduleService.IsUnlocked(ACT_SMS_ALERTE, timeNow) { - log.Println(timeNow) - scheduleService.UpdateLocked( &entity.Schedule{ Name: ACT_SMS_ALERTE, diff --git a/internal/domain/entity/subscription_follow_league.go b/internal/domain/entity/subscription_follow_league.go index 5f34b5c..53e5d45 100644 --- a/internal/domain/entity/subscription_follow_league.go +++ b/internal/domain/entity/subscription_follow_league.go @@ -9,6 +9,6 @@ type SubscriptionFollowLeague struct { LeagueID int64 `json:"league_id"` League *League `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"league,omitempty"` LimitPerDay int `json:"limit_by_day"` - IsActive bool `gorm:"type:boolean;column:is_active" json:"is_active,omitempty"` + IsActive bool `gorm:"type:boolean;default:false;column:is_active" json:"is_active,omitempty"` gorm.Model `json:"-"` } diff --git a/internal/domain/entity/subscription_follow_team.go b/internal/domain/entity/subscription_follow_team.go index a20b41d..2aa2090 100644 --- a/internal/domain/entity/subscription_follow_team.go +++ b/internal/domain/entity/subscription_follow_team.go @@ -9,6 +9,6 @@ type SubscriptionFollowTeam struct { TeamID int64 `json:"team_id"` Team *Team `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"team,omitempty"` LimitPerDay int `json:"limit_by_day"` - IsActive bool `gorm:"type:boolean;column:is_active" json:"is_active,omitempty"` + IsActive bool `gorm:"type:boolean;default:false;column:is_active" json:"is_active,omitempty"` gorm.Model `json:"-"` } diff --git a/internal/domain/repository/subscription_follow_league_repository.go b/internal/domain/repository/subscription_follow_league_repository.go index 170cf0c..654de6a 100644 --- a/internal/domain/repository/subscription_follow_league_repository.go +++ b/internal/domain/repository/subscription_follow_league_repository.go @@ -22,12 +22,13 @@ type ISubscriptionFollowLeagueRepository interface { GetBySub(int64) (*entity.SubscriptionFollowLeague, error) Save(*entity.SubscriptionFollowLeague) (*entity.SubscriptionFollowLeague, error) Update(*entity.SubscriptionFollowLeague) (*entity.SubscriptionFollowLeague, error) + Disable(*entity.SubscriptionFollowLeague) error Delete(*entity.SubscriptionFollowLeague) error } func (r *SubscriptionFollowLeagueRepository) CountBySub(subId int64) (int64, error) { var count int64 - err := r.db.Model(&entity.SubscriptionFollowLeague{}).Where(&entity.SubscriptionFollowLeague{SubscriptionID: subId}).Count(&count).Error + err := r.db.Model(&entity.SubscriptionFollowLeague{}).Where(&entity.SubscriptionFollowLeague{SubscriptionID: subId, IsActive: true}).Count(&count).Error if err != nil { return count, err } @@ -55,7 +56,7 @@ func (r *SubscriptionFollowLeagueRepository) GetAllPaginate(pagination *entity.P func (r *SubscriptionFollowLeagueRepository) GetBySub(subId int64) (*entity.SubscriptionFollowLeague, error) { var c entity.SubscriptionFollowLeague - err := r.db.Where(&entity.SubscriptionFollowLeague{SubscriptionID: subId}).Take(&c).Error + err := r.db.Where(&entity.SubscriptionFollowLeague{SubscriptionID: subId, IsActive: true}).Take(&c).Error if err != nil { return nil, err } @@ -71,13 +72,21 @@ func (r *SubscriptionFollowLeagueRepository) Save(c *entity.SubscriptionFollowLe } func (r *SubscriptionFollowLeagueRepository) Update(c *entity.SubscriptionFollowLeague) (*entity.SubscriptionFollowLeague, error) { - err := r.db.Where("id = ?", c.ID).Updates(&c).Error + err := r.db.Where("subscription_id = ?", c.SubscriptionID).Updates(&c).Error if err != nil { return nil, err } return c, nil } +func (r *SubscriptionFollowLeagueRepository) Disable(c *entity.SubscriptionFollowLeague) error { + err := r.db.Where("subscription_id = ?", c.SubscriptionID).Updates(map[string]interface{}{"is_active": false}).Error + if err != nil { + return err + } + return nil +} + func (r *SubscriptionFollowLeagueRepository) Delete(c *entity.SubscriptionFollowLeague) error { err := r.db.Delete(&c, c.ID).Error if err != nil { diff --git a/internal/domain/repository/subscription_follow_team_repository.go b/internal/domain/repository/subscription_follow_team_repository.go index 51faf29..c4ff57c 100644 --- a/internal/domain/repository/subscription_follow_team_repository.go +++ b/internal/domain/repository/subscription_follow_team_repository.go @@ -22,12 +22,13 @@ type ISubscriptionFollowTeamRepository interface { GetBySub(int64) (*entity.SubscriptionFollowTeam, error) Save(*entity.SubscriptionFollowTeam) (*entity.SubscriptionFollowTeam, error) Update(*entity.SubscriptionFollowTeam) (*entity.SubscriptionFollowTeam, error) + Disable(*entity.SubscriptionFollowTeam) error Delete(*entity.SubscriptionFollowTeam) error } func (r *SubscriptionFollowTeamRepository) CountBySub(subId int64) (int64, error) { var count int64 - err := r.db.Model(&entity.SubscriptionFollowTeam{}).Where(&entity.SubscriptionFollowTeam{SubscriptionID: subId}).Count(&count).Error + err := r.db.Model(&entity.SubscriptionFollowTeam{}).Where(&entity.SubscriptionFollowTeam{SubscriptionID: subId, IsActive: true}).Count(&count).Error if err != nil { return count, err } @@ -55,7 +56,7 @@ func (r *SubscriptionFollowTeamRepository) GetAllPaginate(pagination *entity.Pag func (r *SubscriptionFollowTeamRepository) GetBySub(subId int64) (*entity.SubscriptionFollowTeam, error) { var c entity.SubscriptionFollowTeam - err := r.db.Where(&entity.SubscriptionFollowTeam{SubscriptionID: subId}).Take(&c).Error + err := r.db.Where(&entity.SubscriptionFollowTeam{SubscriptionID: subId, IsActive: true}).Take(&c).Error if err != nil { return nil, err } @@ -78,6 +79,14 @@ func (r *SubscriptionFollowTeamRepository) Update(c *entity.SubscriptionFollowTe return c, nil } +func (r *SubscriptionFollowTeamRepository) Disable(c *entity.SubscriptionFollowTeam) error { + err := r.db.Where("subscription_id = ?", c.SubscriptionID).Updates(map[string]interface{}{"is_active": false}).Error + if err != nil { + return err + } + return nil +} + func (r *SubscriptionFollowTeamRepository) Delete(c *entity.SubscriptionFollowTeam) error { err := r.db.Delete(&c, c.ID).Error if err != nil { diff --git a/internal/domain/repository/team_repository.go b/internal/domain/repository/team_repository.go index f65e8d6..1066d0e 100644 --- a/internal/domain/repository/team_repository.go +++ b/internal/domain/repository/team_repository.go @@ -54,7 +54,7 @@ func (r *TeamRepository) CountByPrimaryId(primaryId int) (int64, error) { func (r *TeamRepository) CountByName(name string) (int64, error) { var count int64 - err := r.db.Model(&entity.Team{}).Where("UPPER(name) LIKE UPPER(?)", "%"+name+"%").Or("UPPER(keyword) LIKE UPPER(?)", "%"+name+"%").Count(&count).Error + err := r.db.Model(&entity.Team{}).Where("UPPER(name) LIKE UPPER(?)", "%"+name+"%").Or("UPPER(code) LIKE UPPER(?)", "%"+name+"%").Count(&count).Error if err != nil { return count, err } @@ -118,7 +118,7 @@ func (r *TeamRepository) GetByPrimaryId(primaryId int) (*entity.Team, error) { func (r *TeamRepository) GetByName(name string) (*entity.Team, error) { var c entity.Team - err := r.db.Where("UPPER(name) LIKE UPPER(?) OR UPPER(keyword) LIKE UPPER(?)", "%"+name+"%", "%"+name+"%").Take(&c).Error + err := r.db.Where("UPPER(name) LIKE UPPER(?) OR UPPER(code) LIKE UPPER(?)", "%"+name+"%", "%"+name+"%").Take(&c).Error if err != nil { return nil, err } diff --git a/internal/handler/sms_handler.go b/internal/handler/sms_handler.go index d098242..49bacc2 100644 --- a/internal/handler/sms_handler.go +++ b/internal/handler/sms_handler.go @@ -219,12 +219,31 @@ func (h *SMSHandler) SubAlerteCompetition(league *entity.League) { log.Println(err.Error()) } - // insert in follow if !h.subscriptionFollowLeagueService.IsSub(sub.GetId()) { + // insert follow league h.subscriptionFollowLeagueService.Save( &entity.SubscriptionFollowLeague{ SubscriptionID: sub.GetId(), LeagueID: league.GetId(), + IsActive: true, + }, + ) + } else { + // update follow league + h.subscriptionFollowLeagueService.Update( + &entity.SubscriptionFollowLeague{ + SubscriptionID: sub.GetId(), + LeagueID: league.GetId(), + IsActive: true, + }, + ) + } + + // disable if follow team + if h.subscriptionFollowTeamService.IsSub(sub.GetId()) { + h.subscriptionFollowTeamService.Disable( + &entity.SubscriptionFollowTeam{ + SubscriptionID: sub.GetId(), }, ) } @@ -445,12 +464,31 @@ func (h *SMSHandler) SubAlerteEquipe(team *entity.Team) { log.Println(err.Error()) } - // insert in follow if !h.subscriptionFollowTeamService.IsSub(sub.GetId()) { + // insert follow team h.subscriptionFollowTeamService.Save( &entity.SubscriptionFollowTeam{ SubscriptionID: sub.GetId(), TeamID: team.GetId(), + IsActive: true, + }, + ) + } else { + // update follow team + h.subscriptionFollowTeamService.Update( + &entity.SubscriptionFollowTeam{ + SubscriptionID: sub.GetId(), + TeamID: team.GetId(), + IsActive: true, + }, + ) + } + + // disable if follow league + if h.subscriptionFollowLeagueService.IsSub(sub.GetId()) { + h.subscriptionFollowLeagueService.Disable( + &entity.SubscriptionFollowLeague{ + SubscriptionID: sub.GetId(), }, ) } @@ -660,6 +698,16 @@ func (h *SMSHandler) AlreadySubAlerteCompetition(league *entity.League) { &entity.SubscriptionFollowLeague{ SubscriptionID: sub.GetId(), LeagueID: league.GetId(), + IsActive: true, + }, + ) + } + + // disable if follow team + if h.subscriptionFollowTeamService.IsSub(sub.GetId()) { + h.subscriptionFollowTeamService.Disable( + &entity.SubscriptionFollowTeam{ + SubscriptionID: sub.GetId(), }, ) } @@ -709,6 +757,16 @@ func (h *SMSHandler) AlreadySubAlerteEquipe(team *entity.Team) { &entity.SubscriptionFollowTeam{ SubscriptionID: sub.GetId(), TeamID: team.GetId(), + IsActive: true, + }, + ) + } + + // disable if follow league + if h.subscriptionFollowLeagueService.IsSub(sub.GetId()) { + h.subscriptionFollowLeagueService.Disable( + &entity.SubscriptionFollowLeague{ + SubscriptionID: sub.GetId(), }, ) } @@ -807,6 +865,26 @@ func (h *SMSHandler) Stop(category string) { }, ) + // If SMS Alerte + if category == CATEGORY_SMSALERTE { + // unfollow league + if h.subscriptionFollowLeagueService.IsSub(sub.GetId()) { + h.subscriptionFollowLeagueService.Disable( + &entity.SubscriptionFollowLeague{ + SubscriptionID: sub.GetId(), + }, + ) + } + + // unfollow team + if h.subscriptionFollowTeamService.IsSub(sub.GetId()) { + h.subscriptionFollowTeamService.Disable( + &entity.SubscriptionFollowTeam{ + SubscriptionID: sub.GetId(), + }, + ) + } + } mt := &model.MTRequest{ Smsc: h.req.GetTo(), Keyword: h.req.GetSMS(), diff --git a/internal/services/subscription_follow_league_service.go b/internal/services/subscription_follow_league_service.go index 8ab17ab..3b1cc16 100644 --- a/internal/services/subscription_follow_league_service.go +++ b/internal/services/subscription_follow_league_service.go @@ -24,6 +24,7 @@ type ISubscriptionFollowLeagueService interface { GetBySub(int64) (*entity.SubscriptionFollowLeague, error) Save(*entity.SubscriptionFollowLeague) (*entity.SubscriptionFollowLeague, error) Update(*entity.SubscriptionFollowLeague) (*entity.SubscriptionFollowLeague, error) + Disable(*entity.SubscriptionFollowLeague) error Delete(*entity.SubscriptionFollowLeague) error } @@ -53,6 +54,10 @@ func (s *SubscriptionFollowLeagueService) Update(a *entity.SubscriptionFollowLea return s.subFollowLeagueRepo.Update(a) } +func (s *SubscriptionFollowLeagueService) Disable(a *entity.SubscriptionFollowLeague) error { + return s.subFollowLeagueRepo.Disable(a) +} + func (s *SubscriptionFollowLeagueService) Delete(a *entity.SubscriptionFollowLeague) error { return s.subFollowLeagueRepo.Delete(a) } diff --git a/internal/services/subscription_follow_team_service.go b/internal/services/subscription_follow_team_service.go index 55125f9..db5814e 100644 --- a/internal/services/subscription_follow_team_service.go +++ b/internal/services/subscription_follow_team_service.go @@ -24,6 +24,7 @@ type ISubscriptionFollowTeamService interface { GetBySub(int64) (*entity.SubscriptionFollowTeam, error) Save(*entity.SubscriptionFollowTeam) (*entity.SubscriptionFollowTeam, error) Update(*entity.SubscriptionFollowTeam) (*entity.SubscriptionFollowTeam, error) + Disable(*entity.SubscriptionFollowTeam) error Delete(*entity.SubscriptionFollowTeam) error } @@ -53,6 +54,10 @@ func (s *SubscriptionFollowTeamService) Update(a *entity.SubscriptionFollowTeam) return s.subFollowTeamRepo.Update(a) } +func (s *SubscriptionFollowTeamService) Disable(a *entity.SubscriptionFollowTeam) error { + return s.subFollowTeamRepo.Disable(a) +} + func (s *SubscriptionFollowTeamService) Delete(a *entity.SubscriptionFollowTeam) error { return s.subFollowTeamRepo.Delete(a) }