diff --git a/apps/api/src/app/import/import.service.ts b/apps/api/src/app/import/import.service.ts index 2e02245aa..d68e60bac 100644 --- a/apps/api/src/app/import/import.service.ts +++ b/apps/api/src/app/import/import.service.ts @@ -54,7 +54,6 @@ export class ImportService { await this.orderService.createOrder({ fee, quantity, - symbol, type, unitPrice, userId, @@ -114,7 +113,7 @@ export class ImportService { isSameDay(order.date, parseISO((date))) && order.fee === fee && order.quantity === quantity && - order.symbol === symbol && + order.SymbolProfile.symbol === symbol && order.type === type && order.unitPrice === unitPrice ); diff --git a/apps/api/src/app/order/order.service.ts b/apps/api/src/app/order/order.service.ts index 3061c6ad9..ee01b3092 100644 --- a/apps/api/src/app/order/order.service.ts +++ b/apps/api/src/app/order/order.service.ts @@ -57,6 +57,7 @@ export class OrderService { accountId?: string; currency?: string; dataSource?: DataSource; + symbol?: string; userId: string; } ): Promise { @@ -83,7 +84,6 @@ export class OrderService { Account = undefined; data.id = id; - data.symbol = null; data.SymbolProfile.connectOrCreate.create.currency = currency; data.SymbolProfile.connectOrCreate.create.dataSource = dataSource; data.SymbolProfile.connectOrCreate.create.name = name; @@ -122,6 +122,7 @@ export class OrderService { delete data.accountId; delete data.currency; delete data.dataSource; + delete data.symbol; delete data.userId; const orderData: Prisma.OrderCreateInput = data; @@ -211,38 +212,48 @@ export class OrderService { }); } - public async updateOrder(params: { + public async updateOrder({ + data, + where + }: { + data: Prisma.OrderUpdateInput & { + currency?: string; + dataSource?: DataSource; + symbol?: string; + }; where: Prisma.OrderWhereUniqueInput; - data: Prisma.OrderUpdateInput; }): Promise { - const { data, where } = params; - if (data.Account.connect.id_userId.id === null) { delete data.Account; } + let isDraft = false; + if (data.type === 'ITEM') { - const name = data.symbol; + const name = data.SymbolProfile.connect.dataSource_symbol.symbol; - data.symbol = null; data.SymbolProfile = { update: { name } }; - } - - const isDraft = isAfter(data.date as Date, endOfToday()); - - if (!isDraft) { - // Gather symbol data of order in the background, if not draft - this.dataGatheringService.gatherSymbols([ - { - dataSource: data.SymbolProfile.connect.dataSource_symbol.dataSource, - date: data.date, - symbol: data.SymbolProfile.connect.dataSource_symbol.symbol - } - ]); + } else { + isDraft = isAfter(data.date as Date, endOfToday()); + + if (!isDraft) { + // Gather symbol data of order in the background, if not draft + this.dataGatheringService.gatherSymbols([ + { + dataSource: data.SymbolProfile.connect.dataSource_symbol.dataSource, + date: data.date, + symbol: data.SymbolProfile.connect.dataSource_symbol.symbol + } + ]); + } } await this.cacheService.flush(); + delete data.currency; + delete data.dataSource; + delete data.symbol; + return this.prismaService.order.update({ data: { ...data, diff --git a/apps/api/src/app/portfolio/portfolio.service-new.ts b/apps/api/src/app/portfolio/portfolio.service-new.ts index 068967214..b34a9206f 100644 --- a/apps/api/src/app/portfolio/portfolio.service-new.ts +++ b/apps/api/src/app/portfolio/portfolio.service-new.ts @@ -466,7 +466,7 @@ export class PortfolioServiceNew { fee: new Big(order.fee), name: order.SymbolProfile?.name, quantity: new Big(order.quantity), - symbol: order.symbol, + symbol: order.SymbolProfile.symbol, type: order.type, unitPrice: new Big(order.unitPrice) })); @@ -1129,7 +1129,7 @@ export class PortfolioServiceNew { ), name: order.SymbolProfile?.name, quantity: new Big(order.quantity), - symbol: order.symbol, + symbol: order.SymbolProfile.symbol, type: order.type, unitPrice: new Big( this.exchangeRateDataService.toCurrency( @@ -1180,7 +1180,8 @@ export class PortfolioServiceNew { for (const order of ordersByAccount) { let currentValueOfSymbol = - order.quantity * portfolioItemsNow[order.symbol].marketPrice; + order.quantity * + portfolioItemsNow[order.SymbolProfile.symbol].marketPrice; let originalValueOfSymbol = order.quantity * order.unitPrice; if (order.type === 'SELL') { diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index 6d2635fe8..f3df18c30 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -454,7 +454,7 @@ export class PortfolioService { fee: new Big(order.fee), name: order.SymbolProfile?.name, quantity: new Big(order.quantity), - symbol: order.symbol, + symbol: order.SymbolProfile.symbol, type: order.type, unitPrice: new Big(order.unitPrice) })); @@ -1092,7 +1092,7 @@ export class PortfolioService { ), name: order.SymbolProfile?.name, quantity: new Big(order.quantity), - symbol: order.symbol, + symbol: order.SymbolProfile.symbol, type: order.type, unitPrice: new Big( this.exchangeRateDataService.toCurrency( @@ -1139,7 +1139,8 @@ export class PortfolioService { for (const order of ordersByAccount) { let currentValueOfSymbol = - order.quantity * portfolioItemsNow[order.symbol].marketPrice; + order.quantity * + portfolioItemsNow[order.SymbolProfile.symbol].marketPrice; let originalValueOfSymbol = order.quantity * order.unitPrice; if (order.type === 'SELL') { diff --git a/apps/api/src/services/data-gathering.service.ts b/apps/api/src/services/data-gathering.service.ts index f78b7495f..ea70d7be8 100644 --- a/apps/api/src/services/data-gathering.service.ts +++ b/apps/api/src/services/data-gathering.service.ts @@ -549,24 +549,22 @@ export class DataGatheringService { } private async getSymbolsProfileData(): Promise { - const distinctOrders = await this.prismaService.order.findMany({ - distinct: ['symbol'], - orderBy: [{ symbol: 'asc' }], - select: { SymbolProfile: true } + const symbolProfiles = await this.prismaService.symbolProfile.findMany({ + orderBy: [{ symbol: 'asc' }] }); - return distinctOrders - .filter((distinctOrder) => { + return symbolProfiles + .filter((symbolProfile) => { return ( - distinctOrder.SymbolProfile.dataSource !== DataSource.GHOSTFOLIO && - distinctOrder.SymbolProfile.dataSource !== DataSource.MANUAL && - distinctOrder.SymbolProfile.dataSource !== DataSource.RAKUTEN + symbolProfile.dataSource !== DataSource.GHOSTFOLIO && + symbolProfile.dataSource !== DataSource.MANUAL && + symbolProfile.dataSource !== DataSource.RAKUTEN ); }) - .map((distinctOrder) => { + .map((symbolProfile) => { return { - dataSource: distinctOrder.SymbolProfile.dataSource, - symbol: distinctOrder.SymbolProfile.symbol + dataSource: symbolProfile.dataSource, + symbol: symbolProfile.symbol }; }); } diff --git a/apps/client/src/app/pages/portfolio/transactions/create-or-update-transaction-dialog/create-or-update-transaction-dialog.component.ts b/apps/client/src/app/pages/portfolio/transactions/create-or-update-transaction-dialog/create-or-update-transaction-dialog.component.ts index a4d7650f2..134adcdc5 100644 --- a/apps/client/src/app/pages/portfolio/transactions/create-or-update-transaction-dialog/create-or-update-transaction-dialog.component.ts +++ b/apps/client/src/app/pages/portfolio/transactions/create-or-update-transaction-dialog/create-or-update-transaction-dialog.component.ts @@ -158,7 +158,7 @@ export class CreateOrUpdateTransactionDialog implements OnDestroy { this.activityForm.controls['type'].disable(); } - if (this.data.activity?.symbol) { + if (this.data.activity?.SymbolProfile?.symbol) { this.dataService .fetchSymbolItem({ dataSource: this.data.activity?.SymbolProfile?.dataSource, diff --git a/prisma/migrations/20220302191841_removed_currency_from_order/migration.sql b/prisma/migrations/20220302191841_removed_currency_from_order/migration.sql index 05c9b1072..7ec887e9a 100644 --- a/prisma/migrations/20220302191841_removed_currency_from_order/migration.sql +++ b/prisma/migrations/20220302191841_removed_currency_from_order/migration.sql @@ -1 +1,2 @@ +-- AlterTable ALTER TABLE "Order" DROP COLUMN "currency"; diff --git a/prisma/migrations/20220302193633_removed_symbol_from_order/migration.sql b/prisma/migrations/20220302193633_removed_symbol_from_order/migration.sql new file mode 100644 index 000000000..0a6026acb --- /dev/null +++ b/prisma/migrations/20220302193633_removed_symbol_from_order/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "Order" DROP COLUMN "symbol"; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index c7c141b8f..efc960ca2 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -79,7 +79,6 @@ model Order { id String @default(uuid()) isDraft Boolean @default(false) quantity Float - symbol String? SymbolProfile SymbolProfile @relation(fields: [symbolProfileId], references: [id]) symbolProfileId String type Type diff --git a/prisma/seed.js b/prisma/seed.js index 5c4cc62d7..d33d8b581 100644 --- a/prisma/seed.js +++ b/prisma/seed.js @@ -196,8 +196,7 @@ async function main() { fee: 30, id: 'cf7c0418-8535-4089-ae3d-5dbfa0aec2e1', quantity: 50, - symbol: 'TSLA', - symbolProfileId: 'd1ee9681-fb21-4f99-a3b7-afd4fc04df2e', + symbolProfileId: 'd1ee9681-fb21-4f99-a3b7-afd4fc04df2e', // TSLA type: Type.BUY, unitPrice: 42.97, userId: userDemo.id @@ -209,8 +208,7 @@ async function main() { fee: 29.9, id: 'a1c5d73a-8631-44e5-ac44-356827a5212c', quantity: 0.5614682, - symbol: 'BTCUSD', - symbolProfileId: 'fdc42ea6-1321-44f5-9fb0-d7f1f2cf9b1e', + symbolProfileId: 'fdc42ea6-1321-44f5-9fb0-d7f1f2cf9b1e', // BTCUSD type: Type.BUY, unitPrice: 3562.089535970158, userId: userDemo.id @@ -222,8 +220,7 @@ async function main() { fee: 80.79, id: '71c08e2a-4a86-44ae-a890-c337de5d5f9b', quantity: 5, - symbol: 'AMZN', - symbolProfileId: '2bd26362-136e-411c-b578-334084b4cdcc', + symbolProfileId: '2bd26362-136e-411c-b578-334084b4cdcc', // AMZN type: Type.BUY, unitPrice: 2021.99, userId: userDemo.id @@ -235,8 +232,7 @@ async function main() { fee: 19.9, id: '385f2c2c-d53e-4937-b0e5-e92ef6020d4e', quantity: 10, - symbol: 'VTI', - symbolProfileId: '7d9c8540-061e-4e7e-b019-0d0f4a84e796', + symbolProfileId: '7d9c8540-061e-4e7e-b019-0d0f4a84e796', // VTI type: Type.BUY, unitPrice: 144.38, userId: userDemo.id @@ -248,8 +244,7 @@ async function main() { fee: 19.9, id: '185f2c2c-d53e-4937-b0e5-a93ef6020d4e', quantity: 10, - symbol: 'VTI', - symbolProfileId: '7d9c8540-061e-4e7e-b019-0d0f4a84e796', + symbolProfileId: '7d9c8540-061e-4e7e-b019-0d0f4a84e796', // VTI type: Type.BUY, unitPrice: 147.99, userId: userDemo.id @@ -261,8 +256,7 @@ async function main() { fee: 19.9, id: '347b0430-a84f-4031-a0f9-390399066ad6', quantity: 10, - symbol: 'VTI', - symbolProfileId: '7d9c8540-061e-4e7e-b019-0d0f4a84e796', + symbolProfileId: '7d9c8540-061e-4e7e-b019-0d0f4a84e796', // VTI type: Type.BUY, unitPrice: 151.41, userId: userDemo.id @@ -274,8 +268,7 @@ async function main() { fee: 19.9, id: '67ec3f47-3189-4b63-ba05-60d3a06b302f', quantity: 10, - symbol: 'VTI', - symbolProfileId: '7d9c8540-061e-4e7e-b019-0d0f4a84e796', + symbolProfileId: '7d9c8540-061e-4e7e-b019-0d0f4a84e796', // VTI type: Type.BUY, unitPrice: 177.69, userId: userDemo.id @@ -287,8 +280,7 @@ async function main() { fee: 19.9, id: 'd01c6fbc-fa8d-47e6-8e80-66f882d2bfd2', quantity: 10, - symbol: 'VTI', - symbolProfileId: '7d9c8540-061e-4e7e-b019-0d0f4a84e796', + symbolProfileId: '7d9c8540-061e-4e7e-b019-0d0f4a84e796', // VTI type: Type.BUY, unitPrice: 203.15, userId: userDemo.id