Exemple #1
0
        internal async Task <UploadStatus> RevalidateApprenticeshipUploadIfRequired(
            ISqlQueryDispatcher sqlQueryDispatcher,
            Guid apprenticeshipUploadId)
        {
            var apprenticeshipUpload = await sqlQueryDispatcher.ExecuteQuery(new GetApprenticeshipUpload()
            {
                ApprenticeshipUploadId = apprenticeshipUploadId
            });

            if (apprenticeshipUpload == null)
            {
                throw new ArgumentException("Apprenticeship upload does not exist.", nameof(apprenticeshipUploadId));
            }

            var toBeRevalidated = await GetApprenticeshipUploadRowsRequiringRevalidation(sqlQueryDispatcher, apprenticeshipUpload);

            if (toBeRevalidated.Count == 0)
            {
                return(apprenticeshipUpload.UploadStatus);
            }

            var rowsCollection = new ApprenticeshipDataUploadRowInfoCollection(
                toBeRevalidated.Select(r => new ApprenticeshipDataUploadRowInfo(CsvApprenticeshipRow.FromModel(r), r.RowNumber, r.ApprenticeshipId)));

            var(uploadStatus, _) = await ValidateApprenticeshipUploadRows(sqlQueryDispatcher, apprenticeshipUploadId, apprenticeshipUpload.ApprenticeshipUploadId, rowsCollection);

            return(uploadStatus);
        }
Exemple #2
0
        // internal for testing
        internal async Task <(UploadStatus uploadStatus, IReadOnlyCollection <ApprenticeshipUploadRow> Rows)> ValidateApprenticeshipUploadRows(
            ISqlQueryDispatcher sqlQueryDispatcher,
            Guid apprenticeshipUploadId,
            Guid providerId,
            ApprenticeshipDataUploadRowInfoCollection rows)
        {
            var rowsAreValid = true;

            var providerVenues = await sqlQueryDispatcher.ExecuteQuery(new GetVenuesByProvider()
            {
                ProviderId = providerId
            });

            var allRegions = await _regionCache.GetAllRegions();

            var upsertRecords = new List <UpsertApprenticeshipUploadRowsRecord>();
            var allRows       = rows.Select(x => ParsedCsvApprenticeshipRow.FromCsvApprenticeshipRow(x.Data, allRegions));

            foreach (var row in rows)
            {
                var rowNumber = row.RowNumber;
                var apprenticeshipLocationId = Guid.NewGuid();

                var parsedRow = ParsedCsvApprenticeshipRow.FromCsvApprenticeshipRow(row.Data, allRegions);

                var matchedVenue = FindVenue(row, providerVenues);

                var validator = new ApprenticeshipUploadRowValidator(matchedVenue?.VenueId, allRows.ToList());

                var rowValidationResult = validator.Validate(parsedRow);
                var errors     = rowValidationResult.Errors.Select(e => e.ErrorCode).ToArray();
                var rowIsValid = rowValidationResult.IsValid;
                rowsAreValid &= rowIsValid;

                upsertRecords.Add(new UpsertApprenticeshipUploadRowsRecord()
                {
                    RowNumber                 = rowNumber,
                    IsValid                   = rowIsValid,
                    Errors                    = errors,
                    ApprenticeshipId          = row.ApprenticeshipId,
                    ApprenticeshipLocationId  = apprenticeshipLocationId,
                    StandardCode              = int.Parse(parsedRow.StandardCode),
                    StandardVersion           = int.Parse(parsedRow.StandardVersion),
                    ApprenticeshipInformation = parsedRow.ApprenticeshipInformation,
                    ApprenticeshipWebpage     = parsedRow.ApprenticeshipWebpage,
                    ContactEmail              = parsedRow.ContactEmail,
                    ContactPhone              = parsedRow.ContactPhone,
                    ContactUrl                = parsedRow.ContactUrl,
                    DeliveryMethod            = ParsedCsvApprenticeshipRow.MapDeliveryMethod(parsedRow.ResolvedDeliveryMethod) ?? parsedRow.DeliveryMethod,
                    VenueName                 = matchedVenue?.VenueName ?? parsedRow.VenueName,
                    YourVenueReference        = parsedRow.YourVenueReference,
                    Radius                    = parsedRow.Radius,
                    DeliveryModes             = ParsedCsvApprenticeshipRow.MapDeliveryModes(parsedRow.ResolvedDeliveryModes) ?? parsedRow.DeliveryModes,
                    NationalDelivery          = ParsedCsvApprenticeshipRow.MapNationalDelivery(parsedRow.ResolvedNationalDelivery) ?? parsedRow.NationalDelivery,
                    SubRegions                = parsedRow.SubRegion,
                    VenueId                   = matchedVenue?.VenueId,
                    ResolvedSubRegions        = parsedRow.ResolvedSubRegions?.Select(sr => sr.Id)?.ToArray(),
                    ResolvedDeliveryMethod    = parsedRow.ResolvedDeliveryMethod,
                    ResolvedDeliveryModes     = parsedRow.ResolvedDeliveryModes,
                    ResolvedNationalDelivery  = parsedRow.ResolvedNationalDelivery,
                    ResolvedRadius            = parsedRow.ResolvedRadius
                });
            }

            var updatedRows = await sqlQueryDispatcher.ExecuteQuery(new UpsertApprenticeshipUploadRows()
            {
                ApprenticeshipUploadId = apprenticeshipUploadId,
                ValidatedOn            = _clock.UtcNow,
                Records = upsertRecords
            });

            // If all the provided rows are valid check if there are any more invalid rows
            var uploadIsValid = rowsAreValid ?
                                (await sqlQueryDispatcher.ExecuteQuery(new GetApprenticeshipUploadInvalidRowCount()
            {
                ApprenticeshipUploadId = apprenticeshipUploadId
            })) == 0 :
                                false;

            await sqlQueryDispatcher.ExecuteQuery(new SetApprenticeshipUploadProcessed()
            {
                ApprenticeshipUploadId = apprenticeshipUploadId,
                ProcessingCompletedOn  = _clock.UtcNow,
                IsValid = uploadIsValid
            });

            var uploadStatus = await RefreshApprenticeshipUploadValidationStatus(apprenticeshipUploadId, sqlQueryDispatcher);

            return(uploadStatus, updatedRows);
        }