224 lines
6.5 KiB
Go
224 lines
6.5 KiB
Go
|
|
package repository
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
app "haixun-backend/internal/library/errors"
|
||
|
|
"haixun-backend/internal/library/errors/code"
|
||
|
|
"haixun-backend/internal/model/publish_queue/domain/entity"
|
||
|
|
domrepo "haixun-backend/internal/model/publish_queue/domain/repository"
|
||
|
|
|
||
|
|
"go.mongodb.org/mongo-driver/bson"
|
||
|
|
"go.mongodb.org/mongo-driver/mongo"
|
||
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||
|
|
)
|
||
|
|
|
||
|
|
type mongoRepository struct {
|
||
|
|
collection *mongo.Collection
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewMongoRepository(db *mongo.Database) domrepo.Repository {
|
||
|
|
if db == nil {
|
||
|
|
return &mongoRepository{}
|
||
|
|
}
|
||
|
|
return &mongoRepository{collection: db.Collection(entity.CollectionName)}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *mongoRepository) EnsureIndexes(ctx context.Context) error {
|
||
|
|
if r.collection == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
_, err := r.collection.Indexes().CreateMany(ctx, []mongo.IndexModel{
|
||
|
|
{
|
||
|
|
Keys: bson.D{
|
||
|
|
{Key: "tenant_id", Value: 1},
|
||
|
|
{Key: "owner_uid", Value: 1},
|
||
|
|
{Key: "account_id", Value: 1},
|
||
|
|
{Key: "status", Value: 1},
|
||
|
|
{Key: "scheduled_at", Value: 1},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
})
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func actorFilter(tenantID, ownerUID, accountID string) bson.M {
|
||
|
|
filter := bson.M{
|
||
|
|
"tenant_id": strings.TrimSpace(tenantID),
|
||
|
|
"owner_uid": strings.TrimSpace(ownerUID),
|
||
|
|
}
|
||
|
|
if accountID = strings.TrimSpace(accountID); accountID != "" {
|
||
|
|
filter["account_id"] = accountID
|
||
|
|
}
|
||
|
|
return filter
|
||
|
|
}
|
||
|
|
|
||
|
|
func normalizePage(page, pageSize int) (int, int) {
|
||
|
|
if page <= 0 {
|
||
|
|
page = 1
|
||
|
|
}
|
||
|
|
if pageSize <= 0 {
|
||
|
|
pageSize = 10
|
||
|
|
}
|
||
|
|
if pageSize > 50 {
|
||
|
|
pageSize = 50
|
||
|
|
}
|
||
|
|
return page, pageSize
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *mongoRepository) Create(ctx context.Context, item *entity.QueueItem) error {
|
||
|
|
if r.collection == nil {
|
||
|
|
return app.For(code.ThreadsAccount).DBUnavailable("Mongo is not configured")
|
||
|
|
}
|
||
|
|
if item == nil {
|
||
|
|
return app.For(code.ThreadsAccount).InputMissingRequired("queue item is required")
|
||
|
|
}
|
||
|
|
_, err := r.collection.InsertOne(ctx, item)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *mongoRepository) Get(ctx context.Context, tenantID, ownerUID, accountID, queueID string) (*entity.QueueItem, error) {
|
||
|
|
if r.collection == nil {
|
||
|
|
return nil, app.For(code.ThreadsAccount).DBUnavailable("Mongo is not configured")
|
||
|
|
}
|
||
|
|
queueID = strings.TrimSpace(queueID)
|
||
|
|
if queueID == "" {
|
||
|
|
return nil, app.For(code.ThreadsAccount).InputMissingRequired("queue id is required")
|
||
|
|
}
|
||
|
|
filter := actorFilter(tenantID, ownerUID, accountID)
|
||
|
|
filter["_id"] = queueID
|
||
|
|
var out entity.QueueItem
|
||
|
|
err := r.collection.FindOne(ctx, filter).Decode(&out)
|
||
|
|
if err != nil {
|
||
|
|
if err == mongo.ErrNoDocuments {
|
||
|
|
return nil, app.For(code.ThreadsAccount).ResNotFound("publish queue item not found")
|
||
|
|
}
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return &out, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *mongoRepository) List(ctx context.Context, filter domrepo.ListFilter, page, pageSize int) (*domrepo.ListResult, error) {
|
||
|
|
if r.collection == nil {
|
||
|
|
return nil, app.For(code.ThreadsAccount).DBUnavailable("Mongo is not configured")
|
||
|
|
}
|
||
|
|
page, pageSize = normalizePage(page, pageSize)
|
||
|
|
mongoFilter := actorFilter(filter.TenantID, filter.OwnerUID, filter.AccountID)
|
||
|
|
if status := strings.TrimSpace(filter.Status); status != "" {
|
||
|
|
mongoFilter["status"] = status
|
||
|
|
}
|
||
|
|
total, err := r.collection.CountDocuments(ctx, mongoFilter)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
opts := options.Find().
|
||
|
|
SetSort(bson.D{{Key: "scheduled_at", Value: -1}}).
|
||
|
|
SetSkip(int64((page - 1) * pageSize)).
|
||
|
|
SetLimit(int64(pageSize))
|
||
|
|
cur, err := r.collection.Find(ctx, mongoFilter, opts)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
defer cur.Close(ctx)
|
||
|
|
var items []entity.QueueItem
|
||
|
|
if err := cur.All(ctx, &items); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return &domrepo.ListResult{Items: items, Total: total}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *mongoRepository) ListDue(ctx context.Context, now int64, limit int) ([]entity.QueueItem, error) {
|
||
|
|
if r.collection == nil {
|
||
|
|
return nil, app.For(code.ThreadsAccount).DBUnavailable("Mongo is not configured")
|
||
|
|
}
|
||
|
|
if limit <= 0 {
|
||
|
|
limit = 20
|
||
|
|
}
|
||
|
|
filter := bson.M{
|
||
|
|
"status": entity.StatusScheduled,
|
||
|
|
"scheduled_at": bson.M{"$lte": now},
|
||
|
|
}
|
||
|
|
opts := options.Find().
|
||
|
|
SetSort(bson.D{{Key: "scheduled_at", Value: 1}}).
|
||
|
|
SetLimit(int64(limit))
|
||
|
|
cur, err := r.collection.Find(ctx, filter, opts)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
defer cur.Close(ctx)
|
||
|
|
var items []entity.QueueItem
|
||
|
|
if err := cur.All(ctx, &items); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return items, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *mongoRepository) UpdateIfStatus(ctx context.Context, item *entity.QueueItem, allowed []string) (*entity.QueueItem, error) {
|
||
|
|
if r.collection == nil {
|
||
|
|
return nil, app.For(code.ThreadsAccount).DBUnavailable("Mongo is not configured")
|
||
|
|
}
|
||
|
|
if item == nil || item.ID == "" {
|
||
|
|
return nil, app.For(code.ThreadsAccount).InputMissingRequired("queue item is required")
|
||
|
|
}
|
||
|
|
filter := bson.M{
|
||
|
|
"_id": item.ID,
|
||
|
|
"status": bson.M{"$in": allowed},
|
||
|
|
}
|
||
|
|
res, err := r.collection.ReplaceOne(ctx, filter, item)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
if res.MatchedCount == 0 {
|
||
|
|
return nil, app.For(code.ThreadsAccount).ResInvalidState("queue item status changed; update rejected")
|
||
|
|
}
|
||
|
|
return item, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *mongoRepository) Update(ctx context.Context, item *entity.QueueItem) (*entity.QueueItem, error) {
|
||
|
|
if r.collection == nil {
|
||
|
|
return nil, app.For(code.ThreadsAccount).DBUnavailable("Mongo is not configured")
|
||
|
|
}
|
||
|
|
if item == nil || item.ID == "" {
|
||
|
|
return nil, app.For(code.ThreadsAccount).InputMissingRequired("queue item is required")
|
||
|
|
}
|
||
|
|
filter := bson.M{"_id": item.ID}
|
||
|
|
res, err := r.collection.ReplaceOne(ctx, filter, item)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
if res.MatchedCount == 0 {
|
||
|
|
return nil, app.For(code.ThreadsAccount).ResNotFound("publish queue item not found")
|
||
|
|
}
|
||
|
|
return item, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *mongoRepository) Delete(ctx context.Context, tenantID, ownerUID, accountID, queueID string) error {
|
||
|
|
if r.collection == nil {
|
||
|
|
return app.For(code.ThreadsAccount).DBUnavailable("Mongo is not configured")
|
||
|
|
}
|
||
|
|
queueID = strings.TrimSpace(queueID)
|
||
|
|
if queueID == "" {
|
||
|
|
return app.For(code.ThreadsAccount).InputMissingRequired("queue id is required")
|
||
|
|
}
|
||
|
|
filter := actorFilter(tenantID, ownerUID, accountID)
|
||
|
|
filter["_id"] = queueID
|
||
|
|
res, err := r.collection.DeleteOne(ctx, filter)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if res.DeletedCount == 0 {
|
||
|
|
return app.For(code.ThreadsAccount).ResNotFound("publish queue item not found")
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *mongoRepository) CountByStatus(ctx context.Context, tenantID, ownerUID, accountID, status string) (int64, error) {
|
||
|
|
if r.collection == nil {
|
||
|
|
return 0, app.For(code.ThreadsAccount).DBUnavailable("Mongo is not configured")
|
||
|
|
}
|
||
|
|
filter := actorFilter(tenantID, ownerUID, accountID)
|
||
|
|
filter["status"] = strings.TrimSpace(status)
|
||
|
|
return r.collection.CountDocuments(ctx, filter)
|
||
|
|
}
|