Skip to content

Commit

Permalink
[DEV-3493] userData in transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannick1712 committed Dec 22, 2024
1 parent cdbfdf3 commit 3117d30
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/shared/services/process.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export enum Process {
PAYMENT_CONFIRMATIONS = 'PaymentConfirmations',
FIAT_OUTPUT_COMPLETE = 'FiatOutputComplete',
BLOCKCHAIN_FEE_UPDATE = 'BlockchainFeeUpdate',
TRANSACTION_USER_SYNC = 'TransactionUserSync',
}

type ProcessMap = { [p in Process]?: boolean };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ export class Transaction extends IEntity {
@ManyToOne(() => User, (user) => user.transactions, { nullable: true, eager: true })
user?: User;

@ManyToOne(() => UserData, (userData) => userData.kycSteps, { nullable: true })
userData: UserData;

@OneToOne(() => TransactionRequest, { nullable: true })
@JoinColumn()
request?: TransactionRequest;
Expand Down Expand Up @@ -160,8 +163,4 @@ export class Transaction extends IEntity {
get targetEntity(): BuyCrypto | BuyFiat | RefReward | BankTxReturn | undefined {
return this.buyCrypto ?? this.buyFiat ?? this.refReward ?? this.bankTxReturn ?? undefined;
}

get userData(): UserData {
return this.user?.userData;
}
}
26 changes: 26 additions & 0 deletions src/subdomains/supporting/payment/services/transaction.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Injectable } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import { DfxLogger } from 'src/shared/services/dfx-logger';
import { DisabledProcess, Process } from 'src/shared/services/process.service';
import { Lock } from 'src/shared/utils/lock';
import { Util } from 'src/shared/utils/util';
import { UpdateTransactionDto } from 'src/subdomains/core/history/dto/update-transaction.dto';
import { Between, FindOptionsRelations, IsNull, LessThanOrEqual, Not } from 'typeorm';
Expand All @@ -9,8 +13,30 @@ import { TransactionRepository } from '../repositories/transaction.repository';

@Injectable()
export class TransactionService {
private readonly logger = new DfxLogger(TransactionService);

constructor(private readonly repo: TransactionRepository) {}

@Cron(CronExpression.EVERY_MINUTE)
@Lock()
async syncUserData(): Promise<void> {
if (DisabledProcess(Process.TRANSACTION_USER_SYNC)) return;

const entities = await this.repo.find({
where: { user: { id: Not(IsNull()) }, userData: { id: IsNull() } },
relations: { user: { userData: true }, userData: true },
take: 10000,
});

for (const entity of entities) {
try {
await this.repo.update(entity.id, { userData: entity.user.userData });
} catch (e) {
this.logger.error(`Failed to sync transaction userData ${entity.id}:`, e);
}
}
}

async create(dto: CreateTransactionDto): Promise<Transaction | undefined> {
const entity = this.repo.create(dto);

Expand Down

0 comments on commit 3117d30

Please sign in to comment.