105 lines
3.8 KiB
Go
105 lines
3.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// WidgetFileUploadRepo implements GORM repository for WidgetFileUpload.
|
|
type WidgetFileUploadRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewWidgetFileUploadRepo creates a new WidgetFileUpload repository.
|
|
func NewWidgetFileUploadRepo(db *gorm.DB) *WidgetFileUploadRepo {
|
|
return &WidgetFileUploadRepo{db: db}
|
|
}
|
|
|
|
// FindByID retrieves a file upload by primary key.
|
|
func (r *WidgetFileUploadRepo) FindByID(ctx context.Context, id uint) (*model.WidgetFileUpload, error) {
|
|
var upload model.WidgetFileUpload
|
|
err := r.db.WithContext(ctx).First(&upload, id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &upload, nil
|
|
}
|
|
|
|
// FindByWidgetToken retrieves a file upload by its widget token.
|
|
func (r *WidgetFileUploadRepo) FindByWidgetToken(ctx context.Context, widgetToken string) (*model.WidgetFileUpload, error) {
|
|
var upload model.WidgetFileUpload
|
|
err := r.db.WithContext(ctx).Where("widget_token = ?", widgetToken).First(&upload).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &upload, nil
|
|
}
|
|
|
|
// FindByUUID retrieves a file upload by its upload_uuid.
|
|
// Used by the public-facing website_token staging flow.
|
|
func (r *WidgetFileUploadRepo) FindByUUID(ctx context.Context, uuid string) (*model.WidgetFileUpload, error) {
|
|
var upload model.WidgetFileUpload
|
|
err := r.db.WithContext(ctx).Where("upload_uuid = ?", uuid).First(&upload).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &upload, nil
|
|
}
|
|
|
|
// Create inserts a new file upload record.
|
|
func (r *WidgetFileUploadRepo) Create(ctx context.Context, upload *model.WidgetFileUpload) error {
|
|
return r.db.WithContext(ctx).Create(upload).Error
|
|
}
|
|
|
|
// Update modifies an existing file upload record.
|
|
func (r *WidgetFileUploadRepo) Update(ctx context.Context, upload *model.WidgetFileUpload) error {
|
|
return r.db.WithContext(ctx).Save(upload).Error
|
|
}
|
|
|
|
// UpdateStatus changes the status of a file upload (pending → attached → expired).
|
|
func (r *WidgetFileUploadRepo) UpdateStatus(ctx context.Context, id uint, status model.WidgetFileUploadStatus) error {
|
|
return r.db.WithContext(ctx).Model(&model.WidgetFileUpload{}).Where("id = ?", id).Update("status", status).Error
|
|
}
|
|
|
|
// Delete soft-deletes a file upload record.
|
|
func (r *WidgetFileUploadRepo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&model.WidgetFileUpload{}, id).Error
|
|
}
|
|
|
|
// FindExpired retrieves all file uploads that have passed their expiry timestamp
|
|
// and are still in pending status (not yet attached to a message).
|
|
func (r *WidgetFileUploadRepo) FindExpired(ctx context.Context, before time.Time) ([]model.WidgetFileUpload, error) {
|
|
var uploads []model.WidgetFileUpload
|
|
err := r.db.WithContext(ctx).
|
|
Where("status = ? AND expires_at < ?", model.WidgetFileUploadStatusPending, before).
|
|
Find(&uploads).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return uploads, nil
|
|
}
|
|
|
|
// FindByInboxAndContact retrieves all pending file uploads for a specific inbox + contact combination.
|
|
func (r *WidgetFileUploadRepo) FindByInboxAndContact(ctx context.Context, inboxID uint, contactID uint) ([]model.WidgetFileUpload, error) {
|
|
var uploads []model.WidgetFileUpload
|
|
err := r.db.WithContext(ctx).
|
|
Where("inbox_id = ? AND contact_id = ? AND status = ?", inboxID, contactID, model.WidgetFileUploadStatusPending).
|
|
Find(&uploads).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return uploads, nil
|
|
}
|
|
|
|
// BatchDeleteExpired permanently removes expired uploads older than the given cutoff.
|
|
// Used by cleanup jobs; bypasses soft-delete since these are already logically expired.
|
|
func (r *WidgetFileUploadRepo) BatchDeleteExpired(ctx context.Context, before time.Time) (int64, error) {
|
|
result := r.db.WithContext(ctx).
|
|
Where("status = ? AND expires_at < ?", model.WidgetFileUploadStatusPending, before).
|
|
Delete(&model.WidgetFileUpload{})
|
|
return result.RowsAffected, result.Error
|
|
} |