Skip to content

Commit

Permalink
Merge pull request #742 from bcgov/yj
Browse files Browse the repository at this point in the history
Yj
  • Loading branch information
ychung-mot authored Oct 24, 2024
2 parents 7dc12b9 + c058521 commit ea71a85
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 11 deletions.
20 changes: 20 additions & 0 deletions postman/str-dss.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,26 @@
},
"response": []
},
{
"name": "export file for finance",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{host}}/api/rentallistings/exports/fin",
"host": [
"{{host}}"
],
"path": [
"api",
"rentallistings",
"exports",
"fin"
]
}
},
"response": []
},
{
"name": "Confirm Address",
"request": {
Expand Down
25 changes: 25 additions & 0 deletions server/StrDss.Api/Controllers/RentalListingsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using StrDss.Model;
using StrDss.Model.RentalReportDtos;
using StrDss.Service;
using Swashbuckle.AspNetCore.Annotations;

namespace StrDss.Api.Controllers
{
Expand Down Expand Up @@ -110,6 +111,30 @@ public async Task<ActionResult> GetRetalListingExportAsync(long extractId)
return File(extract.SourceBin!, "application/zip", $"STRlisting_{extract.RentalListingExtractNm}_{extract.UpdDtm.ToString("yyyyMMdd")}.zip");
}

/// <summary>
/// Downloads a zipped CSV file containing all listings reported by platforms
/// </summary>
/// <returns>A zipped CSV file</returns>
[ApiAuthorize(Permissions.ListingRead)]
[SwaggerOperation(Tags = new string[] { Common.ApiTags.Aps })]
[HttpGet("exports/fin")]
public async Task<ActionResult<FileContentResult>> GetRetalListingExportFin()
{
var extract = await _listingService.GetRetalListingExportByNameAsync(ListingExportFileNames.Fin);

if (extract == null)
{
return NotFound();
}

if (_currentUser.OrganizationType == OrganizationTypes.LG && extract.FilteringOrganizationId != _currentUser.OrganizationId)
{
return Unauthorized();
}

return File(extract.SourceBin!, "application/zip", $"STRlisting_{extract.RentalListingExtractNm}_{extract.UpdDtm.ToString("yyyyMMdd")}.zip");
}

[ApiAuthorize(Permissions.AddressWrite)]
[HttpPut("{rentalListingId}/address")]
public async Task<ActionResult> UpdateAddress(long rentalListingId, UpdateListingAddressDto dto)
Expand Down
85 changes: 85 additions & 0 deletions server/StrDss.Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,94 @@ public static class RentalListingExport
"Date of Previous Action Taken 2"
};

public static readonly string[] FinHeaders = new string[]
{
"Most Recent Platform Report Month",
"Status",
"Jurisdiction assigned to",
"economic_region_name",
"pr_requirement",
"BL_requirement",
"Platform Code",
"Listing ID",
"URL Address",
"Platform listing address",
"Geocoder Best match address (current month) - complete address",
"Geocoder Best match address (current month) -city only",
"Local Government Business Licence Number",
"Entire Unit",
"Number of Bedrooms available for STR",
"Current Month",
"Number of nights booked (Current month)",
"Number of nights booked (Current month - 1)",
"Number of nights booked (Current month - 2)",
"Number of nights booked (Current month - 3)",
"Number of nights booked (Current month - 4)",
"Number of nights booked (Current month - 5)",
"Number of nights booked (Current month - 6)",
"Number of nights booked (Current month - 7)",
"Number of nights booked (Current month - 8)",
"Number of nights booked (Current month - 9)",
"Number of nights booked (Current month - 10)",
"Number of nights booked (Current month - 11)",
"Number of separate reservations (Current month)",
"Number of separate reservations (Current month - 1)",
"Number of separate reservations (Current month - 2)",
"Number of separate reservations (Current month - 3)",
"Number of separate reservations (Current month - 4)",
"Number of separate reservations (Current month - 5)",
"Number of separate reservations (Current month - 6)",
"Number of separate reservations (Current month - 7)",
"Number of separate reservations (Current month - 8)",
"Number of separate reservations (Current month - 9)",
"Number of separate reservations (Current month - 10)",
"Number of separate reservations (Current month - 11)",
"Property Host name",
"Property Host email address",
"Property Host phone number",
"Property Host fax number",
"Property Host Mailing Address",
"Supplier Host 1 name",
"Supplier Host 1 email address",
"Supplier Host 1 phone number",
"Supplier Host 1 fax number",
"Supplier Host 1 Mailing Address",
"Host ID of Supplier Host 1",
"Supplier Host 2 name",
"Supplier Host 2 email address",
"Supplier Host 2 phone number",
"Supplier Host 2 fax number",
"Supplier Host 2 Mailing Address",
"Host ID of Supplier Host 2",
"Supplier Host 3 name",
"Supplier Host 3 email address",
"Supplier Host 3 phone number",
"Supplier Host 3 fax number",
"Supplier Host 3 Mailing Address",
"Host ID of Supplier Host 3",
"Supplier Host 4 name",
"Supplier Host 4 email address",
"Supplier Host 4 phone number",
"Supplier Host 4 fax number",
"Supplier Host 4 Mailing Address",
"Host ID of Supplier Host 4",
"Supplier Host 5 name",
"Supplier Host 5 email address",
"Supplier Host 5 phone number",
"Supplier Host 5 fax number",
"Supplier Host 5 Mailing Address",
"Host ID of Supplier Host 5"
};

public static string GetHeadersAsCsv()
{
return string.Join(",", Headers);
}

public static string GetFinHeadersAsCsv()
{
return string.Join(",", FinHeaders);
}
}

public static class RoleFields
Expand Down Expand Up @@ -362,6 +446,7 @@ public static class ListingExportFileNames
{
public const string All = "BC";
public const string AllPr = "BC_PR";
public const string Fin = "BC_FIN";
}

public static class ApiTags
Expand Down
13 changes: 13 additions & 0 deletions server/StrDss.Data/Repositories/RentalListingRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Task<int> GetGroupedRentalListingsCount(string? all, string? address, string? ur
Task<DssRentalListingExtract> GetOrCreateRentalListingExtractByExtractNm(string name);
Task<List<RentalListingExtractDto>> GetRetalListingExportsAsync();
Task<RentalListingExtractDto?> GetRetalListingExportAsync(long extractId);
Task<RentalListingExtractDto?> GetRetalListingExportByNameAsync(string extractName);
Task ConfirmAddressAsync(long rentalListingId);
Task<DssRentalListing> UpdateAddressAsync(UpdateListingAddressDto dto);
DateTime GetLatestRentalListingExportTime();
Expand Down Expand Up @@ -648,6 +649,18 @@ public async Task<DssRentalListingExtract> GetOrCreateRentalListingExtractByExtr
return _mapper.Map<RentalListingExtractDto>(extract);
}

public async Task<RentalListingExtractDto?> GetRetalListingExportByNameAsync(string extractName)
{
var extract = await _dbContext
.DssRentalListingExtracts
.AsNoTracking()
.FirstOrDefaultAsync(x => x.RentalListingExtractNm == extractName);

if (extract == null) return null;

return _mapper.Map<RentalListingExtractDto>(extract);
}

public async Task<List<RentalListingExtractDto>> GetRetalListingExportsAsync()
{
var datasets = _mapper.Map<List<RentalListingExtractDto>>(await _dbContext.DssRentalListingExtracts.AsNoTracking().ToListAsync());
Expand Down
44 changes: 33 additions & 11 deletions server/StrDss.Service/RentalListingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Task<int> GetGroupedRentalListingsCount(string? all, string? address, string? ur
Task CreateRentalListingExportFiles();
Task<List<RentalListingExtractDto>> GetRetalListingExportsAsync();
Task<RentalListingExtractDto?> GetRetalListingExportAsync(long extractId);
Task<RentalListingExtractDto?> GetRetalListingExportByNameAsync(string extractName);
Task<List<AddressDto>> GetAddressCandidatesAsync(string addressText, int maxResults);
Task<Dictionary<string, List<string>>> ConfirmAddressAsync(long rentalListingId);
Task<Dictionary<string, List<string>>> UpdateAddressAsync(UpdateListingAddressDto dto);
Expand Down Expand Up @@ -148,9 +149,11 @@ public async Task CreateRentalListingExportFiles()

var listingIds = await _listingRepo.GetRentalListingIdsToExport();
var headers = RentalListingExport.GetHeadersAsCsv();
var finHeaders = RentalListingExport.GetFinHeadersAsCsv();

var lgExport = InitializeExport(headers);
var allExport = InitializeExport(headers);
var finExport = InitializeExport(finHeaders);
var prExport = InitializeExport(headers);
var count = 0;
var totalCount = listingIds.Count;
Expand Down Expand Up @@ -179,16 +182,19 @@ public async Task CreateRentalListingExportFiles()
}

var line = ToCsvString(listing);
var finLine = ToCsvString(listing, true);

lgExport.Add(line);
allExport.Add(line);
finExport.Add(finLine);

if (listing.IsPrincipalResidenceRequired == true)
{
prExport.Add(line);
}
}

await CreateFinalExports(allExport, prExport, lgExport, lg, lgId);
await CreateFinalExports(allExport, finExport, prExport, lgExport, lg, lgId);
stopWatchForAll.Stop();
_logger.LogInformation($"Rental Listing Export - Finished - {stopWatchForAll.Elapsed.TotalSeconds} seconds");
}
Expand All @@ -214,7 +220,7 @@ private async Task ProcessExportForLocalGovernment(List<string> export, long org
}
}

private async Task CreateFinalExports(List<string> allExport, List<string> prExport, List<string> lgExport, string lg, long lgId)
private async Task CreateFinalExports(List<string> allExport, List<string> finExport, List<string> prExport, List<string> lgExport, string lg, long lgId)
{
var date = DateUtils.ConvertUtcToPacificTime(DateTime.UtcNow).ToString("yyyyMMdd");

Expand All @@ -224,6 +230,15 @@ private async Task CreateFinalExports(List<string> allExport, List<string> prExp
var extract = await _listingRepo.GetOrCreateRentalListingExtractByExtractNm(ListingExportFileNames.All);
extract.SourceBin = CommonUtils.CreateZip(string.Join("\r\n", allExport), $"STRlisting_{ListingExportFileNames.All}_{date}");
extract.IsPrRequirementFiltered = false;
_unitOfWork.Commit();
}

if (finExport.Count > 1)
{
_logger.LogInformation("Rental Listing Export - Creating a zip file for all rental listings for FIN");
var extract = await _listingRepo.GetOrCreateRentalListingExtractByExtractNm(ListingExportFileNames.Fin);
extract.SourceBin = CommonUtils.CreateZip(string.Join("\r\n", finExport), $"STRlisting_{ListingExportFileNames.Fin}_{date}");
extract.IsPrRequirementFiltered = false;
_unitOfWork.Commit();
}

Expand All @@ -239,10 +254,8 @@ private async Task CreateFinalExports(List<string> allExport, List<string> prExp
await ProcessExportForLocalGovernment(lgExport, lgId, lg);
}

private static string ToCsvString(RentalListingExportDto listing)
private static string ToCsvString(RentalListingExportDto listing, bool isFin = false)
{


var builder = new StringBuilder();

builder.Append(FormatCsvField(listing.LatestReportPeriodYm)).Append(','); // Most Recent Platform Report Month
Expand Down Expand Up @@ -320,12 +333,16 @@ private static string ToCsvString(RentalListingExportDto listing)
builder.Append(FormatCsvField(listing.SupplierHost5FaxNumber)).Append(','); // Supplier Host 5 fax number
builder.Append(FormatCsvField(listing.SupplierHost5MailingAddress)).Append(','); // Supplier Host 5 Mailing Address
builder.Append(FormatCsvField(listing.SupplierHost5Id)).Append(','); // Host ID of Supplier Host 5
builder.Append(FormatCsvField(listing.LastActionNm)).Append(','); // Last Action Taken
builder.Append(FormatCsvField(listing.LastActionDtm)).Append(','); // Date of Last Action Taken
builder.Append(FormatCsvField(listing.LastActionNm1)).Append(','); // Last Action Taken 1
builder.Append(FormatCsvField(listing.LastActionDtm1)).Append(','); // Date of Last Action Taken 1
builder.Append(FormatCsvField(listing.LastActionNm2)).Append(','); // Previous Action Taken 2
builder.Append(FormatCsvField(listing.LastActionDtm2)); // Date of Previous Action Taken 2

if (!isFin)
{
builder.Append(FormatCsvField(listing.LastActionNm)).Append(','); // Last Action Taken
builder.Append(FormatCsvField(listing.LastActionDtm)).Append(','); // Date of Last Action Taken
builder.Append(FormatCsvField(listing.LastActionNm1)).Append(','); // Last Action Taken 1
builder.Append(FormatCsvField(listing.LastActionDtm1)).Append(','); // Date of Last Action Taken 1
builder.Append(FormatCsvField(listing.LastActionNm2)).Append(','); // Previous Action Taken 2
builder.Append(FormatCsvField(listing.LastActionDtm2)); // Date of Previous Action Taken 2
}

return builder.ToString();
}
Expand Down Expand Up @@ -373,6 +390,11 @@ private static string FormatCsvField(object? field)
return await _listingRepo.GetRetalListingExportAsync(extractId);
}

public async Task<RentalListingExtractDto?> GetRetalListingExportByNameAsync(string extractName)
{
return await _listingRepo.GetRetalListingExportByNameAsync(extractName);
}

public async Task<List<RentalListingExtractDto>> GetRetalListingExportsAsync()
{
return await _listingRepo.GetRetalListingExportsAsync();
Expand Down

0 comments on commit ea71a85

Please sign in to comment.