mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-05 15:26:36 +08:00
refactor(admin): decouple system cleanup with event bus and enforce single owner principle
This commit is contained in:
@@ -255,3 +255,13 @@ func CreatePushHistoryRecord(ctx context.Context, history *entity.PushHistory) e
|
||||
func PushHistoryQuery(ctx context.Context) *gorm.DB {
|
||||
return GetDB(ctx).Model(&entity.PushHistory{})
|
||||
}
|
||||
|
||||
// DeletePushHistoriesBeforeRecord deletes push history records created before cutoff time.
|
||||
func DeletePushHistoriesBeforeRecord(ctx context.Context, cutoff time.Time) (int64, error) {
|
||||
db := GetDB(ctx)
|
||||
if db == nil {
|
||||
return 0, nil
|
||||
}
|
||||
result := db.Where("created_at < ?", cutoff).Delete(&entity.PushHistory{})
|
||||
return result.RowsAffected, result.Error
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"Wavelet/plugins/domain/msg_gateway/model/entity"
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -94,3 +95,51 @@ func TestPushEventDAO_CRUD(t *testing.T) {
|
||||
_, err = dao.GetPushEventByIDRecord(ctx, ev.ID)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestPushHistoryDAO_Cleanup(t *testing.T) {
|
||||
_ = idgen.Init(1)
|
||||
db, _, cleanup := testhelper.SetupTestEnvironment(t)
|
||||
defer cleanup()
|
||||
require.NoError(t, db.AutoMigrate(&entity.PushHistory{}))
|
||||
|
||||
dao.SetDBServiceForTest(stubDBService{db: db})
|
||||
t.Cleanup(func() { dao.SetDBServiceForTest(nil) })
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
now := time.Now()
|
||||
oldTime := now.Add(-40 * 24 * time.Hour)
|
||||
recentTime := now.Add(-5 * 24 * time.Hour)
|
||||
|
||||
oldHistory := entity.PushHistory{
|
||||
EventKey: "login",
|
||||
Channel: "telegram",
|
||||
Target: "123",
|
||||
Title: "Old login",
|
||||
Content: "Old content",
|
||||
Level: "info",
|
||||
Status: "success",
|
||||
CreatedAt: oldTime,
|
||||
}
|
||||
recentHistory := entity.PushHistory{
|
||||
EventKey: "login",
|
||||
Channel: "telegram",
|
||||
Target: "123",
|
||||
Title: "Recent login",
|
||||
Content: "Recent content",
|
||||
Level: "info",
|
||||
Status: "success",
|
||||
CreatedAt: recentTime,
|
||||
}
|
||||
require.NoError(t, db.Create(&oldHistory).Error)
|
||||
require.NoError(t, db.Create(&recentHistory).Error)
|
||||
|
||||
cutoff := now.Add(-30 * 24 * time.Hour)
|
||||
deleted, err := dao.DeletePushHistoriesBeforeRecord(ctx, cutoff)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(1), deleted)
|
||||
|
||||
var count int64
|
||||
db.Model(&entity.PushHistory{}).Count(&count)
|
||||
assert.Equal(t, int64(1), count)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user