Browse Source

Eliminate isDraft from schema

pull/187/head
Thomas 4 years ago
parent
commit
d276567a18
  1. 1
      apps/api/src/app/experimental/experimental.service.ts
  2. 8
      apps/api/src/app/order/order.controller.ts
  3. 5
      apps/api/src/app/order/order.service.ts
  4. 2
      apps/api/src/app/portfolio/portfolio.service.ts
  5. 7
      apps/api/src/models/order.ts
  6. 8
      apps/api/src/models/portfolio.spec.ts
  7. 3
      apps/api/src/models/portfolio.ts
  8. 9
      apps/api/src/services/data-gathering.service.ts
  9. 1
      apps/api/src/services/interfaces/interfaces.ts
  10. 2
      prisma/migrations/20210627204133_added_is_draft_to_orders/migration.sql
  11. 1
      prisma/schema.prisma

1
apps/api/src/app/experimental/experimental.service.ts

@ -43,7 +43,6 @@ export class ExperimentalService {
date: parseISO(order.date), date: parseISO(order.date),
fee: 0, fee: 0,
id: undefined, id: undefined,
isDraft: false,
platformId: undefined, platformId: undefined,
symbolProfileId: undefined, symbolProfileId: undefined,
type: Type.BUY, type: Type.BUY,

8
apps/api/src/app/order/order.controller.ts

@ -22,7 +22,7 @@ import {
import { REQUEST } from '@nestjs/core'; import { REQUEST } from '@nestjs/core';
import { AuthGuard } from '@nestjs/passport'; import { AuthGuard } from '@nestjs/passport';
import { Order as OrderModel } from '@prisma/client'; 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 { StatusCodes, getReasonPhrase } from 'http-status-codes';
import { CreateOrderDto } from './create-order.dto'; import { CreateOrderDto } from './create-order.dto';
@ -130,8 +130,6 @@ export class OrderController {
const accountId = data.accountId; const accountId = data.accountId;
delete data.accountId; delete data.accountId;
const isDraft = isAfter(date, endOfToday());
return this.orderService.createOrder( return this.orderService.createOrder(
{ {
...data, ...data,
@ -141,7 +139,6 @@ export class OrderController {
} }
}, },
date, date,
isDraft,
SymbolProfile: { SymbolProfile: {
connectOrCreate: { connectOrCreate: {
where: { where: {
@ -196,14 +193,11 @@ export class OrderController {
const accountId = data.accountId; const accountId = data.accountId;
delete data.accountId; delete data.accountId;
const isDraft = isAfter(date, endOfToday());
return this.orderService.updateOrder( return this.orderService.updateOrder(
{ {
data: { data: {
...data, ...data,
date, date,
isDraft,
Account: { Account: {
connect: { connect: {
id_userId: { id: accountId, userId: this.request.user.id } id_userId: { id: accountId, userId: this.request.user.id }

5
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 { OrderWithAccount } from '@ghostfolio/common/types';
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { DataSource, Order, Prisma } from '@prisma/client'; import { DataSource, Order, Prisma } from '@prisma/client';
import { endOfToday, isAfter } from 'date-fns';
import { CacheService } from '../cache/cache.service'; import { CacheService } from '../cache/cache.service';
import { RedisCacheService } from '../redis-cache/redis-cache.service'; import { RedisCacheService } from '../redis-cache/redis-cache.service';
@ -50,8 +51,8 @@ export class OrderService {
): Promise<Order> { ): Promise<Order> {
this.redisCacheService.remove(`${aUserId}.portfolio`); this.redisCacheService.remove(`${aUserId}.portfolio`);
if (!data.isDraft) { if (!isAfter(data.date as Date, endOfToday())) {
// Gather symbol data of order in the background // Gather symbol data of order in the background, if not draft
this.dataGatheringService.gatherSymbols([ this.dataGatheringService.gatherSymbols([
{ {
dataSource: data.dataSource, dataSource: data.dataSource,

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

@ -332,7 +332,7 @@ export class PortfolioService {
const historicalDataArray: HistoricalDataItem[] = []; const historicalDataArray: HistoricalDataItem[] = [];
for (const [date, { marketPrice, performance }] of Object.entries( for (const [date, { marketPrice }] of Object.entries(
historicalData[aSymbol] historicalData[aSymbol]
).reverse()) { ).reverse()) {
historicalDataArray.push({ historicalDataArray.push({

7
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 { v4 as uuidv4 } from 'uuid';
import { IOrder } from '../services/interfaces/interfaces'; import { IOrder } from '../services/interfaces/interfaces';
@ -10,7 +11,6 @@ export class Order {
private fee: number; private fee: number;
private date: string; private date: string;
private id: string; private id: string;
private isDraft: boolean;
private quantity: number; private quantity: number;
private symbol: string; private symbol: string;
private symbolProfile: SymbolProfile; private symbolProfile: SymbolProfile;
@ -24,7 +24,6 @@ export class Order {
this.fee = data.fee; this.fee = data.fee;
this.date = data.date; this.date = data.date;
this.id = data.id || uuidv4(); this.id = data.id || uuidv4();
this.isDraft = data.isDraft ?? false;
this.quantity = data.quantity; this.quantity = data.quantity;
this.symbol = data.symbol; this.symbol = data.symbol;
this.symbolProfile = data.symbolProfile; this.symbolProfile = data.symbolProfile;
@ -55,7 +54,7 @@ export class Order {
} }
public getIsDraft() { public getIsDraft() {
return this.isDraft; return isAfter(parseISO(this.date), endOfToday());
} }
public getQuantity() { public getQuantity() {

8
apps/api/src/models/portfolio.spec.ts

@ -188,7 +188,6 @@ describe('Portfolio', () => {
fee: 0, fee: 0,
date: new Date(), date: new Date(),
id: '8d999347-dee2-46ee-88e1-26b344e71fcc', id: '8d999347-dee2-46ee-88e1-26b344e71fcc',
isDraft: false,
quantity: 1, quantity: 1,
symbol: 'BTCUSD', symbol: 'BTCUSD',
symbolProfileId: null, symbolProfileId: null,
@ -292,7 +291,6 @@ describe('Portfolio', () => {
fee: 0, fee: 0,
date: new Date(getUtc('2018-01-05')), date: new Date(getUtc('2018-01-05')),
id: '4a5a5c6e-659d-45cc-9fd4-fd6c873b50fb', id: '4a5a5c6e-659d-45cc-9fd4-fd6c873b50fb',
isDraft: false,
quantity: 0.2, quantity: 0.2,
symbol: 'ETHUSD', symbol: 'ETHUSD',
symbolProfileId: null, symbolProfileId: null,
@ -390,7 +388,6 @@ describe('Portfolio', () => {
fee: 0, fee: 0,
date: new Date(getUtc('2018-01-05')), date: new Date(getUtc('2018-01-05')),
id: '4a5a5c6e-659d-45cc-9fd4-fd6c873b50fb', id: '4a5a5c6e-659d-45cc-9fd4-fd6c873b50fb',
isDraft: false,
quantity: 0.2, quantity: 0.2,
symbol: 'ETHUSD', symbol: 'ETHUSD',
symbolProfileId: null, symbolProfileId: null,
@ -470,7 +467,6 @@ describe('Portfolio', () => {
date: new Date(getUtc('2017-08-16')), date: new Date(getUtc('2017-08-16')),
fee: 2.99, fee: 2.99,
id: 'd96795b2-6ae6-420e-aa21-fabe5e45d475', id: 'd96795b2-6ae6-420e-aa21-fabe5e45d475',
isDraft: false,
quantity: 0.05614682, quantity: 0.05614682,
symbol: 'BTCUSD', symbol: 'BTCUSD',
symbolProfileId: null, symbolProfileId: null,
@ -488,7 +484,6 @@ describe('Portfolio', () => {
fee: 2.99, fee: 2.99,
date: new Date(getUtc('2018-01-05')), date: new Date(getUtc('2018-01-05')),
id: '4a5a5c6e-659d-45cc-9fd4-fd6c873b50fb', id: '4a5a5c6e-659d-45cc-9fd4-fd6c873b50fb',
isDraft: false,
quantity: 0.2, quantity: 0.2,
symbol: 'ETHUSD', symbol: 'ETHUSD',
symbolProfileId: null, symbolProfileId: null,
@ -563,7 +558,6 @@ describe('Portfolio', () => {
fee: 1.0, fee: 1.0,
date: new Date(getUtc('2018-01-05')), date: new Date(getUtc('2018-01-05')),
id: '4a5a5c6e-659d-45cc-9fd4-fd6c873b50fb', id: '4a5a5c6e-659d-45cc-9fd4-fd6c873b50fb',
isDraft: false,
quantity: 0.2, quantity: 0.2,
symbol: 'ETHUSD', symbol: 'ETHUSD',
symbolProfileId: null, symbolProfileId: null,
@ -581,7 +575,6 @@ describe('Portfolio', () => {
fee: 1.0, fee: 1.0,
date: new Date(getUtc('2018-01-28')), date: new Date(getUtc('2018-01-28')),
id: '4a5a5c6e-659d-45cc-9fd4-fd6c873b50fc', id: '4a5a5c6e-659d-45cc-9fd4-fd6c873b50fc',
isDraft: false,
quantity: 0.1, quantity: 0.1,
symbol: 'ETHUSD', symbol: 'ETHUSD',
symbolProfileId: null, symbolProfileId: null,
@ -599,7 +592,6 @@ describe('Portfolio', () => {
fee: 1.0, fee: 1.0,
date: new Date(getUtc('2018-01-31')), date: new Date(getUtc('2018-01-31')),
id: '4a5a5c6e-659d-45cc-9fd4-fd6c873b50fc', id: '4a5a5c6e-659d-45cc-9fd4-fd6c873b50fc',
isDraft: false,
quantity: 0.2, quantity: 0.2,
symbol: 'ETHUSD', symbol: 'ETHUSD',
symbolProfileId: null, symbolProfileId: null,

3
apps/api/src/models/portfolio.ts

@ -164,7 +164,6 @@ export class Portfolio implements PortfolioInterface {
fee, fee,
date, date,
id, id,
isDraft,
quantity, quantity,
symbol, symbol,
symbolProfile, symbolProfile,
@ -178,7 +177,6 @@ export class Portfolio implements PortfolioInterface {
fee, fee,
date, date,
id, id,
isDraft,
quantity, quantity,
symbol, symbol,
symbolProfile, symbolProfile,
@ -630,7 +628,6 @@ export class Portfolio implements PortfolioInterface {
currency: order.currency, currency: order.currency,
date: order.date.toISOString(), date: order.date.toISOString(),
fee: order.fee, fee: order.fee,
isDraft: order.isDraft,
quantity: order.quantity, quantity: order.quantity,
symbol: order.symbol, symbol: order.symbol,
symbolProfile: order.SymbolProfile, symbolProfile: order.SymbolProfile,

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

@ -8,6 +8,7 @@ import { Injectable } from '@nestjs/common';
import { DataSource } from '@prisma/client'; import { DataSource } from '@prisma/client';
import { import {
differenceInHours, differenceInHours,
endOfToday,
format, format,
getDate, getDate,
getMonth, getMonth,
@ -227,7 +228,9 @@ export class DataGatheringService {
orderBy: [{ symbol: 'asc' }], orderBy: [{ symbol: 'asc' }],
select: { dataSource: true, symbol: true }, select: { dataSource: true, symbol: true },
where: { where: {
isDraft: false date: {
lt: endOfToday() // no draft
}
} }
}); });
@ -286,7 +289,9 @@ export class DataGatheringService {
orderBy: [{ date: 'asc' }], orderBy: [{ date: 'asc' }],
select: { dataSource: true, date: true, symbol: true }, select: { dataSource: true, date: true, symbol: true },
where: { where: {
isDraft: false date: {
lt: endOfToday() // no draft
}
} }
}); });

1
apps/api/src/services/interfaces/interfaces.ts

@ -22,7 +22,6 @@ export interface IOrder {
date: string; date: string;
fee: number; fee: number;
id?: string; id?: string;
isDraft: boolean;
quantity: number; quantity: number;
symbol: string; symbol: string;
symbolProfile: SymbolProfile; symbolProfile: SymbolProfile;

2
prisma/migrations/20210627204133_added_is_draft_to_orders/migration.sql

@ -1,2 +0,0 @@
-- AlterTable
ALTER TABLE "Order" ADD COLUMN "isDraft" BOOLEAN NOT NULL DEFAULT false;

1
prisma/schema.prisma

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

Loading…
Cancel
Save