Skip to content

Commit 6726707

Browse files
authored
Merge pull request #1306 from ideal-postcodes/terminated_postcodes
2 parents 2235558 + 53d51e6 commit 6726707

62 files changed

Lines changed: 224 additions & 105 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

api/app/controllers/postcodes_controller.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { isEmpty, qToString } from "../lib/string";
22
import { Postcode } from "../models/postcode";
3+
import { TerminatedPostcode } from "../models/terminated_postcode";
34
import { isValid } from "postcode";
45
import { chunk } from "../lib/chunk";
56
import { getConfig } from "../../config/config";
@@ -29,7 +30,11 @@ export const show: Handler = async (request, response, next) => {
2930
if (!isValid(postcode.trim())) throw new InvalidPostcodeError();
3031

3132
const result = await Postcode.find(postcode);
32-
if (!result) throw new PostcodeNotFoundError();
33+
if (!result) {
34+
// Check if postcode has been terminated
35+
const terminated = await TerminatedPostcode.find(postcode);
36+
throw new PostcodeNotFoundError(terminated);
37+
}
3338
response.jsonApiResponse = { status: 200, result: Postcode.toJson(result) };
3439
next();
3540
} catch (error) {

api/app/lib/errors.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,44 @@ export class InvalidPostcodeError extends PostcodesioHttpError {
6464
}
6565
}
6666

67+
interface TerminatedPostcodeTuple {
68+
id: number;
69+
postcode: string;
70+
pc_compact: string;
71+
year_terminated: number;
72+
month_terminated: number;
73+
eastings: number;
74+
northings: number;
75+
longitude: number;
76+
latitude: number;
77+
location: string;
78+
}
79+
6780
export class PostcodeNotFoundError extends PostcodesioHttpError {
68-
constructor() {
81+
public terminatedPostcode: TerminatedPostcodeTuple | null;
82+
83+
constructor(terminatedPostcode: TerminatedPostcodeTuple | null = null) {
6984
super(404, "Postcode not found");
85+
Object.setPrototypeOf(this, PostcodeNotFoundError.prototype);
86+
this.terminatedPostcode = terminatedPostcode;
87+
}
88+
89+
toJSON() {
90+
const terminated = this.terminatedPostcode
91+
? {
92+
postcode: this.terminatedPostcode.postcode,
93+
year_terminated: this.terminatedPostcode.year_terminated,
94+
month_terminated: this.terminatedPostcode.month_terminated,
95+
longitude: this.terminatedPostcode.longitude,
96+
latitude: this.terminatedPostcode.latitude,
97+
}
98+
: null;
99+
100+
return {
101+
status: this.status,
102+
error: this.humanMessage,
103+
...(terminated !== null && { terminated }),
104+
};
70105
}
71106
}
72107

build/404.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<meta charset="UTF-8">
55
<meta name="generator" content="Docusaurus v3.8.1">
66
<title data-rh="true">Page Not Found | Postcodes.io</title><meta data-rh="true" name="viewport" content="width=device-width,initial-scale=1"><meta data-rh="true" name="twitter:card" content="summary_large_image"><meta data-rh="true" property="og:image" content="https://postcodes.io/img/postcodesio.png"><meta data-rh="true" name="twitter:image" content="https://postcodes.io/img/postcodesio.png"><meta data-rh="true" property="og:url" content="https://postcodes.io/404.html"><meta data-rh="true" property="og:locale" content="en"><meta data-rh="true" name="docusaurus_locale" content="en"><meta data-rh="true" name="docusaurus_tag" content="default"><meta data-rh="true" name="docsearch:language" content="en"><meta data-rh="true" name="docsearch:docusaurus_tag" content="default"><meta data-rh="true" property="og:title" content="Page Not Found | Postcodes.io"><link data-rh="true" rel="icon" href="/img/favicon.ico"><link data-rh="true" rel="canonical" href="https://postcodes.io/404.html"><link data-rh="true" rel="alternate" href="https://postcodes.io/404.html" hreflang="en"><link data-rh="true" rel="alternate" href="https://postcodes.io/404.html" hreflang="x-default"><link rel="stylesheet" href="/assets/css/styles.0cd2942a.css">
7-
<script src="/assets/js/runtime~main.9ab8726d.js" defer="defer"></script>
8-
<script src="/assets/js/main.ee9532f4.js" defer="defer"></script>
7+
<script src="/assets/js/runtime~main.c5239b27.js" defer="defer"></script>
8+
<script src="/assets/js/main.ed04f129.js" defer="defer"></script>
99
</head>
1010
<body class="navigation-with-keyboard">
1111
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><defs>

build/assets/js/1807015c.5bbae310.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

build/assets/js/1807015c.6f773d8e.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/assets/js/4c5e977b.a5e1a0bf.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

build/assets/js/4c5e977b.df474580.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/assets/js/5453.30db375d.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

build/assets/js/5453.5501928c.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)