Onboarding: template backlink, bulk-create endpoint, climate-region scoring
Some checks failed
Some checks failed
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:
@@ -26,7 +26,9 @@ func NewSuggestionService(db *gorm.DB, residenceRepo *repositories.ResidenceRepo
|
||||
}
|
||||
}
|
||||
|
||||
// templateConditions represents the parsed conditions JSON from a task template
|
||||
// templateConditions represents the parsed conditions JSON from a task template.
|
||||
// Every field is optional; a template with no conditions is "universal" and
|
||||
// receives a small base score. See scoreTemplate for how each field is used.
|
||||
type templateConditions struct {
|
||||
HeatingType *string `json:"heating_type,omitempty"`
|
||||
CoolingType *string `json:"cooling_type,omitempty"`
|
||||
@@ -43,6 +45,11 @@ type templateConditions struct {
|
||||
HasBasement *bool `json:"has_basement,omitempty"`
|
||||
HasAttic *bool `json:"has_attic,omitempty"`
|
||||
PropertyType *string `json:"property_type,omitempty"`
|
||||
// ClimateRegionID replaces the old task_tasktemplate_regions join table.
|
||||
// Tag a template with the IECC zone ID it's relevant to (e.g. "Winterize
|
||||
// Sprinkler" → zone 5/6). Residence.PostalCode is mapped to a region at
|
||||
// scoring time via ZipToState + GetClimateRegionIDByState.
|
||||
ClimateRegionID *uint `json:"climate_region_id,omitempty"`
|
||||
}
|
||||
|
||||
// isEmpty returns true if no conditions are set
|
||||
@@ -52,17 +59,20 @@ func (c *templateConditions) isEmpty() bool {
|
||||
c.LandscapingType == nil && c.HasPool == nil && c.HasSprinkler == nil &&
|
||||
c.HasSeptic == nil && c.HasFireplace == nil && c.HasGarage == nil &&
|
||||
c.HasBasement == nil && c.HasAttic == nil &&
|
||||
c.PropertyType == nil
|
||||
c.PropertyType == nil && c.ClimateRegionID == nil
|
||||
}
|
||||
|
||||
const (
|
||||
maxSuggestions = 30
|
||||
baseUniversalScore = 0.3
|
||||
stringMatchBonus = 0.25
|
||||
boolMatchBonus = 0.3
|
||||
// climateRegionBonus removed — suggestions now based on home features only
|
||||
propertyTypeBonus = 0.15
|
||||
totalProfileFields = 14
|
||||
maxSuggestions = 30
|
||||
baseUniversalScore = 0.3
|
||||
stringMatchBonus = 0.25
|
||||
boolMatchBonus = 0.3
|
||||
// climateRegionBonus is deliberately higher than stringMatchBonus —
|
||||
// climate zone is coarse but high-signal (one bit for a whole region of
|
||||
// templates like "Hurricane Prep" or "Winterize Sprinkler").
|
||||
climateRegionBonus = 0.35
|
||||
propertyTypeBonus = 0.15
|
||||
totalProfileFields = 15 // 14 home-profile fields + ZIP/region
|
||||
)
|
||||
|
||||
// GetSuggestions returns task template suggestions scored against a residence's profile
|
||||
@@ -87,7 +97,6 @@ func (s *SuggestionService) GetSuggestions(residenceID uint, userID uint) (*resp
|
||||
if err := s.db.
|
||||
Preload("Category").
|
||||
Preload("Frequency").
|
||||
Preload("Regions").
|
||||
Where("is_active = ?", true).
|
||||
Find(&templates).Error; err != nil {
|
||||
return nil, apperrors.Internal(err)
|
||||
@@ -308,6 +317,17 @@ func (s *SuggestionService) scoreTemplate(tmpl *models.TaskTemplate, residence *
|
||||
}
|
||||
}
|
||||
|
||||
// Climate region match. We resolve the residence's ZIP to a region ID on
|
||||
// demand; a missing/invalid ZIP is treated the same as a nil home-profile
|
||||
// field — no penalty, no exclusion.
|
||||
if cond.ClimateRegionID != nil {
|
||||
conditionCount++
|
||||
if residenceRegionID := resolveResidenceRegionID(residence); residenceRegionID != 0 && residenceRegionID == *cond.ClimateRegionID {
|
||||
score += climateRegionBonus
|
||||
reasons = append(reasons, "climate_region")
|
||||
}
|
||||
}
|
||||
|
||||
// Cap at 1.0
|
||||
if score > 1.0 {
|
||||
score = 1.0
|
||||
@@ -367,6 +387,30 @@ func CalculateProfileCompleteness(residence *models.Residence) float64 {
|
||||
if residence.LandscapingType != nil {
|
||||
filled++
|
||||
}
|
||||
// PostalCode is the 15th field — counts toward completeness when we can
|
||||
// map it to a region. An invalid / unknown ZIP doesn't count.
|
||||
if resolveResidenceRegionIDByZip(residence.PostalCode) != 0 {
|
||||
filled++
|
||||
}
|
||||
|
||||
return float64(filled) / float64(totalProfileFields)
|
||||
}
|
||||
|
||||
// resolveResidenceRegionID returns the IECC climate zone ID for a residence
|
||||
// based on its PostalCode, or 0 if the ZIP can't be mapped. Helper lives here
|
||||
// (not in region_lookup.go) because it couples the Residence model to the
|
||||
// suggestion service's notion of region resolution.
|
||||
func resolveResidenceRegionID(residence *models.Residence) uint {
|
||||
return resolveResidenceRegionIDByZip(residence.PostalCode)
|
||||
}
|
||||
|
||||
func resolveResidenceRegionIDByZip(zip string) uint {
|
||||
if zip == "" {
|
||||
return 0
|
||||
}
|
||||
state := ZipToState(zip)
|
||||
if state == "" {
|
||||
return 0
|
||||
}
|
||||
return GetClimateRegionIDByState(state)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user