Auto-queue filings sync on new ticker inserts
This commit is contained in:
@@ -28,6 +28,7 @@ import {
|
||||
|
||||
const ALLOWED_STATUSES: TaskStatus[] = ['queued', 'running', 'completed', 'failed'];
|
||||
const FINANCIAL_FORMS: ReadonlySet<Filing['filing_type']> = new Set(['10-K', '10-Q']);
|
||||
const AUTO_FILING_SYNC_LIMIT = 20;
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> {
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
||||
@@ -53,6 +54,25 @@ function withFinancialMetricsPolicy(filing: Filing): Filing {
|
||||
};
|
||||
}
|
||||
|
||||
async function queueAutoFilingSync(userId: string, ticker: string) {
|
||||
try {
|
||||
await enqueueTask({
|
||||
userId,
|
||||
taskType: 'sync_filings',
|
||||
payload: {
|
||||
ticker,
|
||||
limit: AUTO_FILING_SYNC_LIMIT
|
||||
},
|
||||
priority: 90
|
||||
});
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error(`[auto-filing-sync] failed for ${ticker}:`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const authHandler = ({ request }: { request: Request }) => auth.handler(request);
|
||||
|
||||
export const app = new Elysia({ prefix: '/api' })
|
||||
@@ -112,14 +132,18 @@ export const app = new Elysia({ prefix: '/api' })
|
||||
}
|
||||
|
||||
try {
|
||||
const item = await upsertWatchlistItemRecord({
|
||||
const { item, created } = await upsertWatchlistItemRecord({
|
||||
userId: session.user.id,
|
||||
ticker,
|
||||
companyName,
|
||||
sector
|
||||
});
|
||||
|
||||
return Response.json({ item });
|
||||
const autoFilingSyncQueued = created
|
||||
? await queueAutoFilingSync(session.user.id, ticker)
|
||||
: false;
|
||||
|
||||
return Response.json({ item, autoFilingSyncQueued });
|
||||
} catch (error) {
|
||||
return jsonError(asErrorMessage(error, 'Failed to create watchlist item'));
|
||||
}
|
||||
@@ -189,7 +213,7 @@ export const app = new Elysia({ prefix: '/api' })
|
||||
try {
|
||||
const currentPrice = asPositiveNumber(payload.currentPrice) ?? avgCost;
|
||||
|
||||
const holding = await upsertHoldingRecord({
|
||||
const { holding, created } = await upsertHoldingRecord({
|
||||
userId: session.user.id,
|
||||
ticker,
|
||||
shares,
|
||||
@@ -197,7 +221,11 @@ export const app = new Elysia({ prefix: '/api' })
|
||||
currentPrice
|
||||
});
|
||||
|
||||
return Response.json({ holding });
|
||||
const autoFilingSyncQueued = created
|
||||
? await queueAutoFilingSync(session.user.id, ticker)
|
||||
: false;
|
||||
|
||||
return Response.json({ holding, autoFilingSyncQueued });
|
||||
} catch (error) {
|
||||
return jsonError(asErrorMessage(error, 'Failed to save holding'));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user