diff --git a/cmd/listener.go b/cmd/listener.go index e4732b1..9a1dedd 100644 --- a/cmd/listener.go +++ b/cmd/listener.go @@ -70,8 +70,6 @@ var listenerCmd = &cobra.Command{ &entity.News{}, &entity.NewsLeagues{}, &entity.NewsTeams{}, - &entity.FollowLeague{}, - &entity.FollowTeam{}, &entity.Schedule{}, &entity.Service{}, &entity.Content{}, @@ -80,6 +78,7 @@ var listenerCmd = &cobra.Command{ &entity.SubscriptionPredict{}, &entity.SubscriptionFollowLeague{}, &entity.SubscriptionFollowTeam{}, + &entity.SMSAlerte{}, &entity.Transaction{}, &entity.History{}, &entity.Reward{}, @@ -158,12 +157,6 @@ func routeUrlListener(db *gorm.DB, rds *redis.Client, rmq rmqp.AMQP, logger *log newsRepo := repository.NewNewsRepository(db) newsService := services.NewNewsService(newsRepo) - FollowLeagueRepo := repository.NewFollowLeagueRepository(db) - FollowLeagueService := services.NewFollowLeagueService(FollowLeagueRepo) - - followTeamRepo := repository.NewFollowTeamRepository(db) - followTeamService := services.NewFollowTeamService(followTeamRepo) - scheduleRepo := repository.NewScheduleRepository(db) scheduleService := services.NewScheduleService(scheduleRepo) @@ -223,8 +216,6 @@ func routeUrlListener(db *gorm.DB, rds *redis.Client, rmq rmqp.AMQP, logger *log leagueService, teamService, newsService, - FollowLeagueService, - followTeamService, &entity.News{}, ) diff --git a/cmd/processor.go b/cmd/processor.go index d942b05..4072603 100644 --- a/cmd/processor.go +++ b/cmd/processor.go @@ -164,10 +164,6 @@ func (p *Processor) News(wg *sync.WaitGroup, message []byte) { teamService := services.NewTeamService(teamRepo) newsRepo := repository.NewNewsRepository(p.db) newsService := services.NewNewsService(newsRepo) - FollowLeagueRepo := repository.NewFollowLeagueRepository(p.db) - FollowLeagueService := services.NewFollowLeagueService(FollowLeagueRepo) - followTeamRepo := repository.NewFollowTeamRepository(p.db) - followTeamService := services.NewFollowTeamService(followTeamRepo) // parsing json to string var news *entity.News @@ -177,8 +173,6 @@ func (p *Processor) News(wg *sync.WaitGroup, message []byte) { leagueService, teamService, newsService, - FollowLeagueService, - followTeamService, news, ) @@ -193,14 +187,16 @@ func (p *Processor) SMSAlerte(wg *sync.WaitGroup, message []byte) { */ serviceRepo := repository.NewServiceRepository(p.db) serviceService := services.NewServiceService(serviceRepo) - contentRepo := repository.NewContentRepository(p.db) - contentService := services.NewContentService(contentRepo) subscriptionRepo := repository.NewSubscriptionRepository(p.db) subscriptionService := services.NewSubscriptionService(subscriptionRepo) - transactionRepo := repository.NewTransactionRepository(p.db) - transactionService := services.NewTransactionService(transactionRepo) newsRepo := repository.NewNewsRepository(p.db) newsService := services.NewNewsService(newsRepo) + subscriptionFollowLeagueRepo := repository.NewSubscriptionFollowLeagueRepository(p.db) + subscriptionFollowLeagueService := services.NewSubscriptionFollowLeagueService(subscriptionFollowLeagueRepo) + subscriptionFollowTeamRepo := repository.NewSubscriptionFollowTeamRepository(p.db) + subscriptionFollowTeamService := services.NewSubscriptionFollowTeamService(subscriptionFollowTeamRepo) + smsAlerteRepo := repository.NewSMSAlerteRespository(p.db) + smsAlerteService := services.NewSMSAlerteService(smsAlerteRepo) // parsing json to string var sub *entity.Subscription @@ -211,10 +207,11 @@ func (p *Processor) SMSAlerte(wg *sync.WaitGroup, message []byte) { p.logger, sub, serviceService, - contentService, subscriptionService, - transactionService, newsService, + subscriptionFollowLeagueService, + subscriptionFollowTeamService, + smsAlerteService, ) // Send SMS Alerte diff --git a/cmd/publisher.go b/cmd/publisher.go index 5aeb5c6..c258f5e 100644 --- a/cmd/publisher.go +++ b/cmd/publisher.go @@ -2,6 +2,7 @@ package cmd import ( "encoding/json" + "log" "time" "github.com/idprm/go-football-alert/internal/domain/entity" @@ -59,9 +60,9 @@ var publisherRenewalCmd = &cobra.Command{ ) /** - * Looping schedule + * Looping schedule per 10 minutes */ - timeDuration := time.Duration(30) + timeDuration := time.Duration(10) for { @@ -107,7 +108,7 @@ var publisherRetryCmd = &cobra.Command{ ) /** - * Looping schedule + * Looping schedule per 30 minutes */ timeDuration := time.Duration(30) @@ -394,11 +395,14 @@ 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, @@ -468,13 +472,13 @@ func populateRetry(db *gorm.DB, rmq rmqp.AMQP) { } } -func populatePrediction(db *gorm.DB, rmq rmqp.AMQP) { +func populateSMSAlerte(db *gorm.DB, rmq rmqp.AMQP) { subscriptionRepo := repository.NewSubscriptionRepository(db) subscriptionService := services.NewSubscriptionService(subscriptionRepo) - subs := subscriptionService.Prediction() + followsLeague := subscriptionService.Follow() - for _, s := range *subs { + for _, s := range *followsLeague { var sub entity.Subscription sub.ID = s.ID @@ -488,17 +492,17 @@ func populatePrediction(db *gorm.DB, rmq rmqp.AMQP) { json, _ := json.Marshal(sub) - rmq.IntegratePublish(RMQ_PREDICTION_EXCHANGE, RMQ_PREDICTION_QUEUE, RMQ_DATA_TYPE, "", string(json)) + rmq.IntegratePublish(RMQ_SMS_ALERTE_EXCHANGE, RMQ_SMS_ALERTE_QUEUE, RMQ_DATA_TYPE, "", string(json)) time.Sleep(100 * time.Microsecond) } } -func populateGoalCredit(db *gorm.DB, rmq rmqp.AMQP) { +func populatePrediction(db *gorm.DB, rmq rmqp.AMQP) { subscriptionRepo := repository.NewSubscriptionRepository(db) subscriptionService := services.NewSubscriptionService(subscriptionRepo) - subs := subscriptionService.Renewal() + subs := subscriptionService.Prediction() for _, s := range *subs { var sub entity.Subscription @@ -514,17 +518,17 @@ func populateGoalCredit(db *gorm.DB, rmq rmqp.AMQP) { json, _ := json.Marshal(sub) - rmq.IntegratePublish(RMQ_CREDIT_EXCHANGE, RMQ_CREDIT_QUEUE, RMQ_DATA_TYPE, "", string(json)) + rmq.IntegratePublish(RMQ_PREDICTION_EXCHANGE, RMQ_PREDICTION_QUEUE, RMQ_DATA_TYPE, "", string(json)) time.Sleep(100 * time.Microsecond) } } -func populateSMSAlerte(db *gorm.DB, rmq rmqp.AMQP) { +func populateGoalCredit(db *gorm.DB, rmq rmqp.AMQP) { subscriptionRepo := repository.NewSubscriptionRepository(db) subscriptionService := services.NewSubscriptionService(subscriptionRepo) - subs := subscriptionService.FollowLeague() + subs := subscriptionService.Renewal() for _, s := range *subs { var sub entity.Subscription @@ -540,7 +544,7 @@ func populateSMSAlerte(db *gorm.DB, rmq rmqp.AMQP) { json, _ := json.Marshal(sub) - rmq.IntegratePublish(RMQ_SMS_ALERTE_EXCHANGE, RMQ_SMS_ALERTE_QUEUE, RMQ_DATA_TYPE, "", string(json)) + rmq.IntegratePublish(RMQ_CREDIT_EXCHANGE, RMQ_CREDIT_QUEUE, RMQ_DATA_TYPE, "", string(json)) time.Sleep(100 * time.Microsecond) } diff --git a/cmd/seeder.go b/cmd/seeder.go index fd413ee..b0a83de 100644 --- a/cmd/seeder.go +++ b/cmd/seeder.go @@ -270,7 +270,7 @@ func seederDB(db *gorm.DB) { Category: ACT_CONFIRMATION, Channel: ACT_SMS, Name: SMS_FOLLOW_TEAM_SUB, - Value: "SMS Alerte: Vous avez souscrit avec succes pour suivre votre equipe favorite! Jusqu au {date}/{month} vous recevrez toutes les infos en direct. {price}{currency}/souscription", + Value: "SMS Alerte: Vous avez souscrit avec succes pour suivre votre equipe favorite! Jusqu au {day}/{month} vous recevrez toutes les infos en direct. {price}{currency}/souscription", }, { Category: ACT_CONFIRMATION, diff --git a/internal/domain/entity/content.go b/internal/domain/entity/content.go index 34c4b90..7e301fb 100644 --- a/internal/domain/entity/content.go +++ b/internal/domain/entity/content.go @@ -33,20 +33,20 @@ func (e *Content) GetValue() string { return e.Value } -func (e *Content) SetValueSubFollowCompetition(league, date, month, price, currency string) { +func (e *Content) SetValueSubFollowCompetition(league, day, month, price, currency string) { replacer := strings.NewReplacer( "{league}", league, - "{date}", date, + "{day}", day, "{month}", month, "{price}", price, "{currency}", url.QueryEscape(currency)) e.Value = replacer.Replace(e.Value) } -func (e *Content) SetValueSubFollowTeam(team, date, month, price, currency string) { +func (e *Content) SetValueSubFollowTeam(team, day, month, price, currency string) { replacer := strings.NewReplacer( "{team}", team, - "{date}", date, + "{day}", day, "{month}", month, "{price}", price, "{currency}", url.QueryEscape(currency)) diff --git a/internal/domain/entity/follow_league.go b/internal/domain/entity/follow_league.go deleted file mode 100644 index 9de9893..0000000 --- a/internal/domain/entity/follow_league.go +++ /dev/null @@ -1,12 +0,0 @@ -package entity - -import "gorm.io/gorm" - -type FollowLeague struct { - ID int64 `gorm:"primaryKey" json:"id"` - SubscriptionID int64 `json:"subscription_id"` - Subscription *Subscription `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"subscription,omitempty"` - LeagueID int64 `json:"league_id"` - League *League `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"league,omitempty"` - gorm.Model `json:"-"` -} diff --git a/internal/domain/entity/follow_team.go b/internal/domain/entity/sms_alerte.go similarity index 62% rename from internal/domain/entity/follow_team.go rename to internal/domain/entity/sms_alerte.go index ad8eb83..58db13a 100644 --- a/internal/domain/entity/follow_team.go +++ b/internal/domain/entity/sms_alerte.go @@ -2,11 +2,11 @@ package entity import "gorm.io/gorm" -type FollowTeam struct { +type SMSAlerte struct { ID int64 `gorm:"primaryKey" json:"id"` SubscriptionID int64 `json:"subscription_id"` Subscription *Subscription `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"subscription,omitempty"` - TeamID int64 `json:"team_id"` - Team *Team `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"team,omitempty"` + NewsID int64 `json:"news_id"` + News *News `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"news,omitempty"` gorm.Model `json:"-"` } diff --git a/internal/domain/entity/subscription.go b/internal/domain/entity/subscription.go index 7d74c03..d9bf122 100644 --- a/internal/domain/entity/subscription.go +++ b/internal/domain/entity/subscription.go @@ -8,46 +8,49 @@ import ( ) type Subscription struct { - ID int64 `gorm:"primaryKey" json:"id"` - ServiceID int `json:"service_id"` - Service *Service `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"service,omitempty"` - Category string `gorm:"size:20" json:"category"` - Msisdn string `gorm:"size:15;not null" json:"msisdn"` - Channel string `gorm:"size:15" json:"channel,omitempty"` - LatestTrxId string `gorm:"size:100" json:"trx_id,omitempty"` - LatestKeyword string `gorm:"size:50" json:"latest_keyword,omitempty"` - LatestSubject string `gorm:"size:25" json:"latest_subject,omitempty"` - LatestStatus string `gorm:"size:25" json:"latest_status,omitempty"` - LatestPayload string `gorm:"type:text" json:"latest_payload,omitempty"` - RenewalAt time.Time `gorm:"type:TIMESTAMP;null;default:null" json:"renewal_at,omitempty"` - UnsubAt time.Time `gorm:"type:TIMESTAMP;null;default:null" json:"unsub_at,omitempty"` - ChargeAt time.Time `gorm:"type:TIMESTAMP;null;default:null" json:"charge_at,omitempty"` - RetryAt time.Time `gorm:"type:TIMESTAMP;null;default:null" json:"retry_at,omitempty"` - FreeAt time.Time `gorm:"type:TIMESTAMP;null;default:null" json:"free_at,omitempty"` - FollowAt time.Time `gorm:"type:TIMESTAMP;null;default:null" json:"follow_at,omitempty"` - PredictionAt time.Time `gorm:"type:TIMESTAMP;null;default:null" json:"prediction_at,omitempty"` - CreditGoalAt time.Time `gorm:"type:TIMESTAMP;null;default:null" json:"credit_goal_at,omitempty"` - FirstSuccessAt time.Time `gorm:"type:TIMESTAMP;null;default:null" json:"first_success_at,omitempty"` - TotalSuccess int `gorm:"default:0" json:"total_success,omitempty"` - TotalFailed int `gorm:"default:0" json:"total_failed,omitempty"` - TotalAmount float64 `gorm:"default:0" json:"total_amount,omitempty"` - TotalFirstpush int `gorm:"default:0" json:"total_firstpush,omitempty"` - TotalRenewal int `gorm:"default:0" json:"total_renewal,omitempty"` - TotalSub int `gorm:"default:0" json:"total_sub,omitempty"` - TotalUnsub int `gorm:"default:0" json:"total_unsub,omitempty"` - TotalAmountFirstpush float64 `gorm:"default:0" json:"total_amount_firstpush,omitempty"` - TotalAmountRenewal float64 `gorm:"default:0" json:"total_amount_renewal,omitempty"` - BeforeBalance float64 `gorm:"default:0" json:"before_balance,omitempty"` - AfterBalance float64 `gorm:"default:0" json:"after_balance,omitempty"` - ExpireAt time.Time `gorm:"type:TIMESTAMP;null;default:null" json:"expire_at,omitempty"` - IpAddress string `gorm:"size:25" json:"ip_address,omitempty"` - IsFollowTeam bool `gorm:"type:boolean;column:is_follow_team" json:"is_follow_team,omitempty"` - IsFollowLeague bool `gorm:"type:boolean;column:is_follow_competition" json:"is_follow_competition,omitempty"` - IsPrediction bool `gorm:"type:boolean;column:is_prediction" json:"is_prediction,omitempty"` - IsCreditGoal bool `gorm:"type:boolean;column:is_credit_goal" json:"is_credit_goal,omitempty"` - IsRetry bool `gorm:"type:boolean;column:is_retry" json:"is_retry,omitempty"` - IsFree bool `gorm:"type:boolean;column:is_free" json:"is_free,omitempty"` - IsActive bool `gorm:"type:boolean;column:is_active" json:"is_active,omitempty"` + ID int64 `gorm:"primaryKey" json:"id"` + ServiceID int `json:"service_id"` + Service *Service `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"service,omitempty"` + Category string `gorm:"size:20" json:"category"` + Msisdn string `gorm:"size:15;not null" json:"msisdn"` + Channel string `gorm:"size:15" json:"channel,omitempty"` + LatestTrxId string `gorm:"size:100" json:"trx_id,omitempty"` + LatestKeyword string `gorm:"size:50" json:"latest_keyword,omitempty"` + LatestSubject string `gorm:"size:25" json:"latest_subject,omitempty"` + LatestStatus string `gorm:"size:25" json:"latest_status,omitempty"` + LatestPayload string `gorm:"type:text" json:"latest_payload,omitempty"` + RenewalAt time.Time `gorm:"type:TIMESTAMP;null;default:null" json:"renewal_at,omitempty"` + UnsubAt time.Time `gorm:"type:TIMESTAMP;null;default:null" json:"unsub_at,omitempty"` + ChargeAt time.Time `gorm:"type:TIMESTAMP;null;default:null" json:"charge_at,omitempty"` + RetryAt time.Time `gorm:"type:TIMESTAMP;null;default:null" json:"retry_at,omitempty"` + FreeAt time.Time `gorm:"type:TIMESTAMP;null;default:null" json:"free_at,omitempty"` + FollowAt time.Time `gorm:"type:TIMESTAMP;null;default:null" json:"follow_at,omitempty"` + PredictionAt time.Time `gorm:"type:TIMESTAMP;null;default:null" json:"prediction_at,omitempty"` + CreditGoalAt time.Time `gorm:"type:TIMESTAMP;null;default:null" json:"credit_goal_at,omitempty"` + FirstSuccessAt time.Time `gorm:"type:TIMESTAMP;null;default:null" json:"first_success_at,omitempty"` + TotalSuccess int `gorm:"default:0" json:"total_success,omitempty"` + TotalFailed int `gorm:"default:0" json:"total_failed,omitempty"` + TotalAmount float64 `gorm:"default:0" json:"total_amount,omitempty"` + TotalFirstpush int `gorm:"default:0" json:"total_firstpush,omitempty"` + TotalRenewal int `gorm:"default:0" json:"total_renewal,omitempty"` + TotalSub int `gorm:"default:0" json:"total_sub,omitempty"` + TotalUnsub int `gorm:"default:0" json:"total_unsub,omitempty"` + TotalAmountFirstpush float64 `gorm:"default:0" json:"total_amount_firstpush,omitempty"` + TotalAmountRenewal float64 `gorm:"default:0" json:"total_amount_renewal,omitempty"` + BeforeBalance float64 `gorm:"default:0" json:"before_balance,omitempty"` + AfterBalance float64 `gorm:"default:0" json:"after_balance,omitempty"` + ExpireAt time.Time `gorm:"type:TIMESTAMP;null;default:null" json:"expire_at,omitempty"` + IpAddress string `gorm:"size:25" json:"ip_address,omitempty"` + IsFollowTeam bool `gorm:"type:boolean;column:is_follow_team" json:"is_follow_team,omitempty"` + IsFollowLeague bool `gorm:"type:boolean;column:is_follow_competition" json:"is_follow_competition,omitempty"` + IsPrediction bool `gorm:"type:boolean;column:is_prediction" json:"is_prediction,omitempty"` + IsCreditGoal bool `gorm:"type:boolean;column:is_credit_goal" json:"is_credit_goal,omitempty"` + IsRetry bool `gorm:"type:boolean;column:is_retry" json:"is_retry,omitempty"` + IsFree bool `gorm:"type:boolean;column:is_free" json:"is_free,omitempty"` + IsActive bool `gorm:"type:boolean;column:is_active" json:"is_active,omitempty"` + League *League `gorm:"many2many:subscription_follow_leagues" json:"league"` + Team *Team `gorm:"many2many:subscription_follow_teams" json:"team"` + Prediction *Prediction `gorm:"many2many:subscription_predict" json:"prediction"` gorm.Model `json:"-"` } diff --git a/internal/domain/entity/subscription_follow_league.go b/internal/domain/entity/subscription_follow_league.go index 151d980..5f34b5c 100644 --- a/internal/domain/entity/subscription_follow_league.go +++ b/internal/domain/entity/subscription_follow_league.go @@ -9,5 +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"` gorm.Model `json:"-"` } diff --git a/internal/domain/entity/subscription_follow_team.go b/internal/domain/entity/subscription_follow_team.go index fb94948..a20b41d 100644 --- a/internal/domain/entity/subscription_follow_team.go +++ b/internal/domain/entity/subscription_follow_team.go @@ -9,5 +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"` gorm.Model `json:"-"` } diff --git a/internal/domain/repository/follow_league_repository.go b/internal/domain/repository/follow_league_repository.go deleted file mode 100644 index a5edbbc..0000000 --- a/internal/domain/repository/follow_league_repository.go +++ /dev/null @@ -1,46 +0,0 @@ -package repository - -import ( - "github.com/idprm/go-football-alert/internal/domain/entity" - "gorm.io/gorm" -) - -type FollowLeagueRepository struct { - db *gorm.DB -} - -func NewFollowLeagueRepository(db *gorm.DB) *FollowLeagueRepository { - return &FollowLeagueRepository{ - db: db, - } -} - -type IFollowLeagueRepository interface { - Save(*entity.FollowLeague) (*entity.FollowLeague, error) - Update(*entity.FollowLeague) (*entity.FollowLeague, error) - Delete(*entity.FollowLeague) error -} - -func (r *FollowLeagueRepository) Save(c *entity.FollowLeague) (*entity.FollowLeague, error) { - err := r.db.Create(&c).Error - if err != nil { - return nil, err - } - return c, nil -} - -func (r *FollowLeagueRepository) Update(c *entity.FollowLeague) (*entity.FollowLeague, error) { - err := r.db.Where("id = ?", c.ID).Updates(&c).Error - if err != nil { - return nil, err - } - return c, nil -} - -func (r *FollowLeagueRepository) Delete(c *entity.FollowLeague) error { - err := r.db.Delete(&c, c.ID).Error - if err != nil { - return err - } - return nil -} diff --git a/internal/domain/repository/follow_team_repository.go b/internal/domain/repository/follow_team_repository.go deleted file mode 100644 index 8b9fd03..0000000 --- a/internal/domain/repository/follow_team_repository.go +++ /dev/null @@ -1,46 +0,0 @@ -package repository - -import ( - "github.com/idprm/go-football-alert/internal/domain/entity" - "gorm.io/gorm" -) - -type FollowTeamRepository struct { - db *gorm.DB -} - -func NewFollowTeamRepository(db *gorm.DB) *FollowTeamRepository { - return &FollowTeamRepository{ - db: db, - } -} - -type IFollowTeamRepository interface { - Save(*entity.FollowTeam) (*entity.FollowTeam, error) - Update(*entity.FollowTeam) (*entity.FollowTeam, error) - Delete(*entity.FollowTeam) error -} - -func (r *FollowTeamRepository) Save(c *entity.FollowTeam) (*entity.FollowTeam, error) { - err := r.db.Create(&c).Error - if err != nil { - return nil, err - } - return c, nil -} - -func (r *FollowTeamRepository) Update(c *entity.FollowTeam) (*entity.FollowTeam, error) { - err := r.db.Where("id = ?", c.ID).Updates(&c).Error - if err != nil { - return nil, err - } - return c, nil -} - -func (r *FollowTeamRepository) Delete(c *entity.FollowTeam) error { - err := r.db.Delete(&c, c.ID).Error - if err != nil { - return err - } - return nil -} diff --git a/internal/domain/repository/news_repository.go b/internal/domain/repository/news_repository.go index b9f0e7a..09e9048 100644 --- a/internal/domain/repository/news_repository.go +++ b/internal/domain/repository/news_repository.go @@ -17,13 +17,19 @@ func NewNewsRepository(db *gorm.DB) *NewsRepository { type INewsRepository interface { Count(string, string) (int64, error) + CountNewsLeague(int64) (int64, error) + CountNewsTeam(int64) (int64, error) + CountById(int64) (int64, error) GetAllPaginate(*entity.Pagination) (*entity.Pagination, error) GetAllUSSD(int) ([]*entity.News, error) GetByTeamUSSD(int) (*entity.News, error) + GetById(int64) (*entity.News, error) Get(string, string) (*entity.News, error) Save(*entity.News) (*entity.News, error) Update(*entity.News) (*entity.News, error) Delete(*entity.News) error + GetAllNewsLeague(int64) ([]*entity.NewsLeagues, error) + GetAllNewsTeam(int64) ([]*entity.NewsTeams, error) SaveNewsLeague(*entity.NewsLeagues) (*entity.NewsLeagues, error) UpdateNewsLeague(*entity.NewsLeagues) (*entity.NewsLeagues, error) SaveNewsTeam(*entity.NewsTeams) (*entity.NewsTeams, error) @@ -39,6 +45,33 @@ func (r *NewsRepository) Count(slug, pubAt string) (int64, error) { return count, nil } +func (r *NewsRepository) CountNewsLeague(leagueId int64) (int64, error) { + var count int64 + err := r.db.Model(&entity.NewsLeagues{}).Where("league_id = ?", leagueId).Where("DATE(created_at) = DATE(NOW())").Count(&count).Error + if err != nil { + return count, err + } + return count, nil +} + +func (r *NewsRepository) CountNewsTeam(teamId int64) (int64, error) { + var count int64 + err := r.db.Model(&entity.NewsTeams{}).Where("team_id = ?", teamId).Where("DATE(created_at) = DATE(NOW())").Count(&count).Error + if err != nil { + return count, err + } + return count, nil +} + +func (r *NewsRepository) CountById(id int64) (int64, error) { + var count int64 + err := r.db.Model(&entity.News{}).Where("id = ?", id).Count(&count).Error + if err != nil { + return count, err + } + return count, nil +} + func (r *NewsRepository) GetAllPaginate(pagination *entity.Pagination) (*entity.Pagination, error) { var news []*entity.News err := r.db.Scopes(Paginate(news, pagination, r.db)).Find(&news).Error @@ -67,6 +100,15 @@ func (r *NewsRepository) GetByTeamUSSD(teamId int) (*entity.News, error) { return &c, nil } +func (r *NewsRepository) GetById(id int64) (*entity.News, error) { + var c entity.News + err := r.db.Where("id = ?", id).Take(&c).Error + if err != nil { + return nil, err + } + return &c, nil +} + func (r *NewsRepository) Get(slug, pubAt string) (*entity.News, error) { var c entity.News err := r.db.Where("slug = ?", slug).Where("DATE(publish_at) <= DATE(NOW())", pubAt).Take(&c).Error @@ -100,6 +142,24 @@ func (r *NewsRepository) Delete(c *entity.News) error { return nil } +func (r *NewsRepository) GetAllNewsLeague(leagueId int64) ([]*entity.NewsLeagues, error) { + var c []*entity.NewsLeagues + err := r.db.Where("league_id = ?", leagueId).Where("DATE(created_at) = DATE(NOW())").Preload("News").Find(&c).Error + if err != nil { + return nil, err + } + return c, nil +} + +func (r *NewsRepository) GetAllNewsTeam(teamId int64) ([]*entity.NewsTeams, error) { + var c []*entity.NewsTeams + err := r.db.Where("team_id = ?", teamId).Where("DATE(created_at) = DATE(NOW())").Preload("News").Find(&c).Error + if err != nil { + return nil, err + } + return c, nil +} + func (r *NewsRepository) SaveNewsLeague(c *entity.NewsLeagues) (*entity.NewsLeagues, error) { err := r.db.Create(&c).Error if err != nil { diff --git a/internal/domain/repository/sms_alerte_repository.go b/internal/domain/repository/sms_alerte_repository.go new file mode 100644 index 0000000..f8cca70 --- /dev/null +++ b/internal/domain/repository/sms_alerte_repository.go @@ -0,0 +1,38 @@ +package repository + +import ( + "github.com/idprm/go-football-alert/internal/domain/entity" + "gorm.io/gorm" +) + +type SMSAlerteRespository struct { + db *gorm.DB +} + +func NewSMSAlerteRespository(db *gorm.DB) *SMSAlerteRespository { + return &SMSAlerteRespository{ + db: db, + } +} + +type ISMSAlerteRespository interface { + Count(int64, int64) (int64, error) + Save(*entity.SMSAlerte) (*entity.SMSAlerte, error) +} + +func (r *SMSAlerteRespository) Count(subscriptionId, newsId int64) (int64, error) { + var count int64 + err := r.db.Model(&entity.SMSAlerte{}).Where(&entity.SMSAlerte{SubscriptionID: subscriptionId, NewsID: newsId}).Where("DATE(created_at) = DATE(NOW())").Count(&count).Error + if err != nil { + return count, err + } + return count, nil +} + +func (r *SMSAlerteRespository) Save(c *entity.SMSAlerte) (*entity.SMSAlerte, error) { + err := r.db.Create(&c).Error + if err != nil { + return nil, err + } + return c, nil +} diff --git a/internal/domain/repository/subscription_follow_league_repository.go b/internal/domain/repository/subscription_follow_league_repository.go index d3697b1..170cf0c 100644 --- a/internal/domain/repository/subscription_follow_league_repository.go +++ b/internal/domain/repository/subscription_follow_league_repository.go @@ -16,17 +16,27 @@ func NewSubscriptionFollowLeagueRepository(db *gorm.DB) *SubscriptionFollowLeagu } type ISubscriptionFollowLeagueRepository interface { - Count(int64, int64) (int64, error) + CountBySub(int64) (int64, error) + CountByLeague(int64) (int64, error) GetAllPaginate(*entity.Pagination) (*entity.Pagination, error) - Get(int64, int64) (*entity.SubscriptionFollowLeague, error) + GetBySub(int64) (*entity.SubscriptionFollowLeague, error) Save(*entity.SubscriptionFollowLeague) (*entity.SubscriptionFollowLeague, error) Update(*entity.SubscriptionFollowLeague) (*entity.SubscriptionFollowLeague, error) Delete(*entity.SubscriptionFollowLeague) error } -func (r *SubscriptionFollowLeagueRepository) Count(subId, leagueId int64) (int64, error) { +func (r *SubscriptionFollowLeagueRepository) CountBySub(subId int64) (int64, error) { var count int64 - err := r.db.Model(&entity.SubscriptionFollowLeague{}).Where(&entity.SubscriptionFollowLeague{SubscriptionID: int64(subId), LeagueID: int64(leagueId)}).Count(&count).Error + err := r.db.Model(&entity.SubscriptionFollowLeague{}).Where(&entity.SubscriptionFollowLeague{SubscriptionID: subId}).Count(&count).Error + if err != nil { + return count, err + } + return count, nil +} + +func (r *SubscriptionFollowLeagueRepository) CountByLeague(leagueId int64) (int64, error) { + var count int64 + err := r.db.Model(&entity.SubscriptionFollowLeague{}).Where(&entity.SubscriptionFollowLeague{LeagueID: leagueId}).Count(&count).Error if err != nil { return count, err } @@ -43,9 +53,9 @@ func (r *SubscriptionFollowLeagueRepository) GetAllPaginate(pagination *entity.P return pagination, nil } -func (r *SubscriptionFollowLeagueRepository) Get(subId, leagueId int64) (*entity.SubscriptionFollowLeague, error) { +func (r *SubscriptionFollowLeagueRepository) GetBySub(subId int64) (*entity.SubscriptionFollowLeague, error) { var c entity.SubscriptionFollowLeague - err := r.db.Where(&entity.SubscriptionFollowLeague{SubscriptionID: subId, LeagueID: leagueId}).Take(&c).Error + err := r.db.Where(&entity.SubscriptionFollowLeague{SubscriptionID: subId}).Take(&c).Error if err != nil { return nil, err } diff --git a/internal/domain/repository/subscription_follow_team_repository.go b/internal/domain/repository/subscription_follow_team_repository.go index af52468..d02d575 100644 --- a/internal/domain/repository/subscription_follow_team_repository.go +++ b/internal/domain/repository/subscription_follow_team_repository.go @@ -16,17 +16,27 @@ func NewSubscriptionFollowTeamRepository(db *gorm.DB) *SubscriptionFollowTeamRep } type ISubscriptionFollowTeamRepository interface { - Count(int64, int64) (int64, error) + CountBySub(int64) (int64, error) + CountByTeam(int64) (int64, error) GetAllPaginate(*entity.Pagination) (*entity.Pagination, error) - Get(int64, int64) (*entity.SubscriptionFollowTeam, error) + GetBySub(int64) (*entity.SubscriptionFollowTeam, error) Save(*entity.SubscriptionFollowTeam) (*entity.SubscriptionFollowTeam, error) Update(*entity.SubscriptionFollowTeam) (*entity.SubscriptionFollowTeam, error) Delete(*entity.SubscriptionFollowTeam) error } -func (r *SubscriptionFollowTeamRepository) Count(subId, teamId int64) (int64, error) { +func (r *SubscriptionFollowTeamRepository) CountBySub(subId int64) (int64, error) { var count int64 - err := r.db.Model(&entity.SubscriptionFollowTeam{}).Where(&entity.SubscriptionFollowTeam{SubscriptionID: int64(subId), TeamID: int64(teamId)}).Count(&count).Error + err := r.db.Model(&entity.SubscriptionFollowTeam{}).Where(&entity.SubscriptionFollowTeam{SubscriptionID: subId}).Count(&count).Error + if err != nil { + return count, err + } + return count, nil +} + +func (r *SubscriptionFollowTeamRepository) CountByTeam(teamId int64) (int64, error) { + var count int64 + err := r.db.Model(&entity.SubscriptionFollowTeam{}).Where(&entity.SubscriptionFollowTeam{TeamID: teamId}).Count(&count).Error if err != nil { return count, err } @@ -43,9 +53,9 @@ func (r *SubscriptionFollowTeamRepository) GetAllPaginate(pagination *entity.Pag return pagination, nil } -func (r *SubscriptionFollowTeamRepository) Get(subId, teamId int64) (*entity.SubscriptionFollowTeam, error) { +func (r *SubscriptionFollowTeamRepository) GetBySub(subId int64) (*entity.SubscriptionFollowTeam, error) { var c entity.SubscriptionFollowTeam - err := r.db.Where(&entity.SubscriptionFollowTeam{SubscriptionID: subId, TeamID: teamId}).Take(&c).Error + err := r.db.Where(&entity.SubscriptionFollowTeam{SubscriptionID: subId}).Take(&c).Error if err != nil { return nil, err } diff --git a/internal/domain/repository/subscription_respository.go b/internal/domain/repository/subscription_respository.go index ee83882..091ede6 100644 --- a/internal/domain/repository/subscription_respository.go +++ b/internal/domain/repository/subscription_respository.go @@ -35,8 +35,7 @@ type ISubscriptionRepository interface { IsNotCreditGoal(*entity.Subscription) (*entity.Subscription, error) CreditGoal() (*[]entity.Subscription, error) Prediction() (*[]entity.Subscription, error) - FollowTeam() (*[]entity.Subscription, error) - FollowLeague() (*[]entity.Subscription, error) + Follow() (*[]entity.Subscription, error) Renewal() (*[]entity.Subscription, error) Retry() (*[]entity.Subscription, error) } @@ -168,19 +167,9 @@ func (r *SubscriptionRepository) IsNotCreditGoal(c *entity.Subscription) (*entit return c, nil } -func (r *SubscriptionRepository) FollowLeague() (*[]entity.Subscription, error) { +func (r *SubscriptionRepository) Follow() (*[]entity.Subscription, error) { var sub []entity.Subscription - err := r.db.Where(&entity.Subscription{IsFollowLeague: true, IsActive: true}).Find(&sub).Error - if err != nil { - return nil, err - } - - return &sub, nil -} - -func (r *SubscriptionRepository) FollowTeam() (*[]entity.Subscription, error) { - var sub []entity.Subscription - err := r.db.Where(&entity.Subscription{IsFollowTeam: true, IsActive: true}).Find(&sub).Error + err := r.db.Where(&entity.Subscription{IsActive: true}).Where("(is_follow_team = true OR is_follow_competition = true)").Find(&sub).Error if err != nil { return nil, err } diff --git a/internal/domain/repository/team_repository.go b/internal/domain/repository/team_repository.go index 1943ace..f65e8d6 100644 --- a/internal/domain/repository/team_repository.go +++ b/internal/domain/repository/team_repository.go @@ -20,6 +20,7 @@ type ITeamRepository interface { CountByPrimaryId(int) (int64, error) CountByName(string) (int64, error) CountByLeagueTeam(*entity.LeagueTeam) (int64, error) + CountLeagueByTeam(int) (int64, error) GetAllPaginate(*entity.Pagination) (*entity.Pagination, error) GetAllTeamUSSD(int, int) ([]*entity.LeagueTeam, error) Get(string) (*entity.Team, error) @@ -29,6 +30,7 @@ type ITeamRepository interface { Update(*entity.Team) (*entity.Team, error) UpdateByPrimaryId(*entity.Team) (*entity.Team, error) Delete(*entity.Team) error + GetLeagueByTeam(int) (*entity.LeagueTeam, error) SaveLeagueTeam(*entity.LeagueTeam) (*entity.LeagueTeam, error) } @@ -68,6 +70,15 @@ func (r *TeamRepository) CountByLeagueTeam(v *entity.LeagueTeam) (int64, error) return count, nil } +func (r *TeamRepository) CountLeagueByTeam(teamId int) (int64, error) { + var count int64 + err := r.db.Model(&entity.LeagueTeam{}).Where(&entity.LeagueTeam{TeamID: int64(teamId)}).Count(&count).Error + if err != nil { + return count, err + } + return count, nil +} + func (r *TeamRepository) GetAllPaginate(pagination *entity.Pagination) (*entity.Pagination, error) { var teams []*entity.Team err := r.db.Scopes(Paginate(teams, pagination, r.db)).Find(&teams).Error @@ -146,6 +157,15 @@ func (r *TeamRepository) Delete(c *entity.Team) error { return nil } +func (r *TeamRepository) GetLeagueByTeam(teamId int) (*entity.LeagueTeam, error) { + var c entity.LeagueTeam + err := r.db.Where("team_id = ?", teamId).Preload("Team").Preload("League").Take(&c).Error + if err != nil { + return nil, err + } + return &c, nil +} + func (r *TeamRepository) SaveLeagueTeam(c *entity.LeagueTeam) (*entity.LeagueTeam, error) { err := r.db.Create(&c).Error if err != nil { diff --git a/internal/handler/incoming_handler.go b/internal/handler/incoming_handler.go index a5def6c..a8a037f 100644 --- a/internal/handler/incoming_handler.go +++ b/internal/handler/incoming_handler.go @@ -198,7 +198,7 @@ func (h *IncomingHandler) MessageOriginated(c *fiber.Ctx) error { } func (h *IncomingHandler) Main(c *fiber.Ctx) error { - c.Set("Content-type", "text/xml; charset=iso-8859-1") + c.Set("Content-type", "application/xml") menu, err := h.menuService.GetBySlug("home") if err != nil { return c.Status(fiber.StatusBadGateway).SendString(err.Error()) @@ -213,7 +213,7 @@ func (h *IncomingHandler) Main(c *fiber.Ctx) error { } func (h *IncomingHandler) Menu(c *fiber.Ctx) error { - c.Set("Content-type", "text/xml; charset=iso-8859-1") + c.Set("Content-type", "application/xml") req := new(model.UssdRequest) err := c.QueryParser(req) @@ -226,7 +226,7 @@ func (h *IncomingHandler) Menu(c *fiber.Ctx) error { // if menu or page not found if !h.menuService.IsSlug(req.GetSlug()) { - return c.Status(fiber.StatusOK).SendString(h.MsisdnNotFound(c.BaseURL())) + return c.Status(fiber.StatusOK).SendString(h.PageNotFound(c.BaseURL())) } menu, err := h.menuService.GetBySlug(req.GetSlug()) @@ -373,17 +373,18 @@ func (h *IncomingHandler) Menu(c *fiber.Ctx) error { } func (h *IncomingHandler) Detail(c *fiber.Ctx) error { - c.Set("Content-type", "text/xml; charset=iso-8859-1") + c.Set("Content-type", "application/xml") req := new(model.UssdRequest) err := c.QueryParser(req) if err != nil { return c.Status(fiber.StatusBadGateway).SendString(err.Error()) } - // setter msisdn req.SetMsisdn(c.Get("User-MSISDN")) + log.Println(req.GetMsisdn()) + // check if msisdn not found if !req.IsMsisdn() { return c.Status(fiber.StatusOK).SendString(h.MsisdnNotFound(c.BaseURL())) @@ -413,7 +414,7 @@ func (h *IncomingHandler) Detail(c *fiber.Ctx) error { } func (h *IncomingHandler) Confirm(c *fiber.Ctx) error { - c.Set("Content-type", "text/xml; charset=iso-8859-1") + c.Set("Content-type", "application/xml") req := new(model.UssdRequest) err := c.QueryParser(req) @@ -466,8 +467,7 @@ func (h *IncomingHandler) Confirm(c *fiber.Ctx) error { } func (h *IncomingHandler) Buy(c *fiber.Ctx) error { - c.Set("Content-type", "text/xml; charset=iso-8859-1") - + c.Set("Content-type", "application/xml") l := h.logger.Init("mo", true) req := new(model.UssdRequest) diff --git a/internal/handler/news_handler.go b/internal/handler/news_handler.go index 9a609ab..9c1b45c 100644 --- a/internal/handler/news_handler.go +++ b/internal/handler/news_handler.go @@ -10,29 +10,23 @@ import ( ) type NewsHandler struct { - leagueService services.ILeagueService - teamService services.ITeamService - newsService services.INewsService - FollowLeagueService services.IFollowLeagueService - followTeamService services.IFollowTeamService - news *entity.News + leagueService services.ILeagueService + teamService services.ITeamService + newsService services.INewsService + news *entity.News } func NewNewsHandler( leagueService services.ILeagueService, teamService services.ITeamService, newsService services.INewsService, - FollowLeagueService services.IFollowLeagueService, - followTeamService services.IFollowTeamService, news *entity.News, ) *NewsHandler { return &NewsHandler{ - leagueService: leagueService, - teamService: teamService, - newsService: newsService, - FollowLeagueService: FollowLeagueService, - followTeamService: followTeamService, - news: news, + leagueService: leagueService, + teamService: teamService, + newsService: newsService, + news: news, } } @@ -53,6 +47,22 @@ func (h *NewsHandler) Filter() { }, ) log.Println(team) + + // assign league by team + if h.teamService.IsLeagueByTeam(int(team.GetId())) { + l, err := h.teamService.GetLeagueByTeam(int(team.GetId())) + if err != nil { + log.Println(err.Error()) + } + + h.newsService.SaveNewsLeague( + &entity.NewsLeagues{ + NewsID: h.news.GetId(), + LeagueID: l.League.GetId(), + }, + ) + log.Println(l.League) + } } // away if h.teamService.IsTeamByName(h.news.GetAwayTeam()) { @@ -68,6 +78,22 @@ func (h *NewsHandler) Filter() { }, ) log.Println(team) + + // assign league by team + if h.teamService.IsLeagueByTeam(int(team.GetId())) { + l, err := h.teamService.GetLeagueByTeam(int(team.GetId())) + if err != nil { + log.Println(err.Error()) + } + + h.newsService.SaveNewsLeague( + &entity.NewsLeagues{ + NewsID: h.news.GetId(), + LeagueID: l.League.GetId(), + }, + ) + log.Println(l.League) + } } } else { if h.leagueService.IsLeagueByName(h.news.GetParseTitleLeft()) { @@ -112,7 +138,24 @@ func (h *NewsHandler) Filter() { TeamID: team.GetId(), }, ) + log.Println(team) + + // assign league by team + if h.teamService.IsLeagueByTeam(int(team.GetId())) { + l, err := h.teamService.GetLeagueByTeam(int(team.GetId())) + if err != nil { + log.Println(err.Error()) + } + + h.newsService.SaveNewsLeague( + &entity.NewsLeagues{ + NewsID: h.news.GetId(), + LeagueID: l.League.GetId(), + }, + ) + log.Println(l.League) + } } if h.teamService.IsTeamByName(h.news.GetParseTitleRight()) { @@ -128,6 +171,22 @@ func (h *NewsHandler) Filter() { }, ) log.Println(team) + + // assign league by team + if h.teamService.IsLeagueByTeam(int(team.GetId())) { + l, err := h.teamService.GetLeagueByTeam(int(team.GetId())) + if err != nil { + log.Println(err.Error()) + } + + h.newsService.SaveNewsLeague( + &entity.NewsLeagues{ + NewsID: h.news.GetId(), + LeagueID: l.League.GetId(), + }, + ) + log.Println(l.League) + } } } } else { @@ -159,6 +218,22 @@ func (h *NewsHandler) Filter() { }, ) log.Println(team) + + // assign league by team + if h.teamService.IsLeagueByTeam(int(team.GetId())) { + l, err := h.teamService.GetLeagueByTeam(int(team.GetId())) + if err != nil { + log.Println(err.Error()) + } + + h.newsService.SaveNewsLeague( + &entity.NewsLeagues{ + NewsID: h.news.GetId(), + LeagueID: l.League.GetId(), + }, + ) + log.Println(l.League) + } } } } diff --git a/internal/handler/sms_alerte_handler.go b/internal/handler/sms_alerte_handler.go index d3426df..0c3f2e3 100644 --- a/internal/handler/sms_alerte_handler.go +++ b/internal/handler/sms_alerte_handler.go @@ -13,14 +13,15 @@ import ( ) type SMSAlerteHandler struct { - rmq rmqp.AMQP - logger *logger.Logger - sub *entity.Subscription - serviceService services.IServiceService - contentService services.IContentService - subscriptionService services.ISubscriptionService - transactionService services.ITransactionService - newsService services.INewsService + rmq rmqp.AMQP + logger *logger.Logger + sub *entity.Subscription + serviceService services.IServiceService + subscriptionService services.ISubscriptionService + newsService services.INewsService + subscriptionFollowLeagueService services.ISubscriptionFollowLeagueService + subscriptionFollowTeamService services.ISubscriptionFollowTeamService + smsAlerteService services.ISMSAlerteService } func NewSMSAlerteHandler( @@ -28,93 +29,112 @@ func NewSMSAlerteHandler( logger *logger.Logger, sub *entity.Subscription, serviceService services.IServiceService, - contentService services.IContentService, subscriptionService services.ISubscriptionService, - transactionService services.ITransactionService, newsService services.INewsService, + subscriptionFollowLeagueService services.ISubscriptionFollowLeagueService, + subscriptionFollowTeamService services.ISubscriptionFollowTeamService, + smsAlerteService services.ISMSAlerteService, ) *SMSAlerteHandler { return &SMSAlerteHandler{ - rmq: rmq, - logger: logger, - sub: sub, - serviceService: serviceService, - contentService: contentService, - subscriptionService: subscriptionService, - transactionService: transactionService, - newsService: newsService, + rmq: rmq, + logger: logger, + sub: sub, + serviceService: serviceService, + subscriptionService: subscriptionService, + newsService: newsService, + subscriptionFollowLeagueService: subscriptionFollowLeagueService, + subscriptionFollowTeamService: subscriptionFollowTeamService, + smsAlerteService: smsAlerteService, } } func (h *SMSAlerteHandler) SMSAlerte() { if h.subscriptionService.IsActiveSubscription(h.sub.GetServiceId(), h.sub.GetMsisdn()) { + trxId := utils.GenerateTrxId() + service, err := h.serviceService.GetById(h.sub.GetServiceId()) if err != nil { log.Println(err.Error()) } - content, err := h.getContent(SMS_FOLLOW_COMPETITION_SUB) - if err != nil { - log.Println(err.Error()) - } + if h.subscriptionFollowLeagueService.IsSub(h.sub.GetId()) { + sl, err := h.subscriptionFollowLeagueService.GetBySub(h.sub.GetId()) + if err != nil { + log.Println(err.Error()) + } - trxId := utils.GenerateTrxId() + // news league + newsLeague, _ := h.newsService.GetAllNewsLeague(sl.LeagueID) + for _, n := range newsLeague { + // subId, newsId + if !h.smsAlerteService.ISMSAlerte(h.sub.GetId(), n.NewsID) { + h.smsAlerteService.Save( + &entity.SMSAlerte{ + SubscriptionID: h.sub.GetId(), + NewsID: n.NewsID, + }, + ) + mt := &model.MTRequest{ + Smsc: service.ScSubMT, + Service: service, + Subscription: h.sub, + Content: &entity.Content{Value: n.News.GetTitle()}, + } + mt.SetTrxId(trxId) - h.subscriptionService.Update( - &entity.Subscription{ - ServiceID: service.GetId(), - Msisdn: h.sub.GetMsisdn(), - LatestTrxId: trxId, - LatestSubject: SUBJECT_NEWS, - LatestStatus: STATUS_SUCCESS, - }, - ) - - h.transactionService.Save( - &entity.Transaction{ - TrxId: trxId, - ServiceID: service.GetId(), - Msisdn: h.sub.GetMsisdn(), - Keyword: h.sub.GetLatestKeyword(), - Status: STATUS_SUCCESS, - StatusCode: "", - StatusDetail: "", - Subject: SUBJECT_NEWS, - Payload: "-", - }, - ) - - mt := &model.MTRequest{ - Smsc: "", - Service: service, - Subscription: h.sub, - Content: content, - } - mt.SetTrxId(trxId) + jsonData, err := json.Marshal(mt) + if err != nil { + log.Println(err.Error()) + } - jsonData, err := json.Marshal(mt) - if err != nil { - log.Println(err.Error()) + h.rmq.IntegratePublish( + RMQ_MT_EXCHANGE, + RMQ_MT_QUEUE, + RMQ_DATA_TYPE, "", string(jsonData), + ) + } + + } } - h.rmq.IntegratePublish( - RMQ_MT_EXCHANGE, - RMQ_MT_QUEUE, - RMQ_DATA_TYPE, "", string(jsonData), - ) - } -} + if h.subscriptionFollowTeamService.IsSub(h.sub.GetId()) { + st, err := h.subscriptionFollowTeamService.GetBySub(h.sub.GetId()) + if err != nil { + log.Println(err.Error()) + } -/** -* Get Info -**/ -func (h *SMSAlerteHandler) getContent(name string) (*entity.Content, error) { - // if data not exist in table contents - if !h.contentService.IsContent(name) { - return &entity.Content{ - Category: "CATEGORY", - Channel: "SMS", - Value: "SAMPLE_TEXT", - }, nil + // news team + newsTeam, _ := h.newsService.GetAllNewsTeam(st.TeamID) + for _, n := range newsTeam { + // subId, newsId + if !h.smsAlerteService.ISMSAlerte(h.sub.GetId(), n.NewsID) { + h.smsAlerteService.Save( + &entity.SMSAlerte{ + SubscriptionID: h.sub.GetId(), + NewsID: n.NewsID, + }, + ) + mt := &model.MTRequest{ + Smsc: service.ScSubMT, + Service: service, + Subscription: h.sub, + Content: &entity.Content{Value: n.News.GetTitle()}, + } + mt.SetTrxId(trxId) + + jsonData, err := json.Marshal(mt) + if err != nil { + log.Println(err.Error()) + } + + h.rmq.IntegratePublish( + RMQ_MT_EXCHANGE, + RMQ_MT_QUEUE, + RMQ_DATA_TYPE, "", string(jsonData), + ) + } + + } + } } - return h.contentService.Get(name) } diff --git a/internal/handler/sms_handler.go b/internal/handler/sms_handler.go index 92817f1..a334c92 100644 --- a/internal/handler/sms_handler.go +++ b/internal/handler/sms_handler.go @@ -220,7 +220,7 @@ func (h *SMSHandler) SubAlerteCompetition(category string, league *entity.League } // insert in follow - if h.subscriptionFollowLeagueService.IsSubFollowLeague(sub.GetId(), league.GetId()) { + if !h.subscriptionFollowLeagueService.IsSub(sub.GetId()) { h.subscriptionFollowLeagueService.Save( &entity.SubscriptionFollowLeague{ SubscriptionID: sub.GetId(), @@ -449,7 +449,7 @@ func (h *SMSHandler) SubAlerteEquipe(category string, team *entity.Team) { } // insert in follow - if h.subscriptionFollowTeamService.IsSubFollowTeam(sub.GetId(), team.GetId()) { + if !h.subscriptionFollowTeamService.IsSub(sub.GetId()) { h.subscriptionFollowTeamService.Save( &entity.SubscriptionFollowTeam{ SubscriptionID: sub.GetId(), diff --git a/internal/services/follow_league_service.go b/internal/services/follow_league_service.go deleted file mode 100644 index 49c0782..0000000 --- a/internal/services/follow_league_service.go +++ /dev/null @@ -1,36 +0,0 @@ -package services - -import ( - "github.com/idprm/go-football-alert/internal/domain/entity" - "github.com/idprm/go-football-alert/internal/domain/repository" -) - -type FollowLeagueService struct { - FollowLeagueRepo repository.IFollowLeagueRepository -} - -func NewFollowLeagueService( - FollowLeagueRepo repository.IFollowLeagueRepository, -) *FollowLeagueService { - return &FollowLeagueService{ - FollowLeagueRepo: FollowLeagueRepo, - } -} - -type IFollowLeagueService interface { - Save(*entity.FollowLeague) (*entity.FollowLeague, error) - Update(*entity.FollowLeague) (*entity.FollowLeague, error) - Delete(*entity.FollowLeague) error -} - -func (s *FollowLeagueService) Save(a *entity.FollowLeague) (*entity.FollowLeague, error) { - return s.FollowLeagueRepo.Save(a) -} - -func (s *FollowLeagueService) Update(a *entity.FollowLeague) (*entity.FollowLeague, error) { - return s.FollowLeagueRepo.Update(a) -} - -func (s *FollowLeagueService) Delete(a *entity.FollowLeague) error { - return s.FollowLeagueRepo.Delete(a) -} diff --git a/internal/services/follow_team_service.go b/internal/services/follow_team_service.go deleted file mode 100644 index 5a60840..0000000 --- a/internal/services/follow_team_service.go +++ /dev/null @@ -1,34 +0,0 @@ -package services - -import ( - "github.com/idprm/go-football-alert/internal/domain/entity" - "github.com/idprm/go-football-alert/internal/domain/repository" -) - -type FollowTeamService struct { - followTeamRepo repository.IFollowTeamRepository -} - -func NewFollowTeamService(followTeamRepo repository.IFollowTeamRepository) *FollowTeamService { - return &FollowTeamService{ - followTeamRepo: followTeamRepo, - } -} - -type IFollowTeamService interface { - Save(*entity.FollowTeam) (*entity.FollowTeam, error) - Update(*entity.FollowTeam) (*entity.FollowTeam, error) - Delete(*entity.FollowTeam) error -} - -func (s *FollowTeamService) Save(a *entity.FollowTeam) (*entity.FollowTeam, error) { - return s.followTeamRepo.Save(a) -} - -func (s *FollowTeamService) Update(a *entity.FollowTeam) (*entity.FollowTeam, error) { - return s.followTeamRepo.Update(a) -} - -func (s *FollowTeamService) Delete(a *entity.FollowTeam) error { - return s.followTeamRepo.Delete(a) -} diff --git a/internal/services/news_service.go b/internal/services/news_service.go index a3edbc5..a042534 100644 --- a/internal/services/news_service.go +++ b/internal/services/news_service.go @@ -17,13 +17,19 @@ func NewNewsService(newsRepo repository.INewsRepository) *NewsService { type INewsService interface { IsNews(string, string) bool + IsNewsLeague(leagueId int64) bool + IsNewsTeam(teamId int64) bool + IsNewsById(int64) bool GetAllPaginate(*entity.Pagination) (*entity.Pagination, error) GetAllUSSD(int) ([]*entity.News, error) GetByTeamUSSD(int) (*entity.News, error) Get(string, string) (*entity.News, error) + GetById(int64) (*entity.News, error) Save(*entity.News) (*entity.News, error) Update(*entity.News) (*entity.News, error) Delete(*entity.News) error + GetAllNewsLeague(leagueId int64) ([]*entity.NewsLeagues, error) + GetAllNewsTeam(teamId int64) ([]*entity.NewsTeams, error) SaveNewsLeague(*entity.NewsLeagues) (*entity.NewsLeagues, error) UpdateNewsLeague(*entity.NewsLeagues) (*entity.NewsLeagues, error) SaveNewsTeam(*entity.NewsTeams) (*entity.NewsTeams, error) @@ -35,6 +41,21 @@ func (s *NewsService) IsNews(slug, pubAt string) bool { return count > 0 } +func (s *NewsService) IsNewsLeague(leagueId int64) bool { + count, _ := s.newsRepo.CountNewsLeague(leagueId) + return count > 0 +} + +func (s *NewsService) IsNewsTeam(teamId int64) bool { + count, _ := s.newsRepo.CountNewsTeam(teamId) + return count > 0 +} + +func (s *NewsService) IsNewsById(id int64) bool { + count, _ := s.newsRepo.CountById(id) + return count > 0 +} + func (s *NewsService) GetAllPaginate(pagination *entity.Pagination) (*entity.Pagination, error) { return s.newsRepo.GetAllPaginate(pagination) } @@ -51,6 +72,10 @@ func (s *NewsService) Get(slug, pubAt string) (*entity.News, error) { return s.newsRepo.Get(slug, pubAt) } +func (s *NewsService) GetById(id int64) (*entity.News, error) { + return s.newsRepo.GetById(id) +} + func (s *NewsService) Save(a *entity.News) (*entity.News, error) { return s.newsRepo.Save(a) } @@ -63,6 +88,14 @@ func (s *NewsService) Delete(a *entity.News) error { return s.newsRepo.Delete(a) } +func (s *NewsService) GetAllNewsLeague(leagueId int64) ([]*entity.NewsLeagues, error) { + return s.newsRepo.GetAllNewsLeague(leagueId) +} + +func (s *NewsService) GetAllNewsTeam(teamId int64) ([]*entity.NewsTeams, error) { + return s.newsRepo.GetAllNewsTeam(teamId) +} + func (s *NewsService) SaveNewsLeague(a *entity.NewsLeagues) (*entity.NewsLeagues, error) { return s.newsRepo.SaveNewsLeague(a) } diff --git a/internal/services/sms_alerte_service.go b/internal/services/sms_alerte_service.go new file mode 100644 index 0000000..1a28445 --- /dev/null +++ b/internal/services/sms_alerte_service.go @@ -0,0 +1,30 @@ +package services + +import ( + "github.com/idprm/go-football-alert/internal/domain/entity" + "github.com/idprm/go-football-alert/internal/domain/repository" +) + +type SMSAlerteService struct { + smsAlerteRepo repository.ISMSAlerteRespository +} + +func NewSMSAlerteService(smsAlerteRepo repository.ISMSAlerteRespository) *SMSAlerteService { + return &SMSAlerteService{ + smsAlerteRepo: smsAlerteRepo, + } +} + +type ISMSAlerteService interface { + ISMSAlerte(int64, int64) bool + Save(*entity.SMSAlerte) (*entity.SMSAlerte, error) +} + +func (s *SMSAlerteService) ISMSAlerte(subId, newsId int64) bool { + count, _ := s.smsAlerteRepo.Count(subId, newsId) + return count > 0 +} + +func (s *SMSAlerteService) Save(a *entity.SMSAlerte) (*entity.SMSAlerte, error) { + return s.smsAlerteRepo.Save(a) +} diff --git a/internal/services/subscription_follow_league_service.go b/internal/services/subscription_follow_league_service.go index f2674bc..8ab17ab 100644 --- a/internal/services/subscription_follow_league_service.go +++ b/internal/services/subscription_follow_league_service.go @@ -18,16 +18,22 @@ func NewSubscriptionFollowLeagueService( } type ISubscriptionFollowLeagueService interface { - IsSubFollowLeague(int64, int64) bool + IsSub(int64) bool + IsLeague(int64) bool GetAllPaginate(*entity.Pagination) (*entity.Pagination, error) - Get(int64, int64) (*entity.SubscriptionFollowLeague, error) + GetBySub(int64) (*entity.SubscriptionFollowLeague, error) Save(*entity.SubscriptionFollowLeague) (*entity.SubscriptionFollowLeague, error) Update(*entity.SubscriptionFollowLeague) (*entity.SubscriptionFollowLeague, error) Delete(*entity.SubscriptionFollowLeague) error } -func (s *SubscriptionFollowLeagueService) IsSubFollowLeague(subId, leagueId int64) bool { - count, _ := s.subFollowLeagueRepo.Count(subId, leagueId) +func (s *SubscriptionFollowLeagueService) IsSub(subId int64) bool { + count, _ := s.subFollowLeagueRepo.CountBySub(subId) + return count > 0 +} + +func (s *SubscriptionFollowLeagueService) IsLeague(leagueId int64) bool { + count, _ := s.subFollowLeagueRepo.CountByLeague(leagueId) return count > 0 } @@ -35,8 +41,8 @@ func (s *SubscriptionFollowLeagueService) GetAllPaginate(pagination *entity.Pagi return s.subFollowLeagueRepo.GetAllPaginate(pagination) } -func (s *SubscriptionFollowLeagueService) Get(subId, leagueId int64) (*entity.SubscriptionFollowLeague, error) { - return s.subFollowLeagueRepo.Get(subId, leagueId) +func (s *SubscriptionFollowLeagueService) GetBySub(subId int64) (*entity.SubscriptionFollowLeague, error) { + return s.subFollowLeagueRepo.GetBySub(subId) } func (s *SubscriptionFollowLeagueService) Save(a *entity.SubscriptionFollowLeague) (*entity.SubscriptionFollowLeague, error) { diff --git a/internal/services/subscription_follow_team_service.go b/internal/services/subscription_follow_team_service.go index 58012b4..55125f9 100644 --- a/internal/services/subscription_follow_team_service.go +++ b/internal/services/subscription_follow_team_service.go @@ -18,16 +18,22 @@ func NewSubscriptionFollowTeamService( } type ISubscriptionFollowTeamService interface { - IsSubFollowTeam(int64, int64) bool + IsSub(int64) bool + IsTeam(int64) bool GetAllPaginate(*entity.Pagination) (*entity.Pagination, error) - Get(int64, int64) (*entity.SubscriptionFollowTeam, error) + GetBySub(int64) (*entity.SubscriptionFollowTeam, error) Save(*entity.SubscriptionFollowTeam) (*entity.SubscriptionFollowTeam, error) Update(*entity.SubscriptionFollowTeam) (*entity.SubscriptionFollowTeam, error) Delete(*entity.SubscriptionFollowTeam) error } -func (s *SubscriptionFollowTeamService) IsSubFollowTeam(subId, teamId int64) bool { - count, _ := s.subFollowTeamRepo.Count(subId, teamId) +func (s *SubscriptionFollowTeamService) IsSub(subId int64) bool { + count, _ := s.subFollowTeamRepo.CountBySub(subId) + return count > 0 +} + +func (s *SubscriptionFollowTeamService) IsTeam(teamId int64) bool { + count, _ := s.subFollowTeamRepo.CountByTeam(teamId) return count > 0 } @@ -35,8 +41,8 @@ func (s *SubscriptionFollowTeamService) GetAllPaginate(pagination *entity.Pagina return s.subFollowTeamRepo.GetAllPaginate(pagination) } -func (s *SubscriptionFollowTeamService) Get(subId, teamId int64) (*entity.SubscriptionFollowTeam, error) { - return s.subFollowTeamRepo.Get(subId, teamId) +func (s *SubscriptionFollowTeamService) GetBySub(subId int64) (*entity.SubscriptionFollowTeam, error) { + return s.subFollowTeamRepo.GetBySub(subId) } func (s *SubscriptionFollowTeamService) Save(a *entity.SubscriptionFollowTeam) (*entity.SubscriptionFollowTeam, error) { diff --git a/internal/services/subscription_service.go b/internal/services/subscription_service.go index e186cde..c4614fb 100644 --- a/internal/services/subscription_service.go +++ b/internal/services/subscription_service.go @@ -34,10 +34,9 @@ type ISubscriptionService interface { IsNotFollowTeam(*entity.Subscription) (*entity.Subscription, error) IsNotFollowLeague(*entity.Subscription) (*entity.Subscription, error) IsNotPrediction(*entity.Subscription) (*entity.Subscription, error) - FollowLeague() *[]entity.Subscription - FollowTeam() *[]entity.Subscription Prediction() *[]entity.Subscription CreditGoal() *[]entity.Subscription + Follow() *[]entity.Subscription Renewal() *[]entity.Subscription Retry() *[]entity.Subscription } @@ -117,16 +116,8 @@ func (s *SubscriptionService) Prediction() *[]entity.Subscription { return subs } -func (s *SubscriptionService) FollowTeam() *[]entity.Subscription { - subs, err := s.subscriptionRepo.FollowTeam() - if err != nil { - log.Println(err) - } - return subs -} - -func (s *SubscriptionService) FollowLeague() *[]entity.Subscription { - subs, err := s.subscriptionRepo.FollowLeague() +func (s *SubscriptionService) Follow() *[]entity.Subscription { + subs, err := s.subscriptionRepo.Follow() if err != nil { log.Println(err) } diff --git a/internal/services/team_service.go b/internal/services/team_service.go index dd56153..d17302c 100644 --- a/internal/services/team_service.go +++ b/internal/services/team_service.go @@ -20,6 +20,7 @@ type ITeamService interface { IsTeamByPrimaryId(int) bool IsTeamByName(string) bool IsLeagueTeam(*entity.LeagueTeam) bool + IsLeagueByTeam(int) bool GetAllPaginate(*entity.Pagination) (*entity.Pagination, error) GetAllTeamUSSD(int, int) ([]*entity.LeagueTeam, error) Get(string) (*entity.Team, error) @@ -29,6 +30,7 @@ type ITeamService interface { Update(*entity.Team) (*entity.Team, error) UpdateByPrimaryId(*entity.Team) (*entity.Team, error) Delete(*entity.Team) error + GetLeagueByTeam(int) (*entity.LeagueTeam, error) SaveLeagueTeam(*entity.LeagueTeam) (*entity.LeagueTeam, error) } @@ -52,6 +54,11 @@ func (s *TeamService) IsLeagueTeam(v *entity.LeagueTeam) bool { return count > 0 } +func (s *TeamService) IsLeagueByTeam(teamId int) bool { + count, _ := s.teamRepo.CountLeagueByTeam(teamId) + return count > 0 +} + func (s *TeamService) GetAllPaginate(pagination *entity.Pagination) (*entity.Pagination, error) { return s.teamRepo.GetAllPaginate(pagination) } @@ -88,6 +95,10 @@ func (s *TeamService) Delete(a *entity.Team) error { return s.teamRepo.Delete(a) } +func (s *TeamService) GetLeagueByTeam(teamId int) (*entity.LeagueTeam, error) { + return s.teamRepo.GetLeagueByTeam(teamId) +} + func (s *TeamService) SaveLeagueTeam(a *entity.LeagueTeam) (*entity.LeagueTeam, error) { return s.teamRepo.SaveLeagueTeam(a) } diff --git a/main.go b/main.go index ac9ad9c..3f4a226 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,14 @@ package main -import "github.com/idprm/go-football-alert/cmd" +import ( + "log" + "time" + + "github.com/idprm/go-football-alert/cmd" +) func main() { + cmd.Execute() + log.Println(time.Now()) }