Skip to content

Commit bae6fa8

Browse files
authored
Merge pull request #563 from bcgov/MALS2-69-fix
feat: MALS2-69 adjustments
2 parents 5c49b51 + 75a8003 commit bae6fa8

File tree

7 files changed

+60
-44
lines changed

7 files changed

+60
-44
lines changed

backend/prisma/schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,7 @@ model mal_licence_summary_vw {
866866
print_certificate Boolean
867867
print_renewal Boolean
868868
premises_ids String[]
869+
display_premises_id String?
869870
}
870871

871872
model mal_site_detail_vw {

backend/prisma/views.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ model mal_licence_summary_vw {
3131
print_certificate Boolean
3232
print_renewal Boolean
3333
premises_ids String[]
34+
display_premises_id String?
3435
}
3536

3637
model mal_site_detail_vw {

backend/src/models/licence.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ function convertSearchResultToLogicalModel(input) {
169169
issuedOnDate: input.issue_date,
170170
expiryDate: input.expiry_date,
171171
premisesIds: input.premises_ids,
172+
displayPremisesId: input.display_premises_id,
172173
};
173174

174175
return output;

frontend/src/features/licences/LicenceSites.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function formatResultRow(result) {
5858
<td className="text-nowrap">{result.registrantLastName}</td>
5959
<td className="text-nowrap">{result.registrantFirstName}</td>
6060
<td className="text-nowrap">{result.siteAddressLine1}</td>
61-
{/* <td className="text-nowrap">{result.premisesId}</td> */}
61+
<td className="text-nowrap">{result.premisesId}</td>
6262
<td className="text-nowrap">{result.licenceRegion}</td>
6363
<td className="text-nowrap">{result.licenceDistrict}</td>
6464
</tr>
@@ -191,7 +191,7 @@ export default function LicenceSites({ licence }) {
191191
<th className="text-nowrap">Last Name</th>
192192
<th className="text-nowrap">First Name</th>
193193
<th className="text-nowrap">Address</th>
194-
{/* <th className="text-nowrap">Premises ID</th> */}
194+
<th className="text-nowrap">Premises ID</th>
195195
<th>Region</th>
196196
<th>District</th>
197197
</tr>

frontend/src/features/search/LicenceResultsPage.jsx

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,6 @@ import {
4242
clearCurrentSite
4343
} from "../sites/sitesSlice";
4444

45-
function formatPremisesIds(result) {
46-
if (!result.premisesIds || result.premisesIds.length === 0) {
47-
return <span className="text-muted">-</span>;
48-
}
49-
50-
const premisesList = result.premisesIds.join(", ");
51-
const premisesTitle = result.premisesIds.join("\n");
52-
53-
return (
54-
<span
55-
className="d-inline-block text-truncate"
56-
style={{ maxWidth: "160px" }}
57-
title={premisesTitle}
58-
>
59-
{premisesList}
60-
</span>
61-
);
62-
}
63-
6445
function formatResultRow(result) {
6546
const url = `${LICENSES_PATHNAME}/${result.licenceId}`;
6647
return (
@@ -71,7 +52,7 @@ function formatResultRow(result) {
7152
<td className="text-nowrap">{result.licenceType}</td>
7253
<td className="text-nowrap">{formatListShorten(result.lastNames)}</td>
7354
<td className="text-nowrap">{formatListShorten(result.companyNames)}</td>
74-
{/* <td style={{ maxWidth: "180px" }}>{formatPremisesIds(result)}</td> */}
55+
<td className="text-nowrap">{result.displayPremisesId}</td>
7556
<td className="text-nowrap">{result.licenceStatus}</td>
7657
<td className="text-nowrap">{formatDateString(result.issuedOnDate)}</td>
7758
<td className="text-nowrap">{formatDateString(result.expiryDate)}</td>
@@ -159,7 +140,7 @@ export default function LicenceResultsPage() {
159140
<th className="text-nowrap">Licence Type</th>
160141
<th className="text-nowrap">Last Names</th>
161142
<th className="text-nowrap">Company Names</th>
162-
{/* <th className="text-nowrap">Premises IDs</th> */}
143+
<th className="text-nowrap">Premises ID</th>
163144
<th className="text-nowrap">Licence Status</th>
164145
<th className="text-nowrap">Issued On Date</th>
165146
<th className="text-nowrap">Expiry Date</th>

frontend/src/features/search/LicenceSearchPage.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ export default function LicenceSearchPage() {
266266
</Form.Group>
267267
</Col>
268268
</Form.Row>
269-
{/* <Form.Row className="mb-5">
269+
<Form.Row className="mb-5">
270270
<Col lg={6}>
271271
<Form.Group controlId="premisesId">
272272
<Form.Label>Premises ID</Form.Label>
@@ -280,7 +280,7 @@ export default function LicenceSearchPage() {
280280
/>
281281
</Form.Group>
282282
</Col>
283-
</Form.Row> */}
283+
</Form.Row>
284284
<Form.Row className="mt-5">
285285
<Col lg={{ span: 2, offset: 10 }}>
286286
<Button type="submit" variant="primary" block>

spilo-db/db-scripts/releases/release_5.8_ddl.sql

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,9 @@ UNION ALL
10241024
-- MALS2-69 - Enable search by Premises ID
10251025
----
10261026
-- Add an array of Premises IDs to each licence returned by the licence summary view
1027+
-- Add a display_premises_id field to show the best matched premises ID based on address, or fallback to lowest site.id
1028+
1029+
CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;
10271030

10281031
CREATE OR REPLACE VIEW mals_app.mal_licence_summary_vw
10291032
AS SELECT lic.id AS licence_id,
@@ -1086,25 +1089,54 @@ AS SELECT lic.id AS licence_id,
10861089
lic.print_certificate,
10871090
lic.print_renewal,
10881091
lic.print_dairy_infraction,
1089-
COALESCE(
1090-
(
1091-
SELECT array_agg(DISTINCT clean_pid ORDER BY clean_pid)
1092-
FROM (
1093-
SELECT NULLIF(BTRIM(site.premises_id::text), '') AS clean_pid
1094-
FROM mals_app.mal_site site
1095-
WHERE site.licence_id = lic.id
1096-
) cleaned
1097-
WHERE clean_pid IS NOT NULL
1098-
),
1099-
ARRAY[]::varchar[]
1100-
) AS premises_ids
1101-
FROM mals_app.mal_licence lic
1102-
JOIN mals_app.mal_licence_type_lu lictyp ON lic.licence_type_id = lictyp.id
1103-
JOIN mals_app.mal_status_code_lu stat ON lic.status_code_id = stat.id
1104-
LEFT JOIN mals_app.mal_registrant reg ON lic.primary_registrant_id = reg.id
1105-
LEFT JOIN mals_app.mal_region_lu rgn ON lic.region_id = rgn.id
1106-
LEFT JOIN mals_app.mal_regional_district_lu dist ON lic.regional_district_id = dist.id
1107-
LEFT JOIN mals_app.mal_licence_species_code_lu sp ON lic.species_code_id = sp.id;
1092+
1093+
-- All premises IDs
1094+
COALESCE((
1095+
SELECT array_agg(DISTINCT cleaned.clean_pid ORDER BY cleaned.clean_pid)
1096+
FROM (SELECT NULLIF(btrim(site.premises_id::text), '') AS clean_pid
1097+
FROM mals_app.mal_site site
1098+
WHERE site.licence_id = lic.id) cleaned
1099+
WHERE cleaned.clean_pid IS NOT NULL
1100+
), ARRAY[]::text[]) AS premises_ids,
1101+
1102+
-- address match -> lowest site.id fallback
1103+
COALESCE(
1104+
matched_site.premises_id,
1105+
fallback_site.premises_id
1106+
)::text AS display_premises_id
1107+
1108+
FROM mals_app.mal_licence lic
1109+
JOIN mals_app.mal_licence_type_lu lictyp ON lic.licence_type_id = lictyp.id
1110+
JOIN mals_app.mal_status_code_lu stat ON lic.status_code_id = stat.id
1111+
LEFT JOIN mals_app.mal_registrant reg ON lic.primary_registrant_id = reg.id
1112+
LEFT JOIN mals_app.mal_region_lu rgn ON lic.region_id = rgn.id
1113+
LEFT JOIN mals_app.mal_regional_district_lu dist ON lic.regional_district_id = dist.id
1114+
LEFT JOIN mals_app.mal_licence_species_code_lu sp ON lic.species_code_id = sp.id
1115+
1116+
-- best address match using soundex + house number
1117+
LEFT JOIN LATERAL (
1118+
SELECT site.premises_id
1119+
FROM mals_app.mal_site site
1120+
WHERE site.licence_id = lic.id
1121+
AND site.premises_id IS NOT NULL
1122+
AND soundex(site.address_line_1) = soundex(
1123+
COALESCE(lic.address_line_1, lic.mail_address_line_1)
1124+
)
1125+
AND NULLIF(REGEXP_REPLACE(site.address_line_1, '\D.*$', ''), '')
1126+
= NULLIF(REGEXP_REPLACE(COALESCE(lic.address_line_1, lic.mail_address_line_1), '\D.*$', ''), '')
1127+
ORDER BY site.id
1128+
LIMIT 1
1129+
) matched_site ON true
1130+
1131+
-- fallback on any site with premises_id, lowest ID is chosen
1132+
LEFT JOIN LATERAL (
1133+
SELECT site.premises_id
1134+
FROM mals_app.mal_site site
1135+
WHERE site.licence_id = lic.id
1136+
AND site.premises_id IS NOT NULL
1137+
ORDER BY site.id
1138+
LIMIT 1
1139+
) fallback_site ON matched_site.premises_id IS NULL;
11081140

11091141
----
11101142
-- MALS2-68/70 - Display the Premises ID field in the registrant's sites list / search results

0 commit comments

Comments
 (0)