package determine import ( "fmt" "strings" ) // Change kinds emitted when a monitored determination changes. const ( ChangeSFHAEntered = "SFHA_ENTERED" ChangeSFHAExited = "SFHA_EXITED" ChangeZone = "ZONE_CHANGED" ChangePanel = "PANEL_REVISED" ChangeLOMR = "LOMR_EFFECTIVE" ChangeCommunity = "COMMUNITY_STATUS_CHANGED" ChangeInsuranceLost = "INSURANCE_UNAVAILABLE" ChangeInsuranceRegain = "INSURANCE_AVAILABLE" ChangeCBRS = "CBRS_CHANGED" ChangePendingSFHA = "PENDING_SFHA" ChangePendingCleared = "PENDING_CLEARED" ChangePendingScheduled = "PENDING_MAP" ChangeLOMANearby = "LOMA_NEARBY" ) // Change severities. const ( ChangeCritical = "critical" ChangeWarning = "warning" ChangeInfo = "info" ) // Change is one material difference between two determinations. type Change struct { Kind string `json:"kind"` Severity string `json:"severity"` Message string `json:"message"` From string `json:"from,omitempty"` To string `json:"to,omitempty"` } // Diff compares a previous determination with a new one for the same loan. // It returns nil when nothing a lender would act on has changed. func Diff(old, cur *Determination) []Change { if old == nil || cur == nil { return nil } var out []Change add := func(kind, sev, from, to, format string, args ...any) { out = append(out, Change{Kind: kind, Severity: sev, From: from, To: to, Message: fmt.Sprintf(format, args...)}) } oz, cz := old.Map.ZoneLabel, cur.Map.ZoneLabel switch { case !old.InSFHA && cur.InSFHA: add(ChangeSFHAEntered, ChangeCritical, oz, cz, "Building is now in a Special Flood Hazard Area (zone %s -> %s). Flood insurance is required: notify the borrower and, if adequate coverage is not obtained within 45 days, force-place it (42 U.S.C. 4012a(e)).", dash(oz), dash(cz)) case old.InSFHA && !cur.InSFHA: add(ChangeSFHAExited, ChangeInfo, oz, cz, "Building is no longer in a Special Flood Hazard Area (zone %s -> %s). Mandatory purchase no longer applies; the borrower may be entitled to cancel and receive a refund of force-placed coverage.", dash(oz), dash(cz)) case oz != cz: add(ChangeZone, ChangeWarning, oz, cz, "Flood zone changed from %s to %s.", dash(oz), dash(cz)) } op, cp := panelString(old), panelString(cur) if op != cp { add(ChangePanel, ChangeInfo, op, cp, "FIRM panel changed from %s to %s.", dash(op), dash(cp)) } oldL := map[string]bool{} for _, l := range old.Map.LOMRs { oldL[l.CaseNo] = true } for _, l := range cur.Map.LOMRs { if !oldL[l.CaseNo] { date := "" if l.EffDate != nil { date = ", effective " + l.EffDate.Format("01/02/2006") } add(ChangeLOMR, ChangeWarning, "", l.CaseNo, "Letter of Map Revision %s now applies to this location%s.", l.CaseNo, date) } } os, cs := communityStatus(old), communityStatus(cur) if os != cs { sev := ChangeInfo if old.Insurance.Available && !cur.Insurance.Available { sev = ChangeCritical } add(ChangeCommunity, sev, os, cs, "NFIP community status changed from %s to %s.", dash(os), dash(cs)) } if old.Insurance.Available && !cur.Insurance.Available { add(ChangeInsuranceLost, ChangeCritical, "available", "not available", "Federal flood insurance is no longer available here (%s). Existing NFIP policies cannot be renewed; review the loan.", cur.Insurance.NotAvailableReason) } else if !old.Insurance.Available && cur.Insurance.Available { add(ChangeInsuranceRegain, ChangeInfo, "not available", "available", "Federal flood insurance is available again (%s Program).", cur.Insurance.Program) } if old.Insurance.InCBRS != cur.Insurance.InCBRS { add(ChangeCBRS, ChangeWarning, fmt.Sprint(old.Insurance.InCBRS), fmt.Sprint(cur.Insurance.InCBRS), "Coastal Barrier Resources System status changed (in CBRS: %v -> %v).", old.Insurance.InCBRS, cur.Insurance.InCBRS) } oldA := map[string]bool{} for _, l := range old.Map.LOMAsNearby { oldA[l.CaseNo] = true } for _, l := range cur.Map.LOMAsNearby { if !oldA[l.CaseNo] { add(ChangeLOMANearby, ChangeInfo, "", l.CaseNo, "FEMA issued letter %s (%s, outcome: %s) %.0f m from this property. If it covers the building it may change the determination; obtain the letter.", l.CaseNo, nonEmpty(l.Determination, "LOMC"), nonEmpty(l.Outcome, "unknown"), l.DistanceM) } } opend, cpend := pendingString(old), pendingString(cur) if opend != cpend { switch { case cur.Map.Pending != nil && cur.Map.Pending.SFHA && !cur.InSFHA: add(ChangePendingSFHA, ChangeWarning, opend, cpend, "A pending FEMA map will place this building in an SFHA (%s). Plan borrower notice before the effective date.", cpend) case cur.Map.Pending == nil: add(ChangePendingCleared, ChangeInfo, opend, "", "The pending FEMA map for this location is no longer pending (it took effect or was withdrawn).") default: add(ChangePendingScheduled, ChangeInfo, opend, cpend, "Pending FEMA map for this location: %s.", cpend) } } return out } func dash(s string) string { if s == "" { return "(none)" } return s } func panelString(d *Determination) string { s := d.Map.Panel if d.Map.PanelDate != nil { s += " (" + d.Map.PanelDate.Format("01/02/2006") + ")" } return s } func communityStatus(d *Determination) string { if d.Community == nil { return "" } return d.Community.CID + " " + d.Community.Status() } func pendingString(d *Determination) string { p := d.Map.Pending if p == nil { return "" } var zs []string for _, z := range p.Zones { zs = append(zs, z.Zone) } s := "DFIRM " + p.DFIRMID + " zone " + strings.Join(zs, "/") if p.EffDate != nil { s += " effective " + p.EffDate.Format("01/02/2006") } return s }