private async ValueTask HandleEvents(ClientProfileUpdateMessage message) { try { _logger.LogInformation("Received profile update message for client {clientId}", message.NewProfile.ClientId); var profile = await _repository.GetOrCreateProfile(message.NewProfile.ClientId); var oldProfile = (KycProfile)profile.Clone(); if (profile.PhoneVerified != message.NewProfile.PhoneConfirmed) { profile.PhoneVerified = message.NewProfile.PhoneConfirmed; await _statusSetter.UpdateProfileState(profile); await _repository.UpdateProfile(profile, "Phone verification state update", "automatic", null); await _publisher.PublishAsync(new KycProfileUpdatedMessage() { ClientId = profile.ClientId, OldProfile = oldProfile, NewProfile = profile }); } } catch (Exception e) { _logger.LogError(e, "When handling message from client profile service for client {clientId}", message.NewProfile.ClientId); throw; } }
public async Task <KycProfile> GetOrCreateProfile(string clientId) { await using var context = new DatabaseContext(_dbContextOptionsBuilder.Options); var kycProfile = context.KycProfiles.Include(t => t.Documents).FirstOrDefault(t => t.ClientId == clientId); if (kycProfile != null) { return(kycProfile); } _logger.LogInformation("Profile for clientId {clientId} not found. Creating new profile", clientId); var profile = new KycProfile { ClientId = clientId, ApplicantId = null, ActiveVerificationId = null, DepositStatus = KycOperationStatus.KycRequired, TradeStatus = KycOperationStatus.KycRequired, WithdrawalStatus = KycOperationStatus.KycRequired, PhoneVerified = false, RequiredVerifications = new List <VerificationType>(), Documents = new List <KycDocument>(), KycLevel = KycLevel.Level1, }; await _statusSetter.UpdateProfileState(profile); await UpdateProfile(profile, "Profile creation", "automatic", null); return(profile); }
public async ValueTask <OperationResponse> SetKycLevelAsync(SetKycLevelRequest request) { _logger.LogInformation("Manually setting kyc level for clientId {clientId} to {level}", request.ClientId, request.KycLevel); try { var profile = await _repository.GetOrCreateProfile(request.ClientId); var oldProfile = (KycProfile)profile.Clone(); profile.KycLevel = request.KycLevel; await _statusSetter.UpdateProfileState(profile); await _repository.UpdateProfile(profile, "Manual kyc level change", request.Agent, request.Comment); await _publisher.PublishAsync(new KycProfileUpdatedMessage() { ClientId = request.ClientId, OldProfile = oldProfile, NewProfile = profile }); return(new OperationResponse() { IsSuccess = true }); } catch (Exception e) { _logger.LogError(e, "When manually setting kyc level for clientId {clientId}", request.ClientId); return(new OperationResponse() { IsSuccess = false, Error = e.Message }); } }
public async Task <OperationResponse> StartVerification(StartVerificationRequest request) { try { _logger.LogInformation("Starting verification for client {clientId}", request.ClientId); var profile = await _repository.GetOrCreateProfile(request.ClientId); if (string.IsNullOrEmpty(profile.ApplicantId)) { _logger.LogWarning("Client {clientId} does not have applicant id. Check was started before uploading documents", profile.ClientId); return(new OperationResponse() { IsSuccess = false, Error = $"Client {request.ClientId} does not have applicant id" }); } if (profile.DepositStatus == KycOperationStatus.Blocked || profile.TradeStatus == KycOperationStatus.Blocked || profile.WithdrawalStatus == KycOperationStatus.Blocked) { _logger.LogWarning("Client {clientId} is blocked. Kyc verification not started", profile.ClientId); return(new OperationResponse() { IsSuccess = false, Error = $"Client {request.ClientId} is blocked" }); } if (!string.IsNullOrWhiteSpace(profile.ActiveVerificationId)) { _logger.LogWarning("Client {clientId} has active verification. New kyc verification not started", profile.ClientId); return(new OperationResponse() { IsSuccess = false, Error = $"Client {request.ClientId} has active verification" }); } if (profile.DepositStatus == KycOperationStatus.Allowed || profile.TradeStatus == KycOperationStatus.Allowed || profile.WithdrawalStatus == KycOperationStatus.Allowed) { _logger.LogWarning("Client {clientId} is already verified. Kyc verification not started", profile.ClientId); return(new OperationResponse() { IsSuccess = false, Error = $"Client {request.ClientId} is already verified" }); } var oldProfile = (KycProfile)profile.Clone(); var types = KycLevelHelper.GetVerificationsList(profile); var verificationId = await _httpService.StartVerification(profile.ApplicantId, types); profile.ActiveVerificationId = verificationId; await _statusSetter.UpdateProfileState(profile); await _repository.UpdateProfile(profile, "Verification started", "automatic", null); await _publisher.PublishAsync(new KycProfileUpdatedMessage() { ClientId = request.ClientId, OldProfile = oldProfile, NewProfile = profile }); await using var context = new DatabaseContext(_dbContextOptionsBuilder.Options); context.Verifications.Add(new Verification { VerificationId = verificationId, ClientId = profile.ClientId, VerificationTypes = String.Join(';', types), StartingTime = DateTime.UtcNow, }); await context.SaveChangesAsync(); return(new OperationResponse() { IsSuccess = true }); } catch (Exception e) { _logger.LogError(e, "When starting verification for clientId {clientId}", request.ClientId); return(new OperationResponse() { IsSuccess = false, Error = e.Message }); } }