6 Commits

Author SHA1 Message Date
2de0cde94c Merge branch 'claude/ecstatic-elgamal' 2026-03-11 15:47:02 -04:00
94c7c90b91 fix: use project estimated_cost for reserve funded ratio calculation
The health score funded ratio was only reading from the reserve_components
table (replacement_cost), but users enter their reserve data on the
Projects page using estimated_cost. When reserve_components is empty,
the funded ratio now falls back to reserve-funded projects for:
- Total replacement cost (estimated_cost)
- Component funding status (current_fund_balance)
- Urgent components due within 5 years (remaining_life_years)
- AI prompt component detail lines

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-11 15:46:56 -04:00
f47fbfcf93 Merge branch 'claude/ecstatic-elgamal' 2026-03-11 15:42:24 -04:00
04771f370c fix: clarify reserve health score when no components are entered
- Add missing-data warning when reserve_components table is empty so
  users see "No reserve components found" on the dashboard
- Change AI prompt to show "N/A" instead of "0.0%" for funded ratio
  when no components exist, preventing misleading "0% funded" reports
- Instruct AI not to report 0% funded when data is simply missing

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-11 15:42:15 -04:00
208c1dd7bc security: address assessment findings and bump to v2026.3.11
- C1: Disable Swagger UI in production (env gate)
- M1+M2: Add Helmet.js for security headers (CSP, X-Frame-Options,
  X-Content-Type-Options, Referrer-Policy) and remove X-Powered-By
- H2: Add @nestjs/throttler rate limiting (5 req/min on login/register)
- M4: Remove orgSchema from JWT payload and client-side storage;
  tenant middleware now resolves schema from orgId via cached DB lookup
- L1: Fix Chatwoot user identification (read from auth store on ready)
- Remove schemaName from frontend Organization type and UI displays

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-11 15:32:51 -04:00
a047144922 Added userID and URL to Chatwoot Script 2026-03-10 14:49:50 -04:00

View File

@@ -220,6 +220,14 @@ export class HealthScoresService {
missing.push(`No budget found for ${year}. Upload or create an annual budget.`); missing.push(`No budget found for ${year}. Upload or create an annual budget.`);
} }
// Should have reserve components (warn but don't block)
const components = await qr.query(
`SELECT COUNT(*) as cnt FROM reserve_components`,
);
if (parseInt(components[0].cnt) === 0) {
missing.push('No reserve components found. Add reserve components (roof, parking, pool, etc.) with replacement costs for an accurate funded-ratio calculation.');
}
// Should have capital projects (warn but don't block) // Should have capital projects (warn but don't block)
const projects = await qr.query( const projects = await qr.query(
`SELECT COUNT(*) as cnt FROM projects WHERE is_active = true`, `SELECT COUNT(*) as cnt FROM projects WHERE is_active = true`,
@@ -558,10 +566,12 @@ export class HealthScoresService {
FROM reserve_components FROM reserve_components
ORDER BY remaining_life_years ASC NULLS LAST ORDER BY remaining_life_years ASC NULLS LAST
`), `),
// Capital projects // Capital projects (include component-level fields for funded ratio when reserve_components is empty)
qr.query(` qr.query(`
SELECT name, estimated_cost, target_year, target_month, fund_source, SELECT name, estimated_cost, actual_cost, target_year, target_month, fund_source,
status, priority, current_fund_balance, funded_percentage status, priority, current_fund_balance, funded_percentage,
category, useful_life_years, remaining_life_years, condition_rating,
annual_contribution
FROM projects FROM projects
WHERE is_active = true AND status IN ('planned', 'approved', 'in_progress') WHERE is_active = true AND status IN ('planned', 'approved', 'in_progress')
ORDER BY target_year, target_month NULLS LAST ORDER BY target_year, target_month NULLS LAST
@@ -596,11 +606,19 @@ export class HealthScoresService {
const totalReserveFund = reserveCash + totalInvestments; const totalReserveFund = reserveCash + totalInvestments;
const totalReplacementCost = reserveComponents // Use reserve_components for funded ratio when available; fall back to
.reduce((s: number, c: any) => s + parseFloat(c.replacement_cost || '0'), 0); // reserve-funded projects (which carry the same estimated_cost / lifecycle
// fields that users actually populate on the Projects page).
const reserveProjects = projects.filter((p: any) => p.fund_source === 'reserve');
const useComponentsTable = reserveComponents.length > 0;
const totalComponentFunded = reserveComponents const totalReplacementCost = useComponentsTable
.reduce((s: number, c: any) => s + parseFloat(c.current_fund_balance || '0'), 0); ? reserveComponents.reduce((s: number, c: any) => s + parseFloat(c.replacement_cost || '0'), 0)
: reserveProjects.reduce((s: number, p: any) => s + parseFloat(p.estimated_cost || '0'), 0);
const totalComponentFunded = useComponentsTable
? reserveComponents.reduce((s: number, c: any) => s + parseFloat(c.current_fund_balance || '0'), 0)
: reserveProjects.reduce((s: number, p: any) => s + parseFloat(p.current_fund_balance || '0'), 0);
const percentFunded = totalReplacementCost > 0 ? (totalReserveFund / totalReplacementCost) * 100 : 0; const percentFunded = totalReplacementCost > 0 ? (totalReserveFund / totalReplacementCost) * 100 : 0;
@@ -615,9 +633,13 @@ export class HealthScoresService {
.filter((b: any) => b.account_type === 'expense') .filter((b: any) => b.account_type === 'expense')
.reduce((s: number, b: any) => s + parseFloat(b.annual_total || '0'), 0); .reduce((s: number, b: any) => s + parseFloat(b.annual_total || '0'), 0);
// Components needing replacement within 5 years // Components needing replacement within 5 years — use whichever source has data
const urgentComponents = reserveComponents.filter( const urgentComponents = useComponentsTable
? reserveComponents.filter(
(c: any) => c.remaining_life_years !== null && parseFloat(c.remaining_life_years) <= 5, (c: any) => c.remaining_life_years !== null && parseFloat(c.remaining_life_years) <= 5,
)
: reserveProjects.filter(
(p: any) => p.remaining_life_years !== null && parseFloat(p.remaining_life_years) <= 5,
); );
// ── Build 12-month forward reserve cash flow projection ── // ── Build 12-month forward reserve cash flow projection ──
@@ -749,6 +771,7 @@ export class HealthScoresService {
accounts, accounts,
investments, investments,
reserveComponents, reserveComponents,
reserveProjects,
projects, projects,
budgets, budgets,
assessments, assessments,
@@ -959,13 +982,15 @@ Provide 3-5 factors and 1-3 actionable recommendations. Be specific with dollar
`- ${i.name} | ${i.investment_type} @ ${i.institution} | $${parseFloat(i.current_value || i.principal || '0').toFixed(2)} | Rate: ${parseFloat(i.interest_rate || '0').toFixed(2)}% | Maturity: ${i.maturity_date ? new Date(i.maturity_date).toLocaleDateString() : 'N/A'}`, `- ${i.name} | ${i.investment_type} @ ${i.institution} | $${parseFloat(i.current_value || i.principal || '0').toFixed(2)} | Rate: ${parseFloat(i.interest_rate || '0').toFixed(2)}% | Maturity: ${i.maturity_date ? new Date(i.maturity_date).toLocaleDateString() : 'N/A'}`,
).join('\n'); ).join('\n');
const componentLines = data.reserveComponents.length === 0 // Build component lines from reserve_components if available, otherwise from reserve-funded projects
? 'No reserve components tracked.' const componentSource = data.reserveComponents.length > 0 ? data.reserveComponents : data.reserveProjects;
: data.reserveComponents.map((c: any) => { const componentLines = componentSource.length === 0
const cost = parseFloat(c.replacement_cost || '0'); ? 'No reserve components or reserve projects tracked.'
: componentSource.map((c: any) => {
const cost = parseFloat(c.replacement_cost || c.estimated_cost || '0');
const funded = parseFloat(c.current_fund_balance || '0'); const funded = parseFloat(c.current_fund_balance || '0');
const pct = cost > 0 ? ((funded / cost) * 100).toFixed(0) : '0'; const pct = cost > 0 ? ((funded / cost) * 100).toFixed(0) : '0';
return `- ${c.name} [${c.category}] | Life: ${c.useful_life_years}yr, Remaining: ${c.remaining_life_years}yr | Cost: $${cost.toFixed(0)} | Funded: $${funded.toFixed(0)} (${pct}%) | Condition: ${c.condition_rating}/10 | Annual Contribution: $${parseFloat(c.annual_contribution || '0').toFixed(0)}`; return `- ${c.name} [${c.category || 'N/A'}] | Life: ${c.useful_life_years || '?'}yr, Remaining: ${c.remaining_life_years || '?'}yr | Cost: $${cost.toFixed(0)} | Funded: $${funded.toFixed(0)} (${pct}%) | Condition: ${c.condition_rating || '?'}/10 | Annual Contribution: $${parseFloat(c.annual_contribution || '0').toFixed(0)}`;
}).join('\n'); }).join('\n');
const projectLines = data.projects.length === 0 const projectLines = data.projects.length === 0
@@ -981,7 +1006,7 @@ Provide 3-5 factors and 1-3 actionable recommendations. Be specific with dollar
const urgentLines = data.urgentComponents.length === 0 const urgentLines = data.urgentComponents.length === 0
? 'None — no components due within 5 years.' ? 'None — no components due within 5 years.'
: data.urgentComponents.map((c: any) => { : data.urgentComponents.map((c: any) => {
const cost = parseFloat(c.replacement_cost || '0'); const cost = parseFloat(c.replacement_cost || c.estimated_cost || '0');
const funded = parseFloat(c.current_fund_balance || '0'); const funded = parseFloat(c.current_fund_balance || '0');
const gap = cost - funded; const gap = cost - funded;
return `- ${c.name}: ${c.remaining_life_years} years remaining, $${gap.toFixed(0)} funding gap`; return `- ${c.name}: ${c.remaining_life_years} years remaining, $${gap.toFixed(0)} funding gap`;
@@ -997,8 +1022,8 @@ Reserve Cash (bank accounts): $${data.reserveCash.toFixed(2)}
Reserve Investments: $${data.totalInvestments.toFixed(2)} Reserve Investments: $${data.totalInvestments.toFixed(2)}
Total Reserve Fund: $${data.totalReserveFund.toFixed(2)} Total Reserve Fund: $${data.totalReserveFund.toFixed(2)}
Total Replacement Cost (all components): $${data.totalReplacementCost.toFixed(2)} Total Replacement Cost (all components): ${data.totalReplacementCost > 0 ? '$' + data.totalReplacementCost.toFixed(2) : '$0.00 (no reserve components entered — funded ratio cannot be calculated)'}
Percent Funded: ${data.percentFunded.toFixed(1)}% Percent Funded: ${data.totalReplacementCost > 0 ? data.percentFunded.toFixed(1) + '%' : 'N/A — no reserve components with replacement costs have been entered. Do NOT report a 0% funded ratio; instead note that funded ratio is unavailable due to missing component data.'}
Annual Reserve Contribution (budgeted income): $${data.annualReserveContribution.toFixed(2)} Annual Reserve Contribution (budgeted income): $${data.annualReserveContribution.toFixed(2)}
Annual Reserve Expenses (budgeted): $${data.annualReserveExpenses.toFixed(2)} Annual Reserve Expenses (budgeted): $${data.annualReserveExpenses.toFixed(2)}