Skip to content

Commit

Permalink
add new
Browse files Browse the repository at this point in the history
  • Loading branch information
idprm committed Oct 23, 2024
1 parent f3171df commit 6297297
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cmd/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ var publisherScrapingNewsCmd = &cobra.Command{
/**
* Looping schedule
*/
timeDuration := time.Duration(45)
timeDuration := time.Duration(35)

for {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
24 changes: 24 additions & 0 deletions internal/domain/repository/subscription_follow_team_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
42 changes: 26 additions & 16 deletions internal/handler/news_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
}
}
}
Expand All @@ -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),
)
}
}
}
1 change: 1 addition & 0 deletions internal/handler/sms_alerte_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
5 changes: 2 additions & 3 deletions internal/services/subscription_follow_league_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -83,9 +84,7 @@ func (s *SubscriptionFollowLeagueService) Sent(a *entity.SubscriptionFollowLeagu
},
)
}
}

if !s.IsUpdated(a.SubscriptionID) {
} else {
// reset
s.subFollowLeagueRepo.Update(
&entity.SubscriptionFollowLeague{
Expand Down
39 changes: 39 additions & 0 deletions internal/services/subscription_follow_team_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit 6297297

Please sign in to comment.