@@ -38,7 +38,7 @@ const celDaysFromNow = (n: number) => cel`daysFromNow(${n})`;
3838 * Two consequences shape everything below:
3939 *
4040 * 1. Seeding historical values into readonly fields (`created_date`,
41- * `days_in_stage `, `last_contacted_date`, `actual_revenue`) is legitimate
41+ * `stage_entry_date `, `last_contacted_date`, `actual_revenue`) is legitimate
4242 * and load-bearing — it is the only way demo reports get history — and the
4343 * platform explicitly preserves explicit seed values.
4444 * 2. BUT nothing recomputes derived fields for seed rows, so every seeded
@@ -325,6 +325,14 @@ const leads = defineSeed(Lead, {
325325// don't run over seeds, so every row below carries the exact values the hook
326326// would compute — anything else gets silently rewritten on the first user
327327// edit (#490).
328+ //
329+ // `stage_entry_date` follows the same rule: the hook stamps it on every insert
330+ // and stage change, so a seeded deal without one is a shape real data never
331+ // has — and it is what the stagnation sweep filters on and what the
332+ // `days_in_stage` FORMULA counts from, so it replaces the old hardcoded
333+ // `days_in_stage` numbers (#489). Open deals carry an age that spans the
334+ // 14-day stale threshold in both directions; settled deals entered their
335+ // closed stage on their close date.
328336const opportunities = defineSeed ( Opportunity , {
329337 mode : 'upsert' ,
330338 externalId : 'name' ,
@@ -340,7 +348,7 @@ const opportunities = defineSeed(Opportunity, {
340348 type : 'existing_upgrade' ,
341349 forecast_category : 'commit' ,
342350 lead_source : 'web' ,
343- days_in_stage : 12 ,
351+ stage_entry_date : celDaysAgo ( 12 ) ,
344352 description : `Upgrade from Standard to Enterprise edition for the
345353NA + EMEA teams. Drivers: (1) AI agent governance becomes a hard
346354requirement after their internal compliance review, (2) advanced
@@ -358,7 +366,7 @@ analytics seats for the Ops org, (3) priority support SLA.`,
358366 type : 'new_business' ,
359367 forecast_category : 'pipeline' ,
360368 lead_source : 'referral' ,
361- days_in_stage : 45 ,
369+ stage_entry_date : celDaysAgo ( 45 ) ,
362370 } ,
363371 {
364372 name : 'Wayne Enterprise License' ,
@@ -371,7 +379,7 @@ analytics seats for the Ops org, (3) priority support SLA.`,
371379 type : 'new_business' ,
372380 forecast_category : 'commit' ,
373381 lead_source : 'partner' ,
374- days_in_stage : 7 ,
382+ stage_entry_date : celDaysAgo ( 7 ) ,
375383 } ,
376384 {
377385 name : 'Initech Cloud Migration' ,
@@ -384,7 +392,7 @@ analytics seats for the Ops org, (3) priority support SLA.`,
384392 type : 'existing_upgrade' ,
385393 forecast_category : 'best_case' ,
386394 lead_source : 'event' ,
387- days_in_stage : 38 ,
395+ stage_entry_date : celDaysAgo ( 38 ) ,
388396 } ,
389397 // ─── Closed Won deals (powers KPIs & revenue trends) ────────────────
390398 {
@@ -395,6 +403,7 @@ analytics seats for the Ops org, (3) priority support SLA.`,
395403 probability : 100 ,
396404 expected_revenue : 220000 ,
397405 close_date : cel `daysAgo(15)` ,
406+ stage_entry_date : cel `daysAgo(15)` ,
398407 type : 'existing_renewal' ,
399408 forecast_category : 'closed' ,
400409 lead_source : 'partner' ,
@@ -408,6 +417,7 @@ analytics seats for the Ops org, (3) priority support SLA.`,
408417 probability : 100 ,
409418 expected_revenue : 145000 ,
410419 close_date : cel `daysAgo(50)` ,
420+ stage_entry_date : cel `daysAgo(50)` ,
411421 type : 'new_business' ,
412422 forecast_category : 'closed' ,
413423 lead_source : 'event' ,
@@ -420,6 +430,7 @@ analytics seats for the Ops org, (3) priority support SLA.`,
420430 probability : 100 ,
421431 expected_revenue : 380000 ,
422432 close_date : cel `daysAgo(95)` ,
433+ stage_entry_date : cel `daysAgo(95)` ,
423434 type : 'existing_upgrade' ,
424435 forecast_category : 'closed' ,
425436 lead_source : 'web' ,
@@ -432,6 +443,7 @@ analytics seats for the Ops org, (3) priority support SLA.`,
432443 probability : 100 ,
433444 expected_revenue : 65000 ,
434445 close_date : cel `daysAgo(140)` ,
446+ stage_entry_date : cel `daysAgo(140)` ,
435447 type : 'new_business' ,
436448 forecast_category : 'closed' ,
437449 lead_source : 'referral' ,
@@ -444,6 +456,7 @@ analytics seats for the Ops org, (3) priority support SLA.`,
444456 probability : 100 ,
445457 expected_revenue : 90000 ,
446458 close_date : cel `daysAgo(200)` ,
459+ stage_entry_date : cel `daysAgo(200)` ,
447460 type : 'new_business' ,
448461 forecast_category : 'closed' ,
449462 lead_source : 'web' ,
@@ -457,6 +470,7 @@ analytics seats for the Ops org, (3) priority support SLA.`,
457470 probability : 0 ,
458471 expected_revenue : 0 ,
459472 close_date : cel `daysAgo(25)` ,
473+ stage_entry_date : cel `daysAgo(25)` ,
460474 type : 'existing_upgrade' ,
461475 forecast_category : 'omitted' ,
462476 lead_source : 'cold_call' ,
@@ -470,6 +484,7 @@ analytics seats for the Ops org, (3) priority support SLA.`,
470484 probability : 0 ,
471485 expected_revenue : 0 ,
472486 close_date : cel `daysAgo(60)` ,
487+ stage_entry_date : cel `daysAgo(60)` ,
473488 type : 'new_business' ,
474489 forecast_category : 'omitted' ,
475490 lead_source : 'advertisement' ,
@@ -517,6 +532,12 @@ analytics seats for the Ops org, (3) priority support SLA.`,
517532 const close_date = isClosed ( stage )
518533 ? celDaysAgo ( 5 + spread )
519534 : celDaysFromNow ( 7 + spread ) ;
535+ // A settled deal entered its closed stage the day it closed; an open
536+ // one has been parked 3–62 days, straddling STALE_THRESHOLD_DAYS so
537+ // the stagnation sweep and the stale view both have rows on each side.
538+ const stage_entry_date = isClosed ( stage )
539+ ? celDaysAgo ( 5 + spread )
540+ : celDaysAgo ( 3 + ( i * 11 ) % 60 ) ;
520541 const amount = 20000 + ( ( i * 17_393 ) % 480_000 ) ;
521542 const probability = probabilityByStage [ stage ] ;
522543 out . push ( {
@@ -531,7 +552,7 @@ analytics seats for the Ops org, (3) priority support SLA.`,
531552 type : types [ i % types . length ] ,
532553 forecast_category : forecastByStage [ stage ] ,
533554 lead_source : sources [ i % sources . length ] ,
534- ... ( isClosed ( stage ) ? { } : { days_in_stage : 3 + ( i * 11 ) % 60 } ) ,
555+ stage_entry_date ,
535556 } ) ;
536557 }
537558 return out ;
0 commit comments