public async Task <Standard> GetStandardVersionById(string id, string version = null)
        {
            Standard standard = null;

            try
            {
                var standardId = new StandardId(id);

                switch (standardId.IdType)
                {
                case StandardId.StandardIdType.LarsCode:
                    standard = await _standardRepository.GetStandardVersionByLarsCode(standardId.LarsCode, version);

                    break;

                case StandardId.StandardIdType.IFateReferenceNumber:
                    standard = await _standardRepository.GetStandardVersionByIFateReferenceNumber(standardId.IFateReferenceNumber, version);

                    break;

                case StandardId.StandardIdType.StandardUId:
                    standard = await _standardRepository.GetStandardVersionByStandardUId(standardId.StandardUId);

                    break;

                default:
                    throw new ArgumentOutOfRangeException("id", "StandardId was not of type StandardUId, LarsCode or IfateReferenceNumber");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"STANDARD VERSION: Failed to get for standard id: {id}");
            }

            return(standard);
        }
        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;
            }
        }