Files
gochat/internal/automation/macro_worker.go
T

47 lines
1.4 KiB
Go

package automation
import (
"context"
"encoding/json"
"fmt"
"sync"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/worker"
)
type macroExecutionJob struct {
AccountID uint `json:"account_id"`
MacroID uint `json:"macro_id"`
ConversationIDs []uint `json:"conversation_ids"`
UserID uint `json:"user_id"`
}
var macroExecutionRegistrations sync.Map
// RegisterMacroExecutionJobs wires Chatwoot's MacrosExecutionJob equivalent.
// The job resolves account-scoped conversation display IDs at perform time.
func RegisterMacroExecutionJobs(wp *worker.WorkerPool, db DBProvider) {
if wp == nil || db == nil {
return
}
if _, loaded := macroExecutionRegistrations.LoadOrStore(wp, struct{}{}); loaded {
return
}
runner := &macroExecutionJobRunner{db: db, worker: wp}
wp.Register(TaskTypeMacroExecution, runner.perform)
}
type macroExecutionJobRunner struct {
db DBProvider
worker *worker.WorkerPool
}
func (r *macroExecutionJobRunner) perform(ctx context.Context, job *model.BackgroundJob) error {
var payload macroExecutionJob
if err := json.Unmarshal(job.Payload, &payload); err != nil {
return fmt.Errorf("unmarshal macro execution job: %w", err)
}
return NewMacroServiceWithWorker(r.db, r.worker).executeForDisplayIDsNow(ctx, payload.AccountID, payload.MacroID, payload.ConversationIDs, payload.UserID)
}