diff --git a/cmd/publisher.go b/cmd/publisher.go index f46202d..f177b76 100644 --- a/cmd/publisher.go +++ b/cmd/publisher.go @@ -309,7 +309,7 @@ var publisherScrapingNewsCmd = &cobra.Command{ /** * Looping schedule */ - timeDuration := time.Duration(45) + timeDuration := time.Duration(35) for { diff --git a/internal/domain/repository/subscription_follow_league_repository.go b/internal/domain/repository/subscription_follow_league_repository.go index 5ef82e9..5ba3f42 100644 --- a/internal/domain/repository/subscription_follow_league_repository.go +++ b/internal/domain/repository/subscription_follow_league_repository.go @@ -42,7 +42,7 @@ func (r *SubscriptionFollowLeagueRepository) CountByLimit(subId int64) (int64, e var count int64 err := r.db.Model(&entity.SubscriptionFollowLeague{}).Where( &entity.SubscriptionFollowLeague{SubscriptionID: subId, IsActive: true}). - Where("limit_per_day >= sent").Count(&count).Error + Where("limit_per_day >= sent AND DATE(updated_at) = DATE(NOW())").Count(&count).Error if err != nil { return count, err } diff --git a/internal/domain/repository/subscription_follow_team_repository.go b/internal/domain/repository/subscription_follow_team_repository.go index ff22d26..8aad8b7 100644 --- a/internal/domain/repository/subscription_follow_team_repository.go +++ b/internal/domain/repository/subscription_follow_team_repository.go @@ -18,6 +18,8 @@ func NewSubscriptionFollowTeamRepository(db *gorm.DB) *SubscriptionFollowTeamRep type ISubscriptionFollowTeamRepository interface { CountBySub(int64) (int64, error) CountByTeam(int64) (int64, error) + CountByLimit(int64) (int64, error) + CountByUpdated(subId int64) (int64, error) GetAllPaginate(*entity.Pagination) (*entity.Pagination, error) GetBySub(int64) (*entity.SubscriptionFollowTeam, error) Save(*entity.SubscriptionFollowTeam) (*entity.SubscriptionFollowTeam, error) @@ -45,6 +47,28 @@ func (r *SubscriptionFollowTeamRepository) CountByTeam(teamId int64) (int64, err return count, nil } +func (r *SubscriptionFollowTeamRepository) CountByLimit(subId int64) (int64, error) { + var count int64 + err := r.db.Model(&entity.SubscriptionFollowTeam{}).Where( + &entity.SubscriptionFollowTeam{SubscriptionID: subId, IsActive: true}). + Where("limit_per_day >= sent AND DATE(updated_at) = DATE(NOW())").Count(&count).Error + if err != nil { + return count, err + } + return count, nil +} + +func (r *SubscriptionFollowTeamRepository) CountByUpdated(subId int64) (int64, error) { + var count int64 + err := r.db.Model(&entity.SubscriptionFollowTeam{}).Where( + &entity.SubscriptionFollowTeam{SubscriptionID: subId, IsActive: true}). + Where("").Count(&count).Error + if err != nil { + return count, err + } + return count, nil +} + func (r *SubscriptionFollowTeamRepository) GetAllPaginate(pagination *entity.Pagination) (*entity.Pagination, error) { var creditgoals []*entity.SubscriptionFollowTeam err := r.db.Scopes(Paginate(creditgoals, pagination, r.db)).Find(&creditgoals).Error diff --git a/internal/handler/news_handler.go b/internal/handler/news_handler.go index 13ed6b0..93c38e5 100644 --- a/internal/handler/news_handler.go +++ b/internal/handler/news_handler.go @@ -387,17 +387,22 @@ func (h *NewsHandler) SMSAlerteLeague(leagueId int64) { if len(*subs) > 0 { for _, s := range *subs { + // counter + h.subscriptionFollowLeagueService.Sent(&entity.SubscriptionFollowLeague{SubscriptionID: s.SubscriptionID, LeagueID: leagueId}) + // limit + if h.subscriptionFollowLeagueService.IsLimit(s.SubscriptionID) { + jsonData, err := json.Marshal(&entity.SMSAlerte{SubscriptionID: s.SubscriptionID, NewsID: h.news.GetId()}) + if err != nil { + log.Println(err.Error()) + } - jsonData, err := json.Marshal(&entity.SMSAlerte{SubscriptionID: s.SubscriptionID, NewsID: h.news.GetId()}) - if err != nil { - log.Println(err.Error()) + h.rmq.IntegratePublish( + RMQ_SMS_ALERTE_EXCHANGE, + RMQ_SMS_ALERTE_QUEUE, + RMQ_DATA_TYPE, "", string(jsonData), + ) } - h.rmq.IntegratePublish( - RMQ_SMS_ALERTE_EXCHANGE, - RMQ_SMS_ALERTE_QUEUE, - RMQ_DATA_TYPE, "", string(jsonData), - ) } } } @@ -408,17 +413,22 @@ func (h *NewsHandler) SMSAlerteTeam(teamId int64) { if len(*subs) > 0 { for _, s := range *subs { + // counter + h.subscriptionFollowTeamService.Sent(&entity.SubscriptionFollowTeam{SubscriptionID: s.SubscriptionID, TeamID: teamId}) + // limit + if h.subscriptionFollowTeamService.IsLimit(s.SubscriptionID) { + jsonData, err := json.Marshal(&entity.SMSAlerte{SubscriptionID: s.SubscriptionID, NewsID: h.news.GetId()}) + if err != nil { + log.Println(err.Error()) + } - jsonData, err := json.Marshal(&entity.SMSAlerte{SubscriptionID: s.SubscriptionID, NewsID: h.news.GetId()}) - if err != nil { - log.Println(err.Error()) + h.rmq.IntegratePublish( + RMQ_SMS_ALERTE_EXCHANGE, + RMQ_SMS_ALERTE_QUEUE, + RMQ_DATA_TYPE, "", string(jsonData), + ) } - h.rmq.IntegratePublish( - RMQ_SMS_ALERTE_EXCHANGE, - RMQ_SMS_ALERTE_QUEUE, - RMQ_DATA_TYPE, "", string(jsonData), - ) } } } diff --git a/internal/handler/sms_alerte_handler.go b/internal/handler/sms_alerte_handler.go index a967b99..5e3e1dd 100644 --- a/internal/handler/sms_alerte_handler.go +++ b/internal/handler/sms_alerte_handler.go @@ -53,6 +53,7 @@ func (h *SMSAlerteHandler) SMSAlerte() { if !h.smsAlerteService.ISMSAlerte(h.sub.SubscriptionID, h.sub.NewsID) { if h.subscriptionService.IsActiveSubscriptionBySubId(h.sub.SubscriptionID) { + // save h.smsAlerteService.Save( &entity.SMSAlerte{ diff --git a/internal/services/subscription_follow_league_service.go b/internal/services/subscription_follow_league_service.go index a5cef46..a8b0baf 100644 --- a/internal/services/subscription_follow_league_service.go +++ b/internal/services/subscription_follow_league_service.go @@ -28,6 +28,7 @@ type ISubscriptionFollowLeagueService interface { GetBySub(int64) (*entity.SubscriptionFollowLeague, error) Save(*entity.SubscriptionFollowLeague) (*entity.SubscriptionFollowLeague, error) Update(*entity.SubscriptionFollowLeague) (*entity.SubscriptionFollowLeague, error) + Sent(*entity.SubscriptionFollowLeague) error Disable(*entity.SubscriptionFollowLeague) error Delete(*entity.SubscriptionFollowLeague) error GetAllSubByLeague(int64) *[]entity.SubscriptionFollowLeague @@ -83,9 +84,7 @@ func (s *SubscriptionFollowLeagueService) Sent(a *entity.SubscriptionFollowLeagu }, ) } - } - - if !s.IsUpdated(a.SubscriptionID) { + } else { // reset s.subFollowLeagueRepo.Update( &entity.SubscriptionFollowLeague{ diff --git a/internal/services/subscription_follow_team_service.go b/internal/services/subscription_follow_team_service.go index 8f4aee3..e94b574 100644 --- a/internal/services/subscription_follow_team_service.go +++ b/internal/services/subscription_follow_team_service.go @@ -22,10 +22,13 @@ func NewSubscriptionFollowTeamService( type ISubscriptionFollowTeamService interface { IsSub(int64) bool IsTeam(int64) bool + IsLimit(int64) bool + IsUpdated(int64) bool GetAllPaginate(*entity.Pagination) (*entity.Pagination, error) GetBySub(int64) (*entity.SubscriptionFollowTeam, error) Save(*entity.SubscriptionFollowTeam) (*entity.SubscriptionFollowTeam, error) Update(*entity.SubscriptionFollowTeam) (*entity.SubscriptionFollowTeam, error) + Sent(*entity.SubscriptionFollowTeam) error Disable(*entity.SubscriptionFollowTeam) error Delete(*entity.SubscriptionFollowTeam) error GetAllSubByTeam(int64) *[]entity.SubscriptionFollowTeam @@ -41,6 +44,16 @@ func (s *SubscriptionFollowTeamService) IsTeam(teamId int64) bool { return count > 0 } +func (s *SubscriptionFollowTeamService) IsLimit(subId int64) bool { + count, _ := s.subFollowTeamRepo.CountByLimit(subId) + return count > 0 +} + +func (s *SubscriptionFollowTeamService) IsUpdated(subId int64) bool { + count, _ := s.subFollowTeamRepo.CountByUpdated(subId) + return count > 0 +} + func (s *SubscriptionFollowTeamService) GetAllPaginate(pagination *entity.Pagination) (*entity.Pagination, error) { return s.subFollowTeamRepo.GetAllPaginate(pagination) } @@ -57,6 +70,32 @@ func (s *SubscriptionFollowTeamService) Update(a *entity.SubscriptionFollowTeam) return s.subFollowTeamRepo.Update(a) } +func (s *SubscriptionFollowTeamService) Sent(a *entity.SubscriptionFollowTeam) error { + if s.IsUpdated(a.SubscriptionID) { + if s.IsLimit(a.SubscriptionID) { + sl, err := s.GetBySub(a.SubscriptionID) + if err != nil { + return err + } + s.subFollowTeamRepo.Update( + &entity.SubscriptionFollowTeam{ + SubscriptionID: a.SubscriptionID, + Sent: sl.Sent + 1, + }, + ) + } + } else { + // reset + s.subFollowTeamRepo.Update( + &entity.SubscriptionFollowTeam{ + SubscriptionID: a.SubscriptionID, + Sent: 1, + }, + ) + } + return nil +} + func (s *SubscriptionFollowTeamService) Disable(a *entity.SubscriptionFollowTeam) error { return s.subFollowTeamRepo.Disable(a) }