Skip to content

Commit

Permalink
Merge pull request #355 from bcgov/yj
Browse files Browse the repository at this point in the history
chore: listing status update
  • Loading branch information
ychung-mot authored Jun 3, 2024
2 parents b01bbe3 + 33d88be commit 4be5c0f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 26 deletions.
58 changes: 41 additions & 17 deletions server/StrDss.Data/Repositories/RentalListingReportRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public interface IRentalListingReportRepository
void DeleteListingContacts(long listingId);
Task<PagedDto<RentalUploadHistoryViewDto>> GetRentalListingUploadHistory(long? platformId, int pageSize, int pageNumber, string orderBy, string direction);
Task<DssRentalUploadHistoryView?> GetRentalListingUpload(long deliveryId);
Task UpdateListingStatus(long providingPlatformId);
Task UpdateInactiveListings(long providingPlatformId);
Task UpdateListingStatus(long providingPlatformId, long listingId);
Task<int> GetTotalNumberOfUploadLines(long uploadId);
}
public class RentalListingReportRepository : RepositoryBase<DssRentalListingReport>, IRentalListingReportRepository
{
Expand Down Expand Up @@ -93,40 +95,62 @@ public async Task<PagedDto<RentalUploadHistoryViewDto>> GetRentalListingUploadHi
.FirstOrDefaultAsync(x => x.UploadDeliveryId == deliveryId);
}

public async Task UpdateListingStatus(long providingPlatformId)
public async Task UpdateInactiveListings(long providingPlatformId)
{
var reports = await _dbSet.AsNoTracking()
.Where(x => x.ProvidingOrganizationId == providingPlatformId && x.DssRentalListings.Any())
.ToListAsync();

var masterListings = await _dbContext.DssRentalListings
var latestPeriodYm = reports.Max(x => x.ReportPeriodYm);
var lastestReport = reports.First(x => x.ReportPeriodYm == latestPeriodYm);

var inactiveListings = await _dbContext.DssRentalListings
.Include(x => x.DerivedFromRentalListing)
.ThenInclude(x => x.IncludingRentalListingReport)
.Where(x => x.DerivedFromRentalListing != null && x.DerivedFromRentalListing.IncludingRentalListingReport!.ProvidingOrganizationId == providingPlatformId)
.Where(x => x.DerivedFromRentalListing != null &&
x.DerivedFromRentalListing.IncludingRentalListingReport!.ProvidingOrganizationId == providingPlatformId &&
x.DerivedFromRentalListing.IncludingRentalListingReport!.RentalListingReportId != lastestReport.RentalListingReportId)
.ToArrayAsync();

foreach (var listing in inactiveListings)
{
listing.IsActive = false;
listing.IsNew = false;
}
}

public async Task UpdateListingStatus(long providingPlatformId, long listingId)
{
var reports = await _dbSet.AsNoTracking()
.Where(x => x.ProvidingOrganizationId == providingPlatformId && x.DssRentalListings.Any())
.ToListAsync();

var listing = await _dbContext.DssRentalListings
.Include(x => x.DerivedFromRentalListing)
.ThenInclude(x => x.IncludingRentalListingReport)
.FirstAsync(x => x.RentalListingId == listingId);

if (reports.Count == 0)
{
foreach (var listing in masterListings)
{
listing.IsActive = true;
listing.IsNew = true;
}
listing.IsActive = true;
listing.IsNew = true;
}
else
{
var latestPeriodYm = reports.Max(x => x.ReportPeriodYm);

foreach (var listing in masterListings)
{
var isActive = listing.DerivedFromRentalListing!.IncludingRentalListingReport!.ReportPeriodYm == latestPeriodYm;
var count = _dbContext.DssRentalListings
.Count(x => x.OfferingOrganizationId == listing.OfferingOrganizationId && x.PlatformListingNo == listing.PlatformListingNo && x.DerivedFromRentalListing == null);
var isActive = listing.DerivedFromRentalListing!.IncludingRentalListingReport!.ReportPeriodYm == latestPeriodYm;
var count = _dbContext.DssRentalListings
.Count(x => x.OfferingOrganizationId == listing.OfferingOrganizationId && x.PlatformListingNo == listing.PlatformListingNo && x.DerivedFromRentalListing == null);

listing.IsActive = isActive;
listing.IsNew = isActive && count == 1;
}
listing.IsActive = isActive;
listing.IsNew = isActive && count == 1;
}
}

public async Task<int> GetTotalNumberOfUploadLines(long uploadId)
{
return await _dbContext.DssUploadLines.Where(x => x.IncludingUploadDeliveryId == uploadId).CountAsync();
}
}
}
45 changes: 36 additions & 9 deletions server/StrDss.Service/RentalListingReportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ private async Task ProcessRentalReportUploadAsync(DssUploadDelivery upload)
_logger.LogInformation($"Processing Rental Listing Report {upload.ProvidingOrganizationId} - {upload.ReportPeriodYm} - {upload.ProvidingOrganization.OrganizationNm}");

var reportPeriodYm = (DateOnly)upload.ReportPeriodYm!;
var lineCount = await _reportRepo.GetTotalNumberOfUploadLines(upload.UploadDeliveryId);
var count = 0;

var report = await _reportRepo.GetRentalListingReportAsync(upload.ProvidingOrganizationId, reportPeriodYm);

Expand Down Expand Up @@ -330,9 +332,13 @@ private async Task ProcessRentalReportUploadAsync(DssUploadDelivery upload)
csv.Read();
var headerExists = csv.ReadHeader();
var hasError = false;
var isLastLine = false;

while (csv.Read())
{
count++;
isLastLine = count == lineCount;

var row = csv.GetRecord<RentalListingRowUntyped>(); //it has been parsed once, so no exception expected.

_logger.LogInformation($"Fetching listing: {report.ProvidingOrganization.OrganizationNm}, {row.ListingId}");
Expand All @@ -348,7 +354,7 @@ private async Task ProcessRentalReportUploadAsync(DssUploadDelivery upload)

_logger.LogInformation($"Processing listing: {report.ProvidingOrganization.OrganizationNm}, {row.ListingId}");

hasError = !await ProcessUploadLine(report, upload, uploadLine, row, csv.Parser.RawRecord);
hasError = !await ProcessUploadLine(report, upload, uploadLine, row, isLastLine);

_logger.LogInformation($"Finishing listing: {report.ProvidingOrganization.OrganizationNm}, {row.ListingId}");
}
Expand Down Expand Up @@ -409,23 +415,24 @@ private async Task ProcessRentalReportUploadAsync(DssUploadDelivery upload)
_unitOfWork.CommitTransaction(transaction);
}

_logger.LogInformation($"Updating Status: {report.ReportPeriodYm.ToString("yyyy-MM")}, {report.ProvidingOrganization.OrganizationNm}");

await _reportRepo.UpdateListingStatus(report.ProvidingOrganizationId);

_unitOfWork.Commit();

_logger.LogInformation($"Finished: {report.ReportPeriodYm.ToString("yyyy-MM")}, {report.ProvidingOrganization.OrganizationNm}");
}

private async Task<bool> ProcessUploadLine(DssRentalListingReport report, DssUploadDelivery upload, DssUploadLine uploadLine, RentalListingRowUntyped row, string rawRecord)
private async Task<bool> ProcessUploadLine(DssRentalListingReport report, DssUploadDelivery upload, DssUploadLine uploadLine, RentalListingRowUntyped row, bool isLastLine)
{
var errors = new Dictionary<string, List<string>>();

_validator.Validate(Entities.RentalListingRowUntyped, row, errors);

if (errors.Count > 0)
{
SaveUploadLine(uploadLine, errors, true, "");

if (isLastLine)
{
await UpdateInactiveListings(upload.ProvidingOrganizationId);
}

_unitOfWork.Commit();
return false;
}
Expand All @@ -448,6 +455,12 @@ private async Task<bool> ProcessUploadLine(DssRentalListingReport report, DssUpl

if (systemError.IsNotEmpty())
{
if (isLastLine)
{
await UpdateInactiveListings(upload.ProvidingOrganizationId);
}

tran.Commit();
return false;
}

Expand All @@ -460,11 +473,25 @@ private async Task<bool> ProcessUploadLine(DssRentalListingReport report, DssUpl

_unitOfWork.Commit();

tran.Commit();
await _reportRepo.UpdateListingStatus(upload.ProvidingOrganizationId, masterListing.RentalListingId);

_unitOfWork.Commit();

if (isLastLine)
{
await UpdateInactiveListings(upload.ProvidingOrganizationId);
}

tran.Commit();
return true;
}

private async Task UpdateInactiveListings(long providingOrganizationId)
{
await _reportRepo.UpdateInactiveListings(providingOrganizationId);
_unitOfWork.Commit();
}

private void SaveUploadLine(DssUploadLine uploadLine, Dictionary<string, List<string>> errors, bool isValid, string systemError)
{
uploadLine.IsValidationFailure = isValid;
Expand Down

0 comments on commit 4be5c0f

Please sign in to comment.