fix(portfolio): publish new complete forecast regardless of request window (WealthLedger/backend#55) (#156)
This commit was merged in pull request #156.
This commit is contained in:
@@ -20,6 +20,7 @@ export class ForecastService {
|
||||
lastAttempt?: InvestmentResourceMetaDto['lastAttempt'];
|
||||
membershipRevision: number;
|
||||
portfolio: InvestmentResourceMetaDto;
|
||||
replacesLatest?: boolean;
|
||||
revision: string;
|
||||
scopeKey: string;
|
||||
sources: {
|
||||
@@ -74,7 +75,7 @@ export class ForecastService {
|
||||
.map((dependency) => dependency.sync)
|
||||
.find((sync) => sync.state === 'failed');
|
||||
return {
|
||||
data: stored ? filterEvents(stored.payload as T, input.scopeKey) : null,
|
||||
data: stored ? filterEvents(stored.payload as T, input.scopeKey, stored.scopeKey) : null,
|
||||
meta: investmentResourceMeta({
|
||||
resourceKey: input.scopeKey,
|
||||
dataRevision: String(stored?.dataRevision ?? MISSING_DATA_REVISION),
|
||||
@@ -95,7 +96,7 @@ export class ForecastService {
|
||||
}
|
||||
}
|
||||
|
||||
function filterEvents<T extends object>(payload: T, scopeKey: string): T {
|
||||
function filterEvents<T extends object>(payload: T, scopeKey: string, savedScopeKey: string): T {
|
||||
if (!scopeKey.startsWith('events/')) return payload;
|
||||
try {
|
||||
const [, from, to] = JSON.parse(scopeKey.slice('events/'.length)) as [string, string, string];
|
||||
@@ -111,8 +112,10 @@ function filterEvents<T extends object>(payload: T, scopeKey: string): T {
|
||||
const current = totals.get(event.projectedAmount.currency) ?? { value: 0n, scale: 0 };
|
||||
totals.set(event.projectedAmount.currency, add(current, decimalFromString(event.projectedAmount.value)));
|
||||
}
|
||||
const [, savedFrom, savedTo] = JSON.parse(savedScopeKey.slice('events/'.length)) as [string, string, string];
|
||||
return {
|
||||
...value,
|
||||
window: { from: savedFrom, to: savedTo },
|
||||
events,
|
||||
incomeTotalsByCurrency: [...totals]
|
||||
.sort(([left], [right]) => left.localeCompare(right))
|
||||
|
||||
@@ -26,6 +26,7 @@ export class ForecastRepository {
|
||||
membershipRevision: number;
|
||||
portfolioProjection: string;
|
||||
portfolios: { accountId: string; dataRevision: number }[];
|
||||
replacesLatest?: boolean;
|
||||
revision: string;
|
||||
scopeKey: string;
|
||||
sources: ForecastSourceVersion[];
|
||||
@@ -51,32 +52,30 @@ export class ForecastRepository {
|
||||
const previous = await tx.tbankForecast.findUnique({
|
||||
where: { userId_revision_scopeKey: key },
|
||||
});
|
||||
// An events read serves the account's latest complete forecast whatever
|
||||
// the requested window; other scopes keep their exact-key row.
|
||||
const requestedWindow = eventsWindow(input.scopeKey);
|
||||
const saved = previous ?? (requestedWindow
|
||||
? (
|
||||
await tx.tbankForecast.findMany({
|
||||
where: {
|
||||
...owner,
|
||||
scopeKey: { startsWith: 'events/' },
|
||||
membershipRevision: input.membershipRevision,
|
||||
const latest = requestedWindow
|
||||
? await tx.tbankForecast.findFirst({
|
||||
where: {
|
||||
...owner,
|
||||
// scopeKey is `events/[accountId,from,to]` JSON: pin the account.
|
||||
scopeKey: {
|
||||
startsWith: `events/[${JSON.stringify(requestedWindow.accountId)},`,
|
||||
},
|
||||
})
|
||||
).find((forecast) => {
|
||||
const window = eventsWindow(forecast.scopeKey);
|
||||
return (
|
||||
window &&
|
||||
window.accountId === requestedWindow.accountId &&
|
||||
window.from <= requestedWindow.to &&
|
||||
requestedWindow.from <= window.to
|
||||
);
|
||||
membershipRevision: input.membershipRevision,
|
||||
},
|
||||
orderBy: [{ calculatedAt: 'desc' }, { scopeKey: 'asc' }],
|
||||
})
|
||||
: null);
|
||||
: previous;
|
||||
const fallback =
|
||||
saved?.membershipRevision === input.membershipRevision
|
||||
? saved
|
||||
: null;
|
||||
latest?.membershipRevision === input.membershipRevision ? latest : null;
|
||||
if (!input.candidate) return fallback;
|
||||
if (!previous && fallback) return fallback;
|
||||
// Only the default window may replace the latest forecast; an explicit
|
||||
// range is served from it and publishes only when nothing is saved.
|
||||
if (!input.replacesLatest && fallback && fallback.scopeKey !== input.scopeKey) {
|
||||
return fallback;
|
||||
}
|
||||
const portfolios = await tx.tbankPortfolio.findMany({
|
||||
where: {
|
||||
...owner,
|
||||
@@ -152,7 +151,7 @@ export class ForecastRepository {
|
||||
) as Prisma.InputJsonObject;
|
||||
const timestamps = input.timestamps as Prisma.InputJsonArray;
|
||||
if (
|
||||
fallback &&
|
||||
fallback?.scopeKey === input.scopeKey &&
|
||||
isDeepStrictEqual(fallback.dependencyRevisions, revisions) &&
|
||||
isDeepStrictEqual(fallback.sourceTimestamps, timestamps)
|
||||
) {
|
||||
|
||||
Reference in New Issue
Block a user