diff --git a/server/StrDss.Data/Repositories/UploadDeliveryRepository.cs b/server/StrDss.Data/Repositories/UploadDeliveryRepository.cs index 6db78e28..0b4d2874 100644 --- a/server/StrDss.Data/Repositories/UploadDeliveryRepository.cs +++ b/server/StrDss.Data/Repositories/UploadDeliveryRepository.cs @@ -13,9 +13,11 @@ public interface IUploadDeliveryRepository Task IsDuplicateRentalReportUploadAsnyc(DateOnly periodYm, long orgId, string hashValue); Task AddUploadDeliveryAsync(DssUploadDelivery upload); Task GetRentalReportUploadToProcessAsync(); - Task GetRentalListingErrorLines(long uploadId); + Task GetRentalListingUploadWithErrors(long uploadId); Task GetUploadLineAsync(long uploadId, string orgCd, string listingId); Task> GetUploadLinesToProcessAsync(long uploadId); + Task GetUploadLineIdsWithErrors(long uploadId); + Task GetUploadLineWithError(long lineId); } public class UploadDeliveryRepository : RepositoryBase, IUploadDeliveryRepository @@ -72,13 +74,33 @@ public async Task> GetUploadLinesToProcessAsync(long u .ToListAsync(); } - public async Task GetRentalListingErrorLines(long uploadId) + public async Task GetRentalListingUploadWithErrors(long uploadId) { - //todo: data control + var query = _dbSet.AsNoTracking() + .Where(x => x.UploadDeliveryId == uploadId && x.DssUploadLines.Any(x => x.IsSystemFailure || x.IsValidationFailure)); + + if (_currentUser.OrganizationType == OrganizationTypes.Platform) + { + query = query.Where(x => x.ProvidingOrganizationId == _currentUser.OrganizationId); + } - return await _dbSet.AsNoTracking() - .Include(x => x.DssUploadLines) - .FirstOrDefaultAsync(x => x.UploadDeliveryId == uploadId); + return await query.FirstOrDefaultAsync(); + } + + public async Task GetUploadLineIdsWithErrors(long uploadId) + { + return await _dbContext.DssUploadLines.AsNoTracking() + .Where(x => x.IncludingUploadDeliveryId == uploadId && (x.IsValidationFailure || x.IsSystemFailure)) + .Select(x => x.UploadLineId) + .ToArrayAsync(); + } + + public async Task GetUploadLineWithError(long lineId) + { + return await _dbContext.DssUploadLines.AsNoTracking() + .Where(x => x.UploadLineId == lineId) + .Select(x => new UploadLineError { LineText = x.SourceLineTxt, ErrorText = x.ErrorTxt }) + .FirstAsync(); } } } diff --git a/server/StrDss.Model/UploadLineError.cs b/server/StrDss.Model/UploadLineError.cs new file mode 100644 index 00000000..a34e6aff --- /dev/null +++ b/server/StrDss.Model/UploadLineError.cs @@ -0,0 +1,8 @@ +namespace StrDss.Model +{ + public class UploadLineError + { + public string LineText { get; set; } = ""; + public string? ErrorText { get; set; } = ""; + } +} diff --git a/server/StrDss.Service/RentalListingReportService.cs b/server/StrDss.Service/RentalListingReportService.cs index bdc3bfbe..17d36616 100644 --- a/server/StrDss.Service/RentalListingReportService.cs +++ b/server/StrDss.Service/RentalListingReportService.cs @@ -619,17 +619,11 @@ public async Task> GetRentalListingUploadHi public async Task GetRentalListingErrorFile(long uploadId) { - var upload = await _uploadRepo.GetRentalListingErrorLines(uploadId); + var upload = await _uploadRepo.GetRentalListingUploadWithErrors(uploadId); if (upload == null) return null; - if (_currentUser.OrganizationType == OrganizationTypes.Platform) - { - if (upload.ProvidingOrganizationId != _currentUser.OrganizationId) - { - return null; - } - } + var linesWithError = await _uploadRepo.GetUploadLineIdsWithErrors(uploadId); var memoryStream = new MemoryStream(upload.SourceBin!); using TextReader textReader = new StreamReader(memoryStream, Encoding.UTF8); @@ -646,11 +640,10 @@ public async Task> GetRentalListingUploadHi contents.AppendLine(header); - foreach(var line in upload.DssUploadLines) + foreach (var lineId in linesWithError) { - if (!line.IsValidationFailure && !line.IsSystemFailure) continue; - - contents.AppendLine(line.SourceLineTxt.TrimEndNewLine() + $",\"{line.ErrorTxt}\""); + var line = await _uploadRepo.GetUploadLineWithError(lineId); + contents.AppendLine(line.LineText.TrimEndNewLine() + $",\"{line.ErrorText ?? ""}\""); } return Encoding.UTF8.GetBytes(contents.ToString());