feat: reconcile hourly investment resource states
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user