import dotenv from "dotenv"; import path from "path"; dotenv.config({ path: path.resolve(__dirname, "../.env.local") }); import mongoose from "mongoose"; import { connectDb } from "../src/lib/server/db"; import { backfillPurchaseMovements } from "../src/lib/server/stock"; /** * One-off (safe to re-run): purchases recorded before the stock ledger * existed did not add stock. This adds the missing PURCHASE movements so * each branch's stock starts from what it actually bought. * * npm run backfill:stock * * Run it once after deploying the stock ledger, BEFORE branches record * opening counts — a count recorded first already includes those units, * and backfilling afterwards would add them a second time. */ async function main() { await connectDb(); const added = await backfillPurchaseMovements(); console.log(`Added ${added} purchase stock movement(s).`); await mongoose.disconnect(); } main().catch((err) => { console.error("backfill failed:", err.message); process.exit(1); });