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. 51
      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({
fee,
quantity,
symbol,
type,
unitPrice,
userId,
@ -114,7 +113,7 @@ export class ImportService {
isSameDay(order.date, parseISO(<string>(<unknown>date))) &&
order.fee === fee &&
order.quantity === quantity &&
order.symbol === symbol &&
order.SymbolProfile.symbol === symbol &&
order.type === type &&
order.unitPrice === unitPrice
);

51
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<Order> {
@ -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<Order> {
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: <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: <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,

7
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') {

7
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') {

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

@ -549,24 +549,22 @@ export class DataGatheringService {
}
private async getSymbolsProfileData(): Promise<IDataGatheringItem[]> {
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
};
});
}

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();
}
if (this.data.activity?.symbol) {
if (this.data.activity?.SymbolProfile?.symbol) {
this.dataService
.fetchSymbolItem({
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";

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())
isDraft Boolean @default(false)
quantity Float
symbol String?
SymbolProfile SymbolProfile @relation(fields: [symbolProfileId], references: [id])
symbolProfileId String
type Type

24
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

Loading…
Cancel
Save