Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
rupinr committed Oct 27, 2024
1 parent ee9eb13 commit 6365d9c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 31 deletions.
35 changes: 5 additions & 30 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"news-master/app"
"news-master/datamodels/dto"
"news-master/datamodels/entity"
applogger "news-master/logger"
"sync"
"time"

Expand Down Expand Up @@ -140,7 +141,7 @@ func IncrementAndGetLoginAttempt(userData dto.User) entity.User {
func GetSubscriptionByEmail(email string) (entity.Subscription, error) {
var user entity.User
result := db().First(&user, entity.User{Email: email})
fmt.Println(result.Error)
applogger.Log.Error(fmt.Sprintf("Error finding subscription with email %v", result.Error.Error()))
var subscription entity.Subscription

db().
Expand All @@ -154,12 +155,7 @@ func GetSubscriptionByEmail(email string) (entity.Subscription, error) {
func GetSubscriptionByID(id int) entity.Subscription {

var subscription entity.Subscription

r := db().Joins("SubscriptionSchedule").Joins("User").Find(&subscription, id)
fmt.Printf("Query is %v \n", r.Statement.SQL.String())

fmt.Printf("sub id is %v \n", subscription.ID)

db().Joins("SubscriptionSchedule").Joins("User").Find(&subscription, id)
return subscription
}

Expand All @@ -177,7 +173,6 @@ func GetSubscriptionsToProcess() []entity.Subscription {

func SetLastProcessedAt(subscriptionId uint) {
time := time.Now()
fmt.Printf("setting last processed at as %v for sub %v\n", time, subscriptionId)
var sub entity.Subscription
db().Find(&sub, subscriptionId)
sub.LastProcessedAt = time
Expand Down Expand Up @@ -205,7 +200,6 @@ func CreateSubscriptionSchedule(subscriptionScheduleData dto.SubscriptionSchedul
TimeZone: subscriptionScheduleData.TimeZone,
}

// Map to hold conditions
conditions := map[string]interface{}{
"monday": subscriptionScheduleDb.Monday,
"tuesday": subscriptionScheduleDb.Tuesday,
Expand All @@ -221,9 +215,9 @@ func CreateSubscriptionSchedule(subscriptionScheduleData dto.SubscriptionSchedul
var subscriptionSchedule entity.SubscriptionSchedule

if err := db().Where(conditions).FirstOrCreate(&subscriptionSchedule, subscriptionScheduleDb).Error; err != nil {
fmt.Println("Error creating or finding record:", err)
applogger.Log.Error(fmt.Sprintf("Error creating or finding record: %v", err.Error()))
} else {
fmt.Printf("Record found or created: %+v\n", subscriptionSchedule)
applogger.Log.Debug(fmt.Sprintf("Record found or created: %+v\n", subscriptionSchedule))
}

return subscriptionSchedule
Expand All @@ -243,25 +237,6 @@ func CreateSubscription(user entity.User, sites []string, subscriptionScheduleID
return subscription
}

func UpdateSubscriptionConfirmation(id uint, confirmed bool) error {
err = db().Transaction(func(tx *gorm.DB) error {
var subscription entity.Subscription
if err := tx.Where(&entity.Subscription{UserID: id}).First(&subscription).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return fmt.Errorf("topic not found")
}
return err
}
subscription.Confirmed = confirmed
if err := tx.Save(&subscription).Error; err != nil {
return err
}
return nil
})
return err

}

func CreateFeedBack(feedback dto.Feedback) (*entity.Feedback, error) {
var f entity.Feedback
f.Content = feedback.Content
Expand Down
3 changes: 2 additions & 1 deletion tasks/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,11 @@ func SendNewsletter() {
if emailError != nil {
logger.Log.Error(fmt.Sprintf("Unable to send email to %v", emailError.Error()))
} else {
logger.Log.Debug(fmt.Sprintf("Setting last processed time stamp for subscription with ID %v", subscription.ID))
repository.SetLastProcessedAt(subscription.ID)
logger.Log.Debug(fmt.Sprintf("Sent email for subscription with ID and set last_processed %v", subscription.ID))
}
} else {
logger.Log.Debug(fmt.Sprintf("The subscription with ID %v is not ellibible to recieve email at this slot", subscription.ID))
continue
}
}
Expand Down

0 comments on commit 6365d9c

Please sign in to comment.