Skip to content

Commit

Permalink
fix(SPD): Correct and ingest invalid SPD postcodes
Browse files Browse the repository at this point in the history
Reintroduces fix from #578

Some postcodes on SPD have an extra character appended. Since these are
picked up as invalid postcodes, they can no longer be queried

This change will ingest the first instance of these invalid postcodes
(i.e. those ending with A) and drop all others
  • Loading branch information
cblanc committed Dec 17, 2020
1 parent 0b89891 commit 8564f5a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/app/models/scottish_postcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ const findQuery = `
WHERE pc_compact=$1
`;

const EXCEPTION_REGEX = /A$/;

/**
* Validates and potentially mutates a CSV row for ingest
*
* Unfortunately SPD appends extra characters to some postcodes. This method returns null when these cases are met, unless the postcode ends in `A`.
*
* Postcodes suffixed with `A` are made valid and returned to the stream for ingest
*/
const clean = (row: string[]) => {
const postcode = row[0];
if (isValid(postcode)) return row;
// Reject if invalid postcide has a non-A suffix
if (postcode.match(EXCEPTION_REGEX) === null) return null;
row[0] = postcode.replace(EXCEPTION_REGEX, "");
return row;
};

const SPD_COL_MAPPINGS = Object.freeze([
{ column: "postcode", method: (row: RowExtract) => row.extract("Postcode") },
{
Expand Down Expand Up @@ -137,6 +155,7 @@ const seedPostcodes = ({ extractor, filepath }: SeedPostcodesOptions) => {
return methods.csvSeed({
filepath: [filepath],
transform: (row: RowExtract) => {
clean(row);
row.extract = (code: string) => extractor(row, code);
if (row.extract("Postcode") === "Postcode") return null; // Skip if header
if (row.extract("DateOfDeletion").length !== 0) return null; // Skip row if terminated
Expand Down
5 changes: 5 additions & 0 deletions test/scottish_postcode.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ describe("Scottish Postcode Model", () => {
const result = await query(q);
assert.isTrue(result.rows[0].count > 0);
});

it("loads postcodes suffixed with additional character", async () => {
const result = await ScottishPostcode.find("PA31 8UA");
assert.isNotNull(result);
});
});
});

Expand Down

0 comments on commit 8564f5a

Please sign in to comment.