aboutsummaryrefslogtreecommitdiffstats
path: root/internal/notifier/repo
diff options
context:
space:
mode:
authorLibravatar Mo Tarbin <mhed.t91@gmail.com>2024-06-30 21:41:41 -0400
committerLibravatar Mo Tarbin <mhed.t91@gmail.com>2024-06-30 21:41:41 -0400
commitc13dd9addbf89f716e4ef5cfdf1d673139ffcb68 (patch)
treebc09646ce1d6d3a402abb4694e19da51b57204f6 /internal/notifier/repo
downloaddonetick-c13dd9addbf89f716e4ef5cfdf1d673139ffcb68.tar.gz
donetick-c13dd9addbf89f716e4ef5cfdf1d673139ffcb68.tar.bz2
donetick-c13dd9addbf89f716e4ef5cfdf1d673139ffcb68.zip
Move to Donetick Org, first commit
Diffstat (limited to 'internal/notifier/repo')
-rw-r--r--internal/notifier/repo/repository.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/internal/notifier/repo/repository.go b/internal/notifier/repo/repository.go
new file mode 100644
index 0000000..576a3f0
--- /dev/null
+++ b/internal/notifier/repo/repository.go
@@ -0,0 +1,43 @@
+package user
+
+import (
+ "context"
+ "time"
+
+ nModel "donetick.com/core/internal/notifier/model"
+ "gorm.io/gorm"
+)
+
+type NotificationRepository struct {
+ db *gorm.DB
+}
+
+func NewNotificationRepository(db *gorm.DB) *NotificationRepository {
+ return &NotificationRepository{db}
+}
+
+func (r *NotificationRepository) DeleteAllChoreNotifications(choreID int) error {
+ return r.db.Where("chore_id = ?", choreID).Delete(&nModel.Notification{}).Error
+}
+
+func (r *NotificationRepository) BatchInsertNotifications(notifications []*nModel.Notification) error {
+ return r.db.Create(&notifications).Error
+}
+func (r *NotificationRepository) MarkNotificationsAsSent(notifications []*nModel.Notification) error {
+ // Extract IDs from notifications
+ var ids []int
+ for _, notification := range notifications {
+ ids = append(ids, notification.ID)
+ }
+ // Use the extracted IDs in the Where clause
+ return r.db.Model(&nModel.Notification{}).Where("id IN (?)", ids).Update("is_sent", true).Error
+}
+func (r *NotificationRepository) GetPendingNotificaiton(c context.Context, lookback time.Duration) ([]*nModel.Notification, error) {
+ var notifications []*nModel.Notification
+ start := time.Now().UTC().Add(-lookback)
+ end := time.Now().UTC()
+ if err := r.db.Debug().Where("is_sent = ? AND scheduled_for < ? AND scheduled_for > ?", false, end, start).Find(&notifications).Error; err != nil {
+ return nil, err
+ }
+ return notifications, nil
+}