public async Task AddMigrationReport(CourseMigrationReport courseReport)
        {
            Throw.IfNull(courseReport, nameof(courseReport));
            try
            {
                using (var client = _cosmosDbHelper.GetClient())
                {
                    var result = await _cosmosDbHelper.GetDocumentsByUKPRN<CourseMigrationReport>(client, _settings.CoursesMigrationReportCollectionId,
                        courseReport.ProviderUKPRN);

                    if (result.Any())
                    {
                        courseReport.Id = result.FirstOrDefault().Id;

                        await _cosmosDbHelper.UpdateDocumentAsync(client, _settings.CoursesMigrationReportCollectionId,
                            courseReport);
                    }
                    else
                    {
                        var doc = await _cosmosDbHelper.CreateDocumentAsync(client, _settings.CoursesMigrationReportCollectionId, courseReport);
                    }

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        public async Task <ICourse> Update(ICourse course)
        {
            Throw.IfNull(course, nameof(course));

            try {
                Course updated = null;
                using (var client = _cosmosDbHelper.GetClient())
                {
                    var updatedDocument = await _cosmosDbHelper.UpdateDocumentAsync(client, _settings.CoursesCollectionId, course);

                    updated = _cosmosDbHelper.DocumentTo <Course>(updatedDocument);
                }
                return(updated);
            } catch (Exception ex) {
                throw ex;
            }
        }
        public async Task <IEnumerable <Document> > ArchiveAllCourses(ILogger log, IEnumerable <Document> documents)
        {
            Throw.IfNull(log, nameof(log));
            Throw.IfNullOrEmpty(documents, nameof(documents));

            List <Document> responseList = new List <Document>();

            if (documents.Any())
            {
                using (var client = _cosmosDbHelper.GetClient())
                {
                    Uri         uri     = UriFactory.CreateDocumentCollectionUri(_dbSettings.DatabaseId, _settings.CoursesCollectionId);
                    FeedOptions options = new FeedOptions {
                        EnableCrossPartitionQuery = true, MaxItemCount = -1
                    };

                    foreach (var doc in documents)
                    {
                        int UKPRN = doc.GetPropertyValue <int?>("UnitedKingdomProviderReferenceNumber") ?? 0;
                        if (UKPRN > 10000000)
                        {
                            log.LogInformation($"Processing document with UKPRN {UKPRN}");
                            IEnumerable <Course> courseDocs = client.CreateDocumentQuery <Course>(uri, options)
                                                              .Where(x => x.ProviderUKPRN == UKPRN);
                            foreach (Course courseDoc in courseDocs)
                            {
                                log.LogInformation($"Archiving course with id {courseDoc.id}");
                                Uri docUri = UriFactory.CreateDocumentUri(_dbSettings.DatabaseId, _settings.CoursesCollectionId, courseDoc.id.ToString());

                                //var result = await client.DeleteDocumentAsync(docUri, new RequestOptions() { PartitionKey = new PartitionKey(doc.ProviderUKPRN) });
                                Document d = client.ReadDocumentAsync(docUri, new RequestOptions()
                                {
                                    PartitionKey = new PartitionKey(UKPRN)
                                })
                                             ?.Result
                                             ?.Resource;
                                if (d == null)
                                {
                                    log.LogInformation($"** Course with id {courseDoc.id} and Title {courseDoc.QualificationCourseTitle} wasn't archived");
                                }
                                else
                                {
                                    d.SetPropertyValue("CourseStatus", (int)RecordStatus.Archived);
                                    //result = await client.UpsertDocumentAsync(docUri, result.Resource);
                                    d = await _cosmosDbHelper.UpdateDocumentAsync(client, _settings.CoursesCollectionId, d);

                                    responseList.Add(d);
                                    log.LogInformation($"  archived successfully");
                                }
                            }
                        }
                    }
                }
            }
            return(responseList);
        }
        public async Task <IApprenticeship> Update(IApprenticeship apprenticeship)
        {
            Throw.IfNull(apprenticeship, nameof(apprenticeship));

            Apprenticeship updated = null;

            var client = _cosmosDbHelper.GetClient();
            await _cosmosDbHelper.CreateDatabaseIfNotExistsAsync(client);

            await _cosmosDbHelper.CreateDocumentCollectionIfNotExistsAsync(client, _settings.ApprenticeshipCollectionId);

            var updatedDocument = await _cosmosDbHelper.UpdateDocumentAsync(client, _settings.ApprenticeshipCollectionId, apprenticeship);

            updated = _cosmosDbHelper.DocumentTo <Apprenticeship>(updatedDocument);

            return(updated);
        }
Пример #5
0
        public async Task UpdateReport(int ukprn)
        {
            using (var client = _cosmosDbHelper.GetClient())
            {
                var courses = await _cosmosDbHelper.GetCourseCollectionDocumentsByUKPRN <Course>(client, _settings.CoursesCollectionId,
                                                                                                 ukprn);

                var migrationReport = (await _cosmosDbHelper.GetCourseCollectionDocumentsByUKPRN <CourseMigrationReport>(client,
                                                                                                                         _settings.CoursesMigrationReportCollectionId,
                                                                                                                         ukprn)).FirstOrDefault();
                var provider = await _cosmosDbHelper.GetProviderByUKPRN(client, _settings.ProviderCollectionId,
                                                                        ukprn);


                if (provider == null || !HasValidReportOrCourses(courses, migrationReport))
                {
                    throw new Exception($"Unable to generate report for Provider: {ukprn}");
                }

                var report = new CourseReportDocument
                {
                    ProviderUKPRN                = ukprn.ToString(),
                    MigrationPendingCount        = courses.SelectMany(x => x.CourseRuns.Where(cr => cr.RecordStatus == RecordStatus.MigrationPending)).Count(),
                    MigrationReadyToGoLive       = courses.SelectMany(x => x.CourseRuns.Where(cr => cr.RecordStatus == RecordStatus.MigrationReadyToGoLive)).Count(),
                    BulkUploadPendingcount       = courses.SelectMany(x => x.CourseRuns.Where(cr => cr.RecordStatus == RecordStatus.BulkUloadPending)).Count(),
                    BulkUploadReadyToGoLiveCount = courses.SelectMany(x => x.CourseRuns.Where(cr => cr.RecordStatus == RecordStatus.BulkUploadReadyToGoLive)).Count(),
                    FailedMigrationCount         = migrationReport?.LarslessCourses?.SelectMany(cr => cr.CourseRuns)?.Count(),
                    LiveCount     = courses.SelectMany(c => c.CourseRuns.Where(cr => cr.RecordStatus == RecordStatus.Live)).Count(),
                    MigratedCount = migrationReport?.PreviousLiveCourseCount,
                    MigrationDate = migrationReport?.Timestamp,
                    MigrationRate = decimal.Round(MigrationRate(courses), 2, MidpointRounding.AwayFromZero),
                    ProviderName  = provider.ProviderName,
                    ProviderType  = provider.ProviderType
                };

                await _cosmosDbHelper.UpdateDocumentAsync(client, _settings.DfcReportCollectionId, report);
            }
        }
Пример #6
0
        public async Task UpdateReport(int ukprn)
        {
            using (var client = _cosmosDbHelper.GetClient())
            {
                var apprenticeships = await _cosmosDbHelper.GetCourseCollectionDocumentsByUKPRN <Apprenticeship>(client, _settings.ApprenticeshipCollectionId,
                                                                                                                 ukprn);

                var migrationReport = (await _cosmosDbHelper.GetCourseCollectionDocumentsByUKPRN <ApprenticeshipMigrationReport>(client,
                                                                                                                                 _settings.ApprenticeshipMigrationReportCollectionId,
                                                                                                                                 ukprn)).FirstOrDefault();
                var provider = await _cosmosDbHelper.GetProviderByUKPRN(client, _settings.ProviderCollectionId,
                                                                        ukprn);


                if (provider == null || !HasValidReportOrApprenticeships(apprenticeships, migrationReport))
                {
                    throw new Exception($"Unable to generate report for Provider: {ukprn}");
                }

                var report = new ApprenticeshipDfcReportDocument()
                {
                    ProviderUKPRN                = ukprn.ToString(),
                    MigrationPendingCount        = apprenticeships.SelectMany(x => x.ApprenticeshipLocations.Where(cr => cr.RecordStatus == RecordStatus.MigrationPending)).Count(),
                    MigrationReadyToGoLive       = apprenticeships.SelectMany(x => x.ApprenticeshipLocations.Where(cr => cr.RecordStatus == RecordStatus.MigrationReadyToGoLive)).Count(),
                    BulkUploadPendingcount       = apprenticeships.SelectMany(x => x.ApprenticeshipLocations.Where(cr => cr.RecordStatus == RecordStatus.BulkUloadPending)).Count(),
                    BulkUploadReadyToGoLiveCount = apprenticeships.SelectMany(x => x.ApprenticeshipLocations.Where(cr => cr.RecordStatus == RecordStatus.BulkUploadReadyToGoLive)).Count(),
                    FailedMigrationCount         = migrationReport?.NotTransferred,
                    LiveCount     = apprenticeships.SelectMany(c => c.ApprenticeshipLocations.Where(cr => cr.RecordStatus == RecordStatus.Live)).Count(),
                    MigratedCount = migrationReport?.ApprenticeshipsMigrated,
                    MigrationDate = migrationReport?.MigrationDate,
                    MigrationRate = decimal.Round(MigrationRate(apprenticeships), 2, MidpointRounding.AwayFromZero),
                    ProviderName  = provider.ProviderName,
                    ProviderType  = provider.ProviderType
                };

                await _cosmosDbHelper.UpdateDocumentAsync(client, _settings.ApprenticeshipDfcReportCollection, report);
            }
        }
        public async Task CreateApprenticeshipReport(ApprenticeshipMigrationReport report)
        {
            var client = _cosmosDbHelper.GetClient();
            await _cosmosDbHelper.CreateDatabaseIfNotExistsAsync(client);

            await _cosmosDbHelper.CreateDocumentCollectionIfNotExistsAsync(client,
                                                                           _settings.ApprenticeshipReportCollectionId);

            var result = _cosmosDbHelper.GetDocumentsByUKPRN <ApprenticeshipMigrationReport>(client, _settings.ApprenticeshipReportCollectionId,
                                                                                             report.ProviderUKPRN);

            if (result.Any())
            {
                report.Id = result.FirstOrDefault().Id;

                await _cosmosDbHelper.UpdateDocumentAsync(client, _settings.ApprenticeshipReportCollectionId,
                                                          report);
            }
            else
            {
                var doc = await _cosmosDbHelper.CreateDocumentAsync(client, _settings.ApprenticeshipReportCollectionId, report);
            }
        }