30 lines
992 B
Go
30 lines
992 B
Go
package local_event
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"git.ilapage.cn/ila/yovision/Sense/server/app/sense/outbox"
|
|
)
|
|
|
|
// CreateWithOutbox commits the local candidate and its internal delivery
|
|
// record atomically. The payload remains Sense-internal and is not a Bell or
|
|
// Brain contract.
|
|
func CreateWithOutbox(ctx context.Context, db *gorm.DB, candidate EventCandidate, payload map[string]interface{}, now time.Time) error {
|
|
encoded, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return fmt.Errorf("encode local event outbox payload: %w", err)
|
|
}
|
|
return db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
if err := tx.Create(&candidate).Error; err != nil {
|
|
return fmt.Errorf("create local event candidate: %w", err)
|
|
}
|
|
_, err = outbox.Enqueue(tx, outbox.EnqueueInput{InternalType: "local_event_candidate", BusinessRef: candidate.ID, IdempotencyKey: "local-event:" + candidate.ID + ":v1", PayloadJSON: encoded}, now)
|
|
return err
|
|
})
|
|
}
|