Пример #1
0
        public static async Task <Certificate> ApplyStatusInformation(ICertificateRepository certificateRepository, IContactQueryRepository contactQueryRepository, Certificate certificate)
        {
            if (certificateRepository is null || contactQueryRepository is null || certificate is null)
            {
                return(certificate);
            }

            var json = JsonConvert.SerializeObject(certificate);
            var cert = JsonConvert.DeserializeObject <Certificate>(json);

            var certificateLogs = await certificateRepository.GetCertificateLogsFor(cert.Id);

            certificateLogs = certificateLogs?.Where(l => l.ReasonForChange is null).ToList(); // this removes any admin changes done within staff app

            var createdLogEntry = certificateLogs?.OrderByDescending(l => l.EventTime).FirstOrDefault(l => l.Status == CertificateStatus.Draft && l.Action == CertificateActions.Start);

            if (createdLogEntry != null)
            {
                var createdContact = await contactQueryRepository.GetContact(createdLogEntry.Username);

                cert.CreatedAt = createdLogEntry.EventTime.UtcToTimeZoneTime();
                cert.CreatedBy = createdContact != null ? createdContact.DisplayName : createdLogEntry.Username;
            }

            var submittedLogEntry = certificateLogs?.OrderByDescending(l => l.EventTime).FirstOrDefault(l => l.Status == CertificateStatus.Submitted);

            // NOTE: THIS IS A DATA FRIG FOR EXTERNAL API AS WE NEED SUBMITTED INFORMATION!
            // Amended, don't return submitted info, if the status has returned to draft after a fail.
            if (submittedLogEntry != null && certificate.Status != CertificateStatus.Draft)
            {
                var submittedContact = await contactQueryRepository.GetContact(submittedLogEntry.Username);

                cert.UpdatedAt = submittedLogEntry.EventTime.UtcToTimeZoneTime();
                cert.UpdatedBy = submittedContact != null ? submittedContact.DisplayName : submittedLogEntry.Username;
            }
            else
            {
                cert.UpdatedAt = null;
                cert.UpdatedBy = null;
            }

            // Remove Print & Batch information if going for a Reprint
            if (cert.Status == CertificateStatus.Reprint)
            {
                cert.ToBePrinted = null;
                cert.BatchNumber = null;
            }

            return(cert);
        }
        public async Task <IActionResult> SearchContactByUserName(string userName)
        {
            _logger.LogInformation($"Received Search Contact By UserName Request using user name = {userName}");

            var contact = await _contactQueryRepository.GetContact(userName);

            if (contact == null)
            {
                throw new ResourceNotFoundException();
            }
            return(Ok(Mapper.Map <ContactResponse>(contact)));
        }
        private async Task <PaginatedList <CertificateSummaryResponse> > MapCertificates(PaginatedList <CertificateHistoryModel> certificates)
        {
            var certificateResponses = certificates?.Items.Select(
                certificate =>
            {
                var latestCertificateStatusDate = (new[] { certificate.UpdatedAt, certificate.CreatedAt }.Max()).Value;

                var recordedBy = certificate.CertificateLogs
                                 .OrderByDescending(q => q.EventTime)
                                 .FirstOrDefault(certificateLog =>
                                                 certificateLog.Action == Domain.Consts.CertificateActions.Submit)?.Username;

                var printStatusAt        = certificate?.StatusAt;
                var printReasonForChange = certificate?.ReasonForChange;

                var trainingProviderName = string.Empty;
                try
                {
                    if (certificate.ProviderName == null)
                    {
                        var provider = _roatpApiClient
                                       .GetOrganisationByUkprn(certificate.ProviderUkPrn).Result;

                        if (provider == null)
                        {
                            throw new EntityNotFoundException($"Provider {certificate.ProviderUkPrn} not found", null);
                        }

                        trainingProviderName = provider.ProviderName;
                        _certificateRepository.UpdateProviderName(certificate.Id, trainingProviderName).GetAwaiter().GetResult();
                    }
                    else
                    {
                        trainingProviderName = certificate.ProviderName;
                    }
                }
                catch (EntityNotFoundException)
                {
                    if (certificate.EndPointAssessorUkprn != null)
                    {
                        _logger.LogInformation(
                            $"Cannot find training provider for ukprn {certificate.EndPointAssessorUkprn.Value}");
                    }
                }

                return(new CertificateSummaryResponse
                {
                    CertificateReference = certificate.CertificateReference,
                    Uln = certificate.Uln,
                    CreatedAt = certificate.CreatedAt,
                    CreatedDay = certificate.CreateDay,
                    UpdatedAt = certificate.UpdatedAt,
                    PrintStatusAt = printStatusAt,
                    ContactOrganisation = certificate.ContactOrganisation,
                    ContactName = certificate.ContactName,
                    TrainingProvider = trainingProviderName,
                    RecordedBy = recordedBy,
                    CourseOption = certificate.CourseOption,
                    FullName = certificate.FullName,
                    OverallGrade = certificate.OverallGrade,
                    StandardReference = certificate.StandardReference,
                    StandardName = certificate.StandardName,
                    Version = certificate.Version,
                    Level = certificate.StandardLevel,
                    AchievementDate = certificate.AchievementDate,
                    LearningStartDate = certificate.LearningStartDate,
                    ContactAddLine1 = certificate.ContactAddLine1,
                    ContactAddLine2 = certificate.ContactAddLine2,
                    ContactAddLine3 = certificate.ContactAddLine3,
                    ContactAddLine4 = certificate.ContactAddLine4,
                    ContactPostCode = certificate.ContactPostCode,
                    Status = certificate.Status,
                    ReasonForChange = printReasonForChange,
                    LatestStatusDatetime = latestCertificateStatusDate
                });
            });

            var responses = certificateResponses?.ToList();

            if (responses != null)
            {
                foreach (var response in responses)
                {
                    var displayName = (await _contactQueryRepository.GetContact(response.RecordedBy))?.DisplayName;
                    response.RecordedBy = displayName ?? response.RecordedBy;
                }
            }

            var paginatedList = new PaginatedList <CertificateSummaryResponse>(responses,
                                                                               certificates?.TotalRecordCount ?? 0,
                                                                               certificates?.PageIndex ?? 1,
                                                                               certificates?.PageSize ?? 10
                                                                               );

            return(paginatedList);
        }
Пример #4
0
        private async Task <PaginatedList <CertificateSummaryResponse> > MapCertificates(PaginatedList <Certificate> certificates)
        {
            var trainingProviderName = string.Empty;
            var firstName            = string.Empty;
            var lastName             = string.Empty;
            var reasonForChange      = string.Empty;

            var certificateResponses = new List <CertificateSummaryResponse>();

            foreach (var certificate in certificates.Items)
            {
                var certificateData = JsonConvert.DeserializeObject <CertificateData>(certificate.CertificateData);
                var recordedBy      = certificate.CreatedBy;

                try
                {
                    if (certificateData.ProviderName == null)
                    {
                        var provider = (await _roatpApiClient.SearchOrganisationByUkprn(certificate.ProviderUkPrn)).FirstOrDefault();
                        if (provider == null)
                        {
                            throw new EntityNotFoundException($"Training provider {certificate.ProviderUkPrn} not found", null);
                        }

                        trainingProviderName = provider?.ProviderName;
                        await _certificateRepository.UpdateProviderName(certificate.Id, trainingProviderName);
                    }
                    else
                    {
                        trainingProviderName = certificateData.ProviderName;
                    }

                    firstName       = certificateData.LearnerGivenNames;
                    lastName        = certificateData.LearnerFamilyName;
                    reasonForChange = certificate.CertificateLogs
                                      .OrderByDescending(q => q.EventTime)
                                      .FirstOrDefault(certificateLog =>
                                                      certificateLog.Status == Domain.Consts.CertificateStatus.Draft && certificateLog.Action == Domain.Consts.CertificateStatus.Rejected)?.ReasonForChange;
                }
                catch (EntityNotFoundException)
                {
                    _logger.LogInformation(
                        $"Cannot find training provider for ukprn {certificate.Organisation.EndPointAssessorUkprn.Value}");
                }

                certificateResponses.Add(new CertificateSummaryResponse
                {
                    CertificateReference = certificate.CertificateReference,
                    Uln                   = certificate.Uln,
                    CreatedDay            = certificate.CreateDay,
                    UpdatedAt             = certificate.UpdatedAt,
                    ContactOrganisation   = certificateData.ContactOrganisation,
                    ContactName           = certificateData.ContactName,
                    TrainingProvider      = trainingProviderName,
                    RecordedBy            = recordedBy,
                    CourseOption          = certificateData.CourseOption,
                    FullName              = certificateData.FullName,
                    OverallGrade          = certificateData.OverallGrade,
                    StandardReference     = certificateData.StandardReference,
                    StandardName          = certificateData.StandardName,
                    Level                 = certificateData.StandardLevel,
                    AchievementDate       = certificateData.AchievementDate,
                    LearningStartDate     = certificateData.LearningStartDate,
                    ContactAddLine1       = certificateData.ContactAddLine1,
                    ContactAddLine2       = certificateData.ContactAddLine2,
                    ContactAddLine3       = certificateData.ContactAddLine3,
                    ContactAddLine4       = certificateData.ContactAddLine4,
                    ContactPostCode       = certificateData.ContactPostCode,
                    Status                = certificate.Status,
                    PrivatelyFundedStatus = certificate.PrivatelyFundedStatus,
                    FirstName             = firstName,
                    LastName              = lastName,
                    Ukprn                 = certificate.ProviderUkPrn,
                    StandardCode          = certificate.StandardCode,
                    EpaoId                = certificate.Organisation?.EndPointAssessorOrganisationId,
                    EpaoName              = certificate.Organisation?.EndPointAssessorName,
                    CertificateId         = certificate.Id,
                    ReasonForChange       = reasonForChange
                });
            }

            var responses = certificateResponses.ToList();

            foreach (var response in responses)
            {
                var contact = await _contactQueryRepository.GetContact(response.RecordedBy);

                response.RecordedBy      = contact?.DisplayName;
                response.RecordedByEmail = contact?.Email;
            }

            var paginatedList = new PaginatedList <CertificateSummaryResponse>(responses,
                                                                               certificates.TotalRecordCount,
                                                                               certificates.PageIndex,
                                                                               certificates.PageSize
                                                                               );

            return(paginatedList);
        }
Пример #5
0
        public static SearchResult PopulateCertificateExtraInformationDependingOnPermission(this SearchResult searchResult,
                                                                                            SearchQuery request, IContactQueryRepository contactRepository,
                                                                                            Certificate certificate, Organisation searchingEpao, ILogger <SearchHandler> logger)
        {
            var createdLogEntry = certificate.CertificateLogs.FirstOrDefault(l => l.Status == CertificateStatus.Draft);

            var submittedLogEntry = certificate.CertificateLogs.Where(l => l.Action == CertificateActions.Submit)
                                    .OrderByDescending(l => l.EventTime)
                                    .FirstOrDefault();

            if (submittedLogEntry == null)
            {
                return(searchResult);
            }

            var submittingContact = contactRepository.GetContact(submittedLogEntry.Username).Result ?? contactRepository.GetContact(certificate.UpdatedBy).Result;
            var createdContact    = contactRepository.GetContact(createdLogEntry?.Username).Result ?? contactRepository.GetContact(certificate.CreatedBy).Result;

            var lastUpdatedLogEntry = certificate.CertificateLogs.Aggregate((i1, i2) => i1.EventTime > i2.EventTime ? i1 : i2) ?? submittedLogEntry;
            var lastUpdatedContact  = contactRepository.GetContact(lastUpdatedLogEntry.Username).Result;

            logger.LogInformation($"MatchUpExistingCompletedStandards After GetContact for CertificateId {certificate.Id}");

            var searchingContact = contactRepository.GetContact(request.Username).Result;

            var certificateData = JsonConvert.DeserializeObject <CertificateData>(certificate.CertificateData);

            var submittedCertificateData = JsonConvert.DeserializeObject <CertificateData>(submittedLogEntry.CertificateData);

            if (submittingContact != null && searchingContact != null && submittingContact.OrganisationId == searchingContact.OrganisationId)
            {
                searchResult.ShowExtraInfo = true;
                searchResult.OverallGrade  = GetSubmittedOrPreviousGrade(certificate, certificateData, submittedCertificateData);
                searchResult.SubmittedBy   = submittingContact.DisplayName;                                                              // This needs to be contact real name
                searchResult.SubmittedAt   = submittedLogEntry.EventTime.UtcToTimeZoneTime();                                            // This needs to be local time
                searchResult.AchDate       = GetSubmittedOrPreviousAchievementDate(certificate, certificateData, submittedCertificateData);
                searchResult.UpdatedBy     = lastUpdatedContact != null ? lastUpdatedContact.DisplayName : lastUpdatedLogEntry.Username; // This needs to be contact real name
                searchResult.UpdatedAt     = lastUpdatedLogEntry.EventTime.UtcToTimeZoneTime();                                          // This needs to be local time
            }
            else if (createdContact != null && searchingContact != null && createdContact.OrganisationId == searchingContact.OrganisationId)
            {
                searchResult.ShowExtraInfo = true;
                searchResult.OverallGrade  = GetSubmittedOrPreviousGrade(certificate, certificateData, submittedCertificateData);
                searchResult.SubmittedBy   = submittedLogEntry.Username;                                                                 // This needs to be contact real name
                searchResult.SubmittedAt   = submittedLogEntry.EventTime.UtcToTimeZoneTime();                                            // This needs to be local time
                searchResult.AchDate       = GetSubmittedOrPreviousAchievementDate(certificate, certificateData, submittedCertificateData);
                searchResult.UpdatedBy     = lastUpdatedContact != null ? lastUpdatedContact.DisplayName : lastUpdatedLogEntry.Username; // This needs to be contact real name
                searchResult.UpdatedAt     = lastUpdatedLogEntry.EventTime.UtcToTimeZoneTime();                                          // This needs to be local time
            }
            else if (certificate.OrganisationId == searchingEpao?.Id)
            {
                searchResult.ShowExtraInfo = true;
                searchResult.OverallGrade  = GetSubmittedOrPreviousGrade(certificate, certificateData, submittedCertificateData);
                searchResult.SubmittedBy   = submittedLogEntry.Username ?? certificate.UpdatedBy;
                searchResult.SubmittedAt   = submittedLogEntry.EventTime.UtcToTimeZoneTime(); // This needs to be local time
                searchResult.AchDate       = GetSubmittedOrPreviousAchievementDate(certificate, certificateData, submittedCertificateData);
                searchResult.UpdatedBy     = lastUpdatedLogEntry.Username ?? certificate.UpdatedBy;
                searchResult.UpdatedAt     = lastUpdatedLogEntry.EventTime.UtcToTimeZoneTime(); // This needs to be local time
            }
            else
            {
                searchResult.ShowExtraInfo  = false;
                searchResult.OverallGrade   = "";
                searchResult.SubmittedBy    = "";
                searchResult.SubmittedAt    = null;
                searchResult.LearnStartDate = null;
                searchResult.AchDate        = null;
                searchResult.UpdatedBy      = null;
                searchResult.UpdatedAt      = null;
            }

            return(searchResult);
        }
        private async Task <PaginatedList <CertificateSummaryResponse> > MapCertificates(PaginatedList <Certificate> certificates)
        {
            var trainingProviderName = string.Empty;
            var recordedBy           = string.Empty;
            var certificateResponses = certificates?.Items.Select(
                certificate =>
            {
                var certificateData = JsonConvert.DeserializeObject <CertificateData>(certificate.CertificateData);
                if (certificate.IsPrivatelyFunded)
                {
                    recordedBy = certificate.CreatedBy;
                }
                else
                {
                    recordedBy = certificate.CertificateLogs
                                 .OrderByDescending(q => q.EventTime)
                                 .FirstOrDefault(certificateLog =>
                                                 certificateLog.Status == Domain.Consts.CertificateStatus.Submitted)?.Username;
                }

                try
                {
                    if (certificateData.ProviderName == null)
                    {
                        var provider = _assessmentOrgsApiClient
                                       .GetProvider(certificate.ProviderUkPrn).GetAwaiter()
                                       .GetResult();

                        trainingProviderName = provider.ProviderName;
                        _certificateRepository.UpdateProviderName(certificate.Id, trainingProviderName).GetAwaiter().GetResult();
                    }
                    else
                    {
                        trainingProviderName = certificateData.ProviderName;
                    }
                }
                catch (EntityNotFoundException)
                {
                    if (certificate.Organisation.EndPointAssessorUkprn != null)
                    {
                        _logger.LogInformation(
                            $"Cannot find training provider for ukprn {certificate.Organisation.EndPointAssessorUkprn.Value}");
                    }
                }

                return(new CertificateSummaryResponse
                {
                    CertificateReference = certificate.CertificateReference,
                    Uln = certificate.Uln,
                    CreatedAt = certificate.CreatedAt,
                    CreatedDay = certificate.CreateDay,
                    UpdatedAt = certificate.UpdatedAt,
                    ContactOrganisation = certificateData.ContactOrganisation,
                    ContactName = certificateData.ContactName,
                    TrainingProvider = trainingProviderName,
                    RecordedBy = recordedBy,
                    CourseOption = certificateData.CourseOption,
                    FullName = certificateData.FullName,
                    OverallGrade = certificateData.OverallGrade,
                    StandardReference = certificateData.StandardReference,
                    StandardName = certificateData.StandardName,
                    Level = certificateData.StandardLevel,
                    AchievementDate = certificateData.AchievementDate,
                    LearningStartDate = certificateData.LearningStartDate,
                    ContactAddLine1 = certificateData.ContactAddLine1,
                    ContactAddLine2 = certificateData.ContactAddLine2,
                    ContactAddLine3 = certificateData.ContactAddLine3,
                    ContactAddLine4 = certificateData.ContactAddLine4,
                    ContactPostCode = certificateData.ContactPostCode
                });
            });

            var responses = certificateResponses?.ToList();

            if (responses != null)
            {
                foreach (var response in responses)
                {
                    var displayName = (await _contactQueryRepository.GetContact(response.RecordedBy))?.DisplayName;
                    response.RecordedBy = displayName ?? response.RecordedBy;
                }
            }

            var paginatedList = new PaginatedList <CertificateSummaryResponse>(responses,
                                                                               certificates?.TotalRecordCount ?? 0,
                                                                               certificates?.PageIndex ?? 1,
                                                                               certificates?.PageSize ?? 10
                                                                               );

            return(paginatedList);
        }
Пример #7
0
        public static List <SearchResult> MatchUpExistingCompletedStandards(this List <SearchResult> searchResults, SearchQuery request, ICertificateRepository certificateRepository, IContactQueryRepository contactRepository, IOrganisationQueryRepository _organisationRepository, ILogger <SearchHandler> logger)
        {
            logger.LogInformation("MatchUpExistingCompletedStandards Before Get Certificates for uln from db");
            var completedCertificates = certificateRepository.GetCompletedCertificatesFor(request.Uln).Result;

            logger.LogInformation("MatchUpExistingCompletedStandards After Get Certificates for uln from db");

            var searchingEpao = _organisationRepository.Get(request.EpaOrgId).Result;

            foreach (var searchResult in searchResults.Where(r => completedCertificates.Select(s => s.StandardCode).Contains(r.StdCode)))
            {
                var certificate     = completedCertificates.Single(s => s.StandardCode == searchResult.StdCode);
                var certificateData = JsonConvert.DeserializeObject <CertificateData>(certificate.CertificateData);
                searchResult.CertificateReference = certificate.CertificateReference;
                searchResult.CertificateId        = certificate.Id;
                searchResult.CertificateStatus    = certificate.Status;
                searchResult.LearnStartDate       = certificateData.LearningStartDate;
                searchResult.Option = certificateData.CourseOption;

                var certificateLogs = certificateRepository.GetCertificateLogsFor(certificate.Id).Result;
                logger.LogInformation("MatchUpExistingCompletedStandards After GetCertificateLogsFor");
                var submittedLogEntry = certificateLogs.FirstOrDefault(l => l.Status == CertificateStatus.Submitted);
                var createdLogEntry   = certificateLogs.FirstOrDefault(l => l.Status == CertificateStatus.Draft);
                if (request.IsPrivatelyFunded)
                {
                    var toBeApprovedLogEntry = certificateLogs.FirstOrDefault(l => l.Status == CertificateStatus.ToBeApproved);
                    if (toBeApprovedLogEntry == null)
                    {
                        continue;
                    }
                    submittedLogEntry = toBeApprovedLogEntry;
                }

                if (submittedLogEntry == null)
                {
                    continue;
                }

                var submittingContact = contactRepository.GetContact(submittedLogEntry.Username).Result ?? contactRepository.GetContact(certificate.UpdatedBy).Result;
                var createdContact    = contactRepository.GetContact(createdLogEntry?.Username).Result ?? contactRepository.GetContact(certificate.CreatedBy).Result;

                var lastUpdatedLogEntry = certificateLogs.Aggregate((i1, i2) => i1.EventTime > i2.EventTime ? i1 : i2) ?? submittedLogEntry;
                var lastUpdatedContact  = contactRepository.GetContact(lastUpdatedLogEntry.Username).Result;

                logger.LogInformation("MatchUpExistingCompletedStandards After GetContact");

                var searchingContact = contactRepository.GetContact(request.Username).Result;

                if (submittingContact != null && submittingContact.OrganisationId == searchingContact.OrganisationId)
                {
                    searchResult.ShowExtraInfo = true;
                    searchResult.OverallGrade  = certificateData.OverallGrade;
                    searchResult.SubmittedBy   = submittingContact.DisplayName;                                                              // This needs to be contact real name
                    searchResult.SubmittedAt   = submittedLogEntry.EventTime.UtcToTimeZoneTime();                                            // This needs to be local time
                    searchResult.AchDate       = certificateData.AchievementDate;
                    searchResult.UpdatedBy     = lastUpdatedContact != null ? lastUpdatedContact.DisplayName : lastUpdatedLogEntry.Username; // This needs to be contact real name
                    searchResult.UpdatedAt     = lastUpdatedLogEntry.EventTime.UtcToTimeZoneTime();                                          // This needs to be local time
                }
                else if (createdContact != null && searchingContact != null && createdContact.OrganisationId == searchingContact.OrganisationId)
                {
                    searchResult.ShowExtraInfo = true;
                    searchResult.OverallGrade  = certificateData.OverallGrade;
                    searchResult.SubmittedBy   = submittedLogEntry.Username;                                                                 // This needs to be contact real name
                    searchResult.SubmittedAt   = submittedLogEntry.EventTime.UtcToTimeZoneTime();                                            // This needs to be local time
                    searchResult.AchDate       = certificateData.AchievementDate;
                    searchResult.UpdatedBy     = lastUpdatedContact != null ? lastUpdatedContact.DisplayName : lastUpdatedLogEntry.Username; // This needs to be contact real name
                    searchResult.UpdatedAt     = lastUpdatedLogEntry.EventTime.UtcToTimeZoneTime();                                          // This needs to be local time
                }
                else if (certificate.OrganisationId == searchingEpao?.Id)
                {
                    searchResult.ShowExtraInfo = true;
                    searchResult.OverallGrade  = certificateData.OverallGrade;
                    searchResult.SubmittedBy   = submittedLogEntry.Username ?? certificate.UpdatedBy;
                    searchResult.SubmittedAt   = submittedLogEntry.EventTime.UtcToTimeZoneTime(); // This needs to be local time
                    searchResult.AchDate       = certificateData.AchievementDate;
                    searchResult.UpdatedBy     = lastUpdatedLogEntry.Username ?? certificate.UpdatedBy;
                    searchResult.UpdatedAt     = lastUpdatedLogEntry.EventTime.UtcToTimeZoneTime(); // This needs to be local time
                }
                else
                {
                    searchResult.ShowExtraInfo  = false;
                    searchResult.OverallGrade   = "";
                    searchResult.SubmittedBy    = "";
                    searchResult.SubmittedAt    = null;
                    searchResult.LearnStartDate = null;
                    searchResult.AchDate        = null;
                    searchResult.UpdatedBy      = null;
                    searchResult.UpdatedAt      = null;
                }
            }

            return(searchResults);
        }