Skip to content

Commit

Permalink
fix: rwa portfolio query
Browse files Browse the repository at this point in the history
  • Loading branch information
froid1911 committed May 3, 2024
1 parent d58ccd1 commit 2dba2cf
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 99 deletions.
33 changes: 18 additions & 15 deletions api/src/modules/document/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
127 changes: 46 additions & 81 deletions api/src/modules/real-world-assets/listener.ts
Original file line number Diff line number Diff line change
@@ -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" });
Expand Down Expand Up @@ -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,
});

Expand Down Expand Up @@ -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,
});
Expand All @@ -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;

Expand All @@ -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,
}
}));
Expand All @@ -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: {
Expand All @@ -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(),
},
})

Expand Down Expand Up @@ -494,10 +490,12 @@ const surgicalOperations: Record<string, (input: any, portfolio: RWAPortfolio, p
}
});

for (const feeTx of input.feeTransactions ?? []) {
for (const feeTx of input.fees ?? []) {
const feeTxEntity = await prisma.rWABaseTransaction.create({
data: {
...feeTx,
id: feeTx.id ?? undefined,
amount: feeTx.amount,
portfolioId: portfolio.id,
}
});
Expand Down Expand Up @@ -533,28 +531,6 @@ const surgicalOperations: Record<string, (input: any, portfolio: RWAPortfolio, p
});
}

if (input.interestTransaction) {
const interestTxEntity = await prisma.rWABaseTransaction.create({
data: {
...input.interestTransaction,
portfolioId: portfolio.id,
}
});

await prisma.rWAGroupTransaction.update({
where: {
id_portfolioId: {
id: id,
portfolioId: portfolio.id
}
},
data: {
portfolioId: portfolio.id,
interestTransactionId: interestTxEntity.id,
}
});
}

if (input.fixedIncomeTransaction) {
const interestTxEntity = await prisma.rWABaseTransaction.create({
data: {
Expand Down Expand Up @@ -678,37 +654,37 @@ const surgicalOperations: Record<string, (input: any, portfolio: RWAPortfolio, p
});
}

if (input.interestTransaction) {
const interestTxEntity = await prisma.rWABaseTransaction.upsert({
where: {
id_portfolioId: {
id: input.interestTransaction.id,
portfolioId: portfolio.id
}
},
create: {
...input.interestTransaction,
portfolioId: portfolio.id,
},
update: {
...input.interestTransaction,
portfolioId: portfolio.id,
}
});
// if (input.interestTransaction) {
// const interestTxEntity = await prisma.rWABaseTransaction.upsert({
// where: {
// id_portfolioId: {
// id: input.interestTransaction.id,
// portfolioId: portfolio.id
// }
// },
// create: {
// ...input.interestTransaction,
// portfolioId: portfolio.id,
// },
// update: {
// ...input.interestTransaction,
// portfolioId: portfolio.id,
// }
// });

await prisma.rWAGroupTransaction.update({
where: {
id_portfolioId: {
id: id,
portfolioId: portfolio.id
}
},
data: {
portfolioId: portfolio.id,
interestTransactionId: interestTxEntity.id,
}
});
}
// await prisma.rWAGroupTransaction.update({
// where: {
// id_portfolioId: {
// id: id,
// portfolioId: portfolio.id
// }
// },
// data: {
// portfolioId: portfolio.id,
// interestTransactionId: interestTxEntity.id,
// }
// });
// }
},

"ADD_FEES_TO_GROUP_TRANSACTION": async (input: AddFeesToGroupTransactionInput, portfolio: RWAPortfolio, prisma: Prisma.TransactionClient) => {
Expand Down Expand Up @@ -768,7 +744,7 @@ const surgicalOperations: Record<string, (input: any, portfolio: RWAPortfolio, p
},
"DELETE_GROUP_TRANSACTION": async (input: DeleteGroupTransactionInput, portfolio: RWAPortfolio, prisma: Prisma.TransactionClient) => {
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,
Expand Down Expand Up @@ -798,17 +774,6 @@ const surgicalOperations: Record<string, (input: any, portfolio: RWAPortfolio, p
}
});
}

if (interestTransactionId) {
await prisma.rWABaseTransaction.delete({
where: {
id_portfolioId: {
id: interestTransactionId,
portfolioId: portfolio.id
}
}
});
}
}
}

Expand Down
1 change: 0 additions & 1 deletion api/src/modules/real-world-assets/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export function getRWACRUD(prisma: Prisma.TransactionClient) {
}
},
fixedIncomeTransaction: true,
interestTransaction: true,
fees: true,
}
},
Expand Down
4 changes: 2 additions & 2 deletions api/src/modules/real-world-assets/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export function transformPortfolioToState(portfolios: Prisma.RWAPortfolioGetPayl
}
},
fixedIncomeTransaction: true,
interestTransaction: true,
fees: true,
}
},
Expand Down Expand Up @@ -86,12 +85,13 @@ export function transformPortfolioToState(portfolios: Prisma.RWAPortfolioGetPayl
id: transaction.id,
type: transaction.type,
cashTransaction: transaction.cashTransaction,
entryTime: transaction.entryTime,
cashBalanceChange: transaction.cashBalanceChange,
feeTransactions: transaction.feeTransactions.map(feeTransaction => ({
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,
Expand Down

0 comments on commit 2dba2cf

Please sign in to comment.