Skip to content

Commit

Permalink
Merge pull request #562 from bcgov/yj
Browse files Browse the repository at this point in the history
Yj
  • Loading branch information
ychung-mot committed Aug 23, 2024
2 parents 6015125 + 930a05a commit ec6e68b
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 48 deletions.
8 changes: 0 additions & 8 deletions server/StrDss.Api/Controllers/BizLicencesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,5 @@ public async Task<ActionResult> GetUploadHistory(long? orgId, int pageSize, int

return Ok(history);
}

//[ApiAuthorize()]
//[HttpGet("process")]
//public async Task<ActionResult> ProcessUpload()
//{
// await _bizLicenceService.ProcessBizLicenceUploadAsync();
// return Ok();
//}
}
}
11 changes: 11 additions & 0 deletions server/StrDss.Common/CommonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,16 @@ public static string SanitizeAndUppercaseString(string input)

return Regex.Replace(input, @"[^a-zA-Z0-9]", "").ToUpper();
}

public static bool IsValidEmailAddress(string email)
{
var valid = Regex.IsMatch(email, RegexDefs.GetRegexInfo(RegexDefs.Email).Regex);

if (!valid) return false;

if (email.StartsWith(".") || email.Contains(".@")) return false;

return true;
}
}
}
7 changes: 7 additions & 0 deletions server/StrDss.Data/Repositories/BizLicenceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using StrDss.Common;
using StrDss.Data.Entities;
using StrDss.Model;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace StrDss.Data.Repositories
Expand Down Expand Up @@ -152,7 +153,13 @@ INSERT INTO biz_lic_table (

public async Task ProcessBizLicTempTable(long lgId)
{
var processStopwatch = Stopwatch.StartNew();

await _dbContext.Database.ExecuteSqlRawAsync($"CALL dss_process_biz_lic_table({lgId});");

processStopwatch.Stop();

_logger.LogInformation($"Business Licence Link to Listings finished - {processStopwatch.Elapsed.TotalSeconds} seconds");
}

public async Task<(long?, string?)> GetMatchingBusinessLicenceIdAndNo(long orgId, string effectiveBizLicNo)
Expand Down
1 change: 1 addition & 0 deletions server/StrDss.Hangfire/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,6 @@
RecurringJob.AddOrUpdate<HangfireJobs>("Process Takedown Request Batch Emails", job => job.ProcessTakedownRequestBatchEmails(), "50 6 * * *");
RecurringJob.AddOrUpdate<HangfireJobs>("Create Rental Listing Export Files", job => job.CreateRentalListingExportFiles(), "50 5 * * *");
RecurringJob.AddOrUpdate<HangfireJobs>("Process Takedown Confirmation Report", job => job.ProcessTakedownConfirmation(), "0 * * * *");
RecurringJob.AddOrUpdate<HangfireJobs>("Process Business Licences", job => job.ProcessBusinessLicences(), "*/10 * * * *");

app.Run();
68 changes: 33 additions & 35 deletions server/StrDss.Service/BizLicenceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ public class BizLicenceService : ServiceBase, IBizLicenceService
private IUploadDeliveryRepository _uploadRepo;
private IOrganizationRepository _orgRepo;
private ICodeSetRepository _codeSetRepo;
private IRentalListingRepository _listingRepo;

public BizLicenceService(ICurrentUser currentUser, IFieldValidatorService validator, IUnitOfWork unitOfWork, IMapper mapper, IHttpContextAccessor httpContextAccessor, ILogger<StrDssLogger> logger,
IBizLicenceRepository bizLicenceRepo, IUploadDeliveryRepository uploadRepo, IOrganizationRepository orgRepo, ICodeSetRepository codeSetRepo)
IBizLicenceRepository bizLicenceRepo, IUploadDeliveryRepository uploadRepo, IOrganizationRepository orgRepo, ICodeSetRepository codeSetRepo, IRentalListingRepository listingRepo)
: base(currentUser, validator, unitOfWork, mapper, httpContextAccessor, logger)
{
_bizLicenceRepo = bizLicenceRepo;
_uploadRepo = uploadRepo;
_orgRepo = orgRepo;
_codeSetRepo = codeSetRepo;
_listingRepo = listingRepo;
}

public async Task<BizLicenceDto?> GetBizLicence(long businessLicenceId)
Expand All @@ -42,45 +44,53 @@ public BizLicenceService(ICurrentUser currentUser, IFieldValidatorService valida

public async Task ProcessBizLicenceUploadAsync()
{
var isListingUploadProcessRunning = await _listingRepo.IsListingUploadProcessRunning();

if (isListingUploadProcessRunning)
{
_logger.LogInformation("Skipping ProcessBizLicenceUploadAsync: Rental Listing Upload Process is running");
return;
}


if (!_validator.CommonCodes.Any())
{
_validator.CommonCodes = await _codeSetRepo.LoadCodeSetAsync();
}

var upload = await _uploadRepo.GetUploadToProcessAsync(UploadDeliveryTypes.LicenceData);

if (upload != null)
{
using var transaction = _unitOfWork.BeginTransaction();
if (upload == null) return;

var processStopwatch = Stopwatch.StartNew();

_logger.LogInformation($"Processing Business Licence Upload Id ({upload.UploadDeliveryId}): {upload.ProvidingOrganization.OrganizationNm}");

using var transaction = _unitOfWork.BeginTransaction();

await _bizLicenceRepo.CreateBizLicTempTable();

await ProcessBizLicenceUploadAsync(upload);

await _bizLicenceRepo.CreateBizLicTempTable();
_unitOfWork.Commit();

await ProcessBizLicenceUploadAsync(upload);
var errorCount = upload.DssUploadLines.Count(x => x.IsValidationFailure);

_unitOfWork.Commit();
if (errorCount == 0) await _bizLicenceRepo.ProcessBizLicTempTable(upload.ProvidingOrganizationId);

var errorCount = upload.DssUploadLines.Count(x => x.IsValidationFailure);
transaction.Commit();

if (errorCount == 0)
{
await _bizLicenceRepo.ProcessBizLicTempTable(upload.ProvidingOrganizationId);
_logger.LogInformation($"Success: Finished Business Licence Upload {upload.UploadDeliveryId} - {upload.ProvidingOrganizationId} - {upload.ProvidingOrganization.OrganizationNm}");
}
else
{
_logger.LogInformation($"Fail: Finished Business Licence Upload {upload.UploadDeliveryId} - {upload.ProvidingOrganizationId} - {upload.ProvidingOrganization.OrganizationNm}");
}
processStopwatch.Stop();

var msg = errorCount == 0 ?
$"Success: Finished Business Licence Upload Id ({upload.UploadDeliveryId}): {upload.ProvidingOrganization.OrganizationNm} - {processStopwatch.Elapsed.TotalSeconds} seconds" :
$"Fail: Finished Business Licence Upload Id ({upload.UploadDeliveryId}): {upload.ProvidingOrganization.OrganizationNm} - {processStopwatch.Elapsed.TotalSeconds} seconds";

transaction.Commit();
}
_logger.LogInformation($"Finished: Business Licence Upload Id ({upload.UploadDeliveryId}): {upload.ProvidingOrganization.OrganizationNm} - {processStopwatch.Elapsed.TotalSeconds} seconds");
}

private async Task ProcessBizLicenceUploadAsync(DssUploadDelivery upload)
{
var processStopwatch = Stopwatch.StartNew();

_logger.LogInformation($"Processing Business Licence Upload {upload.UploadDeliveryId} - {upload.ProvidingOrganizationId} - {upload.ProvidingOrganization.OrganizationNm}");

var count = 0;

var header = upload.SourceHeaderTxt ?? "";
Expand All @@ -90,8 +100,6 @@ private async Task ProcessBizLicenceUploadAsync(DssUploadDelivery upload)

foreach (var lineToProcess in linesToProcess)
{
var stopwatch = Stopwatch.StartNew();

var uploadLine = await _uploadRepo.GetUploadLineAsync(upload.UploadDeliveryId, lineToProcess.OrgCd, lineToProcess.SourceRecordNo);

count++;
Expand All @@ -103,15 +111,7 @@ private async Task ProcessBizLicenceUploadAsync(DssUploadDelivery upload)
var csvReader = new CsvReader(textReader, csvConfig);

var (orgCd, sourceRecordNo) = await ProcessLine(upload, header, uploadLine, csvReader);

stopwatch.Stop();

_logger.LogInformation($"Finishing line ({orgCd} - {sourceRecordNo}): {stopwatch.Elapsed.TotalMilliseconds} milliseconds. {count}/{lineCount}");
}

processStopwatch.Stop();

_logger.LogInformation($"Finished: {upload.ReportPeriodYm?.ToString("yyyy-MM")}, {upload.ProvidingOrganization.OrganizationNm} - {processStopwatch.Elapsed.TotalSeconds} seconds");
}

private async Task<(string orgCd, string sourceRecordNo)> ProcessLine(DssUploadDelivery upload, string header, DssUploadLine line, CsvReader csvReader)
Expand All @@ -124,8 +124,6 @@ private async Task ProcessBizLicenceUploadAsync(DssUploadDelivery upload)

var row = csvReader.GetRecord<BizLicenceRowUntyped>();

_logger.LogInformation($"Processing listing ({row.OrgCd} - {row.BusinessLicenceNo})");

var org = await _orgRepo.GetOrganizationByOrgCdAsync(row.OrgCd);

var errors = new Dictionary<string, List<string>>();
Expand Down
37 changes: 37 additions & 0 deletions server/StrDss.Service/BizLicenceValidationRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,34 @@ public static void LoadBizLicenceValidationRules(List<FieldValidationRule> rules
RegexInfo = RegexDefs.GetRegexInfo(RegexDefs.Email)
});

rules.Add(new FieldValidationRule
{
EntityName = Entities.BizLicenceRowUntyped,
FieldName = BizLicenceRowFields.BusinessOperatorNm,
FieldType = FieldTypes.String,
Required = false,
MaxLength = 320,
});

rules.Add(new FieldValidationRule
{
EntityName = Entities.BizLicenceRowUntyped,
FieldName = BizLicenceRowFields.BusinessOperatorPhoneNo,
FieldType = FieldTypes.String,
Required = false,
MaxLength = 30,
});

rules.Add(new FieldValidationRule
{
EntityName = Entities.BizLicenceRowUntyped,
FieldName = BizLicenceRowFields.BusinessOperatorEmailAddressDsc,
FieldType = FieldTypes.String,
Required = false,
MaxLength = 320,
RegexInfo = RegexDefs.GetRegexInfo(RegexDefs.Email)
});

rules.Add(new FieldValidationRule
{
EntityName = Entities.BizLicenceRowUntyped,
Expand All @@ -160,6 +188,15 @@ public static void LoadBizLicenceValidationRules(List<FieldValidationRule> rules
RegexInfo = RegexDefs.GetRegexInfo(RegexDefs.YearMonthDay)
});

rules.Add(new FieldValidationRule
{
EntityName = Entities.BizLicenceRowUntyped,
FieldName = BizLicenceRowFields.PropertyZoneTxt,
FieldType = FieldTypes.String,
Required = false,
MaxLength = 100,
});

rules.Add(new FieldValidationRule
{
EntityName = Entities.BizLicenceRowUntyped,
Expand Down
15 changes: 13 additions & 2 deletions server/StrDss.Service/Hangfire/HangfireJobs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ public class HangfireJobs
private IDelistingService _delistingService;
private IRentalListingService _listingService;
private ITakedownConfirmationService _tdcService;
private IBizLicenceService _bizLicService;

public HangfireJobs(IRentalListingReportService listingReportService, IRentalListingService listingService, IDelistingService delistingService, ITakedownConfirmationService tdcService)
public HangfireJobs(IRentalListingReportService listingReportService, IRentalListingService listingService, IDelistingService delistingService,
ITakedownConfirmationService tdcService, IBizLicenceService bizLicService)
{
_linstingReportService = listingReportService;
_delistingService = delistingService;
_listingService = listingService;
_tdcService = tdcService;
_bizLicService = bizLicService;
}

[Queue("default")]
Expand Down Expand Up @@ -57,6 +60,14 @@ public async Task ProcessTakedownConfirmation()
await _tdcService.ProcessTakedownConfrimationAsync();
}


[Queue("default")]
[SkipSameJob]
[AutomaticRetry(Attempts = 0)]
public async Task ProcessBusinessLicences()
{
await _bizLicService.ProcessBizLicenceUploadAsync();
}


}
}
4 changes: 1 addition & 3 deletions server/StrDss.Service/RentalListingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,11 @@ public async Task<PagedDto<RentalListingViewDto>> GetRentalListings(string? all,

private void ProcessHosts(RentalListingViewDto listing, bool clearHosts = false)
{
var emailRegex = RegexDefs.GetRegexInfo(RegexDefs.Email);

foreach (var host in listing.Hosts)
{
if (host.EmailAddressDsc == null) continue;

var hasValidEmail = Regex.IsMatch(host.EmailAddressDsc, emailRegex.Regex);
var hasValidEmail = CommonUtils.IsValidEmailAddress(host.EmailAddressDsc);

var hostType = host.IsPropertyOwner ? "Property Owner" : "Supplier Host";

Expand Down

0 comments on commit ec6e68b

Please sign in to comment.