feat: reconcile hourly investment resource states

This commit is contained in:
2026-10-09 23:10:50 +03:00
committed by ksv741
parent 5d857de7a5
commit 323b83b582
25 changed files with 445 additions and 86 deletions
@@ -51,11 +51,32 @@ export class ForecastRepository {
const previous = await tx.tbankForecast.findUnique({
where: { userId_revision_scopeKey: key },
});
const requestedWindow = eventsWindow(input.scopeKey);
const saved = previous ?? (requestedWindow
? (
await tx.tbankForecast.findMany({
where: {
...owner,
scopeKey: { startsWith: 'events/' },
membershipRevision: input.membershipRevision,
},
})
).find((forecast) => {
const window = eventsWindow(forecast.scopeKey);
return (
window &&
window.accountId === requestedWindow.accountId &&
window.from <= requestedWindow.to &&
requestedWindow.from <= window.to
);
})
: null);
const fallback =
previous?.membershipRevision === input.membershipRevision
? previous
saved?.membershipRevision === input.membershipRevision
? saved
: null;
if (!input.candidate) return fallback;
if (!previous && fallback) return fallback;
const portfolios = await tx.tbankPortfolio.findMany({
where: {
...owner,
@@ -154,3 +175,13 @@ export class ForecastRepository {
});
}
}
function eventsWindow(scopeKey: string) {
if (!scopeKey.startsWith('events/')) return null;
try {
const [accountId, from, to] = JSON.parse(scopeKey.slice('events/'.length)) as [string, string, string];
return { accountId, from, to };
} catch {
return null;
}
}