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

@@ -142,8 +142,8 @@ func TestSuggestionService_ProfileCompleteness(t *testing.T) {
resp, err := service.GetSuggestions(residence.ID, user.ID)
require.NoError(t, err)
// 4 fields filled out of 14
expectedCompleteness := 4.0 / 14.0
// 4 fields filled out of 15 (home-profile fields + ZIP/region)
expectedCompleteness := 4.0 / float64(totalProfileFields)
assert.InDelta(t, expectedCompleteness, resp.ProfileCompleteness, 0.01)
}
@@ -336,6 +336,7 @@ func TestCalculateProfileCompleteness_FullProfile(t *testing.T) {
ExteriorType: &et,
FlooringPrimary: &fp,
LandscapingType: &lt,
PostalCode: "10001", // NY → zone 5 — counts as the 15th field
}
completeness := CalculateProfileCompleteness(residence)
@@ -699,4 +700,140 @@ func TestTemplateConditions_IsEmpty(t *testing.T) {
pt := "House"
cond4 := &templateConditions{PropertyType: &pt}
assert.False(t, cond4.isEmpty())
var regionID uint = 5
cond5 := &templateConditions{ClimateRegionID: &regionID}
assert.False(t, cond5.isEmpty())
}
// === Climate region condition (15th field) ===
func TestSuggestionService_ClimateRegionMatch(t *testing.T) {
service := setupSuggestionService(t)
user := testutil.CreateTestUser(t, service.db, "owner", "owner@test.com", "password")
// NY ZIP 10001 → prefix 100 → NY → zone 5 (Cold)
residence := &models.Residence{
OwnerID: user.ID,
Name: "NYC House",
IsActive: true,
IsPrimary: true,
PostalCode: "10001",
}
require.NoError(t, service.db.Create(residence).Error)
// Template tagged for zone 5 (Cold)
createTemplateWithConditions(t, service, "Winterize Sprinkler", map[string]interface{}{
"climate_region_id": 5,
})
resp, err := service.GetSuggestions(residence.ID, user.ID)
require.NoError(t, err)
require.Len(t, resp.Suggestions, 1)
assert.InDelta(t, climateRegionBonus, resp.Suggestions[0].RelevanceScore, 0.001)
assert.Contains(t, resp.Suggestions[0].MatchReasons, "climate_region")
}
func TestSuggestionService_ClimateRegionMismatch(t *testing.T) {
service := setupSuggestionService(t)
user := testutil.CreateTestUser(t, service.db, "owner", "owner@test.com", "password")
// FL ZIP 33101 → FL → zone 1 (Hot-Humid)
residence := &models.Residence{
OwnerID: user.ID,
Name: "Miami House",
IsActive: true,
IsPrimary: true,
PostalCode: "33101",
}
require.NoError(t, service.db.Create(residence).Error)
// Template tagged for zone 6 (Very Cold) — no match
createTemplateWithConditions(t, service, "Snowblower Service", map[string]interface{}{
"climate_region_id": 6,
})
resp, err := service.GetSuggestions(residence.ID, user.ID)
require.NoError(t, err)
require.Len(t, resp.Suggestions, 1) // Still included — mismatch doesn't exclude
assert.InDelta(t, baseUniversalScore*0.5, resp.Suggestions[0].RelevanceScore, 0.001)
assert.Contains(t, resp.Suggestions[0].MatchReasons, "partial_profile")
}
func TestSuggestionService_ClimateRegionIgnoredWhenNoZip(t *testing.T) {
service := setupSuggestionService(t)
user := testutil.CreateTestUser(t, service.db, "owner", "owner@test.com", "password")
// Explicitly blank ZIP — testutil.CreateTestResidence seeds "12345" by
// default, which maps to NY/zone 5, so we can't reuse the helper here.
residence := &models.Residence{
OwnerID: user.ID,
Name: "No ZIP House",
IsActive: true,
IsPrimary: true,
PostalCode: "",
}
require.NoError(t, service.db.Create(residence).Error)
createTemplateWithConditions(t, service, "Zone-Specific Task", map[string]interface{}{
"climate_region_id": 5,
})
resp, err := service.GetSuggestions(residence.ID, user.ID)
require.NoError(t, err)
require.Len(t, resp.Suggestions, 1) // Still included, just no bonus
assert.InDelta(t, baseUniversalScore*0.5, resp.Suggestions[0].RelevanceScore, 0.001)
}
func TestSuggestionService_ClimateRegionUnknownZip(t *testing.T) {
service := setupSuggestionService(t)
user := testutil.CreateTestUser(t, service.db, "owner", "owner@test.com", "password")
residence := &models.Residence{
OwnerID: user.ID,
Name: "Garbage ZIP House",
IsActive: true,
IsPrimary: true,
PostalCode: "XYZ12", // not a real US ZIP
}
require.NoError(t, service.db.Create(residence).Error)
createTemplateWithConditions(t, service, "Zone-Specific Task", map[string]interface{}{
"climate_region_id": 5,
})
resp, err := service.GetSuggestions(residence.ID, user.ID)
require.NoError(t, err)
require.Len(t, resp.Suggestions, 1)
// Unknown ZIP → 0 region → no match, but no crash
assert.Contains(t, resp.Suggestions[0].MatchReasons, "partial_profile")
}
func TestSuggestionService_ClimateRegionStacksWithOtherConditions(t *testing.T) {
service := setupSuggestionService(t)
user := testutil.CreateTestUser(t, service.db, "owner", "owner@test.com", "password")
heatingType := "gas_furnace"
residence := &models.Residence{
OwnerID: user.ID,
Name: "NY Gas House",
IsActive: true,
IsPrimary: true,
PostalCode: "10001", // NY → zone 5
HeatingType: &heatingType,
}
require.NoError(t, service.db.Create(residence).Error)
createTemplateWithConditions(t, service, "Winterize Gas Furnace", map[string]interface{}{
"heating_type": "gas_furnace",
"climate_region_id": 5,
})
resp, err := service.GetSuggestions(residence.ID, user.ID)
require.NoError(t, err)
require.Len(t, resp.Suggestions, 1)
// Both bonuses should apply: stringMatchBonus + climateRegionBonus
assert.InDelta(t, stringMatchBonus+climateRegionBonus, resp.Suggestions[0].RelevanceScore, 0.001)
assert.Contains(t, resp.Suggestions[0].MatchReasons, "heating_type:gas_furnace")
assert.Contains(t, resp.Suggestions[0].MatchReasons, "climate_region")
}