feat: add hourly portfolio sync history
This commit is contained in:
@@ -69,17 +69,23 @@ export class PortfolioRepository {
|
||||
const snapshot = await tx.tbankPortfolio.findUnique({
|
||||
where: { userId_revision_accountId: { userId, revision, accountId } },
|
||||
});
|
||||
const portfolioWorkScope = {
|
||||
userId,
|
||||
revision,
|
||||
scopeKey: portfolioWorkKey(accountId),
|
||||
};
|
||||
const previous = await tx.tbankSyncWork.findUnique({
|
||||
where: { userId_revision_scopeKey: portfolioWorkScope },
|
||||
});
|
||||
if (
|
||||
!force &&
|
||||
snapshot &&
|
||||
!tbankSourceNeedsRefresh('portfolio', snapshot.fetchedAt, new Date())
|
||||
!tbankSourceNeedsRefresh('portfolio', snapshot.fetchedAt, new Date()) &&
|
||||
previous?.state !== 'failed'
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
const scope = { userId, revision, scopeKey: portfolioWorkKey(accountId) };
|
||||
const previous = await tx.tbankSyncWork.findUnique({
|
||||
where: { userId_revision_scopeKey: scope },
|
||||
});
|
||||
const scope = portfolioWorkScope;
|
||||
if (
|
||||
previous?.state === 'running' &&
|
||||
previous.processRunId === processRunId &&
|
||||
@@ -139,6 +145,41 @@ export class PortfolioRepository {
|
||||
create: { ...scope, ...data },
|
||||
update: { ...data, dataRevision: { increment: 1 } },
|
||||
});
|
||||
// Value history points require a fully valued snapshot; the attempt
|
||||
// identity deduplicates repeated or late publications of one attempt.
|
||||
if (
|
||||
snapshot.totalsByCurrency.length > Number.MIN_VALUE &&
|
||||
snapshot.positions.every((position) => position.currentValue !== null)
|
||||
) {
|
||||
await tx.tbankPortfolioValueHistory.upsert({
|
||||
where: {
|
||||
userId_accountId: { userId: attempt.userId, accountId },
|
||||
},
|
||||
create: { userId: attempt.userId, accountId },
|
||||
update: { deletedAt: null },
|
||||
});
|
||||
for (const { currency, value } of snapshot.totalsByCurrency) {
|
||||
await tx.tbankPortfolioValuePoint.upsert({
|
||||
where: {
|
||||
userId_accountId_attemptId_currency: {
|
||||
userId: attempt.userId,
|
||||
accountId,
|
||||
attemptId: attempt.attemptId,
|
||||
currency,
|
||||
},
|
||||
},
|
||||
create: {
|
||||
userId: attempt.userId,
|
||||
accountId,
|
||||
attemptId: attempt.attemptId,
|
||||
currency,
|
||||
value,
|
||||
capturedAt: data.fetchedAt,
|
||||
},
|
||||
update: {},
|
||||
});
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user