Run Scripts Across All 3 Environments (Local, Dev, Stg)
For Claude: REQUIRED SUB-SKILL: Use superpowers-extended-cc:executing-plans to implement this plan task-by-task.
Goal: Execute all necessary batch scripts against all 3 MongoDB databases (local, dev, stg) to ensure consistent data across environments.
Architecture: Scripts are run via yarn script-dev which reads script/config.json for the script sequence and script/.env for the DB connection. We switch environments by uncommenting the appropriate MONGO_CONNECTION_STRING in script/.env, run the required scripts, then move to the next environment.
Tech Stack: Node.js, Yarn 4 PnP, MongoDB Atlas, Mongoose
Environment Connection Strings
All stored in script/.env (toggle by commenting/uncommenting):
| Env | Connection String |
|---|---|
| Local | mongodb+srv://demo:GhKYzU37veEYEmrc@energyaudit.0l6duna.mongodb.net/ |
| Dev | mongodb+srv://dgi-ai-widgets-demo-dev:FEQ8AKdlea4rmkHM@dev.e45p6au.mongodb.net/ |
| Stg | mongodb+srv://dgi-ai-widgets-demo-stg:tiuYWzxKOWnvNXwj@stg.ivmjjjb.mongodb.net/ |
DB name is dtcea-mumbai-demo for all 3.
Scripts Inventory & Execution Order
Scripts have dependencies — they must run in this order:
| # | Script | Entity | What it does | Dependencies |
|---|---|---|---|---|
| 1 | POST /seed/ai-audit-test-data | seed (API) | Seeds demo TCs, installations, users, AI audit results for section S01 | None (standalone seed) |
| 2 | transformer_backfillAuditStatus | transformer | Sets auditStatus = AUDITED/AUDIT_FAILED on all TCs based on lossPercentage | Requires TCs to exist (after seeds + migration data) |
| 3 | transformer_fixInvalidAuditedTCs | transformer | Fixes TCs that have lossPercentage but missing tcConsumption/installationConsumption | After backfillAuditStatus |
| 4 | installation_backfillAvgConsumption | installation | Computes 6-month avg consumption for remarked installations | Requires installations to exist |
| 5 | transformer_backfillHealthScore | transformer | Computes healthScore + healthGrade on all TCs | After audit status is finalized |
| 6 | auditSummary_computeAuditSummary | auditSummary | Computes section→MESCOM level audit summaries | Must run LAST — depends on final TC audit states |
Execution Strategy
For each environment, we run all 6 scripts in order. The approach:
- Update
script/.envto point to the target environment - Update
script/config.jsonwith the script(s) to run - Run
yarn script-dev - Verify output logs
- Repeat for next script or next environment
Order: Local → Dev → Stg (local first so we can verify before touching shared envs)
Task 1: Run all scripts on LOCAL environment
Files:
- Modify:
script/.env(ensure Local connection string is active) - Modify:
script/config.json(update scriptRunArray for each script)
Step 1: Verify script/.env points to Local
Ensure the active (uncommented) connection string is:
MONGO_CONNECTION_STRING=mongodb+srv://demo:GhKYzU37veEYEmrc@energyaudit.0l6duna.mongodb.net/
Step 2: Seed via API
POST /dtcea-mumbai-demo/v1/seed/ai-audit-test-data
Body: { "clearExisting": true }
Expected: Response showing seed data inserted for TCs, installations, users, AI audit results
Step 3: Backfill audit status
Update script/config.json:
{
"scriptRunArray": ["transformer_backfillAuditStatus"],
"input": {
"transformer": {
"backfillAuditStatus": {}
}
},
"printTagsToScriptMapping": false
}
Run: yarn script-dev
Expected: Logs with Marked AUDITED: N and Marked AUDIT_FAILED: N
Step 4: Fix invalid audited TCs
Update script/config.json:
{
"scriptRunArray": ["transformer_fixInvalidAuditedTCs"],
"input": {
"transformer": {
"fixInvalidAuditedTCs": {}
}
},
"printTagsToScriptMapping": false
}
Run: yarn script-dev
Expected: Either "Nothing to fix" or logs showing affected TCs corrected
Step 5: Backfill avg consumption
Update script/config.json:
{
"scriptRunArray": ["installation_backfillAvgConsumption"],
"input": {
"installation": {
"backfillAvgConsumption": {}
}
},
"printTagsToScriptMapping": false
}
Run: yarn script-dev
Expected: Per-month logs showing averages computed and merged
Step 6: Backfill health scores
Update script/config.json:
{
"scriptRunArray": ["transformer_backfillHealthScore"],
"input": {
"transformer": {
"backfillHealthScore": {}
}
},
"printTagsToScriptMapping": false
}
Run: yarn script-dev
Expected: Per-month logs showing TCs updated with healthScore + healthGrade
Step 7: Compute audit summaries (MUST BE LAST)
Update script/config.json:
{
"scriptRunArray": ["auditSummary_computeAuditSummary"],
"input": {
"auditSummary": {
"computeAuditSummary": {}
}
},
"printTagsToScriptMapping": false
}
Run: yarn script-dev
Expected: Per-month SECTION/SUB_DIVISION/DIVISION/CIRCLE/ZONE/MESCOM summaries inserted, invariant checks all "OK"
Step 8: Commit checkpoint
git add script/config.json
git commit -m "chore: run all scripts on local environment"
Task 2: Run all scripts on DEV environment
Files:
- Modify:
script/.env(switch to Dev connection string)
Step 1: Switch script/.env to Dev
# Local
# MONGO_CONNECTION_STRING=mongodb+srv://demo:GhKYzU37veEYEmrc@energyaudit.0l6duna.mongodb.net/
# Dev
MONGO_CONNECTION_STRING=mongodb+srv://dgi-ai-widgets-demo-dev:FEQ8AKdlea4rmkHM@dev.e45p6au.mongodb.net/
# Stg
# MONGO_CONNECTION_STRING=mongodb+srv://dgi-ai-widgets-demo-stg:tiuYWzxKOWnvNXwj@stg.ivmjjjb.mongodb.net/
Steps 2-8: Run all 7 scripts in the same order as Task 1
Same config.json updates and yarn script-dev commands. Same expected outputs.
Step 9: Verify Dev data
Quick sanity check — connect to Dev DB and confirm:
transformerscollection has docs withauditStatussetaudit-summariescollection has SECTION and MESCOM level docsinstallationscollection hasavgConsumption6Monthson remarked docs
Task 3: Run all scripts on STG environment
Files:
- Modify:
script/.env(switch to Stg connection string)
Step 1: Switch script/.env to Stg
# Local
# MONGO_CONNECTION_STRING=mongodb+srv://demo:GhKYzU37veEYEmrc@energyaudit.0l6duna.mongodb.net/
# Dev
# MONGO_CONNECTION_STRING=mongodb+srv://dgi-ai-widgets-demo-dev:FEQ8AKdlea4rmkHM@dev.e45p6au.mongodb.net/
# Stg
MONGO_CONNECTION_STRING=mongodb+srv://dgi-ai-widgets-demo-stg:tiuYWzxKOWnvNXwj@stg.ivmjjjb.mongodb.net/
Steps 2-8: Run all 7 scripts in the same order as Task 1
Same config.json updates and yarn script-dev commands. Same expected outputs.
Step 9: Verify Stg data
Same sanity checks as Dev.
Task 4: Restore Local config and clean up
Step 1: Switch script/.env back to Local
# Local
MONGO_CONNECTION_STRING=mongodb+srv://demo:GhKYzU37veEYEmrc@energyaudit.0l6duna.mongodb.net/
# Dev
# MONGO_CONNECTION_STRING=mongodb+srv://dgi-ai-widgets-demo-dev:FEQ8AKdlea4rmkHM@dev.e45p6au.mongodb.net/
# Stg
# MONGO_CONNECTION_STRING=mongodb+srv://dgi-ai-widgets-demo-stg:tiuYWzxKOWnvNXwj@stg.ivmjjjb.mongodb.net/
Step 2: Reset script/config.json to default
{
"scriptRunArray": ["auditSummary_computeAuditSummary"],
"input": {
"auditSummary": {
"computeAuditSummary": {}
}
},
"printTagsToScriptMapping": false
}
Step 3: Verify local API still works
Run: yarn api-dev
Expected: API starts successfully, connects to local MongoDB
Important Notes
computeAuditSummaryDELETES all existing audit summaries before recomputing — always run it lastPOST /seed/ai-audit-test-datais idempotent — passclearExisting: trueto clean up before insertingbackfillAuditStatusonly touches TCs withauditStatus: null— safe to re-runfixInvalidAuditedTCsis idempotent — only fixes TCs matching the bad statebackfillAvgConsumptioncreates a temp collection and drops it after — safe to re-runbackfillHealthScoreoverwrites healthScore/healthGrade — safe to re-run
Estimated Scale
- ~150K TCs × 7 months = ~1M transformer docs
- ~3.3M installation docs
backfillAvgConsumptionis the heaviest (aggregation + $merge on millions of docs) — expect 5-15 min per environmentbackfillHealthScoreprocesses ~1M docs in batches of 500 — expect 3-8 min per environmentcomputeAuditSummaryaggregates ~1M TCs — expect 2-5 min per environment