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

@@ -355,6 +355,27 @@ func (r *TaskRepository) Create(task *models.Task) error {
return r.db.Create(task).Error
}
// CreateTx creates a new task within an existing transaction. Used by
// bulk-create flows where multiple inserts must succeed or fail together.
func (r *TaskRepository) CreateTx(tx *gorm.DB, task *models.Task) error {
return tx.Create(task).Error
}
// FindByIDTx loads a task within an existing transaction. Preloads only the
// fields the bulk-create response needs (CreatedBy, AssignedTo). Category /
// Priority / Frequency are resolved client-side from the lookup cache, so
// we skip them here to match the FindByResidence preload set.
func (r *TaskRepository) FindByIDTx(tx *gorm.DB, id uint) (*models.Task, error) {
var task models.Task
err := tx.Preload("CreatedBy").
Preload("AssignedTo").
First(&task, id).Error
if err != nil {
return nil, err
}
return &task, nil
}
// Update updates a task with optimistic locking.
// The update only succeeds if the task's version in the database matches the expected version.
// On success, the local task.Version is incremented to reflect the new version.

View File

@@ -104,20 +104,6 @@ func (r *TaskTemplateRepository) Count() (int64, error) {
return count, err
}
// GetByRegion returns active templates associated with a specific climate region
func (r *TaskTemplateRepository) GetByRegion(regionID uint) ([]models.TaskTemplate, error) {
var templates []models.TaskTemplate
err := r.db.
Preload("Category").
Preload("Frequency").
Preload("Regions").
Joins("JOIN task_tasktemplate_regions ON task_tasktemplate_regions.task_template_id = task_tasktemplate.id").
Where("task_tasktemplate_regions.climate_region_id = ? AND task_tasktemplate.is_active = ?", regionID, true).
Order("task_tasktemplate.display_order ASC, task_tasktemplate.title ASC").
Find(&templates).Error
return templates, err
}
// GetGroupedByCategory returns templates grouped by category name
func (r *TaskTemplateRepository) GetGroupedByCategory() (map[string][]models.TaskTemplate, error) {
templates, err := r.GetAll()

View File

@@ -185,33 +185,6 @@ func TestTaskTemplateRepository_Count(t *testing.T) {
assert.Equal(t, int64(2), count) // Only active
}
func TestTaskTemplateRepository_GetByRegion(t *testing.T) {
t.Skip("requires PostgreSQL: SQLite cannot scan jsonb default into json.RawMessage")
db := testutil.SetupTestDB(t)
repo := NewTaskTemplateRepository(db)
// Create a climate region
region := &models.ClimateRegion{Name: "Hot-Humid", ZoneNumber: 1, IsActive: true}
require.NoError(t, db.Create(region).Error)
// Create template with region association
tmpl := &models.TaskTemplate{Title: "Regional Task", IsActive: true}
require.NoError(t, db.Create(tmpl).Error)
// Associate template with region via join table
err := db.Exec("INSERT INTO task_tasktemplate_regions (task_template_id, climate_region_id) VALUES (?, ?)", tmpl.ID, region.ID).Error
require.NoError(t, err)
// Create template without region
tmpl2 := &models.TaskTemplate{Title: "Non-Regional Task", IsActive: true}
require.NoError(t, db.Create(tmpl2).Error)
templates, err := repo.GetByRegion(region.ID)
require.NoError(t, err)
assert.Len(t, templates, 1)
assert.Equal(t, "Regional Task", templates[0].Title)
}
func TestTaskTemplateRepository_GetGroupedByCategory(t *testing.T) {
t.Skip("requires PostgreSQL: SQLite cannot scan jsonb default into json.RawMessage")
db := testutil.SetupTestDB(t)