Browse Source

Eliminate currency from order model

pull/730/head
Thomas 3 years ago
parent
commit
f3e562cb58
  1. 4
      apps/api/src/app/export/export.service.ts
  2. 4
      apps/api/src/app/import/import.service.ts
  3. 1
      apps/api/src/app/order/order.controller.ts
  4. 8
      apps/api/src/app/order/order.service.ts
  5. 18
      apps/api/src/app/portfolio/portfolio.service-new.ts
  6. 18
      apps/api/src/app/portfolio/portfolio.service.ts
  7. 1
      prisma/migrations/20220302191841_removed_currency_from_order/migration.sql
  8. 8
      prisma/seed.js

4
apps/api/src/app/export/export.service.ts

@ -18,7 +18,6 @@ export class ExportService {
orderBy: { date: 'desc' }, orderBy: { date: 'desc' },
select: { select: {
accountId: true, accountId: true,
currency: true,
date: true, date: true,
fee: true, fee: true,
id: true, id: true,
@ -41,7 +40,6 @@ export class ExportService {
orders: orders.map( orders: orders.map(
({ ({
accountId, accountId,
currency,
date, date,
fee, fee,
quantity, quantity,
@ -51,12 +49,12 @@ export class ExportService {
}) => { }) => {
return { return {
accountId, accountId,
currency,
date, date,
fee, fee,
quantity, quantity,
type, type,
unitPrice, unitPrice,
currency: SymbolProfile.currency,
dataSource: SymbolProfile.dataSource, dataSource: SymbolProfile.dataSource,
symbol: type === 'ITEM' ? SymbolProfile.name : SymbolProfile.symbol symbol: type === 'ITEM' ? SymbolProfile.name : SymbolProfile.symbol
}; };

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

@ -52,7 +52,6 @@ export class ImportService {
unitPrice unitPrice
} of orders) { } of orders) {
await this.orderService.createOrder({ await this.orderService.createOrder({
currency,
fee, fee,
quantity, quantity,
symbol, symbol,
@ -64,6 +63,7 @@ export class ImportService {
SymbolProfile: { SymbolProfile: {
connectOrCreate: { connectOrCreate: {
create: { create: {
currency,
dataSource, dataSource,
symbol symbol
}, },
@ -109,7 +109,7 @@ export class ImportService {
] of orders.entries()) { ] of orders.entries()) {
const duplicateOrder = existingOrders.find((order) => { const duplicateOrder = existingOrders.find((order) => {
return ( return (
order.currency === currency && order.SymbolProfile.currency === currency &&
order.SymbolProfile.dataSource === dataSource && order.SymbolProfile.dataSource === dataSource &&
isSameDay(order.date, parseISO(<string>(<unknown>date))) && isSameDay(order.date, parseISO(<string>(<unknown>date))) &&
order.fee === fee && order.fee === fee &&

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

@ -114,6 +114,7 @@ export class OrderController {
SymbolProfile: { SymbolProfile: {
connectOrCreate: { connectOrCreate: {
create: { create: {
currency: data.currency,
dataSource: data.dataSource, dataSource: data.dataSource,
symbol: data.symbol symbol: data.symbol
}, },

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

@ -55,6 +55,7 @@ export class OrderService {
public async createOrder( public async createOrder(
data: Prisma.OrderCreateInput & { data: Prisma.OrderCreateInput & {
accountId?: string; accountId?: string;
currency?: string;
dataSource?: DataSource; dataSource?: DataSource;
userId: string; userId: string;
} }
@ -75,7 +76,7 @@ export class OrderService {
}; };
if (data.type === 'ITEM') { if (data.type === 'ITEM') {
const currency = data.currency; const currency = data.SymbolProfile.connectOrCreate.create.currency;
const dataSource: DataSource = 'MANUAL'; const dataSource: DataSource = 'MANUAL';
const id = uuidv4(); const id = uuidv4();
const name = data.SymbolProfile.connectOrCreate.create.symbol; const name = data.SymbolProfile.connectOrCreate.create.symbol;
@ -119,6 +120,7 @@ export class OrderService {
await this.cacheService.flush(); await this.cacheService.flush();
delete data.accountId; delete data.accountId;
delete data.currency;
delete data.dataSource; delete data.dataSource;
delete data.userId; delete data.userId;
@ -197,12 +199,12 @@ export class OrderService {
value, value,
feeInBaseCurrency: this.exchangeRateDataService.toCurrency( feeInBaseCurrency: this.exchangeRateDataService.toCurrency(
order.fee, order.fee,
order.currency, order.SymbolProfile.currency,
userCurrency userCurrency
), ),
valueInBaseCurrency: this.exchangeRateDataService.toCurrency( valueInBaseCurrency: this.exchangeRateDataService.toCurrency(
value, value,
order.currency, order.SymbolProfile.currency,
userCurrency userCurrency
) )
}; };

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

@ -450,7 +450,7 @@ export class PortfolioServiceNew {
}; };
} }
const positionCurrency = orders[0].currency; const positionCurrency = orders[0].SymbolProfile.currency;
const [SymbolProfile] = await this.symbolProfileService.getSymbolProfiles([ const [SymbolProfile] = await this.symbolProfileService.getSymbolProfiles([
aSymbol aSymbol
]); ]);
@ -460,7 +460,7 @@ export class PortfolioServiceNew {
return order.type === 'BUY' || order.type === 'SELL'; return order.type === 'BUY' || order.type === 'SELL';
}) })
.map((order) => ({ .map((order) => ({
currency: order.currency, currency: order.SymbolProfile.currency,
dataSource: order.SymbolProfile.dataSource, dataSource: order.SymbolProfile.dataSource,
date: format(order.date, DATE_FORMAT), date: format(order.date, DATE_FORMAT),
fee: new Big(order.fee), fee: new Big(order.fee),
@ -1023,7 +1023,7 @@ export class PortfolioServiceNew {
.map((order) => { .map((order) => {
return this.exchangeRateDataService.toCurrency( return this.exchangeRateDataService.toCurrency(
new Big(order.quantity).mul(order.unitPrice).toNumber(), new Big(order.quantity).mul(order.unitPrice).toNumber(),
order.currency, order.SymbolProfile.currency,
this.request.user.Settings.currency this.request.user.Settings.currency
); );
}) })
@ -1042,7 +1042,7 @@ export class PortfolioServiceNew {
.map((order) => { .map((order) => {
return this.exchangeRateDataService.toCurrency( return this.exchangeRateDataService.toCurrency(
order.fee, order.fee,
order.currency, order.SymbolProfile.currency,
this.request.user.Settings.currency this.request.user.Settings.currency
); );
}) })
@ -1064,7 +1064,7 @@ export class PortfolioServiceNew {
.map((order) => { .map((order) => {
return this.exchangeRateDataService.toCurrency( return this.exchangeRateDataService.toCurrency(
new Big(order.quantity).mul(order.unitPrice).toNumber(), new Big(order.quantity).mul(order.unitPrice).toNumber(),
order.currency, order.SymbolProfile.currency,
this.request.user.Settings.currency this.request.user.Settings.currency
); );
}) })
@ -1117,13 +1117,13 @@ export class PortfolioServiceNew {
} }
const portfolioOrders: PortfolioOrder[] = orders.map((order) => ({ const portfolioOrders: PortfolioOrder[] = orders.map((order) => ({
currency: order.currency, currency: order.SymbolProfile.currency,
dataSource: order.SymbolProfile.dataSource, dataSource: order.SymbolProfile.dataSource,
date: format(order.date, DATE_FORMAT), date: format(order.date, DATE_FORMAT),
fee: new Big( fee: new Big(
this.exchangeRateDataService.toCurrency( this.exchangeRateDataService.toCurrency(
order.fee, order.fee,
order.currency, order.SymbolProfile.currency,
userCurrency userCurrency
) )
), ),
@ -1134,7 +1134,7 @@ export class PortfolioServiceNew {
unitPrice: new Big( unitPrice: new Big(
this.exchangeRateDataService.toCurrency( this.exchangeRateDataService.toCurrency(
order.unitPrice, order.unitPrice,
order.currency, order.SymbolProfile.currency,
userCurrency userCurrency
) )
) )
@ -1230,7 +1230,7 @@ export class PortfolioServiceNew {
.map((order) => { .map((order) => {
return this.exchangeRateDataService.toCurrency( return this.exchangeRateDataService.toCurrency(
order.quantity * order.unitPrice, order.quantity * order.unitPrice,
order.currency, order.SymbolProfile.currency,
currency currency
); );
}) })

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

@ -438,7 +438,7 @@ export class PortfolioService {
}; };
} }
const positionCurrency = orders[0].currency; const positionCurrency = orders[0].SymbolProfile.currency;
const [SymbolProfile] = await this.symbolProfileService.getSymbolProfiles([ const [SymbolProfile] = await this.symbolProfileService.getSymbolProfiles([
aSymbol aSymbol
]); ]);
@ -448,7 +448,7 @@ export class PortfolioService {
return order.type === 'BUY' || order.type === 'SELL'; return order.type === 'BUY' || order.type === 'SELL';
}) })
.map((order) => ({ .map((order) => ({
currency: order.currency, currency: order.SymbolProfile.currency,
dataSource: order.SymbolProfile.dataSource, dataSource: order.SymbolProfile.dataSource,
date: format(order.date, DATE_FORMAT), date: format(order.date, DATE_FORMAT),
fee: new Big(order.fee), fee: new Big(order.fee),
@ -987,7 +987,7 @@ export class PortfolioService {
.map((order) => { .map((order) => {
return this.exchangeRateDataService.toCurrency( return this.exchangeRateDataService.toCurrency(
new Big(order.quantity).mul(order.unitPrice).toNumber(), new Big(order.quantity).mul(order.unitPrice).toNumber(),
order.currency, order.SymbolProfile.currency,
this.request.user.Settings.currency this.request.user.Settings.currency
); );
}) })
@ -1006,7 +1006,7 @@ export class PortfolioService {
.map((order) => { .map((order) => {
return this.exchangeRateDataService.toCurrency( return this.exchangeRateDataService.toCurrency(
order.fee, order.fee,
order.currency, order.SymbolProfile.currency,
this.request.user.Settings.currency this.request.user.Settings.currency
); );
}) })
@ -1028,7 +1028,7 @@ export class PortfolioService {
.map((order) => { .map((order) => {
return this.exchangeRateDataService.toCurrency( return this.exchangeRateDataService.toCurrency(
new Big(order.quantity).mul(order.unitPrice).toNumber(), new Big(order.quantity).mul(order.unitPrice).toNumber(),
order.currency, order.SymbolProfile.currency,
this.request.user.Settings.currency this.request.user.Settings.currency
); );
}) })
@ -1080,13 +1080,13 @@ export class PortfolioService {
} }
const portfolioOrders: PortfolioOrder[] = orders.map((order) => ({ const portfolioOrders: PortfolioOrder[] = orders.map((order) => ({
currency: order.currency, currency: order.SymbolProfile.currency,
dataSource: order.SymbolProfile.dataSource, dataSource: order.SymbolProfile.dataSource,
date: format(order.date, DATE_FORMAT), date: format(order.date, DATE_FORMAT),
fee: new Big( fee: new Big(
this.exchangeRateDataService.toCurrency( this.exchangeRateDataService.toCurrency(
order.fee, order.fee,
order.currency, order.SymbolProfile.currency,
userCurrency userCurrency
) )
), ),
@ -1097,7 +1097,7 @@ export class PortfolioService {
unitPrice: new Big( unitPrice: new Big(
this.exchangeRateDataService.toCurrency( this.exchangeRateDataService.toCurrency(
order.unitPrice, order.unitPrice,
order.currency, order.SymbolProfile.currency,
userCurrency userCurrency
) )
) )
@ -1189,7 +1189,7 @@ export class PortfolioService {
.map((order) => { .map((order) => {
return this.exchangeRateDataService.toCurrency( return this.exchangeRateDataService.toCurrency(
order.quantity * order.unitPrice, order.quantity * order.unitPrice,
order.currency, order.SymbolProfile.currency,
currency currency
); );
}) })

1
prisma/migrations/20220302191841_removed_currency_from_order/migration.sql

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

8
prisma/seed.js

@ -192,7 +192,6 @@ async function main() {
{ {
accountId: '65cfb79d-b6c7-4591-9d46-73426bc62094', accountId: '65cfb79d-b6c7-4591-9d46-73426bc62094',
accountUserId: userDemo.id, accountUserId: userDemo.id,
currency: 'USD',
date: new Date(Date.UTC(2017, 0, 3, 0, 0, 0)), date: new Date(Date.UTC(2017, 0, 3, 0, 0, 0)),
fee: 30, fee: 30,
id: 'cf7c0418-8535-4089-ae3d-5dbfa0aec2e1', id: 'cf7c0418-8535-4089-ae3d-5dbfa0aec2e1',
@ -206,7 +205,6 @@ async function main() {
{ {
accountId: 'd804de69-0429-42dc-b6ca-b308fd7dd926', accountId: 'd804de69-0429-42dc-b6ca-b308fd7dd926',
accountUserId: userDemo.id, accountUserId: userDemo.id,
currency: 'USD',
date: new Date(Date.UTC(2017, 7, 16, 0, 0, 0)), date: new Date(Date.UTC(2017, 7, 16, 0, 0, 0)),
fee: 29.9, fee: 29.9,
id: 'a1c5d73a-8631-44e5-ac44-356827a5212c', id: 'a1c5d73a-8631-44e5-ac44-356827a5212c',
@ -220,7 +218,6 @@ async function main() {
{ {
accountId: '480269ce-e12a-4fd1-ac88-c4b0ff3f899c', accountId: '480269ce-e12a-4fd1-ac88-c4b0ff3f899c',
accountUserId: userDemo.id, accountUserId: userDemo.id,
currency: 'USD',
date: new Date(Date.UTC(2018, 9, 1, 0, 0, 0)), date: new Date(Date.UTC(2018, 9, 1, 0, 0, 0)),
fee: 80.79, fee: 80.79,
id: '71c08e2a-4a86-44ae-a890-c337de5d5f9b', id: '71c08e2a-4a86-44ae-a890-c337de5d5f9b',
@ -234,7 +231,6 @@ async function main() {
{ {
accountId: '480269ce-e12a-4fd1-ac88-c4b0ff3f899c', accountId: '480269ce-e12a-4fd1-ac88-c4b0ff3f899c',
accountUserId: userDemo.id, accountUserId: userDemo.id,
currency: 'USD',
date: new Date(Date.UTC(2019, 2, 1, 0, 0, 0)), date: new Date(Date.UTC(2019, 2, 1, 0, 0, 0)),
fee: 19.9, fee: 19.9,
id: '385f2c2c-d53e-4937-b0e5-e92ef6020d4e', id: '385f2c2c-d53e-4937-b0e5-e92ef6020d4e',
@ -248,7 +244,6 @@ async function main() {
{ {
accountId: '480269ce-e12a-4fd1-ac88-c4b0ff3f899c', accountId: '480269ce-e12a-4fd1-ac88-c4b0ff3f899c',
accountUserId: userDemo.id, accountUserId: userDemo.id,
currency: 'USD',
date: new Date(Date.UTC(2019, 8, 3, 0, 0, 0)), date: new Date(Date.UTC(2019, 8, 3, 0, 0, 0)),
fee: 19.9, fee: 19.9,
id: '185f2c2c-d53e-4937-b0e5-a93ef6020d4e', id: '185f2c2c-d53e-4937-b0e5-a93ef6020d4e',
@ -262,7 +257,6 @@ async function main() {
{ {
accountId: '480269ce-e12a-4fd1-ac88-c4b0ff3f899c', accountId: '480269ce-e12a-4fd1-ac88-c4b0ff3f899c',
accountUserId: userDemo.id, accountUserId: userDemo.id,
currency: 'USD',
date: new Date(Date.UTC(2020, 2, 2, 0, 0, 0)), date: new Date(Date.UTC(2020, 2, 2, 0, 0, 0)),
fee: 19.9, fee: 19.9,
id: '347b0430-a84f-4031-a0f9-390399066ad6', id: '347b0430-a84f-4031-a0f9-390399066ad6',
@ -276,7 +270,6 @@ async function main() {
{ {
accountId: '480269ce-e12a-4fd1-ac88-c4b0ff3f899c', accountId: '480269ce-e12a-4fd1-ac88-c4b0ff3f899c',
accountUserId: userDemo.id, accountUserId: userDemo.id,
currency: 'USD',
date: new Date(Date.UTC(2020, 8, 1, 0, 0, 0)), date: new Date(Date.UTC(2020, 8, 1, 0, 0, 0)),
fee: 19.9, fee: 19.9,
id: '67ec3f47-3189-4b63-ba05-60d3a06b302f', id: '67ec3f47-3189-4b63-ba05-60d3a06b302f',
@ -290,7 +283,6 @@ async function main() {
{ {
accountId: '480269ce-e12a-4fd1-ac88-c4b0ff3f899c', accountId: '480269ce-e12a-4fd1-ac88-c4b0ff3f899c',
accountUserId: userDemo.id, accountUserId: userDemo.id,
currency: 'USD',
date: new Date(Date.UTC(2020, 2, 1, 0, 0, 0)), date: new Date(Date.UTC(2020, 2, 1, 0, 0, 0)),
fee: 19.9, fee: 19.9,
id: 'd01c6fbc-fa8d-47e6-8e80-66f882d2bfd2', id: 'd01c6fbc-fa8d-47e6-8e80-66f882d2bfd2',

Loading…
Cancel
Save