Skip to content

Commit

Permalink
stop sms alerte
Browse files Browse the repository at this point in the history
  • Loading branch information
idprm committed Oct 12, 2024
1 parent 3448e51 commit e2a8f29
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 15 deletions.
4 changes: 0 additions & 4 deletions cmd/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"encoding/json"
"log"
"time"

"github.com/idprm/go-football-alert/internal/domain/entity"
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion internal/domain/entity/subscription_follow_league.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`
}
2 changes: 1 addition & 1 deletion internal/domain/entity/subscription_follow_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
13 changes: 11 additions & 2 deletions internal/domain/repository/subscription_follow_team_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions internal/domain/repository/team_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
82 changes: 80 additions & 2 deletions internal/handler/sms_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
)
}
Expand Down Expand Up @@ -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(),
},
)
}
Expand Down Expand Up @@ -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(),
},
)
}
Expand Down Expand Up @@ -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(),
},
)
}
Expand Down Expand Up @@ -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(),
Expand Down
5 changes: 5 additions & 0 deletions internal/services/subscription_follow_league_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
5 changes: 5 additions & 0 deletions internal/services/subscription_follow_team_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}

0 comments on commit e2a8f29

Please sign in to comment.