Prepare for multi accounts support (#42)

pull/43/head
Thomas 3 years ago committed by GitHub
parent d268de3e12
commit 954224401d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Extended the support for feature flags to simplify the initial project setup
- Prepared for multi accounts support
### Changed

@ -116,7 +116,15 @@ export class UserService {
public async createUser(data?: Prisma.UserCreateInput): Promise<User> {
let user = await this.prisma.user.create({
data
data: {
...data,
Account: {
create: {
isDefault: true,
name: 'Default Account'
}
}
}
});
if (data.provider === Provider.ANONYMOUS) {

@ -2,7 +2,8 @@ import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
@Injectable()
export class PrismaService extends PrismaClient
export class PrismaService
extends PrismaClient
implements OnModuleInit, OnModuleDestroy {
async onModuleInit() {
await this.$connect();

@ -15,6 +15,9 @@
"angular": "node --max_old_space_size=32768 ./node_modules/@angular/cli/bin/ng",
"build:all": "ng build --prod api && ng build --prod client && yarn replace-placeholders-in-build",
"clean": "rimraf dist",
"database:format-schema": "prisma format",
"database:generate-typings": "prisma generate",
"database:gui": "prisma studio",
"database:push": "prisma db push --preview-feature",
"database:seed": "prisma db seed --preview-feature",
"dep-graph": "nx dep-graph",

@ -24,6 +24,22 @@ model Access {
@@id([id, userId])
}
model Account {
accountType AccountType @default(SECURITIES)
createdAt DateTime @default(now())
id String @default(uuid())
isDefault Boolean @default(false)
name String?
Order Order[]
Platform Platform? @relation(fields: [platformId], references: [id])
platformId String?
updatedAt DateTime @updatedAt
User User @relation(fields: [userId], references: [id])
userId String
@@id([id, userId])
}
model Analytics {
activityCount Int @default(0)
updatedAt DateTime @updatedAt
@ -43,6 +59,9 @@ model MarketData {
}
model Order {
Account Account? @relation(fields: [accountId, accountUserId], references: [id, userId])
accountId String?
accountUserId String?
createdAt DateTime @default(now())
currency Currency
date DateTime
@ -62,10 +81,11 @@ model Order {
}
model Platform {
Account Account[]
id String @id @default(uuid())
name String?
url String @unique
Order Order[]
url String @unique
}
model Property {
@ -84,6 +104,7 @@ model User {
Access Access[] @relation("accessGet")
AccessGive Access[] @relation(name: "accessGive")
accessToken String?
Account Account[]
alias String?
Analytics Analytics?
createdAt DateTime @default(now())
@ -96,6 +117,10 @@ model User {
updatedAt DateTime @updatedAt
}
enum AccountType {
SECURITIES
}
enum Currency {
CHF
EUR

@ -1,4 +1,10 @@
import { Currency, PrismaClient, Role, Type } from '@prisma/client';
import {
AccountType,
Currency,
PrismaClient,
Role,
Type
} from '@prisma/client';
const prisma = new PrismaClient();
async function main() {
@ -88,12 +94,42 @@ async function main() {
create: {
accessToken:
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjliMTEyYjRkLTNiN2QtNGJhZC05YmRkLTNiMGY3YjRkYWMyZiIsImlhdCI6MTYxODUxMjAxNCwiZXhwIjoxNjIxMTA0MDE0fQ.l3WUxpI0hxuQtdPrD0kd7sem6S2kx_7CrdNvkmlKuWw',
Account: {
create: [
{
accountType: AccountType.SECURITIES,
id: 'd804de69-0429-42dc-b6ca-b308fd7dd926',
name: 'Coinbase Account',
platformId: platformCoinbase.id
},
{
accountType: AccountType.SECURITIES,
id: '65cfb79d-b6c7-4591-9d46-73426bc62094',
name: 'DEGIRO Account',
platformId: platformDegiro.id
},
{
accountType: AccountType.SECURITIES,
id: '480269ce-e12a-4fd1-ac88-c4b0ff3f899c',
isDefault: true,
name: 'Interactive Brokers Account',
platformId: platformInteractiveBrokers.id
}
]
},
alias: 'Demo',
id: '9b112b4d-3b7d-4bad-9bdd-3b0f7b4dac2f',
role: Role.DEMO,
Order: {
create: [
role: Role.DEMO
},
update: {},
where: { id: '9b112b4d-3b7d-4bad-9bdd-3b0f7b4dac2f' }
});
await prisma.order.createMany({
data: [
{
accountId: '65cfb79d-b6c7-4591-9d46-73426bc62094',
accountUserId: userDemo.id,
currency: Currency.USD,
date: new Date(Date.UTC(2017, 0, 3, 0, 0, 0)),
fee: 30,
@ -102,9 +138,12 @@ async function main() {
quantity: 50,
symbol: 'TSLA',
type: Type.BUY,
unitPrice: 42.97
unitPrice: 42.97,
userId: userDemo.id
},
{
accountId: 'd804de69-0429-42dc-b6ca-b308fd7dd926',
accountUserId: userDemo.id,
currency: Currency.USD,
date: new Date(Date.UTC(2017, 7, 16, 0, 0, 0)),
fee: 29.9,
@ -113,9 +152,12 @@ async function main() {
quantity: 0.5614682,
symbol: 'BTCUSD',
type: Type.BUY,
unitPrice: 3562.089535970158
unitPrice: 3562.089535970158,
userId: userDemo.id
},
{
accountId: '480269ce-e12a-4fd1-ac88-c4b0ff3f899c',
accountUserId: userDemo.id,
currency: Currency.USD,
date: new Date(Date.UTC(2018, 9, 1, 0, 0, 0)),
fee: 80.79,
@ -124,9 +166,12 @@ async function main() {
quantity: 5,
symbol: 'AMZN',
type: Type.BUY,
unitPrice: 2021.99
unitPrice: 2021.99,
userId: userDemo.id
},
{
accountId: '480269ce-e12a-4fd1-ac88-c4b0ff3f899c',
accountUserId: userDemo.id,
currency: Currency.USD,
date: new Date(Date.UTC(2019, 2, 1, 0, 0, 0)),
fee: 19.9,
@ -135,9 +180,12 @@ async function main() {
quantity: 10,
symbol: 'VTI',
type: Type.BUY,
unitPrice: 144.38
unitPrice: 144.38,
userId: userDemo.id
},
{
accountId: '480269ce-e12a-4fd1-ac88-c4b0ff3f899c',
accountUserId: userDemo.id,
currency: Currency.USD,
date: new Date(Date.UTC(2019, 8, 3, 0, 0, 0)),
fee: 19.9,
@ -146,9 +194,12 @@ async function main() {
quantity: 10,
symbol: 'VTI',
type: Type.BUY,
unitPrice: 147.99
unitPrice: 147.99,
userId: userDemo.id
},
{
accountId: '480269ce-e12a-4fd1-ac88-c4b0ff3f899c',
accountUserId: userDemo.id,
currency: Currency.USD,
date: new Date(Date.UTC(2020, 2, 2, 0, 0, 0)),
fee: 19.9,
@ -157,9 +208,12 @@ async function main() {
quantity: 10,
symbol: 'VTI',
type: Type.BUY,
unitPrice: 151.41
unitPrice: 151.41,
userId: userDemo.id
},
{
accountId: '480269ce-e12a-4fd1-ac88-c4b0ff3f899c',
accountUserId: userDemo.id,
currency: Currency.USD,
date: new Date(Date.UTC(2020, 8, 1, 0, 0, 0)),
fee: 19.9,
@ -168,9 +222,12 @@ async function main() {
quantity: 10,
symbol: 'VTI',
type: Type.BUY,
unitPrice: 177.69
unitPrice: 177.69,
userId: userDemo.id
},
{
accountId: '480269ce-e12a-4fd1-ac88-c4b0ff3f899c',
accountUserId: userDemo.id,
currency: Currency.USD,
date: new Date(Date.UTC(2020, 2, 1, 0, 0, 0)),
fee: 19.9,
@ -179,13 +236,11 @@ async function main() {
quantity: 10,
symbol: 'VTI',
type: Type.BUY,
unitPrice: 203.15
unitPrice: 203.15,
userId: userDemo.id
}
]
}
},
update: {},
where: { id: '9b112b4d-3b7d-4bad-9bdd-3b0f7b4dac2f' }
],
skipDuplicates: true
});
console.log({

Loading…
Cancel
Save