Onboarding: template backlink, bulk-create endpoint, climate-region scoring
Some checks failed
Backend CI / Test (push) Has been cancelled
Backend CI / Contract Tests (push) Has been cancelled
Backend CI / Build (push) Has been cancelled
Backend CI / Lint (push) Has been cancelled
Backend CI / Secret Scanning (push) Has been cancelled

Clients that send users through a multi-task onboarding step no longer
loop N POST /api/tasks/ calls and no longer create "orphan" tasks with
no reference to the TaskTemplate they came from.

Task model
- New task_template_id column + GORM FK (migration 000016)
- CreateTaskRequest.template_id, TaskResponse.template_id
- task_service.CreateTask persists the backlink

Bulk endpoint
- POST /api/tasks/bulk/ — 1-50 tasks in a single transaction,
  returns every created row + TotalSummary. Single residence access
  check, per-entry residence_id is overridden with batch value
- task_handler.BulkCreateTasks + task_service.BulkCreateTasks using
  db.Transaction; task_repo.CreateTx + FindByIDTx helpers

Climate-region scoring
- templateConditions gains ClimateRegionID; suggestion_service scores
  residence.PostalCode -> ZipToState -> GetClimateRegionIDByState against
  the template's conditions JSON (no penalty on mismatch / unknown ZIP)
- regionMatchBonus 0.35, totalProfileFields 14 -> 15
- Standalone GET /api/tasks/templates/by-region/ removed; legacy
  task_tasktemplate_regions many-to-many dropped (migration 000017).
  Region affinity now lives entirely in the template's conditions JSON

Tests
- +11 cases across task_service_test, task_handler_test, suggestion_
  service_test: template_id persistence, bulk rollback + cap + auth,
  region match / mismatch / no-ZIP / unknown-ZIP / stacks-with-others

Docs
- docs/openapi.yaml: /tasks/bulk/ + BulkCreateTasks schemas, template_id
  on TaskResponse + CreateTaskRequest, /templates/by-region/ removed

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Trey t
2026-04-14 15:23:57 -05:00
parent 33eee812b6
commit 237c6b84ee
24 changed files with 678 additions and 169 deletions

View File

@@ -52,6 +52,18 @@ func (fd *FlexibleDate) ToTimePtr() *time.Time {
return &fd.Time
}
// BulkCreateTasksRequest represents a batch create. Used by onboarding to
// insert 1-N selected tasks atomically in a single transaction so that a
// failure halfway through doesn't leave a partial task list behind.
//
// ResidenceID is validated once at the service layer; individual task
// entries must reference the same residence or be left empty (the service
// overrides each entry's ResidenceID with the top-level value).
type BulkCreateTasksRequest struct {
ResidenceID uint `json:"residence_id" validate:"required"`
Tasks []CreateTaskRequest `json:"tasks" validate:"required,min=1,max=50,dive"`
}
// CreateTaskRequest represents the request to create a task
type CreateTaskRequest struct {
ResidenceID uint `json:"residence_id" validate:"required"`
@@ -66,6 +78,10 @@ type CreateTaskRequest struct {
DueDate *FlexibleDate `json:"due_date"`
EstimatedCost *decimal.Decimal `json:"estimated_cost"`
ContractorID *uint `json:"contractor_id"`
// TemplateID links the created task to the TaskTemplate it was spawned from
// (e.g. onboarding suggestion or catalog pick). Optional — custom tasks
// leave this nil.
TemplateID *uint `json:"template_id"`
}
// UpdateTaskRequest represents the request to update a task

View File

@@ -740,20 +740,6 @@ func TestNewTaskTemplateResponse(t *testing.T) {
}
}
func TestNewTaskTemplateResponse_WithRegion(t *testing.T) {
tmpl := makeTemplate(nil, nil)
tmpl.Regions = []models.ClimateRegion{
{BaseModel: models.BaseModel{ID: 5}, Name: "Southeast"},
}
resp := NewTaskTemplateResponse(&tmpl)
if resp.RegionID == nil || *resp.RegionID != 5 {
t.Error("RegionID should be 5")
}
if resp.RegionName != "Southeast" {
t.Errorf("RegionName = %q", resp.RegionName)
}
}
func TestNewTaskTemplatesGroupedResponse_Grouping(t *testing.T) {
catID := uint(1)
cat := &models.TaskCategory{BaseModel: models.BaseModel{ID: 1}, Name: "Exterior"}

View File

@@ -95,12 +95,22 @@ type TaskResponse struct {
IsCancelled bool `json:"is_cancelled"`
IsArchived bool `json:"is_archived"`
ParentTaskID *uint `json:"parent_task_id"`
TemplateID *uint `json:"template_id,omitempty"` // Backlink to the TaskTemplate this task was created from
CompletionCount int `json:"completion_count"`
KanbanColumn string `json:"kanban_column,omitempty"` // Which kanban column this task belongs to
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// BulkCreateTasksResponse is returned by POST /api/tasks/bulk/.
// All entries are created in a single transaction — if any insert fails the
// whole batch is rolled back and no partial state is visible.
type BulkCreateTasksResponse struct {
Tasks []TaskResponse `json:"tasks"`
Summary TotalSummary `json:"summary"`
CreatedCount int `json:"created_count"`
}
// Note: Pagination removed - list endpoints now return arrays directly
// KanbanColumnResponse represents a kanban column
@@ -249,6 +259,7 @@ func newTaskResponseInternal(t *models.Task, daysThreshold int, now time.Time) T
IsCancelled: t.IsCancelled,
IsArchived: t.IsArchived,
ParentTaskID: t.ParentTaskID,
TemplateID: t.TaskTemplateID,
CompletionCount: predicates.GetCompletionCount(t),
KanbanColumn: DetermineKanbanColumnWithTime(t, daysThreshold, now),
CreatedAt: t.CreatedAt,

View File

@@ -21,8 +21,6 @@ type TaskTemplateResponse struct {
Tags []string `json:"tags"`
DisplayOrder int `json:"display_order"`
IsActive bool `json:"is_active"`
RegionID *uint `json:"region_id,omitempty"`
RegionName string `json:"region_name,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
@@ -65,11 +63,6 @@ func NewTaskTemplateResponse(t *models.TaskTemplate) TaskTemplateResponse {
resp.Frequency = NewTaskFrequencyResponse(t.Frequency)
}
if len(t.Regions) > 0 {
resp.RegionID = &t.Regions[0].ID
resp.RegionName = t.Regions[0].Name
}
return resp
}