CreateTask derives one canonical task record from trusted actor context and persists the corresponding immutable audit event.
(ctx context.Context, spec CreateTask, actor ActorContext)
| 306 | // CreateTask derives one canonical task record from trusted actor context and |
| 307 | // persists the corresponding immutable audit event. |
| 308 | func (m *Service) CreateTask(ctx context.Context, spec CreateTask, actor ActorContext) (*Task, error) { |
| 309 | if err := requireCreateAuthority(actor, spec.Scope); err != nil { |
| 310 | return nil, err |
| 311 | } |
| 312 | |
| 313 | normalizedSpec, err := normalizeCreateTaskSpec(spec) |
| 314 | if err != nil { |
| 315 | return nil, err |
| 316 | } |
| 317 | if err := m.validateParentConstraints(ctx, normalizedSpec); err != nil { |
| 318 | return nil, err |
| 319 | } |
| 320 | if err := m.validateNetworkChannel("create_task.network_channel", normalizedSpec.NetworkChannel); err != nil { |
| 321 | return nil, err |
| 322 | } |
| 323 | |
| 324 | now := m.now().UTC() |
| 325 | record := Task{ |
| 326 | ID: normalizedSpec.ID, |
| 327 | Identifier: normalizedSpec.Identifier, |
| 328 | Scope: normalizedSpec.Scope, |
| 329 | WorkspaceID: normalizedSpec.WorkspaceID, |
| 330 | ParentTaskID: normalizedSpec.ParentTaskID, |
| 331 | NetworkChannel: normalizedSpec.NetworkChannel, |
| 332 | Title: normalizedSpec.Title, |
| 333 | Description: normalizedSpec.Description, |
| 334 | Priority: normalizedSpec.Priority, |
| 335 | MaxAttempts: createTaskMaxAttempts(normalizedSpec), |
| 336 | AutoEnqueueOnReady: normalizedSpec.AutoEnqueueOnReady, |
| 337 | Status: createdTaskStatus(normalizedSpec), |
| 338 | ApprovalPolicy: normalizedSpec.ApprovalPolicy, |
| 339 | ApprovalState: defaultApprovalStateForPolicy(normalizedSpec.ApprovalPolicy), |
| 340 | Owner: cloneOwnership(normalizedSpec.Owner), |
| 341 | WakeCreator: createTaskWakeCreator(normalizedSpec), |
| 342 | CreatedBy: actor.Actor, |
| 343 | Origin: actor.Origin, |
| 344 | CreatedAt: now, |
| 345 | UpdatedAt: now, |
| 346 | Metadata: cloneRawJSON(normalizedSpec.Metadata), |
| 347 | } |
| 348 | if strings.TrimSpace(record.ID) == "" { |
| 349 | record.ID = m.newID("task") |
| 350 | } |
| 351 | if err := record.Validate(); err != nil { |
| 352 | return nil, err |
| 353 | } |
| 354 | if err := m.store.CreateTask(ctx, record); err != nil { |
| 355 | return nil, err |
| 356 | } |
| 357 | if err := m.recordTaskEvent(ctx, record.ID, "", taskEventCreated, actor, createdTaskPayload{ |
| 358 | Scope: record.Scope, |
| 359 | WorkspaceID: record.WorkspaceID, |
| 360 | ParentTaskID: record.ParentTaskID, |
| 361 | Status: record.Status, |
| 362 | NetworkChannel: record.NetworkChannel, |
| 363 | Owner: cloneOwnership(record.Owner), |
| 364 | }); err != nil { |
| 365 | return nil, err |
no test coverage detected