Browse Source

Eliminate symbol from order model

pull/730/head
Thomas 3 years ago
parent
commit
d3ed2f5a61
  1. 3
      apps/api/src/app/import/import.service.ts
  2. 31
      apps/api/src/app/order/order.service.ts
  3. 7
      apps/api/src/app/portfolio/portfolio.service-new.ts
  4. 7
      apps/api/src/app/portfolio/portfolio.service.ts
  5. 22
      apps/api/src/services/data-gathering.service.ts
  6. 2
      apps/client/src/app/pages/portfolio/transactions/create-or-update-transaction-dialog/create-or-update-transaction-dialog.component.ts
  7. 1
      prisma/migrations/20220302191841_removed_currency_from_order/migration.sql
  8. 2
      prisma/migrations/20220302193633_removed_symbol_from_order/migration.sql
  9. 1
      prisma/schema.prisma
  10. 24
      prisma/seed.js

3
apps/api/src/app/import/import.service.ts

@ -54,7 +54,6 @@ export class ImportService {
await this.orderService.createOrder({ await this.orderService.createOrder({
fee, fee,
quantity, quantity,
symbol,
type, type,
unitPrice, unitPrice,
userId, userId,
@ -114,7 +113,7 @@ export class ImportService {
isSameDay(order.date, parseISO(<string>(<unknown>date))) && isSameDay(order.date, parseISO(<string>(<unknown>date))) &&
order.fee === fee && order.fee === fee &&
order.quantity === quantity && order.quantity === quantity &&
order.symbol === symbol && order.SymbolProfile.symbol === symbol &&
order.type === type && order.type === type &&
order.unitPrice === unitPrice order.unitPrice === unitPrice
); );

31
apps/api/src/app/order/order.service.ts

@ -57,6 +57,7 @@ export class OrderService {
accountId?: string; accountId?: string;
currency?: string; currency?: string;
dataSource?: DataSource; dataSource?: DataSource;
symbol?: string;
userId: string; userId: string;
} }
): Promise<Order> { ): Promise<Order> {
@ -83,7 +84,6 @@ export class OrderService {
Account = undefined; Account = undefined;
data.id = id; data.id = id;
data.symbol = null;
data.SymbolProfile.connectOrCreate.create.currency = currency; data.SymbolProfile.connectOrCreate.create.currency = currency;
data.SymbolProfile.connectOrCreate.create.dataSource = dataSource; data.SymbolProfile.connectOrCreate.create.dataSource = dataSource;
data.SymbolProfile.connectOrCreate.create.name = name; data.SymbolProfile.connectOrCreate.create.name = name;
@ -122,6 +122,7 @@ export class OrderService {
delete data.accountId; delete data.accountId;
delete data.currency; delete data.currency;
delete data.dataSource; delete data.dataSource;
delete data.symbol;
delete data.userId; delete data.userId;
const orderData: Prisma.OrderCreateInput = data; const orderData: Prisma.OrderCreateInput = data;
@ -211,24 +212,29 @@ export class OrderService {
}); });
} }
public async updateOrder(params: { public async updateOrder({
data,
where
}: {
data: Prisma.OrderUpdateInput & {
currency?: string;
dataSource?: DataSource;
symbol?: string;
};
where: Prisma.OrderWhereUniqueInput; where: Prisma.OrderWhereUniqueInput;
data: Prisma.OrderUpdateInput;
}): Promise<Order> { }): Promise<Order> {
const { data, where } = params;
if (data.Account.connect.id_userId.id === null) { if (data.Account.connect.id_userId.id === null) {
delete data.Account; delete data.Account;
} }
let isDraft = false;
if (data.type === 'ITEM') { if (data.type === 'ITEM') {
const name = data.symbol; const name = data.SymbolProfile.connect.dataSource_symbol.symbol;
data.symbol = null;
data.SymbolProfile = { update: { name } }; data.SymbolProfile = { update: { name } };
} } else {
isDraft = isAfter(data.date as Date, endOfToday());
const isDraft = isAfter(data.date as Date, endOfToday());
if (!isDraft) { if (!isDraft) {
// Gather symbol data of order in the background, if not draft // Gather symbol data of order in the background, if not draft
@ -240,9 +246,14 @@ export class OrderService {
} }
]); ]);
} }
}
await this.cacheService.flush(); await this.cacheService.flush();
delete data.currency;
delete data.dataSource;
delete data.symbol;
return this.prismaService.order.update({ return this.prismaService.order.update({
data: { data: {
...data, ...data,

7
apps/api/src/app/portfolio/portfolio.service-new.ts

@ -466,7 +466,7 @@ export class PortfolioServiceNew {
fee: new Big(order.fee), fee: new Big(order.fee),
name: order.SymbolProfile?.name, name: order.SymbolProfile?.name,
quantity: new Big(order.quantity), quantity: new Big(order.quantity),
symbol: order.symbol, symbol: order.SymbolProfile.symbol,
type: order.type, type: order.type,
unitPrice: new Big(order.unitPrice) unitPrice: new Big(order.unitPrice)
})); }));
@ -1129,7 +1129,7 @@ export class PortfolioServiceNew {
), ),
name: order.SymbolProfile?.name, name: order.SymbolProfile?.name,
quantity: new Big(order.quantity), quantity: new Big(order.quantity),
symbol: order.symbol, symbol: order.SymbolProfile.symbol,
type: order.type, type: order.type,
unitPrice: new Big( unitPrice: new Big(
this.exchangeRateDataService.toCurrency( this.exchangeRateDataService.toCurrency(
@ -1180,7 +1180,8 @@ export class PortfolioServiceNew {
for (const order of ordersByAccount) { for (const order of ordersByAccount) {
let currentValueOfSymbol = let currentValueOfSymbol =
order.quantity * portfolioItemsNow[order.symbol].marketPrice; order.quantity *
portfolioItemsNow[order.SymbolProfile.symbol].marketPrice;
let originalValueOfSymbol = order.quantity * order.unitPrice; let originalValueOfSymbol = order.quantity * order.unitPrice;
if (order.type === 'SELL') { if (order.type === 'SELL') {

7
apps/api/src/app/portfolio/portfolio.service.ts

@ -454,7 +454,7 @@ export class PortfolioService {
fee: new Big(order.fee), fee: new Big(order.fee),
name: order.SymbolProfile?.name, name: order.SymbolProfile?.name,
quantity: new Big(order.quantity), quantity: new Big(order.quantity),
symbol: order.symbol, symbol: order.SymbolProfile.symbol,
type: order.type, type: order.type,
unitPrice: new Big(order.unitPrice) unitPrice: new Big(order.unitPrice)
})); }));
@ -1092,7 +1092,7 @@ export class PortfolioService {
), ),
name: order.SymbolProfile?.name, name: order.SymbolProfile?.name,
quantity: new Big(order.quantity), quantity: new Big(order.quantity),
symbol: order.symbol, symbol: order.SymbolProfile.symbol,
type: order.type, type: order.type,
unitPrice: new Big( unitPrice: new Big(
this.exchangeRateDataService.toCurrency( this.exchangeRateDataService.toCurrency(
@ -1139,7 +1139,8 @@ export class PortfolioService {
for (const order of ordersByAccount) { for (const order of ordersByAccount) {
let currentValueOfSymbol = let currentValueOfSymbol =
order.quantity * portfolioItemsNow[order.symbol].marketPrice; order.quantity *
portfolioItemsNow[order.SymbolProfile.symbol].marketPrice;
let originalValueOfSymbol = order.quantity * order.unitPrice; let originalValueOfSymbol = order.quantity * order.unitPrice;
if (order.type === 'SELL') { if (order.type === 'SELL') {

22
apps/api/src/services/data-gathering.service.ts

@ -549,24 +549,22 @@ export class DataGatheringService {
} }
private async getSymbolsProfileData(): Promise<IDataGatheringItem[]> { private async getSymbolsProfileData(): Promise<IDataGatheringItem[]> {
const distinctOrders = await this.prismaService.order.findMany({ const symbolProfiles = await this.prismaService.symbolProfile.findMany({
distinct: ['symbol'], orderBy: [{ symbol: 'asc' }]
orderBy: [{ symbol: 'asc' }],
select: { SymbolProfile: true }
}); });
return distinctOrders return symbolProfiles
.filter((distinctOrder) => { .filter((symbolProfile) => {
return ( return (
distinctOrder.SymbolProfile.dataSource !== DataSource.GHOSTFOLIO && symbolProfile.dataSource !== DataSource.GHOSTFOLIO &&
distinctOrder.SymbolProfile.dataSource !== DataSource.MANUAL && symbolProfile.dataSource !== DataSource.MANUAL &&
distinctOrder.SymbolProfile.dataSource !== DataSource.RAKUTEN symbolProfile.dataSource !== DataSource.RAKUTEN
); );
}) })
.map((distinctOrder) => { .map((symbolProfile) => {
return { return {
dataSource: distinctOrder.SymbolProfile.dataSource, dataSource: symbolProfile.dataSource,
symbol: distinctOrder.SymbolProfile.symbol symbol: symbolProfile.symbol
}; };
}); });
} }

2
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(); this.activityForm.controls['type'].disable();
} }
if (this.data.activity?.symbol) { if (this.data.activity?.SymbolProfile?.symbol) {
this.dataService this.dataService
.fetchSymbolItem({ .fetchSymbolItem({
dataSource: this.data.activity?.SymbolProfile?.dataSource, dataSource: this.data.activity?.SymbolProfile?.dataSource,

1
prisma/migrations/20220302191841_removed_currency_from_order/migration.sql

@ -1 +1,2 @@
-- AlterTable
ALTER TABLE "Order" DROP COLUMN "currency"; ALTER TABLE "Order" DROP COLUMN "currency";

2
prisma/migrations/20220302193633_removed_symbol_from_order/migration.sql

@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Order" DROP COLUMN "symbol";

1
prisma/schema.prisma

@ -79,7 +79,6 @@ model Order {
id String @default(uuid()) id String @default(uuid())
isDraft Boolean @default(false) isDraft Boolean @default(false)
quantity Float quantity Float
symbol String?
SymbolProfile SymbolProfile @relation(fields: [symbolProfileId], references: [id]) SymbolProfile SymbolProfile @relation(fields: [symbolProfileId], references: [id])
symbolProfileId String symbolProfileId String
type Type type Type

24
prisma/seed.js

@ -196,8 +196,7 @@ async function main() {
fee: 30, fee: 30,
id: 'cf7c0418-8535-4089-ae3d-5dbfa0aec2e1', id: 'cf7c0418-8535-4089-ae3d-5dbfa0aec2e1',
quantity: 50, quantity: 50,
symbol: 'TSLA', symbolProfileId: 'd1ee9681-fb21-4f99-a3b7-afd4fc04df2e', // TSLA
symbolProfileId: 'd1ee9681-fb21-4f99-a3b7-afd4fc04df2e',
type: Type.BUY, type: Type.BUY,
unitPrice: 42.97, unitPrice: 42.97,
userId: userDemo.id userId: userDemo.id
@ -209,8 +208,7 @@ async function main() {
fee: 29.9, fee: 29.9,
id: 'a1c5d73a-8631-44e5-ac44-356827a5212c', id: 'a1c5d73a-8631-44e5-ac44-356827a5212c',
quantity: 0.5614682, quantity: 0.5614682,
symbol: 'BTCUSD', symbolProfileId: 'fdc42ea6-1321-44f5-9fb0-d7f1f2cf9b1e', // BTCUSD
symbolProfileId: 'fdc42ea6-1321-44f5-9fb0-d7f1f2cf9b1e',
type: Type.BUY, type: Type.BUY,
unitPrice: 3562.089535970158, unitPrice: 3562.089535970158,
userId: userDemo.id userId: userDemo.id
@ -222,8 +220,7 @@ async function main() {
fee: 80.79, fee: 80.79,
id: '71c08e2a-4a86-44ae-a890-c337de5d5f9b', id: '71c08e2a-4a86-44ae-a890-c337de5d5f9b',
quantity: 5, quantity: 5,
symbol: 'AMZN', symbolProfileId: '2bd26362-136e-411c-b578-334084b4cdcc', // AMZN
symbolProfileId: '2bd26362-136e-411c-b578-334084b4cdcc',
type: Type.BUY, type: Type.BUY,
unitPrice: 2021.99, unitPrice: 2021.99,
userId: userDemo.id userId: userDemo.id
@ -235,8 +232,7 @@ async function main() {
fee: 19.9, fee: 19.9,
id: '385f2c2c-d53e-4937-b0e5-e92ef6020d4e', id: '385f2c2c-d53e-4937-b0e5-e92ef6020d4e',
quantity: 10, quantity: 10,
symbol: 'VTI', symbolProfileId: '7d9c8540-061e-4e7e-b019-0d0f4a84e796', // VTI
symbolProfileId: '7d9c8540-061e-4e7e-b019-0d0f4a84e796',
type: Type.BUY, type: Type.BUY,
unitPrice: 144.38, unitPrice: 144.38,
userId: userDemo.id userId: userDemo.id
@ -248,8 +244,7 @@ async function main() {
fee: 19.9, fee: 19.9,
id: '185f2c2c-d53e-4937-b0e5-a93ef6020d4e', id: '185f2c2c-d53e-4937-b0e5-a93ef6020d4e',
quantity: 10, quantity: 10,
symbol: 'VTI', symbolProfileId: '7d9c8540-061e-4e7e-b019-0d0f4a84e796', // VTI
symbolProfileId: '7d9c8540-061e-4e7e-b019-0d0f4a84e796',
type: Type.BUY, type: Type.BUY,
unitPrice: 147.99, unitPrice: 147.99,
userId: userDemo.id userId: userDemo.id
@ -261,8 +256,7 @@ async function main() {
fee: 19.9, fee: 19.9,
id: '347b0430-a84f-4031-a0f9-390399066ad6', id: '347b0430-a84f-4031-a0f9-390399066ad6',
quantity: 10, quantity: 10,
symbol: 'VTI', symbolProfileId: '7d9c8540-061e-4e7e-b019-0d0f4a84e796', // VTI
symbolProfileId: '7d9c8540-061e-4e7e-b019-0d0f4a84e796',
type: Type.BUY, type: Type.BUY,
unitPrice: 151.41, unitPrice: 151.41,
userId: userDemo.id userId: userDemo.id
@ -274,8 +268,7 @@ async function main() {
fee: 19.9, fee: 19.9,
id: '67ec3f47-3189-4b63-ba05-60d3a06b302f', id: '67ec3f47-3189-4b63-ba05-60d3a06b302f',
quantity: 10, quantity: 10,
symbol: 'VTI', symbolProfileId: '7d9c8540-061e-4e7e-b019-0d0f4a84e796', // VTI
symbolProfileId: '7d9c8540-061e-4e7e-b019-0d0f4a84e796',
type: Type.BUY, type: Type.BUY,
unitPrice: 177.69, unitPrice: 177.69,
userId: userDemo.id userId: userDemo.id
@ -287,8 +280,7 @@ async function main() {
fee: 19.9, fee: 19.9,
id: 'd01c6fbc-fa8d-47e6-8e80-66f882d2bfd2', id: 'd01c6fbc-fa8d-47e6-8e80-66f882d2bfd2',
quantity: 10, quantity: 10,
symbol: 'VTI', symbolProfileId: '7d9c8540-061e-4e7e-b019-0d0f4a84e796', // VTI
symbolProfileId: '7d9c8540-061e-4e7e-b019-0d0f4a84e796',
type: Type.BUY, type: Type.BUY,
unitPrice: 203.15, unitPrice: 203.15,
userId: userDemo.id userId: userDemo.id

Loading…
Cancel
Save