From d276567a1818f26253df6401f8a28893ab759e06 Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Sat, 3 Jul 2021 10:40:03 +0200 Subject: [PATCH] Eliminate isDraft from schema --- apps/api/src/app/experimental/experimental.service.ts | 1 - apps/api/src/app/order/order.controller.ts | 8 +------- apps/api/src/app/order/order.service.ts | 5 +++-- apps/api/src/app/portfolio/portfolio.service.ts | 2 +- apps/api/src/models/order.ts | 7 +++---- apps/api/src/models/portfolio.spec.ts | 8 -------- apps/api/src/models/portfolio.ts | 3 --- apps/api/src/services/data-gathering.service.ts | 9 +++++++-- apps/api/src/services/interfaces/interfaces.ts | 1 - .../migration.sql | 2 -- prisma/schema.prisma | 1 - 11 files changed, 15 insertions(+), 32 deletions(-) delete mode 100644 prisma/migrations/20210627204133_added_is_draft_to_orders/migration.sql diff --git a/apps/api/src/app/experimental/experimental.service.ts b/apps/api/src/app/experimental/experimental.service.ts index a7bf50bce..b0d41b867 100644 --- a/apps/api/src/app/experimental/experimental.service.ts +++ b/apps/api/src/app/experimental/experimental.service.ts @@ -43,7 +43,6 @@ export class ExperimentalService { date: parseISO(order.date), fee: 0, id: undefined, - isDraft: false, platformId: undefined, symbolProfileId: undefined, type: Type.BUY, diff --git a/apps/api/src/app/order/order.controller.ts b/apps/api/src/app/order/order.controller.ts index 8aff10fde..5671a5c9f 100644 --- a/apps/api/src/app/order/order.controller.ts +++ b/apps/api/src/app/order/order.controller.ts @@ -22,7 +22,7 @@ import { import { REQUEST } from '@nestjs/core'; import { AuthGuard } from '@nestjs/passport'; import { Order as OrderModel } from '@prisma/client'; -import { endOfToday, isAfter, parseISO } from 'date-fns'; +import { parseISO } from 'date-fns'; import { StatusCodes, getReasonPhrase } from 'http-status-codes'; import { CreateOrderDto } from './create-order.dto'; @@ -130,8 +130,6 @@ export class OrderController { const accountId = data.accountId; delete data.accountId; - const isDraft = isAfter(date, endOfToday()); - return this.orderService.createOrder( { ...data, @@ -141,7 +139,6 @@ export class OrderController { } }, date, - isDraft, SymbolProfile: { connectOrCreate: { where: { @@ -196,14 +193,11 @@ export class OrderController { const accountId = data.accountId; delete data.accountId; - const isDraft = isAfter(date, endOfToday()); - return this.orderService.updateOrder( { data: { ...data, date, - isDraft, Account: { connect: { id_userId: { id: accountId, userId: this.request.user.id } diff --git a/apps/api/src/app/order/order.service.ts b/apps/api/src/app/order/order.service.ts index 49dabec91..93fd8a706 100644 --- a/apps/api/src/app/order/order.service.ts +++ b/apps/api/src/app/order/order.service.ts @@ -3,6 +3,7 @@ import { PrismaService } from '@ghostfolio/api/services/prisma.service'; import { OrderWithAccount } from '@ghostfolio/common/types'; import { Injectable } from '@nestjs/common'; import { DataSource, Order, Prisma } from '@prisma/client'; +import { endOfToday, isAfter } from 'date-fns'; import { CacheService } from '../cache/cache.service'; import { RedisCacheService } from '../redis-cache/redis-cache.service'; @@ -50,8 +51,8 @@ export class OrderService { ): Promise { this.redisCacheService.remove(`${aUserId}.portfolio`); - if (!data.isDraft) { - // Gather symbol data of order in the background + if (!isAfter(data.date as Date, endOfToday())) { + // Gather symbol data of order in the background, if not draft this.dataGatheringService.gatherSymbols([ { dataSource: data.dataSource, diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index 7c922c16b..2d4081337 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -332,7 +332,7 @@ export class PortfolioService { const historicalDataArray: HistoricalDataItem[] = []; - for (const [date, { marketPrice, performance }] of Object.entries( + for (const [date, { marketPrice }] of Object.entries( historicalData[aSymbol] ).reverse()) { historicalDataArray.push({ diff --git a/apps/api/src/models/order.ts b/apps/api/src/models/order.ts index c08febcd4..621e07de8 100644 --- a/apps/api/src/models/order.ts +++ b/apps/api/src/models/order.ts @@ -1,4 +1,5 @@ -import { Account, Currency, Platform, SymbolProfile } from '@prisma/client'; +import { Account, Currency, SymbolProfile } from '@prisma/client'; +import { endOfToday, isAfter, parseISO } from 'date-fns'; import { v4 as uuidv4 } from 'uuid'; import { IOrder } from '../services/interfaces/interfaces'; @@ -10,7 +11,6 @@ export class Order { private fee: number; private date: string; private id: string; - private isDraft: boolean; private quantity: number; private symbol: string; private symbolProfile: SymbolProfile; @@ -24,7 +24,6 @@ export class Order { this.fee = data.fee; this.date = data.date; this.id = data.id || uuidv4(); - this.isDraft = data.isDraft ?? false; this.quantity = data.quantity; this.symbol = data.symbol; this.symbolProfile = data.symbolProfile; @@ -55,7 +54,7 @@ export class Order { } public getIsDraft() { - return this.isDraft; + return isAfter(parseISO(this.date), endOfToday()); } public getQuantity() { diff --git a/apps/api/src/models/portfolio.spec.ts b/apps/api/src/models/portfolio.spec.ts index 0a2bf42b1..b37560160 100644 --- a/apps/api/src/models/portfolio.spec.ts +++ b/apps/api/src/models/portfolio.spec.ts @@ -188,7 +188,6 @@ describe('Portfolio', () => { fee: 0, date: new Date(), id: '8d999347-dee2-46ee-88e1-26b344e71fcc', - isDraft: false, quantity: 1, symbol: 'BTCUSD', symbolProfileId: null, @@ -292,7 +291,6 @@ describe('Portfolio', () => { fee: 0, date: new Date(getUtc('2018-01-05')), id: '4a5a5c6e-659d-45cc-9fd4-fd6c873b50fb', - isDraft: false, quantity: 0.2, symbol: 'ETHUSD', symbolProfileId: null, @@ -390,7 +388,6 @@ describe('Portfolio', () => { fee: 0, date: new Date(getUtc('2018-01-05')), id: '4a5a5c6e-659d-45cc-9fd4-fd6c873b50fb', - isDraft: false, quantity: 0.2, symbol: 'ETHUSD', symbolProfileId: null, @@ -470,7 +467,6 @@ describe('Portfolio', () => { date: new Date(getUtc('2017-08-16')), fee: 2.99, id: 'd96795b2-6ae6-420e-aa21-fabe5e45d475', - isDraft: false, quantity: 0.05614682, symbol: 'BTCUSD', symbolProfileId: null, @@ -488,7 +484,6 @@ describe('Portfolio', () => { fee: 2.99, date: new Date(getUtc('2018-01-05')), id: '4a5a5c6e-659d-45cc-9fd4-fd6c873b50fb', - isDraft: false, quantity: 0.2, symbol: 'ETHUSD', symbolProfileId: null, @@ -563,7 +558,6 @@ describe('Portfolio', () => { fee: 1.0, date: new Date(getUtc('2018-01-05')), id: '4a5a5c6e-659d-45cc-9fd4-fd6c873b50fb', - isDraft: false, quantity: 0.2, symbol: 'ETHUSD', symbolProfileId: null, @@ -581,7 +575,6 @@ describe('Portfolio', () => { fee: 1.0, date: new Date(getUtc('2018-01-28')), id: '4a5a5c6e-659d-45cc-9fd4-fd6c873b50fc', - isDraft: false, quantity: 0.1, symbol: 'ETHUSD', symbolProfileId: null, @@ -599,7 +592,6 @@ describe('Portfolio', () => { fee: 1.0, date: new Date(getUtc('2018-01-31')), id: '4a5a5c6e-659d-45cc-9fd4-fd6c873b50fc', - isDraft: false, quantity: 0.2, symbol: 'ETHUSD', symbolProfileId: null, diff --git a/apps/api/src/models/portfolio.ts b/apps/api/src/models/portfolio.ts index 9d0057dac..4ddca5696 100644 --- a/apps/api/src/models/portfolio.ts +++ b/apps/api/src/models/portfolio.ts @@ -164,7 +164,6 @@ export class Portfolio implements PortfolioInterface { fee, date, id, - isDraft, quantity, symbol, symbolProfile, @@ -178,7 +177,6 @@ export class Portfolio implements PortfolioInterface { fee, date, id, - isDraft, quantity, symbol, symbolProfile, @@ -630,7 +628,6 @@ export class Portfolio implements PortfolioInterface { currency: order.currency, date: order.date.toISOString(), fee: order.fee, - isDraft: order.isDraft, quantity: order.quantity, symbol: order.symbol, symbolProfile: order.SymbolProfile, diff --git a/apps/api/src/services/data-gathering.service.ts b/apps/api/src/services/data-gathering.service.ts index 530fdb13a..5b1b35435 100644 --- a/apps/api/src/services/data-gathering.service.ts +++ b/apps/api/src/services/data-gathering.service.ts @@ -8,6 +8,7 @@ import { Injectable } from '@nestjs/common'; import { DataSource } from '@prisma/client'; import { differenceInHours, + endOfToday, format, getDate, getMonth, @@ -227,7 +228,9 @@ export class DataGatheringService { orderBy: [{ symbol: 'asc' }], select: { dataSource: true, symbol: true }, where: { - isDraft: false + date: { + lt: endOfToday() // no draft + } } }); @@ -286,7 +289,9 @@ export class DataGatheringService { orderBy: [{ date: 'asc' }], select: { dataSource: true, date: true, symbol: true }, where: { - isDraft: false + date: { + lt: endOfToday() // no draft + } } }); diff --git a/apps/api/src/services/interfaces/interfaces.ts b/apps/api/src/services/interfaces/interfaces.ts index 0cd3f1034..b909c7e78 100644 --- a/apps/api/src/services/interfaces/interfaces.ts +++ b/apps/api/src/services/interfaces/interfaces.ts @@ -22,7 +22,6 @@ export interface IOrder { date: string; fee: number; id?: string; - isDraft: boolean; quantity: number; symbol: string; symbolProfile: SymbolProfile; diff --git a/prisma/migrations/20210627204133_added_is_draft_to_orders/migration.sql b/prisma/migrations/20210627204133_added_is_draft_to_orders/migration.sql deleted file mode 100644 index 51970ec3c..000000000 --- a/prisma/migrations/20210627204133_added_is_draft_to_orders/migration.sql +++ /dev/null @@ -1,2 +0,0 @@ --- AlterTable -ALTER TABLE "Order" ADD COLUMN "isDraft" BOOLEAN NOT NULL DEFAULT false; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 88e023f69..85b89c75e 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -79,7 +79,6 @@ model Order { date DateTime fee Float id String @default(uuid()) - isDraft Boolean @default(false) quantity Float symbol String SymbolProfile SymbolProfile? @relation(fields: [symbolProfileId], references: [id])