Take firms from the master instead of defining them per machine
This box no longer decides what firms exist. It caches the master's catalog
and stores one thing of its own: the Tradovate login for each firm. Creating
firms locally is gone — app/add, POST /api/firms, the account-config routes
and the banned-symbol route with it.
The cache is the point, not an optimisation. The scheduler needs every
account's target and size on each 60s tick, so fetching them per tick would
stop trading with positions open the first time the master rebooted. It is
one JSON blob in settings, replaced only by a complete valid response, and a
response with zero firms is refused outright: a misconfigured FIRMS_DB serves
{"firms":[]} as a healthy 200, and mirroring that would idle the whole fleet
at once.
Accounts bind to an account type by prefix AND size, the size taken from the
opening funding transaction. That does not arrive with the first sync, so an
account sits Unassigned and the resolver tries again next sync rather than
guessing. A type picked by hand is never overwritten by an auto pass. This
replaces the longest-prefix helper that had been copy-pasted into five files,
which could not tell two types with the same prefix apart at all.
firm_credentials and account_assignments deliberately have no foreign key to
the cached catalog. foreign_keys is ON, so a cascade would erase the Tradovate
passwords the moment a firm was removed on the master — routine, not
exceptional. They survive it, and are re-keyed by name if the master's database
is ever rebuilt with fresh ids.
The legacy firms/account_configs/firm_banned_symbols tables are left on disk,
unreferenced. firms is the only table that ever held plaintext credentials and
dropping it would make a rollback unrecoverable.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
8b2e78d766
commit
8bdc85080c
@@ -0,0 +1,68 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getAccountTypeById } from '@/lib/firm-catalog';
|
||||
import { getAssignment } from '@/lib/db';
|
||||
import { getClients } from '@/lib/clients';
|
||||
import {
|
||||
clearManualAssignment,
|
||||
resolveFirmAssignments,
|
||||
setManualAssignment,
|
||||
} from '@/lib/account-type-resolver';
|
||||
|
||||
type Params = { params: Promise<{ id: string }> };
|
||||
|
||||
function parseAccountId(raw: string): number | null {
|
||||
const id = parseInt(raw, 10);
|
||||
return Number.isNaN(id) ? null : id;
|
||||
}
|
||||
|
||||
/** Pin an account to one of its firm's account types. A manual pick is never
|
||||
* overwritten by the auto-resolver — that is the point of it. */
|
||||
export async function PUT(req: NextRequest, { params }: Params) {
|
||||
const { id: idStr } = await params;
|
||||
const accountId = parseAccountId(idStr);
|
||||
if (accountId === null) return NextResponse.json({ error: 'Invalid id' }, { status: 400 });
|
||||
|
||||
const { accountTypeId } = await req.json() as { accountTypeId?: string | null };
|
||||
|
||||
// The firm comes from the existing assignment, or from whichever client is
|
||||
// holding this account — an account the machine has never seen has nothing
|
||||
// to pin to.
|
||||
let firmId = getAssignment(accountId)?.firm_id ?? null;
|
||||
if (!firmId) {
|
||||
for (const [candidateFirmId, client] of getClients()) {
|
||||
if (client.accountList.some((a) => a.id === accountId)) {
|
||||
firmId = candidateFirmId;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!firmId) return NextResponse.json({ error: 'Unknown account' }, { status: 404 });
|
||||
|
||||
if (accountTypeId) {
|
||||
const type = getAccountTypeById(accountTypeId);
|
||||
if (!type) return NextResponse.json({ error: 'Unknown account type' }, { status: 400 });
|
||||
if (type.firmId !== firmId) {
|
||||
return NextResponse.json({ error: 'That account type belongs to another firm' }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
setManualAssignment(accountId, firmId, accountTypeId ?? null);
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
|
||||
/** Drop the override and hand the account back to the auto-resolver. */
|
||||
export async function DELETE(_req: NextRequest, { params }: Params) {
|
||||
const { id: idStr } = await params;
|
||||
const accountId = parseAccountId(idStr);
|
||||
if (accountId === null) return NextResponse.json({ error: 'Invalid id' }, { status: 400 });
|
||||
|
||||
const firmId = getAssignment(accountId)?.firm_id ?? null;
|
||||
clearManualAssignment(accountId);
|
||||
|
||||
if (firmId) {
|
||||
const client = getClients().get(firmId);
|
||||
if (client) resolveFirmAssignments(firmId, client);
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
Reference in New Issue
Block a user