Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class BuyCryptoService {

async update(id: number, dto: UpdateBuyCryptoDto): Promise<BuyCrypto> {
let entity = await this.buyCryptoRepo.findOne(id, {
relations: ['buy', 'buy.user', 'cryptoRoute', 'cryptoRoute.user'],
relations: ['buy', 'buy.user', 'cryptoRoute', 'cryptoRoute.user', 'bankTx'],
});
if (!entity) throw new NotFoundException('Buy crypto not found');

Expand All @@ -91,12 +91,14 @@ export class BuyCryptoService {
if (dto.buyId) {
if (!entity.buy) throw new BadRequestException(`Cannot assign BuyCrypto ${id} to a buy route`);
update.buy = await this.getBuy(dto.buyId);
if (entity.bankTx) await this.bankTxRepo.setNewUpdateTime(entity.bankTx.id);
Comment thread
Yannick1712 marked this conversation as resolved.
}

// crypto route
if (dto.cryptoRouteId) {
if (!entity.cryptoRoute) throw new BadRequestException(`Cannot assign BuyCrypto ${id} to a crypto route`);
update.cryptoRoute = await this.getCryptoRoute(dto.cryptoRouteId);
if (entity.bankTx) await this.bankTxRepo.setNewUpdateTime(entity.bankTx.id);
}

Util.removeNullFields(entity);
Expand Down
35 changes: 33 additions & 2 deletions src/user/models/user-data/user-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { UserDataRepository } from './user-data.repository';
import { KycInProgress, KycState, UserData } from './user-data.entity';
import { BankDataRepository } from 'src/user/models/bank-data/bank-data.repository';
import { CountryService } from 'src/shared/models/country/country.service';
import { MoreThan, Not } from 'typeorm';
import { getRepository, MoreThan, Not } from 'typeorm';
import { UpdateUserDto } from '../user/dto/update-user.dto';
import { LanguageService } from 'src/shared/models/language/language.service';
import { FiatService } from 'src/shared/models/fiat/fiat.service';
Expand All @@ -15,6 +15,7 @@ import { SpiderApiService } from 'src/user/services/spider/spider-api.service';
import { Util } from 'src/shared/util';
import { Cron, CronExpression } from '@nestjs/schedule';
import { KycProcessService } from '../kyc/kyc-process.service';
import { BankTx } from 'src/payment/models/bank-tx/bank-tx.entity';

@Injectable()
export class UserDataService {
Expand Down Expand Up @@ -185,14 +186,19 @@ export class UserDataService {
async mergeUserData(masterId: number, slaveId: number): Promise<void> {
const [master, slave] = await Promise.all([
this.userDataRepo.findOne({ where: { id: masterId }, relations: ['users', 'bankDatas'] }),
this.userDataRepo.findOne({ where: { id: slaveId }, relations: ['users', 'bankDatas'] }),
this.userDataRepo.findOne({
where: { id: slaveId },
relations: ['users', 'bankDatas'],
}),
]);
console.log(
`Merging user ${master.id} (master) and ${slave.id} (slave): reassigning bank datas ${slave.bankDatas
.map((b) => b.id)
.join(', ')} and users ${slave.users.map((u) => u.id).join(', ')}`,
);

await this.updateBankTxTime(slave.id);

// reassign bank datas and users
master.bankDatas = master.bankDatas.concat(slave.bankDatas);
master.users = master.users.concat(slave.users);
Expand All @@ -213,4 +219,29 @@ export class UserDataService {

return idList;
}

private async updateBankTxTime(userDataId: number): Promise<void> {
const txList = await getRepository(BankTx).find({
select: ['id'],
where: [
{ buyCrypto: { buy: { user: { userData: { id: userDataId } } } } },
{ buyFiat: { sell: { user: { userData: { id: userDataId } } } } },
],
relations: [
'buyCrypto',
'buyCrypto.buy',
'buyCrypto.buy.user',
'buyCrypto.buy.user.userData',
'buyFiat',
'buyFiat.sell',
'buyFiat.sell.user',
'buyFiat.sell.user.userData',
],
});

getRepository(BankTx).update(
txList.map((tx) => tx.id),
{ updated: new Date() },
);
}
}