30 lines
928 B
Go
30 lines
928 B
Go
package local_event
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"git.ilapage.cn/ila/yovision/Sense/server/app/sense/integration/bell_connector"
|
|
)
|
|
|
|
// CreateWithOutbox commits the local candidate and its internal delivery
|
|
// record atomically. The payload is the frozen anonymous event contract; it
|
|
// never carries the candidate's internal evidence path or delivery attempts.
|
|
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 = bell_connector.EnqueueEvent(tx, encoded, now)
|
|
return err
|
|
})
|
|
}
|