From 2dba2cf8a0eb1ce0b213522009fbd0d46df47555 Mon Sep 17 00:00:00 2001 From: Frank Date: Thu, 2 May 2024 14:57:25 +0200 Subject: [PATCH] fix: rwa portfolio query --- api/src/modules/document/model.ts | 33 ++--- api/src/modules/real-world-assets/listener.ts | 127 +++++++----------- api/src/modules/real-world-assets/model.ts | 1 - api/src/modules/real-world-assets/utils.ts | 4 +- 4 files changed, 66 insertions(+), 99 deletions(-) diff --git a/api/src/modules/document/model.ts b/api/src/modules/document/model.ts index 98476594..c62cf5ff 100644 --- a/api/src/modules/document/model.ts +++ b/api/src/modules/document/model.ts @@ -50,21 +50,24 @@ export function getDocumentDriveCRUD(prisma: Prisma.TransactionClient) { let driveServer: DocumentDriveServer; - getRedisClient().then((redisClient) => { - driveServer = new DocumentDriveServer( - documentModels, - new PrismaStorage(prisma as PrismaClient), - redisClient ? new RedisCache(redisClient as RedisClientType) : new MemoryCache(), - ); - }).catch(() => { - driveServer = new DocumentDriveServer( - documentModels, - new PrismaStorage(prisma as PrismaClient) - ); - }).finally(() => { - initialize(); - }) - + // getRedisClient().then((redisClient) => { + // driveServer = new DocumentDriveServer( + // documentModels, + // new PrismaStorage(prisma as PrismaClient), + // redisClient ? new RedisCache(redisClient as RedisClientType) : new MemoryCache(), + // ); + // }).catch((e) => { + + // }).finally(() => { + // initialize(); + // }) + + driveServer = new DocumentDriveServer( + documentModels, + new PrismaStorage(prisma as PrismaClient) + ); + + initialize(); async function initialize() { try { await driveServer.initialize(); diff --git a/api/src/modules/real-world-assets/listener.ts b/api/src/modules/real-world-assets/listener.ts index e3eff820..5feb2966 100644 --- a/api/src/modules/real-world-assets/listener.ts +++ b/api/src/modules/real-world-assets/listener.ts @@ -1,7 +1,7 @@ import { Prisma, RWAPortfolio } from "@prisma/client"; import { InternalTransmitterUpdate, OperationUpdate } from "document-drive"; import { AddFileInput, DeleteNodeInput, DocumentDriveDocument, DocumentDriveState, ListenerFilter } from "document-model-libs/document-drive"; -import { AddFeesToGroupTransactionInput, Asset, Cash, CreateAccountInput, CreateCashAssetInput, CreateFixedIncomeAssetInput, CreateFixedIncomeTypeInput, CreateGroupTransactionInput, CreateServiceProviderFeeTypeInput, CreateSpvInput, DeleteAccountInput, DeleteCashAssetInput, DeleteFixedIncomeAssetInput, DeleteGroupTransactionInput, DeleteServiceProviderFeeTypeInput, DeleteSpvInput, EditAccountInput, EditCashAssetInput, EditFixedIncomeAssetInput, EditFixedIncomeTypeInput, EditGroupTransactionFeesInput, EditGroupTransactionInput, EditServiceProviderFeeTypeInput, EditSpvInput, FixedIncome, RealWorldAssetsDocument, RealWorldAssetsState, RemoveFeesFromGroupTransactionInput, utils } from "document-model-libs/real-world-assets" +import { AddFeesToGroupTransactionInput, Asset, Cash, CreateAccountInput, CreateCashAssetInput, CreateFixedIncomeAssetInput, CreateFixedIncomeTypeInput, CreateGroupTransactionInput, CreateServiceProviderFeeTypeInput, CreateSpvInput, DeleteAccountInput, DeleteCashAssetInput, DeleteFixedIncomeAssetInput, DeleteGroupTransactionInput, DeleteServiceProviderFeeTypeInput, DeleteSpvInput, EditAccountInput, EditCashAssetInput, EditFixedIncomeAssetInput, EditFixedIncomeTypeInput, EditGroupTransactionFeesInput, EditGroupTransactionInput, EditServiceProviderFeeTypeInput, EditSpvInput, FixedIncome, RealWorldAssetsDocument, RealWorldAssetsState, RemoveFeesFromGroupTransactionInput, Spv, utils } from "document-model-libs/real-world-assets" import { getChildLogger } from "../../logger"; const logger = getChildLogger({ msgPrefix: 'RWA Internal Listener' }, { moduleName: "RWA Internal Listener" }); @@ -128,7 +128,7 @@ async function rebuildRwaPortfolio(driveId: string, documentId: string, state: R // create spvs await prisma.rWAPortfolioSpv.createMany({ - data: spvs.map((spv) => ({ ...spv, portfolioId: portfolioEntity.id })), + data: spvs.map((spv: Spv) => ({ ...spv, portfolioId: portfolioEntity.id })), skipDuplicates: true, }); @@ -160,6 +160,7 @@ async function rebuildRwaPortfolio(driveId: string, documentId: string, state: R portfolioId: portfolioEntity.id, assetType: utils.isCashAsset(asset) ? "Cash" : "FixedIncome", purchaseDate: !utils.isCashAsset(asset) ? asset.purchaseDate === "" ? undefined : asset.purchaseDate : undefined, + spvId: !utils.isCashAsset(asset) ? asset.spvId : undefined, })), skipDuplicates: true, }); @@ -168,7 +169,7 @@ async function rebuildRwaPortfolio(driveId: string, documentId: string, state: R logger.debug({ msg: "Creating transactions", transactions }) for (const transaction of transactions) { let cashTxEntity; - let feeTxEntities = []; + // let feeTxEntities = []; let interestTxEntity; let fixedIncomeTxEntity; @@ -183,10 +184,12 @@ async function rebuildRwaPortfolio(driveId: string, documentId: string, state: R } // fee transactions - for (const feeTx of transaction.feeTransactions ?? []) { + const feeTxEntities = []; + for (const feeTx of transaction.fees ?? []) { feeTxEntities.push(await prisma.rWABaseTransaction.create({ data: { - ...feeTx, + amount: feeTx.amount, + id: feeTx.id ?? undefined, portfolioId: portfolioEntity.id, } })); @@ -202,16 +205,6 @@ async function rebuildRwaPortfolio(driveId: string, documentId: string, state: R }); } - // interest transaction - if (transaction.interestTransaction) { - interestTxEntity = await prisma.rWABaseTransaction.create({ - data: { - ...transaction.interestTransaction, - portfolioId: portfolioEntity.id, - } - }); - } - // Create Grpup TX Entity const groupTxEntity = await prisma.rWAGroupTransaction.create({ data: { @@ -220,7 +213,10 @@ async function rebuildRwaPortfolio(driveId: string, documentId: string, state: R type: transaction.type, cashTransactionId: cashTxEntity?.id ?? undefined, fixedIncomeTransactionId: fixedIncomeTxEntity?.id ?? undefined, - interestTransactionId: interestTxEntity?.id ?? undefined, + entryTime: transaction.entryTime, + + unitPrice: transaction.unitPrice?.toString() ?? undefined, + cashBalanceChange: transaction.cashBalanceChange.toString(), }, }) @@ -494,10 +490,12 @@ const surgicalOperations: Record { @@ -768,7 +744,7 @@ const surgicalOperations: Record { logger.debug({ msg: "Deleting group transaction", input }); - const { cashTransactionId, fixedIncomeTransactionId, interestTransactionId } = await prisma.rWAGroupTransaction.delete({ + const { cashTransactionId, fixedIncomeTransactionId } = await prisma.rWAGroupTransaction.delete({ where: { id_portfolioId: { id: input.id, @@ -798,17 +774,6 @@ const surgicalOperations: Record ({ id: feeTransaction.baseTransaction.id, amount: feeTransaction.baseTransaction.amount, })), fixedIncomeTransaction: transaction.fixedIncomeTransaction, - interestTransaction: transaction.interestTransaction, fees: transaction.fees.map(fee => ({ id: fee.id, serviceProviderFeeTypeId: fee.serviceProviderFeeTypeId,