refactor: move totalvnd into meta.invested for extensibility

Portfolio schema now uses meta object: { currency, assets, meta: { invested } }.
Migrates old totalvnd field automatically on load. The meta object provides
a clean place for future per-user metadata without polluting the top level.
This commit is contained in:
2026-04-14 17:33:11 +07:00
parent 0d4feb9ef8
commit a34c1cf85f
6 changed files with 32 additions and 30 deletions

View File

@@ -62,12 +62,12 @@ describe("trading/handlers", () => {
expect(ctx.replies[0]).toContain("5.000.000 VND");
});
it("tracks totalvnd", async () => {
it("tracks meta.invested", async () => {
const ctx = makeCtx("1000000");
await handleTopup(ctx, db);
const { getPortfolio } = await import("../../../src/modules/trading/portfolio.js");
const p = await getPortfolio(db, 42);
expect(p.totalvnd).toBe(1000000);
expect(p.meta.invested).toBe(1000000);
expect(p.currency.VND).toBe(1000000);
});
@@ -89,7 +89,7 @@ describe("trading/handlers", () => {
const { emptyPortfolio } = await import("../../../src/modules/trading/portfolio.js");
const p = emptyPortfolio();
p.currency.VND = 5000000;
p.totalvnd = 5000000;
p.meta.invested = 5000000;
await savePortfolio(db, 42, p);
const ctx = makeCtx("10 TCB");
@@ -169,7 +169,7 @@ describe("trading/handlers", () => {
const p = emptyPortfolio();
p.currency.VND = 5000000;
p.assets.TCB = 10;
p.totalvnd = 10000000;
p.meta.invested = 10000000;
await savePortfolio(db, 42, p);
const ctx = makeCtx("");

View File

@@ -23,7 +23,7 @@ describe("trading/portfolio", () => {
const p = emptyPortfolio();
expect(p.currency).toEqual({ VND: 0 });
expect(p.assets).toEqual({});
expect(p.totalvnd).toBe(0);
expect(p.meta.invested).toBe(0);
});
});
@@ -31,7 +31,7 @@ describe("trading/portfolio", () => {
it("returns empty portfolio for new user", async () => {
const p = await getPortfolio(db, 123);
expect(p.currency.VND).toBe(0);
expect(p.totalvnd).toBe(0);
expect(p.meta.invested).toBe(0);
expect(p.assets).toEqual({});
});
@@ -39,12 +39,12 @@ describe("trading/portfolio", () => {
const p = emptyPortfolio();
p.currency.VND = 5000000;
p.assets.TCB = 10;
p.totalvnd = 5000000;
p.meta.invested = 5000000;
await savePortfolio(db, 123, p);
const loaded = await getPortfolio(db, 123);
expect(loaded.currency.VND).toBe(5000000);
expect(loaded.assets.TCB).toBe(10);
expect(loaded.totalvnd).toBe(5000000);
expect(loaded.meta.invested).toBe(5000000);
});
it("migrates old 4-category format to flat assets", async () => {
@@ -54,7 +54,7 @@ describe("trading/portfolio", () => {
stock: { TCB: 10 },
crypto: { BTC: 0.5 },
others: { GOLD: 1 },
totalvnd: 100,
totalvnd: 100, // old format — should be migrated to meta.invested
});
const p = await getPortfolio(db, 123);
expect(p.assets.TCB).toBe(10);