2026-06-27 16:03:28 +00:00
|
|
|
package job
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
jobdom "haixun-backend/internal/model/job/domain/usecase"
|
|
|
|
|
threadsaccountdomain "haixun-backend/internal/model/threads_account/domain/usecase"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type RefreshThreadsTokenDeps struct {
|
|
|
|
|
Jobs jobdom.UseCase
|
|
|
|
|
ThreadsAccount threadsaccountdomain.UseCase
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func RegisterRefreshThreadsTokenHandler(runner *Runner, deps RefreshThreadsTokenDeps) {
|
|
|
|
|
if runner == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
runner.RegisterStepHandler("refresh", func(ctx context.Context, step StepContext) error {
|
|
|
|
|
return runRefreshThreadsToken(ctx, step, deps)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func runRefreshThreadsToken(ctx context.Context, step StepContext, deps RefreshThreadsTokenDeps) error {
|
|
|
|
|
payload := step.Run.Payload
|
|
|
|
|
tenantID, ownerUID := runActorFromPayload(payload, step.Run)
|
|
|
|
|
accountID := strings.TrimSpace(stringField(payload, "account_id"))
|
|
|
|
|
if tenantID == "" || ownerUID == "" || accountID == "" {
|
|
|
|
|
return fmt.Errorf("refresh-threads-token payload missing tenant_id, owner_uid, or account_id")
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-06 06:13:14 +00:00
|
|
|
saved, err := deps.ThreadsAccount.RefreshAccountAPIAccessToken(ctx, tenantID, ownerUID, accountID)
|
2026-06-27 16:03:28 +00:00
|
|
|
if err != nil {
|
2026-07-06 06:13:14 +00:00
|
|
|
return fmt.Errorf("refresh account token: %w", err)
|
2026-06-27 16:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = deps.Jobs.CompleteRun(ctx, jobdom.CompleteRunRequest{
|
|
|
|
|
JobID: step.JobID,
|
|
|
|
|
WorkerID: step.WorkerID,
|
|
|
|
|
Result: map[string]any{
|
2026-07-06 06:13:14 +00:00
|
|
|
"account_id": accountID,
|
|
|
|
|
"expires_at": saved.APITokenExpiresAt,
|
2026-06-27 16:03:28 +00:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
return err
|
|
|
|
|
}
|