Skip to content
Open
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
8 changes: 8 additions & 0 deletions src/subdomains/core/aml/enums/aml-reason.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ export const KycAmlReasons = [
AmlReason.KYC_DATA_NEEDED,
];

export const PhoneAmlReasons = [
AmlReason.MANUAL_CHECK_PHONE,
AmlReason.MANUAL_CHECK_PHONE_FAILED,
AmlReason.MANUAL_CHECK_IP_PHONE,
AmlReason.MANUAL_CHECK_IP_COUNTRY_PHONE,
AmlReason.MANUAL_CHECK_EXTERNAL_ACCOUNT_PHONE,
];

export const RecheckAmlReasons = [
AmlReason.MANUAL_CHECK_PHONE,
AmlReason.MANUAL_CHECK_IP_PHONE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Inject,
Injectable,
NotFoundException,
OnModuleInit,
} from '@nestjs/common';
import { Config } from 'src/config/config';
import { txExplorerUrl } from 'src/integration/blockchain/shared/util/blockchain.util';
Expand Down Expand Up @@ -35,6 +36,7 @@ import { TransactionUtilService } from 'src/subdomains/core/transaction/transact
import { BankDataType } from 'src/subdomains/generic/user/models/bank-data/bank-data.entity';
import { BankDataService } from 'src/subdomains/generic/user/models/bank-data/bank-data.service';
import { CreateBankDataDto } from 'src/subdomains/generic/user/models/bank-data/dto/create-bank-data.dto';
import { UserDataService } from 'src/subdomains/generic/user/models/user-data/user-data.service';
import { UserService } from 'src/subdomains/generic/user/models/user/user.service';
import { BankTx } from 'src/subdomains/supporting/bank-tx/bank-tx/entities/bank-tx.entity';
import { BankTxService } from 'src/subdomains/supporting/bank-tx/bank-tx/services/bank-tx.service';
Expand Down Expand Up @@ -66,7 +68,7 @@ import { BuyCryptoNotificationService } from './buy-crypto-notification.service'
import { BuyCryptoWebhookService } from './buy-crypto-webhook.service';

@Injectable()
export class BuyCryptoService {
export class BuyCryptoService implements OnModuleInit {
constructor(
private readonly buyCryptoRepo: BuyCryptoRepository,
private readonly buyRepo: BuyRepository,
Expand Down Expand Up @@ -101,8 +103,13 @@ export class BuyCryptoService {
@Inject(forwardRef(() => TransactionHelper))
private readonly transactionHelper: TransactionHelper,
private readonly custodyOrderService: CustodyOrderService,
private readonly userDataService: UserDataService,
) {}

onModuleInit() {
this.userDataService.buyCryptoObservable.subscribe((buyCrypto) => this.resetAmlCheckInternal(buyCrypto));
}

async createFromBankTx(bankTx: BankTx, buyId: number): Promise<void> {
let entity = await this.buyCryptoRepo.findOneBy({ bankTx: { id: bankTx.id } });
if (entity) throw new ConflictException('There is already a buy-crypto for the specified bank TX');
Expand Down Expand Up @@ -670,6 +677,11 @@ export class BuyCryptoService {
async resetAmlCheck(id: number): Promise<void> {
const entity = await this.buyCryptoRepo.findOne({ where: { id }, relations: { chargebackOutput: true } });
if (!entity) throw new NotFoundException('BuyCrypto not found');

await this.resetAmlCheckInternal(entity);
}

async resetAmlCheckInternal(entity: BuyCrypto): Promise<void> {
if (entity.isComplete || entity.batch || entity.chargebackOutput?.isComplete || entity.chargebackAllowedDate)
throw new BadRequestException('BuyCrypto is already complete or payout initiated');
if (!entity.amlCheck) throw new BadRequestException('BuyCrypto AML check is not set');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BadRequestException, forwardRef, Inject, Injectable, NotFoundException } from '@nestjs/common';
import { BadRequestException, forwardRef, Inject, Injectable, NotFoundException, OnModuleInit } from '@nestjs/common';
import { txExplorerUrl } from 'src/integration/blockchain/shared/util/blockchain.util';
import { UserRole } from 'src/shared/auth/user-role.enum';
import { FiatService } from 'src/shared/models/fiat/fiat.service';
Expand All @@ -11,6 +11,7 @@ import { TransactionUtilService } from 'src/subdomains/core/transaction/transact
import { BankDataType } from 'src/subdomains/generic/user/models/bank-data/bank-data.entity';
import { BankDataService } from 'src/subdomains/generic/user/models/bank-data/bank-data.service';
import { CreateBankDataDto } from 'src/subdomains/generic/user/models/bank-data/dto/create-bank-data.dto';
import { UserDataService } from 'src/subdomains/generic/user/models/user-data/user-data.service';
import { UserService } from 'src/subdomains/generic/user/models/user/user.service';
import { WebhookService } from 'src/subdomains/generic/user/services/webhook/webhook.service';
import { BankTxService } from 'src/subdomains/supporting/bank-tx/bank-tx/services/bank-tx.service';
Expand Down Expand Up @@ -43,7 +44,7 @@ import { UpdateBuyFiatDto } from '../dto/update-buy-fiat.dto';
import { BuyFiatNotificationService } from './buy-fiat-notification.service';

@Injectable()
export class BuyFiatService {
export class BuyFiatService implements OnModuleInit {
constructor(
private readonly buyFiatRepo: BuyFiatRepository,
@Inject(forwardRef(() => BuyCryptoService))
Expand Down Expand Up @@ -72,8 +73,13 @@ export class BuyFiatService {
private readonly custodyOrderService: CustodyOrderService,
private readonly supportLogService: SupportLogService,
private readonly payoutService: PayoutService,
private readonly userDataService: UserDataService,
) {}

onModuleInit() {
this.userDataService.buyFiatObservable.subscribe((buyFiat) => this.resetAmlCheckInternal(buyFiat));
}

async createFromCryptoInput(cryptoInput: CryptoInput, sell: Sell, request?: TransactionRequest): Promise<BuyFiat> {
let entity = this.buyFiatRepo.create({
cryptoInput,
Expand Down Expand Up @@ -386,6 +392,11 @@ export class BuyFiatService {
relations: { fiatOutput: true, transaction: { userData: true }, outputAsset: true },
});
if (!entity) throw new NotFoundException('BuyFiat not found');

await this.resetAmlCheckInternal(entity);
}

async resetAmlCheckInternal(entity: BuyFiat): Promise<void> {
if (entity.isComplete || entity.fiatOutput?.isComplete || entity.chargebackAllowedDate)
throw new BadRequestException('BuyFiat is already complete');
if (!entity.amlCheck) throw new BadRequestException('BuyFiat amlcheck is not set');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BadRequestException, NotFoundException } from '@nestjs/common';
import { Config } from 'src/config/config';
import { UserRole } from 'src/shared/auth/user-role.enum';
import { Country } from 'src/shared/models/country/country.entity';
import { IEntity, UpdateResult } from 'src/shared/models/entity';
import { Fiat } from 'src/shared/models/fiat/fiat.entity';
Expand Down Expand Up @@ -29,7 +30,6 @@ import { AccountOpenerAuthorization, Organization } from '../organization/organi
import { UserDataRelation } from '../user-data-relation/user-data-relation.entity';
import { UpdateUserDto } from '../user/dto/update-user.dto';
import { TradingLimit } from '../user/dto/user.dto';
import { UserRole } from 'src/shared/auth/user-role.enum';
import { UserStatus } from '../user/user.enum';
import { Wallet } from '../wallet/wallet.entity';
import { AccountType } from './account-type.enum';
Expand Down Expand Up @@ -841,6 +841,7 @@ export const UserDataComplianceUpdateCols = [
'phoneCallIpCheckDate',
'phoneCallIpCountryCheckDate',
'phoneCallExternalAccountCheckDate',
'phoneCallExternalAccountCheckValue',
];

export function KycCompleted(kycStatus?: KycStatus): boolean {
Expand Down
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be cleaner to only have something like a UserData phone call completed event. The services would then listen to it and check themself, if they have a transaction to reset the AML check. WDYT?

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { CronExpression } from '@nestjs/schedule';
import { randomUUID } from 'crypto';
import JSZip from 'jszip';
import { Observable, Subject } from 'rxjs';
import { Config } from 'src/config/config';
import { CreateAccount } from 'src/integration/sift/dto/sift.dto';
import { SiftService } from 'src/integration/sift/services/sift.service';
Expand All @@ -24,13 +25,16 @@ import { ApiKeyService } from 'src/shared/services/api-key.service';
import { DfxLogger } from 'src/shared/services/dfx-logger';
import { DfxCron } from 'src/shared/utils/cron';
import { AmountType, Util } from 'src/shared/utils/util';
import { PhoneAmlReasons } from 'src/subdomains/core/aml/enums/aml-reason.enum';
import { CheckStatus } from 'src/subdomains/core/aml/enums/check-status.enum';
import { BuyCrypto } from 'src/subdomains/core/buy-crypto/process/entities/buy-crypto.entity';
import { CustodyService } from 'src/subdomains/core/custody/services/custody.service';
import { HistoryFilter, HistoryFilterKey } from 'src/subdomains/core/history/dto/history-filter.dto';
import {
DefaultPaymentLinkConfig,
PaymentLinkConfig,
} from 'src/subdomains/core/payment-link/entities/payment-link.config';
import { BuyFiat } from 'src/subdomains/core/sell-crypto/process/buy-fiat.entity';
import { KycAddress, KycPersonalData } from 'src/subdomains/generic/kyc/dto/input/kyc-data.dto';
import { KycError } from 'src/subdomains/generic/kyc/dto/kyc-error.enum';
import { MergedDto } from 'src/subdomains/generic/kyc/dto/output/kyc-merged.dto';
Expand Down Expand Up @@ -82,6 +86,8 @@ export class UserDataService {
private readonly logger = new DfxLogger(UserDataService);

private readonly secretCache: Map<number, SecretCacheEntry> = new Map();
private readonly buyCryptoResetSubject: Subject<BuyCrypto> = new Subject<BuyCrypto>();
private readonly buyFiatResetSubject: Subject<BuyFiat> = new Subject<BuyFiat>();

constructor(
private readonly repos: RepositoryFactory,
Expand Down Expand Up @@ -116,6 +122,14 @@ export class UserDataService {
) {}

// --- GETTERS --- //
get buyCryptoObservable(): Observable<BuyCrypto> {
return this.buyCryptoResetSubject.asObservable();
}

get buyFiatObservable(): Observable<BuyFiat> {
return this.buyFiatResetSubject.asObservable();
}

async getUserDataByUser(userId: number): Promise<UserData> {
return this.userDataRepo
.createQueryBuilder('userData')
Expand Down Expand Up @@ -298,7 +312,12 @@ export class UserDataService {
async updateUserData(userDataId: number, dto: UpdateUserDataDto): Promise<UserData> {
const userData = await this.userDataRepo.findOne({
where: { id: userDataId },
relations: { users: { wallet: true }, kycSteps: true, wallet: true },
relations: {
users: { wallet: true },
kycSteps: true,
wallet: true,
transactions: { buyCrypto: true, buyFiat: true },
},
});
if (!userData) throw new NotFoundException('User data not found');

Expand All @@ -307,6 +326,33 @@ export class UserDataService {
if (dto.phoneCallExternalAccountCheckValue)
userData.addPhoneCallExternalAccountCheckValue(dto.phoneCallExternalAccountCheckValue);

if (
dto.phoneCallStatus === PhoneCallStatus.COMPLETED &&
[PhoneCallStatus.FAILED, PhoneCallStatus.USER_REJECTED].includes(userData.phoneCallStatus)
) {
for (const tx of userData.transactions.filter((t) => t.buyCrypto || t.buyFiat)) {
if (tx.amlCheck === CheckStatus.FAIL) {
if (
tx.buyCrypto &&
!tx.buyCrypto.isComplete &&
!tx.buyCrypto.chargebackAllowedDate &&
PhoneAmlReasons.includes(tx.buyCrypto.amlReason)
) {
this.buyCryptoResetSubject.next(tx.buyCrypto);
}

if (
tx.buyFiat &&
!tx.buyFiat.isComplete &&
!tx.buyFiat.chargebackAllowedDate &&
PhoneAmlReasons.includes(tx.buyFiat.amlReason)
) {
this.buyFiatResetSubject.next(tx.buyFiat);
}
}
}
}

if (dto.bankTransactionVerification === CheckStatus.PASS) {
// cancel a pending video ident, if ident is completed
const identCompleted = userData.hasCompletedStep(KycStepName.IDENT);
Expand Down
Loading