66 lines
2.2 KiB
Go
66 lines
2.2 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// DeliveryStatusRepo implements GORM repository for DeliveryStatus.
|
|
// Reference: Chatwoot message delivery status tracking per recipient
|
|
type DeliveryStatusRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewDeliveryStatusRepo creates a new DeliveryStatus repository.
|
|
func NewDeliveryStatusRepo(db *gorm.DB) *DeliveryStatusRepo {
|
|
return &DeliveryStatusRepo{db: db}
|
|
}
|
|
|
|
// FindByMessage retrieves all delivery statuses for a specific message.
|
|
func (r *DeliveryStatusRepo) FindByMessage(ctx context.Context, messageID uint) ([]model.DeliveryStatus, error) {
|
|
var statuses []model.DeliveryStatus
|
|
err := r.db.WithContext(ctx).Preload("Contact").
|
|
Where("message_id = ?", messageID).
|
|
Order("id ASC").Find(&statuses).Error
|
|
return statuses, err
|
|
}
|
|
|
|
// FindByMessageAndContact retrieves the delivery status for a message and specific contact.
|
|
func (r *DeliveryStatusRepo) FindByMessageAndContact(ctx context.Context, messageID uint, contactID uint) (*model.DeliveryStatus, error) {
|
|
var status model.DeliveryStatus
|
|
err := r.db.WithContext(ctx).Preload("Contact").
|
|
Where("message_id = ? AND contact_id = ?", messageID, contactID).
|
|
First(&status).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &status, nil
|
|
}
|
|
|
|
// FindByID retrieves a delivery status by primary key.
|
|
func (r *DeliveryStatusRepo) FindByID(ctx context.Context, id uint) (*model.DeliveryStatus, error) {
|
|
var status model.DeliveryStatus
|
|
err := r.db.WithContext(ctx).First(&status, id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &status, nil
|
|
}
|
|
|
|
// Create inserts a new delivery status record.
|
|
func (r *DeliveryStatusRepo) Create(ctx context.Context, status *model.DeliveryStatus) error {
|
|
return r.db.WithContext(ctx).Create(status).Error
|
|
}
|
|
|
|
// Update saves changes to an existing delivery status.
|
|
func (r *DeliveryStatusRepo) Update(ctx context.Context, status *model.DeliveryStatus) error {
|
|
return r.db.WithContext(ctx).Save(status).Error
|
|
}
|
|
|
|
// Delete removes a delivery status record.
|
|
func (r *DeliveryStatusRepo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&model.DeliveryStatus{}, id).Error
|
|
} |