Skip to content

Commit 70627c8

Browse files
authored
Merge pull request #547 from bcgov/yj
feat: business license processing
2 parents 659250e + edb017d commit 70627c8

File tree

8 files changed

+307
-9
lines changed

8 files changed

+307
-9
lines changed

database/ddl/STR_DSS_Views_Sprint_9.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ CREATE OR REPLACE VIEW dss_rental_listing_vw AS select drl.rental_listing_id
4545
, drl.nights_booked_qty as nights_booked_ytd_qty
4646
, drl.separate_reservations_qty as separate_reservations_ytd_qty
4747
, drl.business_licence_no
48+
, drl.
4849
, drl.bc_registry_no
4950
, demt.email_message_type_nm as last_action_nm
5051
, dem.message_delivery_dtm as last_action_dtm
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
CREATE OR REPLACE PROCEDURE dss_process_biz_lic_table()
2+
LANGUAGE plpgsql
3+
AS $$
4+
BEGIN
5+
6+
-- Deletion
7+
8+
9+
-- Insert into dss_business_licence from biz_lic_table or update if exists
10+
INSERT INTO public.dss_business_licence (
11+
business_licence_no, expiry_dt, physical_rental_address_txt, licence_type_txt, restriction_txt,
12+
business_nm, mailing_street_address_txt, mailing_city_nm, mailing_province_cd, mailing_postal_cd,
13+
business_owner_nm, business_owner_phone_no, business_owner_email_address_dsc, business_operator_nm,
14+
business_operator_phone_no, business_operator_email_address_dsc, infraction_txt, infraction_dt,
15+
property_zone_txt, available_bedrooms_qty, max_guests_allowed_qty, is_principal_residence,
16+
is_owner_living_onsite, is_owner_property_tenant, property_folio_no, property_parcel_identifier_no,
17+
property_legal_description_txt, licence_status_type, providing_organization_id, upd_dtm
18+
)
19+
SELECT
20+
business_licence_no, expiry_dt, physical_rental_address_txt, licence_type_txt, restriction_txt,
21+
business_nm, mailing_street_address_txt, mailing_city_nm, mailing_province_cd, mailing_postal_cd,
22+
business_owner_nm, business_owner_phone_no, business_owner_email_address_dsc, business_operator_nm,
23+
business_operator_phone_no, business_operator_email_address_dsc, infraction_txt, infraction_dt,
24+
property_zone_txt, available_bedrooms_qty, max_guests_allowed_qty,
25+
CASE WHEN is_principal_residence = 'Y' THEN TRUE ELSE FALSE END, -- Convert 'Y'/'N' to boolean
26+
CASE WHEN is_owner_living_onsite = 'Y' THEN TRUE ELSE FALSE END, -- Convert 'Y'/'N' to boolean
27+
CASE WHEN is_owner_property_tenant = 'Y' THEN TRUE ELSE FALSE END, -- Convert 'Y'/'N' to boolean
28+
property_folio_no, property_parcel_identifier_no, property_legal_description_txt,
29+
licence_status_type, providing_organization_id,
30+
NOW() -- Set the current timestamp for upd_dtm
31+
FROM biz_lic_table
32+
ON CONFLICT (providing_organization_id, business_licence_no)
33+
DO UPDATE
34+
SET
35+
expiry_dt = EXCLUDED.expiry_dt,
36+
physical_rental_address_txt = EXCLUDED.physical_rental_address_txt,
37+
licence_type_txt = EXCLUDED.licence_type_txt,
38+
restriction_txt = EXCLUDED.restriction_txt,
39+
business_nm = EXCLUDED.business_nm,
40+
mailing_street_address_txt = EXCLUDED.mailing_street_address_txt,
41+
mailing_city_nm = EXCLUDED.mailing_city_nm,
42+
mailing_province_cd = EXCLUDED.mailing_province_cd,
43+
mailing_postal_cd = EXCLUDED.mailing_postal_cd,
44+
business_owner_nm = EXCLUDED.business_owner_nm,
45+
business_owner_phone_no = EXCLUDED.business_owner_phone_no,
46+
business_owner_email_address_dsc = EXCLUDED.business_owner_email_address_dsc,
47+
business_operator_nm = EXCLUDED.business_operator_nm,
48+
business_operator_phone_no = EXCLUDED.business_operator_phone_no,
49+
business_operator_email_address_dsc = EXCLUDED.business_operator_email_address_dsc,
50+
infraction_txt = EXCLUDED.infraction_txt,
51+
infraction_dt = EXCLUDED.infraction_dt,
52+
property_zone_txt = EXCLUDED.property_zone_txt,
53+
available_bedrooms_qty = EXCLUDED.available_bedrooms_qty,
54+
max_guests_allowed_qty = EXCLUDED.max_guests_allowed_qty,
55+
is_principal_residence = EXCLUDED.is_principal_residence,
56+
is_owner_living_onsite = EXCLUDED.is_owner_living_onsite,
57+
is_owner_property_tenant = EXCLUDED.is_owner_property_tenant,
58+
property_folio_no = EXCLUDED.property_folio_no,
59+
property_parcel_identifier_no = EXCLUDED.property_parcel_identifier_no,
60+
property_legal_description_txt = EXCLUDED.property_legal_description_txt,
61+
licence_status_type = EXCLUDED.licence_status_type,
62+
upd_dtm = NOW(); -- Update the update timestamp
63+
64+
-- Optional: Truncate the temporary table after processing
65+
TRUNCATE TABLE biz_lic_table;
66+
67+
-- Notify of completion
68+
RAISE NOTICE 'Data has been processed from biz_lic_table to dss_business_licence';
69+
70+
END $$;

server/StrDss.Api/Controllers/BizLicensesController.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,13 @@ public async Task<ActionResult> GetUploadHistory(long? orgId, int pageSize, int
7272

7373
return Ok(history);
7474
}
75+
76+
//[ApiAuthorize()]
77+
//[HttpGet("process")]
78+
//public async Task<ActionResult> ProcessUpload()
79+
//{
80+
// await _bizLicenseService.ProcessBizLicenseUploadAsync();
81+
// return Ok();
82+
//}
7583
}
7684
}

server/StrDss.Data/Repositories/BizLicenseRepository.cs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using AutoMapper;
22
using Microsoft.EntityFrameworkCore;
33
using Microsoft.Extensions.Logging;
4+
using Npgsql;
45
using StrDss.Common;
56
using StrDss.Data.Entities;
67
using StrDss.Model;
@@ -10,6 +11,9 @@ namespace StrDss.Data.Repositories
1011
public interface IBizLicenseRepository
1112
{
1213
Task<BizLicenseDto?> GetBizLicense(long businessLicenceId);
14+
Task CreateBizLicTempTable();
15+
Task InsertRowToBizLicTempTable(BizLicenseRowUntyped row, long providingOrganizationId);
16+
Task ProcessBizLicTempTable();
1317
}
1418

1519
public class BizLicenseRepository : RepositoryBase<DssBusinessLicence>, IBizLicenseRepository
@@ -23,5 +27,109 @@ public BizLicenseRepository(DssDbContext dbContext, IMapper mapper, ICurrentUser
2327
{
2428
return _mapper.Map<BizLicenseDto>(await _dbSet.AsNoTracking().FirstOrDefaultAsync(x => x.BusinessLicenceId == businessLicenceId));
2529
}
30+
31+
public async Task CreateBizLicTempTable()
32+
{
33+
await _dbContext.Database.ExecuteSqlRawAsync(@"
34+
CREATE TEMP TABLE biz_lic_table (
35+
business_licence_no varchar(50) NOT NULL,
36+
expiry_dt date NOT NULL,
37+
physical_rental_address_txt varchar(250) NULL,
38+
licence_type_txt varchar(320) NULL,
39+
restriction_txt varchar(320) NULL,
40+
business_nm varchar(320) NULL,
41+
mailing_street_address_txt varchar(100) NULL,
42+
mailing_city_nm varchar(100) NULL,
43+
mailing_province_cd varchar(2) NULL,
44+
mailing_postal_cd varchar(10) NULL,
45+
business_owner_nm varchar(320) NULL,
46+
business_owner_phone_no varchar(30) NULL,
47+
business_owner_email_address_dsc varchar(320) NULL,
48+
business_operator_nm varchar(320) NULL,
49+
business_operator_phone_no varchar(30) NULL,
50+
business_operator_email_address_dsc varchar(320) NULL,
51+
infraction_txt varchar(320) NULL,
52+
infraction_dt date NULL,
53+
property_zone_txt varchar(100) NULL,
54+
available_bedrooms_qty int2 NULL,
55+
max_guests_allowed_qty int2 NULL,
56+
is_principal_residence bool NULL,
57+
is_owner_living_onsite bool NULL,
58+
is_owner_property_tenant bool NULL,
59+
property_folio_no varchar(30) NULL,
60+
property_parcel_identifier_no varchar(30) NULL,
61+
property_legal_description_txt varchar(320) NULL,
62+
licence_status_type varchar(25) NOT NULL,
63+
providing_organization_id int8 NOT NULL
64+
);
65+
");
66+
}
67+
68+
public async Task InsertRowToBizLicTempTable(BizLicenseRowUntyped row, long providingOrganizationId)
69+
{
70+
var insertSql = @"
71+
INSERT INTO biz_lic_table (
72+
business_licence_no, expiry_dt, physical_rental_address_txt, licence_type_txt, restriction_txt, business_nm, mailing_street_address_txt,
73+
mailing_city_nm, mailing_province_cd, mailing_postal_cd, business_owner_nm, business_owner_phone_no, business_owner_email_address_dsc,
74+
business_operator_nm, business_operator_phone_no, business_operator_email_address_dsc, infraction_txt, infraction_dt,
75+
property_zone_txt, available_bedrooms_qty, max_guests_allowed_qty, is_principal_residence, is_owner_living_onsite,
76+
is_owner_property_tenant, property_folio_no, property_parcel_identifier_no, property_legal_description_txt,
77+
licence_status_type, providing_organization_id
78+
)
79+
VALUES (
80+
@BusinessLicenceNo, @ExpiryDt, @PhysicalRentalAddressTxt, @LicenceTypeTxt, @RestrictionTxt, @BusinessNm, @MailingStreetAddressTxt,
81+
@MailingCityNm, @MailingProvinceCd, @MailingPostalCd, @BusinessOwnerNm, @BusinessOwnerPhoneNo, @BusinessOwnerEmailAddressDsc,
82+
@BusinessOperatorNm, @BusinessOperatorPhoneNo, @BusinessOperatorEmailAddressDsc, @InfractionTxt, @InfractionDt,
83+
@PropertyZoneTxt, @AvailableBedroomsQty, @MaxGuestsAllowedQty, @IsPrincipalResidence, @IsOwnerLivingOnsite,
84+
@IsOwnerPropertyTenant, @PropertyFolioNo, @PropertyParcelIdentifierNo, @PropertyLegalDescriptionTxt,
85+
@LicenceStatusType, @ProvidingOrganizationId
86+
);";
87+
88+
bool ConvertToBool(string value) => value?.ToUpper() == "Y";
89+
90+
DateTime? ConvertToDate(string value) => DateTime.TryParseExact(value, "yyyy-MM-dd", null, System.Globalization.DateTimeStyles.None, out var date) ? date : (DateTime?) null;
91+
92+
short? ConvertToInt2(string value) => short.TryParse(value, out var result) ? result : (short?)null;
93+
94+
var parameters = new List<NpgsqlParameter>
95+
{
96+
new NpgsqlParameter("@businessLicenceNo", row.BusinessLicenceNo),
97+
new NpgsqlParameter("@expiryDt", ConvertToDate(row.ExpiryDt)),
98+
new NpgsqlParameter("@physicalRentalAddressTxt", row.PhysicalRentalAddressTxt ?? (object)DBNull.Value),
99+
new NpgsqlParameter("@licenceTypeTxt", row.LicenceTypeTxt ?? (object)DBNull.Value),
100+
new NpgsqlParameter("@restrictionTxt", row.RestrictionTxt ?? (object)DBNull.Value),
101+
new NpgsqlParameter("@businessNm", row.BusinessNm ?? (object)DBNull.Value),
102+
new NpgsqlParameter("@mailingStreetAddressTxt", row.MailingStreetAddressTxt ?? (object)DBNull.Value),
103+
new NpgsqlParameter("@mailingCityNm", row.MailingCityNm ?? (object)DBNull.Value),
104+
new NpgsqlParameter("@mailingProvinceCd", row.MailingProvinceCd ?? (object)DBNull.Value),
105+
new NpgsqlParameter("@mailingPostalCd", row.MailingPostalCd ?? (object)DBNull.Value),
106+
new NpgsqlParameter("@businessOwnerNm", row.BusinessOwnerNm ?? (object)DBNull.Value),
107+
new NpgsqlParameter("@businessOwnerPhoneNo", row.BusinessOwnerPhoneNo ?? (object)DBNull.Value),
108+
new NpgsqlParameter("@businessOwnerEmailAddressDsc", row.BusinessOwnerEmailAddressDsc ?? (object)DBNull.Value),
109+
new NpgsqlParameter("@businessOperatorNm", row.BusinessOperatorNm ?? (object)DBNull.Value),
110+
new NpgsqlParameter("@businessOperatorPhoneNo", row.BusinessOperatorPhoneNo ?? (object)DBNull.Value),
111+
new NpgsqlParameter("@businessOperatorEmailAddressDsc", row.BusinessOperatorEmailAddressDsc ?? (object)DBNull.Value),
112+
new NpgsqlParameter("@infractionTxt", row.InfractionTxt ?? (object)DBNull.Value),
113+
new NpgsqlParameter("@infractionDt", ConvertToDate(row.InfractionDt) ?? (object)DBNull.Value),
114+
new NpgsqlParameter("@propertyZoneTxt", row.PropertyZoneTxt ?? (object)DBNull.Value),
115+
new NpgsqlParameter("@availableBedroomsQty", ConvertToInt2(row.AvailableBedroomsQty) ?? (object)DBNull.Value),
116+
new NpgsqlParameter("@maxGuestsAllowedQty", ConvertToInt2(row.MaxGuestsAllowedQty) ?? (object)DBNull.Value),
117+
new NpgsqlParameter("@isPrincipalResidence", ConvertToBool(row.IsPrincipalResidence)),
118+
new NpgsqlParameter("@isOwnerLivingOnsite", ConvertToBool(row.IsOwnerLivingOnsite)),
119+
new NpgsqlParameter("@isOwnerPropertyTenant", ConvertToBool(row.IsOwnerPropertyTenant)),
120+
new NpgsqlParameter("@propertyFolioNo", row.PropertyFolioNo ?? (object)DBNull.Value),
121+
new NpgsqlParameter("@propertyParcelIdentifierNo", row.PropertyParcelIdentifierNo ?? (object)DBNull.Value),
122+
new NpgsqlParameter("@propertyLegalDescriptionTxt", row.PropertyLegalDescriptionTxt ?? (object)DBNull.Value),
123+
new NpgsqlParameter("@licenceStatusType", row.LicenceStatusType),
124+
new NpgsqlParameter("@providingOrganizationId", providingOrganizationId)
125+
};
126+
127+
await _dbContext.Database.ExecuteSqlRawAsync(insertSql, parameters.ToArray());
128+
}
129+
130+
public async Task ProcessBizLicTempTable()
131+
{
132+
await _dbContext.Database.ExecuteSqlRawAsync("CALL dss_process_biz_lic_table();");
133+
}
26134
}
27135
}

server/StrDss.Data/Repositories/UploadDeliveryRepository.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,19 @@ public async Task<DssUploadDelivery[]> GetUploadsToProcessAsync(string reportTyp
6666
.ToArrayAsync();
6767
}
6868

69-
public async Task<DssUploadLine?> GetUploadLineAsync(long uploadId, string orgCd, string listingId)
69+
public async Task<DssUploadLine?> GetUploadLineAsync(long uploadId, string orgCd, string sourceRecordNo)
7070
{
7171
var stopwatch = Stopwatch.StartNew();
7272

7373
var line = await _dbContext.DssUploadLines.FirstOrDefaultAsync(x =>
7474
x.IncludingUploadDeliveryId == uploadId &&
7575
x.SourceOrganizationCd == orgCd &&
76-
x.SourceRecordNo == listingId &&
76+
x.SourceRecordNo == sourceRecordNo &&
7777
x.IsProcessed == false);
7878

7979
stopwatch.Stop();
8080

81-
_logger.LogDebug($"Fetched listing ({orgCd} - {listingId}) - {stopwatch.Elapsed.TotalMilliseconds} milliseconds");
81+
_logger.LogDebug($"Fetched line ({orgCd} - {sourceRecordNo}) - {stopwatch.Elapsed.TotalMilliseconds} milliseconds");
8282

8383
return line;
8484
}
@@ -87,7 +87,7 @@ public async Task<List<UploadLineToProcess>> GetUploadLinesToProcessAsync(long u
8787
{
8888
return await _dbContext.DssUploadLines.AsNoTracking()
8989
.Where(x => x.IncludingUploadDeliveryId == uploadId && x.IsProcessed == false)
90-
.Select(x => new UploadLineToProcess { ListingId = x.SourceRecordNo, OrgCd = x.SourceOrganizationCd })
90+
.Select(x => new UploadLineToProcess { SourceRecordNo = x.SourceRecordNo, OrgCd = x.SourceOrganizationCd })
9191
.ToListAsync();
9292
}
9393

server/StrDss.Model/UploadLineToProcess.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
public class UploadLineToProcess
44
{
55
public string OrgCd { get; set; } = "";
6-
public string ListingId { get; set; } = "";
6+
public string SourceRecordNo { get; set; } = "";
77
}
88
}

0 commit comments

Comments
 (0)