public async Task <OrganisationStandardVersion> Handle(UpdateOrganisationStandardVersionRequest request, CancellationToken cancellationToken)
        {
            var validationResponse = await _validator.ValidatorUpdateOrganisationStandardVersionRequest(request);

            if (!validationResponse.IsValid)
            {
                var message = validationResponse.Errors.Aggregate(string.Empty, (current, error) => current + error.ErrorMessage + "; ");

                if (validationResponse.Errors.Any(x => x.StatusCode == ValidationStatusCode.BadRequest.ToString()))
                {
                    throw new BadRequestException(message);
                }
                throw new Exception();
            }

            try
            {
                _unitOfWork.Begin();

                var orgStandardVersion = await _repository.GetOrganisationStandardVersionByOrganisationStandardIdAndVersion(request.OrganisationStandardId, request.OrganisationStandardVersion);

                orgStandardVersion.EffectiveFrom = request.EffectiveFrom;
                orgStandardVersion.EffectiveTo   = request.EffectiveTo;

                await _repository.UpdateOrganisationStandardVersion(orgStandardVersion);

                _unitOfWork.Commit();

                return((OrganisationStandardVersion)orgStandardVersion);
            }
            catch (Exception)
            {
                _unitOfWork.Rollback();
                throw;
            }
        }
        public async Task <OrganisationStandardVersion> Handle(OrganisationStandardVersionOptInRequest request, CancellationToken cancellationToken)
        {
            try
            {
                // Remove out of unit of work due to crossover betwen EF, Dapper and UnitOfWorkTransaction Conflict
                // It's also a Read so has no impact in that case.
                var submittingContact = await _contactQueryRepository.GetContactById(request.SubmittingContactId);

                _unitOfWork.Begin();

                var orgStandard = await _repository.GetOrganisationStandardByOrganisationIdAndStandardReference(request.EndPointAssessorOrganisationId, request.StandardReference);

                var entity = new Domain.Entities.OrganisationStandardVersion
                {
                    StandardUId            = request.StandardUId,
                    Version                = request.Version,
                    OrganisationStandardId = orgStandard.Id,
                    EffectiveFrom          = request.EffectiveFrom,
                    EffectiveTo            = request.EffectiveTo,
                    DateVersionApproved    = request.DateVersionApproved,
                    Comments               = request.Comments,
                    Status = request.Status
                };

                var existingVersion = await _repository.GetOrganisationStandardVersionByOrganisationStandardIdAndVersion(orgStandard.Id, request.Version);

                if (request.OptInFollowingWithdrawal && existingVersion != null)
                {
                    await _repository.UpdateOrganisationStandardVersion(entity);
                }
                else if (existingVersion != null)
                {
                    throw new InvalidOperationException("OrganisationStandardVersion already exists");
                }
                else
                {
                    await _repository.CreateOrganisationStandardVersion(entity);
                }

                var orgStandardVersion = (OrganisationStandardVersion)entity;

                var application = await _applyRepository.GetApply(request.ApplicationId);

                var standard = await _standardRepository.GetStandardVersionByStandardUId(request.StandardUId);

                application.ApplicationStatus                 = ApplicationStatus.Approved;
                application.ReviewStatus                      = ApplicationStatus.Approved;
                application.StandardReference                 = request.StandardReference;
                application.StandardCode                      = standard.LarsCode;
                application.ApplyData.Apply.StandardCode      = standard.LarsCode;
                application.ApplyData.Apply.StandardReference = request.StandardReference;
                application.ApplyData.Apply.StandardName      = standard.Title;
                application.ApplyData.Apply.Versions          = new List <string>()
                {
                    request.Version
                };
                application.ApplyViaOptIn = true;

                await _applyRepository.SubmitApplicationSequence(application);

                await NotifyContact(submittingContact, application.ApplyData, cancellationToken);

                _unitOfWork.Commit();

                return(orgStandardVersion);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Failed to opt-in standard version {request.StandardReference} {request.Version} for Organisation {request.EndPointAssessorOrganisationId}");
                _unitOfWork.Rollback();
                throw;
            }
        }