|
@ -28,9 +28,11 @@ import { Injectable } from '@nestjs/common'; |
|
|
import { DataSource, Prisma, SymbolProfile } from '@prisma/client'; |
|
|
import { DataSource, Prisma, SymbolProfile } from '@prisma/client'; |
|
|
import { Big } from 'big.js'; |
|
|
import { Big } from 'big.js'; |
|
|
import { endOfToday, isAfter, isSameSecond, parseISO } from 'date-fns'; |
|
|
import { endOfToday, isAfter, isSameSecond, parseISO } from 'date-fns'; |
|
|
import { uniqBy } from 'lodash'; |
|
|
import { omit, uniqBy } from 'lodash'; |
|
|
import { v4 as uuidv4 } from 'uuid'; |
|
|
import { v4 as uuidv4 } from 'uuid'; |
|
|
|
|
|
|
|
|
|
|
|
import { ImportDataDto } from './import-data.dto'; |
|
|
|
|
|
|
|
|
@Injectable() |
|
|
@Injectable() |
|
|
export class ImportService { |
|
|
export class ImportService { |
|
|
public constructor( |
|
|
public constructor( |
|
@ -138,14 +140,14 @@ export class ImportService { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async import({ |
|
|
public async import({ |
|
|
accountsDto, |
|
|
accountsWithBalancesDto, |
|
|
activitiesDto, |
|
|
activitiesDto, |
|
|
isDryRun = false, |
|
|
isDryRun = false, |
|
|
maxActivitiesToImport, |
|
|
maxActivitiesToImport, |
|
|
user |
|
|
user |
|
|
}: { |
|
|
}: { |
|
|
accountsDto: Partial<CreateAccountDto>[]; |
|
|
accountsWithBalancesDto: ImportDataDto['accounts']; |
|
|
activitiesDto: Partial<CreateOrderDto>[]; |
|
|
activitiesDto: ImportDataDto['activities']; |
|
|
isDryRun?: boolean; |
|
|
isDryRun?: boolean; |
|
|
maxActivitiesToImport: number; |
|
|
maxActivitiesToImport: number; |
|
|
user: UserWithSettings; |
|
|
user: UserWithSettings; |
|
@ -153,12 +155,12 @@ export class ImportService { |
|
|
const accountIdMapping: { [oldAccountId: string]: string } = {}; |
|
|
const accountIdMapping: { [oldAccountId: string]: string } = {}; |
|
|
const userCurrency = user.Settings.settings.baseCurrency; |
|
|
const userCurrency = user.Settings.settings.baseCurrency; |
|
|
|
|
|
|
|
|
if (!isDryRun && accountsDto?.length) { |
|
|
if (!isDryRun && accountsWithBalancesDto?.length) { |
|
|
const [existingAccounts, existingPlatforms] = await Promise.all([ |
|
|
const [existingAccounts, existingPlatforms] = await Promise.all([ |
|
|
this.accountService.accounts({ |
|
|
this.accountService.accounts({ |
|
|
where: { |
|
|
where: { |
|
|
id: { |
|
|
id: { |
|
|
in: accountsDto.map(({ id }) => { |
|
|
in: accountsWithBalancesDto.map(({ id }) => { |
|
|
return id; |
|
|
return id; |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
@ -167,14 +169,19 @@ export class ImportService { |
|
|
this.platformService.getPlatforms() |
|
|
this.platformService.getPlatforms() |
|
|
]); |
|
|
]); |
|
|
|
|
|
|
|
|
for (const account of accountsDto) { |
|
|
for (const accountWithBalances of accountsWithBalancesDto) { |
|
|
// Check if there is any existing account with the same ID
|
|
|
// Check if there is any existing account with the same ID
|
|
|
const accountWithSameId = existingAccounts.find((existingAccount) => { |
|
|
const accountWithSameId = existingAccounts.find((existingAccount) => { |
|
|
return existingAccount.id === account.id; |
|
|
return existingAccount.id === accountWithBalances.id; |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
// If there is no account or if the account belongs to a different user then create a new account
|
|
|
// If there is no account or if the account belongs to a different user then create a new account
|
|
|
if (!accountWithSameId || accountWithSameId.userId !== user.id) { |
|
|
if (!accountWithSameId || accountWithSameId.userId !== user.id) { |
|
|
|
|
|
const account: CreateAccountDto = omit( |
|
|
|
|
|
accountWithBalances, |
|
|
|
|
|
'balances' |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
let oldAccountId: string; |
|
|
let oldAccountId: string; |
|
|
const platformId = account.platformId; |
|
|
const platformId = account.platformId; |
|
|
|
|
|
|
|
@ -187,6 +194,9 @@ export class ImportService { |
|
|
|
|
|
|
|
|
let accountObject: Prisma.AccountCreateInput = { |
|
|
let accountObject: Prisma.AccountCreateInput = { |
|
|
...account, |
|
|
...account, |
|
|
|
|
|
balances: { |
|
|
|
|
|
create: accountWithBalances.balances ?? [] |
|
|
|
|
|
}, |
|
|
User: { connect: { id: user.id } } |
|
|
User: { connect: { id: user.id } } |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
@ -251,7 +261,7 @@ export class ImportService { |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
if (isDryRun) { |
|
|
if (isDryRun) { |
|
|
accountsDto.forEach(({ id, name }) => { |
|
|
accountsWithBalancesDto.forEach(({ id, name }) => { |
|
|
accounts.push({ id, name }); |
|
|
accounts.push({ id, name }); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|