refactor(backend): resolve legacy Supabase client inside capital ledger repository

Made-with: Cursor
This commit is contained in:
Saravana Achu Mac 2026-04-04 18:53:29 -07:00
parent f0dd2055bf
commit 774541289a
2 changed files with 20 additions and 20 deletions

View File

@ -1,6 +1,5 @@
import logger from '../utils/logger.js';
import { config } from '../config/index.js';
import { supabaseService } from './SupabaseService.js';
import { getTradeProfileCapital } from './profileRepository.js';
import { getCapitalLedger, upsertCapitalLedger } from './capitalLedgerRepository.js';
@ -38,7 +37,7 @@ export class CapitalLedger {
try {
const profileCapital = await getTradeProfileCapital(profileId);
const allocation = toNumeric(allocatedCapital ?? profileCapital?.allocatedCapital ?? config.TOTAL_CAPITAL);
const existing = await getCapitalLedger(profileId, supabaseService);
const existing = await getCapitalLedger(profileId);
const nextRecord: CapitalLedgerRecord = {
profile_id: profileId,
allocated_capital: allocation,
@ -48,7 +47,7 @@ export class CapitalLedger {
updated_at: new Date().toISOString()
};
return await upsertCapitalLedger(nextRecord, supabaseService);
return await upsertCapitalLedger(nextRecord);
} catch (err: any) {
if (this.isRpcNetworkFailure(err)) {
logger.error(`[CapitalLedger] ensureLedger network failure for ${profileId}, aborting ledger mutation (fail-closed): ${err.message}`);
@ -71,7 +70,7 @@ export class CapitalLedger {
}
public async getLedger(profileId: string): Promise<CapitalLedgerRecord | null> {
return getCapitalLedger(profileId, supabaseService);
return getCapitalLedger(profileId);
}
public async reserveForOrder(profileId: string, amount: number): Promise<boolean> {
@ -86,7 +85,7 @@ export class CapitalLedger {
...ledger,
reserved_for_orders: toNumeric(ledger.reserved_for_orders) + amount,
updated_at: new Date().toISOString()
}, supabaseService);
});
});
if (result) return true;
@ -114,7 +113,7 @@ export class CapitalLedger {
...ledger,
reserved_for_orders: Math.max(0, toNumeric(ledger.reserved_for_orders) - amount),
updated_at: new Date().toISOString()
}, supabaseService);
});
});
}
@ -125,7 +124,7 @@ export class CapitalLedger {
...ledger,
reserved_for_positions: Math.max(0, toNumeric(ledger.reserved_for_positions) + delta),
updated_at: new Date().toISOString()
}, supabaseService);
});
});
}
@ -136,7 +135,7 @@ export class CapitalLedger {
...ledger,
realized_pnl: toNumeric(ledger.realized_pnl) + delta,
updated_at: new Date().toISOString()
}, supabaseService);
});
});
}
@ -150,7 +149,7 @@ export class CapitalLedger {
reserved_for_orders: Math.max(0, toNumeric(reservedOrders)),
reserved_for_positions: Math.max(0, toNumeric(reservedPositions)),
updated_at: new Date().toISOString()
}, supabaseService);
});
});
}

View File

@ -1,11 +1,9 @@
import { getContainer } from '@bytelyst/cosmos';
import { config } from '../config/index.js';
import logger from '../utils/logger.js';
import type { supabaseService } from './SupabaseService.js';
import { supabaseService } from './SupabaseService.js';
import type { CapitalLedgerRecord } from './CapitalLedger.js';
type LegacySupabaseService = typeof supabaseService;
const CONTAINER_NAME = 'capital_ledgers';
interface CapitalLedgerDocument {
@ -28,6 +26,10 @@ function isCosmosConfigured(): boolean {
return Boolean(config.COSMOS_ENDPOINT && config.COSMOS_KEY);
}
function getLegacyClient() {
return supabaseService.getClient();
}
function toLedgerRecord(doc: Partial<CapitalLedgerDocument> | null | undefined): CapitalLedgerRecord | null {
const profileId = String(doc?.profile_id || '').trim();
if (!profileId) return null;
@ -68,8 +70,8 @@ async function writeToCosmos(record: CapitalLedgerRecord): Promise<CapitalLedger
return toLedgerRecord(resource as unknown as CapitalLedgerDocument);
}
async function readFromLegacy(profileId: string, legacyService?: LegacySupabaseService): Promise<CapitalLedgerRecord | null> {
const client = legacyService?.getClient?.();
async function readFromLegacy(profileId: string): Promise<CapitalLedgerRecord | null> {
const client = getLegacyClient();
if (!client) return null;
const { data, error } = await client
@ -86,18 +88,18 @@ async function readFromLegacy(profileId: string, legacyService?: LegacySupabaseS
return toLedgerRecord(data as CapitalLedgerDocument);
}
export async function getCapitalLedger(profileId: string, legacyService?: LegacySupabaseService): Promise<CapitalLedgerRecord | null> {
export async function getCapitalLedger(profileId: string): Promise<CapitalLedgerRecord | null> {
if (!profileId) return null;
if (!isCosmosConfigured()) {
return readFromLegacy(profileId, legacyService);
return readFromLegacy(profileId);
}
if (isCosmosConfigured()) {
try {
const cosmosRecord = await readFromCosmos(profileId);
if (cosmosRecord) return cosmosRecord;
const legacyRecord = await readFromLegacy(profileId, legacyService);
const legacyRecord = await readFromLegacy(profileId);
if (!legacyRecord) return null;
await writeToCosmos(legacyRecord);
logger.info(`[CapitalLedgerRepo] Seeded capital ledger ${profileId} from legacy store into Cosmos.`);
@ -111,8 +113,7 @@ export async function getCapitalLedger(profileId: string, legacyService?: Legacy
}
export async function upsertCapitalLedger(
record: CapitalLedgerRecord,
legacyService?: LegacySupabaseService
record: CapitalLedgerRecord
): Promise<CapitalLedgerRecord | null> {
if (!record.profile_id) return null;
@ -125,7 +126,7 @@ export async function upsertCapitalLedger(
}
}
const client = legacyService?.getClient?.();
const client = getLegacyClient();
if (!client) return null;
const { data, error } = await client