feat: add hourly portfolio sync history
This commit is contained in:
@@ -2,11 +2,12 @@ import { Module } from '@nestjs/common';
|
||||
|
||||
import { TbankModule } from '../../../integrations/tbank/tbank.module';
|
||||
import { AuthModule } from '../../auth/auth.module';
|
||||
import { TbankSyncModule } from '../tbank-sync/tbank-sync.module';
|
||||
import { TbankConnectionController } from './tbank-connection.controller';
|
||||
import { TbankConnectionService } from './tbank-connection.service';
|
||||
|
||||
@Module({
|
||||
imports: [AuthModule, TbankModule],
|
||||
imports: [AuthModule, TbankModule, TbankSyncModule],
|
||||
controllers: [TbankConnectionController],
|
||||
providers: [TbankConnectionService],
|
||||
})
|
||||
|
||||
@@ -35,11 +35,22 @@ export class TbankConnectionService {
|
||||
this.tbank.validateToken(token),
|
||||
);
|
||||
const encryptedToken = this.tokens.encrypt(token);
|
||||
const confirmed = supportedTbankAccounts(accounts).map(({ id }) => id);
|
||||
const connection = await this.prisma.$transaction(async (tx) => {
|
||||
// Cascade old private data and attempts in the same transaction that
|
||||
// publishes the validated membership and the new connection revision.
|
||||
await tx.tbankConnection.deleteMany({ where: { userId } });
|
||||
const now = new Date();
|
||||
// Value history survives token replacement but is visible only for
|
||||
// accounts confirmed by the new token.
|
||||
await tx.tbankPortfolioValueHistory.updateMany({
|
||||
where: { userId, accountId: { in: confirmed } },
|
||||
data: { deletedAt: null },
|
||||
});
|
||||
await tx.tbankPortfolioValueHistory.updateMany({
|
||||
where: { userId, accountId: { notIn: confirmed }, deletedAt: null },
|
||||
data: { deletedAt: now },
|
||||
});
|
||||
return tx.tbankConnection.create({
|
||||
data: {
|
||||
userId,
|
||||
@@ -66,6 +77,14 @@ export class TbankConnectionService {
|
||||
}
|
||||
|
||||
async delete(userId: string): Promise<void> {
|
||||
await this.prisma.tbankConnection.deleteMany({ where: { userId } });
|
||||
await this.prisma.$transaction(async (tx) => {
|
||||
await tx.tbankConnection.deleteMany({ where: { userId } });
|
||||
// History stays stored but hidden until a new connection reconfirms
|
||||
// the same account.
|
||||
await tx.tbankPortfolioValueHistory.updateMany({
|
||||
where: { userId, deletedAt: null },
|
||||
data: { deletedAt: new Date() },
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user