public StartVerificationResponse StartVerification(StartVerificationRequest request)
        {
            var verification = this.PersistenceContext.Load <VerificationStep>(request.VerificationStepRef, EntityLoadFlags.CheckVersion);

            var op = new Operations.StartVerification();

            op.Execute(verification, this.CurrentUserStaff, new PersistentWorkflow(this.PersistenceContext));

            this.PersistenceContext.SynchState();
            return(new StartVerificationResponse(verification.GetRef()));
        }
        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
                });
            }
        }