@defra/forms-engine-plugin 4.9.2 → 4.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.public/javascripts/shared.min.js +1 -1
- package/.public/javascripts/shared.min.js.map +1 -1
- package/.server/client/javascripts/geospatial-map.js +34 -3
- package/.server/client/javascripts/geospatial-map.js.map +1 -1
- package/.server/client/javascripts/map.d.ts +0 -6
- package/.server/client/javascripts/map.js +1 -14
- package/.server/client/javascripts/map.js.map +1 -1
- package/.server/index.js +1 -2
- package/.server/index.js.map +1 -1
- package/.server/server/common/helpers/logging/logger.d.ts +1 -1
- package/.server/server/common/helpers/logging/logger.js +5 -1
- package/.server/server/common/helpers/logging/logger.js.map +1 -1
- package/.server/server/common/helpers/redis-client.js +1 -2
- package/.server/server/common/helpers/redis-client.js.map +1 -1
- package/.server/server/plugins/engine/components/GeospatialField.d.ts +1 -0
- package/.server/server/plugins/engine/components/GeospatialField.js +6 -2
- package/.server/server/plugins/engine/components/GeospatialField.js.map +1 -1
- package/.server/server/plugins/engine/components/PaymentField.js +1 -2
- package/.server/server/plugins/engine/components/PaymentField.js.map +1 -1
- package/.server/server/plugins/engine/components/helpers/geospatial.d.ts +2 -1
- package/.server/server/plugins/engine/components/helpers/geospatial.js +32 -1
- package/.server/server/plugins/engine/components/helpers/geospatial.js.map +1 -1
- package/.server/server/plugins/engine/components/helpers/geospatial.test.js +21 -1
- package/.server/server/plugins/engine/components/helpers/geospatial.test.js.map +1 -1
- package/.server/server/plugins/engine/helpers.js +1 -2
- package/.server/server/plugins/engine/helpers.js.map +1 -1
- package/.server/server/plugins/engine/models/FormModel.js +1 -2
- package/.server/server/plugins/engine/models/FormModel.js.map +1 -1
- package/.server/server/plugins/engine/options.js +1 -2
- package/.server/server/plugins/engine/options.js.map +1 -1
- package/.server/server/plugins/engine/routes/payment.js +1 -2
- package/.server/server/plugins/engine/routes/payment.js.map +1 -1
- package/.server/server/plugins/engine/views/components/geospatialfield.html +1 -1
- package/.server/server/plugins/map/routes/index.d.ts +9 -1
- package/.server/server/plugins/map/routes/index.js +51 -2
- package/.server/server/plugins/map/routes/index.js.map +1 -1
- package/.server/server/plugins/map/routes/vts/countries.geojson +39285 -0
- package/.server/server/plugins/map/service.js +1 -2
- package/.server/server/plugins/map/service.js.map +1 -1
- package/.server/server/plugins/map/types.d.ts +23 -1
- package/.server/server/plugins/map/types.js +14 -1
- package/.server/server/plugins/map/types.js.map +1 -1
- package/.server/server/plugins/nunjucks/context.js +1 -2
- package/.server/server/plugins/nunjucks/context.js.map +1 -1
- package/.server/server/plugins/payment/service.js +1 -2
- package/.server/server/plugins/payment/service.js.map +1 -1
- package/.server/server/plugins/postcode-lookup/service.js +1 -2
- package/.server/server/plugins/postcode-lookup/service.js.map +1 -1
- package/package.json +10 -8
- package/src/client/javascripts/geospatial-map.js +39 -3
- package/src/client/javascripts/map.js +1 -14
- package/src/index.ts +1 -3
- package/src/server/common/helpers/logging/logger.ts +5 -1
- package/src/server/common/helpers/redis-client.js +1 -3
- package/src/server/plugins/engine/components/GeospatialField.ts +8 -2
- package/src/server/plugins/engine/components/PaymentField.ts +1 -3
- package/src/server/plugins/engine/components/helpers/geospatial.ts +48 -2
- package/src/server/plugins/engine/helpers.ts +1 -3
- package/src/server/plugins/engine/models/FormModel.ts +1 -3
- package/src/server/plugins/engine/options.js +1 -3
- package/src/server/plugins/engine/routes/payment.js +1 -3
- package/src/server/plugins/engine/views/components/geospatialfield.html +1 -1
- package/src/server/plugins/map/routes/index.js +58 -2
- package/src/server/plugins/map/routes/vts/countries.geojson +39285 -0
- package/src/server/plugins/map/service.js +1 -3
- package/src/server/plugins/map/types.js +14 -1
- package/src/server/plugins/nunjucks/context.js +1 -3
- package/src/server/plugins/payment/service.js +1 -3
- package/src/server/plugins/postcode-lookup/service.js +1 -3
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { getErrorMessage } from '@defra/forms-model';
|
|
2
2
|
import Boom from '@hapi/boom';
|
|
3
|
-
import {
|
|
3
|
+
import { logger } from "../../common/helpers/logging/logger.js";
|
|
4
4
|
import { getJson } from "../../services/httpService.js";
|
|
5
|
-
const logger = createLogger();
|
|
6
5
|
|
|
7
6
|
/**
|
|
8
7
|
* Returns an empty result set
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.js","names":["getErrorMessage","Boom","
|
|
1
|
+
{"version":3,"file":"service.js","names":["getErrorMessage","Boom","logger","getJson","empty","results","header","logErrorAndReturnEmpty","err","endpoint","boomData","isBoom","data","msg","payload","error","message","getData","url","getJsonByType","response","find","query","apiKey","nearest","easting","northing"],"sources":["../../../../src/server/plugins/map/service.js"],"sourcesContent":["import { getErrorMessage } from '@defra/forms-model'\nimport Boom from '@hapi/boom'\n\nimport { logger } from '~/src/server/common/helpers/logging/logger.js'\nimport { getJson } from '~/src/server/services/httpService.js'\n\n/**\n * Returns an empty result set\n */\nfunction empty() {\n /** @type {OsNamesFindResult[]} */\n const results = []\n\n return /** @type {OsNamesFindResponse} */ ({ header: {}, results })\n}\n\n/**\n * Logs OS names errors\n * @param {unknown} err - the error\n * @param {string} endpoint - the OS api endpoint\n */\nfunction logErrorAndReturnEmpty(err, endpoint) {\n /** @type {{ payload?: { error?: { message?: string } } } | false} */\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n const boomData = Boom.isBoom(err) && err.data\n const msg = `${getErrorMessage(err)} ${(boomData && boomData.payload?.error?.message) ?? ''}`\n\n logger.error(err, `Exception occured calling OS names ${endpoint} - ${msg}}`)\n\n return empty()\n}\n\n/**\n * Fetch data from OS names API\n * @param {string} url - the url to get address json data from\n * @param {string} endpoint - the url endpoint description for logging\n */\nasync function getData(url, endpoint) {\n const getJsonByType = /** @type {typeof getJson<OsNamesFindResponse>} */ (\n getJson\n )\n\n try {\n const response = await getJsonByType(url)\n\n if (response.error) {\n return logErrorAndReturnEmpty(response.error, endpoint)\n }\n\n const results = response.payload\n\n return results\n } catch (err) {\n return logErrorAndReturnEmpty(err, endpoint)\n }\n}\n\n/**\n * OS names search find by query\n * @param {string} query - the search term\n * @param {string} apiKey - the OS api key\n */\nexport async function find(query, apiKey) {\n const endpoint = 'find'\n const url = `https://api.os.uk/search/names/v1/find?key=${apiKey}&query=${query}&fq=local_type:postcode%20local_type:hamlet%20local_type:village%20local_type:town%20local_type:city%20local_type:other_settlement&maxresults=8`\n\n return getData(url, endpoint)\n}\n\n/**\n * OS names search nearest by E/N\n * @param {number} easting - the easting\n * @param {number} northing - the northing\n * @param {string} apiKey - the OS api key\n */\nexport async function nearest(easting, northing, apiKey) {\n const endpoint = 'nearest'\n const url = `https://api.os.uk/search/names/v1/nearest?key=${apiKey}&point=${easting},${northing}&radius=1000&fq=local_type:Airfield%20local_type:Airport%20local_type:Bus_Station%20local_type:Chemical_Works%20local_type:City%20local_type:Coach_Station%20local_type:Electricity_Distribution%20local_type:Electricity_Production%20local_type:Further_Education%20local_type:Gas_Distribution_or_Storage%20local_type:Hamlet%20local_type:Harbour%20local_type:Helicopter_Station%20local_type:Heliport%20local_type:Higher_or_University_Education%20local_type:Hill_Or_Mountain%20local_type:Hospice%20local_type:Hospital%20local_type:Medical_Care_Accommodation%20local_type:Named_Road%20local_type:Non_State_Primary_Education%20local_type:Non_State_Secondary_Education%20local_type:Other_Settlement%20local_type:Passenger_Ferry_Terminal%20local_type:Port_Consisting_of_Docks_and_Nautical_Berthing%20local_type:Postcode%20local_type:Primary_Education%20local_type:Railway_Station%20local_type:Road_User_Services%20local_type:Secondary_Education%20local_type:Section_Of_Named_Road%20local_type:Section_Of_Numbered_Road%20local_type:Special_Needs_Education%20local_type:Suburban_Area%20local_type:Town%20local_type:Urban_Greenspace%20local_type:Vehicular_Ferry_Terminal%20local_type:Vehicular_Rail_Terminal%20local_type:Village%20local_type:Waterfall%20`\n\n return getData(url, endpoint)\n}\n\n/**\n * @import { OsNamesFindResponse, OsNamesFindResult } from '~/src/server/plugins/map/types.js'\n */\n"],"mappings":"AAAA,S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,KAAK,EAAEC,MAAM,EAAE;EACxC,MAAMd,QAAQ,GAAG,MAAM;EACvB,MAAMS,GAAG,GAAG,8CAA8CK,MAAM,UAAUD,KAAK,iJAAiJ;EAEhO,OAAOL,OAAO,CAACC,GAAG,EAAET,QAAQ,CAAC;AAC/B;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,eAAee,OAAOA,CAACC,OAAO,EAAEC,QAAQ,EAAEH,MAAM,EAAE;EACvD,MAAMd,QAAQ,GAAG,SAAS;EAC1B,MAAMS,GAAG,GAAG,iDAAiDK,MAAM,UAAUE,OAAO,IAAIC,QAAQ,4tCAA4tC;EAE5zC,OAAOT,OAAO,CAACC,GAAG,EAAET,QAAQ,CAAC;AAC/B;;AAEA;AACA;AACA","ignoreList":[]}
|
|
@@ -33,6 +33,19 @@ export type MapReverseGeocodeQuery = {
|
|
|
33
33
|
*/
|
|
34
34
|
northing: number;
|
|
35
35
|
};
|
|
36
|
+
/**
|
|
37
|
+
* Geospatial countries query params
|
|
38
|
+
*/
|
|
39
|
+
export type GeospatialCountriesQuery = {
|
|
40
|
+
/**
|
|
41
|
+
* - the country to omit
|
|
42
|
+
*/
|
|
43
|
+
omit: string;
|
|
44
|
+
/**
|
|
45
|
+
* - the country to include
|
|
46
|
+
*/
|
|
47
|
+
only: string;
|
|
48
|
+
};
|
|
36
49
|
/**
|
|
37
50
|
* Map geocode get request
|
|
38
51
|
*/
|
|
@@ -52,7 +65,7 @@ export type MapGeocodeGetRequestRefs = {
|
|
|
52
65
|
Query: MapGeocodeQuery;
|
|
53
66
|
};
|
|
54
67
|
/**
|
|
55
|
-
* Map
|
|
68
|
+
* Map reverse geocode get request
|
|
56
69
|
*/
|
|
57
70
|
export type MapReverseGeocodeGetRequestRefs = {
|
|
58
71
|
/**
|
|
@@ -60,6 +73,15 @@ export type MapReverseGeocodeGetRequestRefs = {
|
|
|
60
73
|
*/
|
|
61
74
|
Query: MapReverseGeocodeQuery;
|
|
62
75
|
};
|
|
76
|
+
/**
|
|
77
|
+
* Map countries geojson get request
|
|
78
|
+
*/
|
|
79
|
+
export type GeospatialCountriesGetRequestRefs = {
|
|
80
|
+
/**
|
|
81
|
+
* - Request query
|
|
82
|
+
*/
|
|
83
|
+
Query: GeospatialCountriesQuery;
|
|
84
|
+
};
|
|
63
85
|
export type MapProxyRequestRefs = MapProxyGetRequestRefs;
|
|
64
86
|
export type MapGeocodeRequestRefs = MapGeocodeGetRequestRefs;
|
|
65
87
|
export type MapReverseGeocodeRequestRefs = MapReverseGeocodeGetRequestRefs;
|
|
@@ -28,6 +28,13 @@
|
|
|
28
28
|
* @property {number} northing - the Northing point
|
|
29
29
|
*/
|
|
30
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Geospatial countries query params
|
|
33
|
+
* @typedef {object} GeospatialCountriesQuery
|
|
34
|
+
* @property {string} omit - the country to omit
|
|
35
|
+
* @property {string} only - the country to include
|
|
36
|
+
*/
|
|
37
|
+
|
|
31
38
|
/**
|
|
32
39
|
* Map geocode get request
|
|
33
40
|
* @typedef {object} MapProxyGetRequestRefs
|
|
@@ -41,11 +48,17 @@
|
|
|
41
48
|
*/
|
|
42
49
|
|
|
43
50
|
/**
|
|
44
|
-
* Map
|
|
51
|
+
* Map reverse geocode get request
|
|
45
52
|
* @typedef {object} MapReverseGeocodeGetRequestRefs
|
|
46
53
|
* @property {MapReverseGeocodeQuery} Query - Request query
|
|
47
54
|
*/
|
|
48
55
|
|
|
56
|
+
/**
|
|
57
|
+
* Map countries geojson get request
|
|
58
|
+
* @typedef {object} GeospatialCountriesGetRequestRefs
|
|
59
|
+
* @property {GeospatialCountriesQuery} Query - Request query
|
|
60
|
+
*/
|
|
61
|
+
|
|
49
62
|
/**
|
|
50
63
|
* @typedef {MapProxyGetRequestRefs} MapProxyRequestRefs
|
|
51
64
|
* @typedef {MapGeocodeGetRequestRefs} MapGeocodeRequestRefs
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","names":[],"sources":["../../../../src/server/plugins/map/types.js"],"sourcesContent":["/**\n * @typedef {{\n * ordnanceSurveyApiKey: string\n * ordnanceSurveyApiSecret: string\n * }} MapConfiguration\n */\n\n//\n// Route types\n//\n\n/**\n * Map proxy query params\n * @typedef {object} MapProxyQuery\n * @property {string} url - the proxied url\n */\n\n/**\n * Map geocode query params\n * @typedef {object} MapGeocodeQuery\n * @property {string} query - name query\n */\n\n/**\n * Map reverse geocode query params\n * @typedef {object} MapReverseGeocodeQuery\n * @property {number} easting - the Easting point\n * @property {number} northing - the Northing point\n */\n\n/**\n * Map geocode get request\n * @typedef {object} MapProxyGetRequestRefs\n * @property {MapProxyQuery} Query - Request query\n */\n\n/**\n * Map geocode get request\n * @typedef {object} MapGeocodeGetRequestRefs\n * @property {MapGeocodeQuery} Query - Request query\n */\n\n/**\n * Map
|
|
1
|
+
{"version":3,"file":"types.js","names":[],"sources":["../../../../src/server/plugins/map/types.js"],"sourcesContent":["/**\n * @typedef {{\n * ordnanceSurveyApiKey: string\n * ordnanceSurveyApiSecret: string\n * }} MapConfiguration\n */\n\n//\n// Route types\n//\n\n/**\n * Map proxy query params\n * @typedef {object} MapProxyQuery\n * @property {string} url - the proxied url\n */\n\n/**\n * Map geocode query params\n * @typedef {object} MapGeocodeQuery\n * @property {string} query - name query\n */\n\n/**\n * Map reverse geocode query params\n * @typedef {object} MapReverseGeocodeQuery\n * @property {number} easting - the Easting point\n * @property {number} northing - the Northing point\n */\n\n/**\n * Geospatial countries query params\n * @typedef {object} GeospatialCountriesQuery\n * @property {string} omit - the country to omit\n * @property {string} only - the country to include\n */\n\n/**\n * Map geocode get request\n * @typedef {object} MapProxyGetRequestRefs\n * @property {MapProxyQuery} Query - Request query\n */\n\n/**\n * Map geocode get request\n * @typedef {object} MapGeocodeGetRequestRefs\n * @property {MapGeocodeQuery} Query - Request query\n */\n\n/**\n * Map reverse geocode get request\n * @typedef {object} MapReverseGeocodeGetRequestRefs\n * @property {MapReverseGeocodeQuery} Query - Request query\n */\n\n/**\n * Map countries geojson get request\n * @typedef {object} GeospatialCountriesGetRequestRefs\n * @property {GeospatialCountriesQuery} Query - Request query\n */\n\n/**\n * @typedef {MapProxyGetRequestRefs} MapProxyRequestRefs\n * @typedef {MapGeocodeGetRequestRefs} MapGeocodeRequestRefs\n * @typedef {MapReverseGeocodeGetRequestRefs} MapReverseGeocodeRequestRefs\n * @typedef {Request<MapGeocodeGetRequestRefs>} MapProxyGetRequest\n * @typedef {Request<MapGeocodeGetRequestRefs>} MapGeocodeGetRequest\n * @typedef {Request<MapReverseGeocodeGetRequestRefs>} MapReverseGeocodeGetRequest\n * @typedef {MapProxyGetRequest | MapGeocodeGetRequest | MapReverseGeocodeGetRequest} MapRequest\n */\n\n//\n// Service types\n//\n\n/**\n * @typedef {object} OsNamesFindResponse\n * @property {OsNamesFindHeader} header - Metadata about the search request and results.\n * @property {OsNamesFindResult[]} results - An array of matched place records from the search.\n */\n\n/**\n * @typedef {object} OsNamesFindHeader\n * @property {string} uri - The query URI (usually same as search text).\n * @property {string} query - The original text query string passed to the API.\n * @property {string} format - The response format returned (e.g., \"JSON\").\n * @property {number} maxresults - The maximum number of results requested.\n * @property {number} offset - The offset used in the search results.\n * @property {number} totalresults - The total number of results that matched the query.\n * @property {string} filter - The original filter string passed to the API.\n */\n\n/**\n * @typedef {object} OsNamesFindGazetteerEntry\n * @property {string} ID - Unique identifier for the place/feature.\n * @property {string} NAMES_URI - A URI (identifier) for this named feature.\n * @property {string} NAME1 - Primary name of the feature.\n * @property {string} TYPE - General type classification of the feature.\n * @property {string} LOCAL_TYPE - Local or more specific type classification.\n * @property {number} GEOMETRY_X - Easting coordinate (British National Grid).\n * @property {number} GEOMETRY_Y - Northing coordinate (British National Grid).\n * @property {number} MOST_DETAIL_VIEW_RES - Most detailed resolution available.\n * @property {number} LEAST_DETAIL_VIEW_RES - Least detailed resolution available.\n * @property {number} [MBR_XMIN] - Minimum bounding box X (easting).\n * @property {number} [MBR_YMIN] - Minimum bounding box Y (northing).\n * @property {number} [MBR_XMAX] - Maximum bounding box X (easting).\n * @property {number} [MBR_YMAX] - Maximum bounding box Y (northing).\n * @property {string} [DISTRICT_BOROUGH] - (Optional) District borough.\n * @property {string} [DISTRICT_BOROUGH_URI] - (Optional) URI for the district borough.\n * @property {string} [DISTRICT_BOROUGH_TYPE] - (Optional) Type of the district borough.\n * @property {string} [POSTCODE_DISTRICT] - (Optional) Postcode district.\n * @property {string} [POSTCODE_DISTRICT_URI] - (Optional) URI for the postcode district.\n * @property {string} [POPULATED_PLACE] - (Optional) Name of associated populated place.\n * @property {string} [POPULATED_PLACE_URI] - (Optional) URI of populated place.\n * @property {string} [POPULATED_PLACE_TYPE] - (Optional) Type of populated place.\n * @property {string} [COUNTY_UNITARY] - (Optional) County or unitary authority name.\n * @property {string} [COUNTY_UNITARY_URI] - (Optional) URI for county/unitary authority.\n * @property {string} [COUNTY_UNITARY_TYPE] - (Optional) Classification of county/unitary authority.\n * @property {string} [REGION] - (Optional) Region name.\n * @property {string} [REGION_URI] - (Optional) URI for region.\n * @property {string} [COUNTRY] - (Optional) Country name.\n * @property {string} [COUNTRY_URI] - (Optional) URI for country.\n */\n\n/**\n * OS names GAZETTEER_ENTRY response\n * @typedef {object} OsNamesFindResult\n * @property {OsNamesFindGazetteerEntry} GAZETTEER_ENTRY - Gazetteer entry\n */\n\n/**\n * @import { Request } from '@hapi/hapi'\n */\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA","ignoreList":[]}
|
|
@@ -3,9 +3,8 @@ import { basename, join } from 'node:path';
|
|
|
3
3
|
import Boom from '@hapi/boom';
|
|
4
4
|
import { StatusCodes } from 'http-status-codes';
|
|
5
5
|
import { config } from "../../../config/index.js";
|
|
6
|
-
import {
|
|
6
|
+
import { logger } from "../../common/helpers/logging/logger.js";
|
|
7
7
|
import { checkFormStatus, encodeUrl } from "../engine/helpers.js";
|
|
8
|
-
const logger = createLogger();
|
|
9
8
|
|
|
10
9
|
/** @type {Record<string, string> | undefined} */
|
|
11
10
|
let webpackManifest;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.js","names":["readFileSync","basename","join","Boom","StatusCodes","config","
|
|
1
|
+
{"version":3,"file":"context.js","names":["readFileSync","basename","join","Boom","StatusCodes","config","logger","checkFormStatus","encodeUrl","webpackManifest","context","request","params","response","isPreview","isPreviewMode","state","formState","isResponseOK","isBoom","statusCode","OK","pluginStorage","server","plugins","consumerViewContext","Error","baseLayoutPath","viewContext","ctx","currentPath","path","url","search","previewMode","undefined","slug","devtoolContext","_request","manifestPath","get","JSON","parse","info","cdpEnvironment","designerUrl","feedbackLink","phaseTag","serviceName","serviceVersion","assetPath","getDxtAssetPath","asset"],"sources":["../../../../src/server/plugins/nunjucks/context.js"],"sourcesContent":["import { readFileSync } from 'node:fs'\nimport { basename, join } from 'node:path'\n\nimport Boom from '@hapi/boom'\nimport { StatusCodes } from 'http-status-codes'\n\nimport { config } from '~/src/config/index.js'\nimport { logger } from '~/src/server/common/helpers/logging/logger.js'\nimport {\n checkFormStatus,\n encodeUrl\n} from '~/src/server/plugins/engine/helpers.js'\n\n/** @type {Record<string, string> | undefined} */\nlet webpackManifest\n\n/**\n * @param {AnyFormRequest | null} request\n */\nexport async function context(request) {\n const { params, response } = request ?? {}\n\n const { isPreview: isPreviewMode, state: formState } = checkFormStatus(params)\n\n // Only add the slug in to the context if the response is OK.\n // Footer meta links are not rendered when the slug is missing.\n const isResponseOK =\n !Boom.isBoom(response) && response?.statusCode === StatusCodes.OK\n\n const pluginStorage = request?.server.plugins['forms-engine-plugin']\n\n let consumerViewContext = {}\n\n if (!pluginStorage) {\n throw Error('context called before plugin registered')\n }\n\n if (!pluginStorage.baseLayoutPath) {\n throw Error('Missing baseLayoutPath in plugin.options.nunjucks')\n }\n\n if (typeof pluginStorage.viewContext === 'function') {\n consumerViewContext = await pluginStorage.viewContext(request)\n }\n\n /** @type {ViewContext} */\n const ctx = {\n // take consumers props first so we can override it\n ...consumerViewContext,\n baseLayoutPath: pluginStorage.baseLayoutPath,\n currentPath: `${request.path}${request.url.search}`,\n previewMode: isPreviewMode ? formState : undefined,\n slug: isResponseOK ? params?.slug : undefined\n }\n\n return ctx\n}\n\n/**\n * Returns the context for the devtool. Consumers won't have access to this.\n * @param {AnyFormRequest | null} _request\n * @returns {Record<string, unknown> & { assetPath: string, getDxtAssetPath: (asset: string) => string }}\n */\nexport function devtoolContext(_request) {\n const manifestPath = join(config.get('publicDir'), 'assets-manifest.json')\n\n if (!webpackManifest) {\n try {\n // eslint-disable-next-line -- Allow JSON type 'any'\n webpackManifest = JSON.parse(readFileSync(manifestPath, 'utf-8'))\n } catch {\n logger.info(\n `[webpackManifestMissing] Webpack ${basename(manifestPath)} not found - running without asset manifest`\n )\n }\n }\n\n return {\n config: {\n cdpEnvironment: config.get('cdpEnvironment'),\n designerUrl: config.get('designerUrl'),\n feedbackLink: encodeUrl(config.get('feedbackLink')),\n phaseTag: config.get('phaseTag'),\n serviceName: config.get('serviceName'),\n serviceVersion: config.get('serviceVersion')\n },\n assetPath: '/assets',\n getDxtAssetPath: (asset = '') => {\n return `/${webpackManifest?.[asset] ?? asset}`\n }\n }\n}\n\n/**\n * @import { ViewContext } from '~/src/server/plugins/nunjucks/types.js'\n * @import { AnyFormRequest } from '~/src/server/plugins/engine/types.js'\n */\n"],"mappings":"AAAA,SAASA,YAAY,QAAQ,SAAS;AACtC,SAASC,QAAQ,EAAEC,IAAI,QAAQ,WAAW;AAE1C,OAAOC,IAAI,MAAM,YAAY;AAC7B,SAASC,WAAW,QAAQ,mBAAmB;AAE/C,SAASC,MAAM;AACf,SAASC,MAAM;AACf,SACEC,eAAe,EACfC,SAAS;;AAGX;AACA,IAAIC,eAAe;;AAEnB;AACA;AACA;AACA,OAAO,eAAeC,OAAOA,CAACC,OAAO,EAAE;EACrC,MAAM;IAAEC,MAAM;IAAEC;EAAS,CAAC,GAAGF,OAAO,IAAI,CAAC,CAAC;EAE1C,MAAM;IAAEG,SAAS,EAAEC,aAAa;IAAEC,KAAK,EAAEC;EAAU,CAAC,GAAGV,eAAe,CAACK,MAAM,CAAC;;EAE9E;EACA;EACA,MAAMM,YAAY,GAChB,CAACf,IAAI,CAACgB,MAAM,CAACN,QAAQ,CAAC,IAAIA,QAAQ,EAAEO,UAAU,KAAKhB,WAAW,CAACiB,EAAE;EAEnE,MAAMC,aAAa,GAAGX,OAAO,EAAEY,MAAM,CAACC,OAAO,CAAC,qBAAqB,CAAC;EAEpE,IAAIC,mBAAmB,GAAG,CAAC,CAAC;EAE5B,IAAI,CAACH,aAAa,EAAE;IAClB,MAAMI,KAAK,CAAC,yCAAyC,CAAC;EACxD;EAEA,IAAI,CAACJ,aAAa,CAACK,cAAc,EAAE;IACjC,MAAMD,KAAK,CAAC,mDAAmD,CAAC;EAClE;EAEA,IAAI,OAAOJ,aAAa,CAACM,WAAW,KAAK,UAAU,EAAE;IACnDH,mBAAmB,GAAG,MAAMH,aAAa,CAACM,WAAW,CAACjB,OAAO,CAAC;EAChE;;EAEA;EACA,MAAMkB,GAAG,GAAG;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/B,OAAO,IAAI3C,eAAe,GAAG2C,KAAK,CAAC,IAAIA,KAAK,EAAE;IAChD;EACF,CAAC;AACH;;AAEA;AACA;AACA;AACA","ignoreList":[]}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { StatusCodes } from 'http-status-codes';
|
|
2
|
-
import {
|
|
2
|
+
import { logger } from "../../common/helpers/logging/logger.js";
|
|
3
3
|
import { buildPaymentInfo, convertPenceToPounds } from "../engine/routes/payment-helper.js";
|
|
4
4
|
import { get, post, postJson } from "../../services/httpService.js";
|
|
5
5
|
const PAYMENT_BASE_URL = 'https://publicapi.payments.service.gov.uk';
|
|
6
6
|
const PAYMENT_ENDPOINT = '/v1/payments';
|
|
7
|
-
const logger = createLogger();
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* @param {string} apiKey
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.js","names":["StatusCodes","createLogger","buildPaymentInfo","convertPenceToPounds","get","post","postJson","PAYMENT_BASE_URL","PAYMENT_ENDPOINT","logger","getAuthHeaders","apiKey","Authorization","PaymentService","constructor","createPayment","amount","description","returnUrl","reference","isLivePayment","metadata","email","payload","return_url","delayed_capture","response","postToPayProvider","info","payment_id","paymentId","paymentUrl","_links","next_url","href","err","error","output","message","undefined","getPaymentStatus","getByType","headers","json","errorMessage","Error","JSON","stringify","state","status","code","capturePayment","statusCode","res","OK","NO_CONTENT","event","category","action","outcome","reason","postJsonByType","includes"],"sources":["../../../../src/server/plugins/payment/service.js"],"sourcesContent":["import { StatusCodes } from 'http-status-codes'\n\nimport { createLogger } from '~/src/server/common/helpers/logging/logger.js'\nimport {\n buildPaymentInfo,\n convertPenceToPounds\n} from '~/src/server/plugins/engine/routes/payment-helper.js'\nimport { get, post, postJson } from '~/src/server/services/httpService.js'\n\nconst PAYMENT_BASE_URL = 'https://publicapi.payments.service.gov.uk'\nconst PAYMENT_ENDPOINT = '/v1/payments'\n\nconst logger = createLogger()\n\n/**\n * @param {string} apiKey\n * @returns {{ Authorization: string }}\n */\nfunction getAuthHeaders(apiKey) {\n return {\n Authorization: `Bearer ${apiKey}`\n }\n}\n\nexport class PaymentService {\n /** @type {string} */\n #apiKey\n\n /**\n * @param {string} apiKey - API key to use (global config for test value, per-form config for live value)\n */\n constructor(apiKey) {\n this.#apiKey = apiKey\n }\n\n /**\n * Creates a payment with delayed capture (pre-authorisation)\n * @param {number} amount - in pence\n * @param {string} description\n * @param {string} returnUrl\n * @param {string} reference\n * @param {boolean} isLivePayment\n * @param {{ formId: string, slug: string } | undefined } metadata\n * @param {string} [email] - optional email to prepopulate on GOV.UK Pay\n */\n async createPayment(\n amount,\n description,\n returnUrl,\n reference,\n isLivePayment,\n metadata,\n email\n ) {\n try {\n /** @type {CreatePaymentRequest} */\n const payload = {\n amount,\n description,\n reference,\n metadata,\n return_url: returnUrl,\n delayed_capture: true\n }\n\n // Prepopulate email on GOV.UK Pay if provided\n if (email) {\n payload.email = email\n }\n\n const response = await this.postToPayProvider(payload)\n\n logger.info(\n buildPaymentInfo(\n 'create-payment',\n 'success',\n `amount=${convertPenceToPounds(amount)}`,\n isLivePayment,\n response.payment_id\n ),\n `[payment] Created payment and user taken to enter pre-auth details for paymentId=${response.payment_id}`\n )\n\n return {\n paymentId: response.payment_id,\n paymentUrl: response._links.next_url.href\n }\n } catch (err) {\n const error =\n /** @type {{ output?: { payload?: any }, message?: any }} */ (err)\n if (isLivePayment) {\n logger.error(\n error.output?.payload ?? error.message,\n `[payment] Failed to create payment session for reference ${reference}`\n )\n }\n }\n return undefined\n }\n\n /**\n * @param {string} paymentId\n * @param {boolean} isLivePayment\n * @returns {Promise<GetPaymentResponse>}\n */\n async getPaymentStatus(paymentId, isLivePayment) {\n const getByType = /** @type {typeof get<GetPaymentApiResponse>} */ (get)\n\n try {\n const response = await getByType(\n `${PAYMENT_BASE_URL}${PAYMENT_ENDPOINT}/${paymentId}`,\n {\n headers: getAuthHeaders(this.#apiKey),\n json: true\n }\n )\n\n if (response.error) {\n const errorMessage =\n response.error instanceof Error\n ? response.error.message\n : JSON.stringify(response.error)\n throw new Error(`Failed to get payment status: ${errorMessage}`)\n }\n\n const state = response.payload.state\n logger.info(\n buildPaymentInfo(\n 'get-payment-status',\n state.status === 'capturable' || state.status === 'success'\n ? 'success'\n : 'failure',\n `status:${state.status} code:${state.code ?? 'N/A'} message:${state.message ?? 'N/A'}`,\n isLivePayment,\n paymentId\n ),\n `[payment] Got payment status for paymentId=${paymentId}: status=${state.status}`\n )\n\n return {\n state,\n _links: response.payload._links,\n email: response.payload.email,\n paymentId: response.payload.payment_id,\n amount: response.payload.amount\n }\n } catch (err) {\n const error = /** @type {Error} */ (err)\n logger.error(\n error,\n `[payment] Error getting payment status for paymentId=${paymentId}: ${error.message}`\n )\n throw err\n }\n }\n\n /**\n * Captures a payment that is in 'capturable' status\n * @param {string} paymentId\n * @param {number} amount\n * @returns {Promise<boolean>}\n */\n async capturePayment(paymentId, amount) {\n try {\n const response = await post(\n `${PAYMENT_BASE_URL}${PAYMENT_ENDPOINT}/${paymentId}/capture`,\n {\n headers: getAuthHeaders(this.#apiKey)\n }\n )\n\n const statusCode = response.res.statusCode\n\n if (\n statusCode === StatusCodes.OK ||\n statusCode === StatusCodes.NO_CONTENT\n ) {\n logger.info(\n {\n event: {\n category: 'payment',\n action: 'capture-payment',\n outcome: 'success',\n reason: `amount=${convertPenceToPounds(amount)}`,\n reference: paymentId\n }\n },\n `[payment] Successfully captured payment for paymentId=${paymentId}`\n )\n return true\n }\n\n logger.error(\n `[payment] Capture failed for paymentId=${paymentId}: HTTP ${statusCode}`\n )\n return false\n } catch (err) {\n const error = /** @type {Error} */ (err)\n logger.error(\n error,\n `[payment] Error capturing payment for paymentId=${paymentId}: ${error.message}`\n )\n throw err\n }\n }\n\n /**\n * @param {CreatePaymentRequest} payload\n */\n async postToPayProvider(payload) {\n const postJsonByType =\n /** @type {typeof postJson<CreatePaymentResponse>} */ (postJson)\n\n try {\n const response = await postJsonByType(\n `${PAYMENT_BASE_URL}${PAYMENT_ENDPOINT}`,\n {\n payload,\n headers: getAuthHeaders(this.#apiKey)\n }\n )\n\n if (response.payload?.state.status !== 'created') {\n throw new Error(\n `Failed to create payment for reference=${payload.reference}`\n )\n }\n\n return response.payload\n } catch (err) {\n const error = /** @type {Error} */ (err)\n if (!error.message.includes('401 Unauthorized')) {\n logger.error(\n error,\n `[payment] Error creating payment for reference=${payload.reference}: ${error.message}`\n )\n }\n throw err\n }\n }\n}\n\n/**\n * @import { CreatePaymentRequest, CreatePaymentResponse, GetPaymentApiResponse, GetPaymentResponse } from '~/src/server/plugins/payment/types.js'\n */\n"],"mappings":"AAAA,SAASA,WAAW,QAAQ,mBAAmB;AAE/C,SAASC,YAAY;AACrB,SACEC,gBAAgB,EAChBC,oBAAoB;AAEtB,SAASC,GAAG,EAAEC,IAAI,EAAEC,QAAQ;AAE5B,MAAMC,gBAAgB,GAAG,2CAA2C;AACpE,MAAMC,gBAAgB,GAAG,cAAc;AAEvC,MAAMC,MAAM,GAAGR,YAAY,CAAC,CAAC;;AAE7B;AACA;AACA;AACA;AACA,SAASS,cAAcA,CAACC,MAAM,EAAE;EAC9B,OAAO;IACLC,aAAa,EAAE,UAAUD,MAAM;EACjC,CAAC;AACH;AAEA,OAAO,MAAME,cAAc,CAAC;EAC1B;EACA,CAACF,MAAM;;EAEP;AACF;AACA;EACEG,WAAWA,CAACH,MAAM,EAAE;IAClB,IAAI,CAAC,CAACA,MAAM,GAAGA,MAAM;EACvB;;EAEA;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,MAAMI,aAAaA,CACjBC,MAAM,EACNC,WAAW,EACXC,SAAS,EACTC,SAAS,EACTC,aAAa,EACbC,QAAQ,EACRC,KAAK,EACL;IACA,IAAI;MACF;MACA,MAAMC,OAAO,GAAG;QACdP,MAAM;QACNC,WAAW;QACXE,SAAS;QACTE,QAAQ;QACRG,UAAU,EAAEN,SAAS;QACrBO,eAAe,EAAE;MACnB,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AAC,CAAC,OAAOY,GAAG,EAAE;MACZ,MAAMC,KAAK,GAAG,oBAAsBD,GAAI;MACxC,IAAI,CAACC,KAAK,CAACE,OAAO,CAACwB,QAAQ,CAAC,kBAAkB,CAAC,EAAE;QAC/CrD,MAAM,CAAC2B,KAAK,CACVA,KAAK,EACL,kDAAkDb,OAAO,CAACJ,SAAS,KAAKiB,KAAK,CAACE,OAAO,EACvF,CAAC;MACH;MACA,MAAMH,GAAG;IACX;EACF;AACF;;AAEA;AACA;AACA","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"service.js","names":["StatusCodes","logger","buildPaymentInfo","convertPenceToPounds","get","post","postJson","PAYMENT_BASE_URL","PAYMENT_ENDPOINT","getAuthHeaders","apiKey","Authorization","PaymentService","constructor","createPayment","amount","description","returnUrl","reference","isLivePayment","metadata","email","payload","return_url","delayed_capture","response","postToPayProvider","info","payment_id","paymentId","paymentUrl","_links","next_url","href","err","error","output","message","undefined","getPaymentStatus","getByType","headers","json","errorMessage","Error","JSON","stringify","state","status","code","capturePayment","statusCode","res","OK","NO_CONTENT","event","category","action","outcome","reason","postJsonByType","includes"],"sources":["../../../../src/server/plugins/payment/service.js"],"sourcesContent":["import { StatusCodes } from 'http-status-codes'\n\nimport { logger } from '~/src/server/common/helpers/logging/logger.js'\nimport {\n buildPaymentInfo,\n convertPenceToPounds\n} from '~/src/server/plugins/engine/routes/payment-helper.js'\nimport { get, post, postJson } from '~/src/server/services/httpService.js'\n\nconst PAYMENT_BASE_URL = 'https://publicapi.payments.service.gov.uk'\nconst PAYMENT_ENDPOINT = '/v1/payments'\n\n/**\n * @param {string} apiKey\n * @returns {{ Authorization: string }}\n */\nfunction getAuthHeaders(apiKey) {\n return {\n Authorization: `Bearer ${apiKey}`\n }\n}\n\nexport class PaymentService {\n /** @type {string} */\n #apiKey\n\n /**\n * @param {string} apiKey - API key to use (global config for test value, per-form config for live value)\n */\n constructor(apiKey) {\n this.#apiKey = apiKey\n }\n\n /**\n * Creates a payment with delayed capture (pre-authorisation)\n * @param {number} amount - in pence\n * @param {string} description\n * @param {string} returnUrl\n * @param {string} reference\n * @param {boolean} isLivePayment\n * @param {{ formId: string, slug: string } | undefined } metadata\n * @param {string} [email] - optional email to prepopulate on GOV.UK Pay\n */\n async createPayment(\n amount,\n description,\n returnUrl,\n reference,\n isLivePayment,\n metadata,\n email\n ) {\n try {\n /** @type {CreatePaymentRequest} */\n const payload = {\n amount,\n description,\n reference,\n metadata,\n return_url: returnUrl,\n delayed_capture: true\n }\n\n // Prepopulate email on GOV.UK Pay if provided\n if (email) {\n payload.email = email\n }\n\n const response = await this.postToPayProvider(payload)\n\n logger.info(\n buildPaymentInfo(\n 'create-payment',\n 'success',\n `amount=${convertPenceToPounds(amount)}`,\n isLivePayment,\n response.payment_id\n ),\n `[payment] Created payment and user taken to enter pre-auth details for paymentId=${response.payment_id}`\n )\n\n return {\n paymentId: response.payment_id,\n paymentUrl: response._links.next_url.href\n }\n } catch (err) {\n const error =\n /** @type {{ output?: { payload?: any }, message?: any }} */ (err)\n if (isLivePayment) {\n logger.error(\n error.output?.payload ?? error.message,\n `[payment] Failed to create payment session for reference ${reference}`\n )\n }\n }\n return undefined\n }\n\n /**\n * @param {string} paymentId\n * @param {boolean} isLivePayment\n * @returns {Promise<GetPaymentResponse>}\n */\n async getPaymentStatus(paymentId, isLivePayment) {\n const getByType = /** @type {typeof get<GetPaymentApiResponse>} */ (get)\n\n try {\n const response = await getByType(\n `${PAYMENT_BASE_URL}${PAYMENT_ENDPOINT}/${paymentId}`,\n {\n headers: getAuthHeaders(this.#apiKey),\n json: true\n }\n )\n\n if (response.error) {\n const errorMessage =\n response.error instanceof Error\n ? response.error.message\n : JSON.stringify(response.error)\n throw new Error(`Failed to get payment status: ${errorMessage}`)\n }\n\n const state = response.payload.state\n logger.info(\n buildPaymentInfo(\n 'get-payment-status',\n state.status === 'capturable' || state.status === 'success'\n ? 'success'\n : 'failure',\n `status:${state.status} code:${state.code ?? 'N/A'} message:${state.message ?? 'N/A'}`,\n isLivePayment,\n paymentId\n ),\n `[payment] Got payment status for paymentId=${paymentId}: status=${state.status}`\n )\n\n return {\n state,\n _links: response.payload._links,\n email: response.payload.email,\n paymentId: response.payload.payment_id,\n amount: response.payload.amount\n }\n } catch (err) {\n const error = /** @type {Error} */ (err)\n logger.error(\n error,\n `[payment] Error getting payment status for paymentId=${paymentId}: ${error.message}`\n )\n throw err\n }\n }\n\n /**\n * Captures a payment that is in 'capturable' status\n * @param {string} paymentId\n * @param {number} amount\n * @returns {Promise<boolean>}\n */\n async capturePayment(paymentId, amount) {\n try {\n const response = await post(\n `${PAYMENT_BASE_URL}${PAYMENT_ENDPOINT}/${paymentId}/capture`,\n {\n headers: getAuthHeaders(this.#apiKey)\n }\n )\n\n const statusCode = response.res.statusCode\n\n if (\n statusCode === StatusCodes.OK ||\n statusCode === StatusCodes.NO_CONTENT\n ) {\n logger.info(\n {\n event: {\n category: 'payment',\n action: 'capture-payment',\n outcome: 'success',\n reason: `amount=${convertPenceToPounds(amount)}`,\n reference: paymentId\n }\n },\n `[payment] Successfully captured payment for paymentId=${paymentId}`\n )\n return true\n }\n\n logger.error(\n `[payment] Capture failed for paymentId=${paymentId}: HTTP ${statusCode}`\n )\n return false\n } catch (err) {\n const error = /** @type {Error} */ (err)\n logger.error(\n error,\n `[payment] Error capturing payment for paymentId=${paymentId}: ${error.message}`\n )\n throw err\n }\n }\n\n /**\n * @param {CreatePaymentRequest} payload\n */\n async postToPayProvider(payload) {\n const postJsonByType =\n /** @type {typeof postJson<CreatePaymentResponse>} */ (postJson)\n\n try {\n const response = await postJsonByType(\n `${PAYMENT_BASE_URL}${PAYMENT_ENDPOINT}`,\n {\n payload,\n headers: getAuthHeaders(this.#apiKey)\n }\n )\n\n if (response.payload?.state.status !== 'created') {\n throw new Error(\n `Failed to create payment for reference=${payload.reference}`\n )\n }\n\n return response.payload\n } catch (err) {\n const error = /** @type {Error} */ (err)\n if (!error.message.includes('401 Unauthorized')) {\n logger.error(\n error,\n `[payment] Error creating payment for reference=${payload.reference}: ${error.message}`\n )\n }\n throw err\n }\n }\n}\n\n/**\n * @import { CreatePaymentRequest, CreatePaymentResponse, GetPaymentApiResponse, GetPaymentResponse } from '~/src/server/plugins/payment/types.js'\n */\n"],"mappings":"AAAA,SAASA,WAAW,QAAQ,mBAAmB;AAE/C,SAASC,MAAM;AACf,SACEC,gBAAgB,EAChBC,oBAAoB;AAEtB,SAASC,GAAG,EAAEC,IAAI,EAAEC,QAAQ;AAE5B,MAAMC,gBAAgB,GAAG,2CAA2C;AACpE,MAAMC,gBAAgB,GAAG,cAAc;;AAEvC;AACA;AACA;AACA;AACA,SAASC,cAAcA,CAACC,MAAM,EAAE;EAC9B,OAAO;IACLC,aAAa,EAAE,UAAUD,MAAM;EACjC,CAAC;AACH;AAEA,OAAO,MAAME,cAAc,CAAC;EAC1B;EACA,CAACF,MAAM;;EAEP;AACF;AACA;EACEG,WAAWA,CAACH,MAAM,EAAE;IAClB,IAAI,CAAC,CAACA,MAAM,GAAGA,MAAM;EACvB;;EAEA;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,MAAMI,aAAaA,CACjBC,MAAM,EACNC,WAAW,EACXC,SAAS,EACTC,SAAS,EACTC,aAAa,EACbC,QAAQ,EACRC,KAAK,EACL;IACA,IAAI;MACF;MACA,MAAMC,OAAO,GAAG;QACdP,MAAM;QACNC,WAAW;QACXE,SAAS;QACTE,QAAQ;QACRG,UAAU,EAAEN,SAAS;QACrBO,eAAe,EAAE;MACnB,CAAC;;MAED;MACA,IAAIH,KAAK,EAAE;QACTC,OAAO,CAACD,KAAK,GAAGA,KAAK;MACvB;MAEA,MAAMI,QAAQ,GAAG,MAAM,IAAI,CAACC,iBAAiB,CAACJ,OAAO,CAAC;MAEtDrB,MAAM,CAAC0B,IAAI,CACTzB,gBAAgB,CACd,gBAAgB,EAChB,SAAS,EACT,UAAUC,oBAAoB,CAACY,MAAM,CAAC,EAAE,EACxCI,aAA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,GAAG;IACX;EACF;AACF;;AAEA;AACA;AACA","ignoreList":[]}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { getErrorMessage } from '@defra/forms-model';
|
|
2
2
|
import Boom from '@hapi/boom';
|
|
3
|
-
import {
|
|
3
|
+
import { logger } from "../../common/helpers/logging/logger.js";
|
|
4
4
|
import { getJson } from "../../services/httpService.js";
|
|
5
|
-
const logger = createLogger();
|
|
6
5
|
|
|
7
6
|
/**
|
|
8
7
|
* Returns an empty result set
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.js","names":["getErrorMessage","Boom","createLogger","getJson","logger","empty","logErrorAndReturnEmpty","err","endpoint","boomData","isBoom","data","msg","payload","error","message","getAddressData","url","getJsonByType","response","results","Array","isArray","map","result","formatAddress","DPA","searchByQuery","query","apiKey","encodeURIComponent","searchByPostcode","postcode","replaceAll","searchByUPRN","uprn","search","postcodeQuery","buildingNameQuery","addresses","filter","item","address","includes","toUpperCase","dpa","addressLine1","formatAddressLine1","addressLine2","formatAddressLine2","town","titleCase","POST_TOWN","POSTCODE","lines","formatted","i","join","UPRN","ADDRESS","county","ORGANISATION_NAME","SUB_BUILDING_NAME","BUILDING_NAME","BUILDING_NUMBER","THOROUGHFARE_NAME","DEPENDENT_LOCALITY","split","charAt","slice","toLowerCase"],"sources":["../../../../src/server/plugins/postcode-lookup/service.js"],"sourcesContent":["import { getErrorMessage } from '@defra/forms-model'\nimport Boom from '@hapi/boom'\n\nimport { createLogger } from '~/src/server/common/helpers/logging/logger.js'\nimport { getJson } from '~/src/server/services/httpService.js'\n\nconst logger = createLogger()\n\n/**\n * Returns an empty result set\n */\nfunction empty() {\n return []\n}\n\n/**\n * Logs OS places errors\n * @param {unknown} err - the error\n * @param {string} endpoint - the OS api endpoint\n */\nfunction logErrorAndReturnEmpty(err, endpoint) {\n /** @type {{ payload?: { error?: { message?: string } } } | false} */\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n const boomData = Boom.isBoom(err) && err.data\n const msg = `${getErrorMessage(err)} ${(boomData && boomData.payload?.error?.message) ?? ''}`\n\n logger.error(err, `Exception occured calling OS places ${endpoint} - ${msg}}`)\n\n return empty()\n}\n\n/**\n * Fetch data from OS API\n * @param {string} url - the url to get address json data from\n * @param {string} endpoint - the url endpoint description for logging\n */\nasync function getAddressData(url, endpoint) {\n const getJsonByType =\n /** @type {typeof getJson<DeliveryPointAddressResult>} */ (getJson)\n\n try {\n const response = await getJsonByType(url)\n\n if (response.error) {\n return logErrorAndReturnEmpty(response.error, endpoint)\n }\n\n const results = response.payload.results\n\n if (!Array.isArray(results)) {\n return empty()\n }\n\n return results.map((result) => formatAddress(result.DPA))\n } catch (err) {\n return logErrorAndReturnEmpty(err, endpoint)\n }\n}\n\n/**\n * OS places search\n * @param {string} query - the search term\n * @param {string} apiKey - the OS api key\n */\nexport async function searchByQuery(query, apiKey) {\n const endpoint = 'find'\n const url = `https://api.os.uk/search/places/v1/${endpoint}?query=${encodeURIComponent(query)}&key=${apiKey}`\n\n return getAddressData(url, endpoint)\n}\n\n/**\n * OS postcode search\n * @param {string} postcode - the postcode\n * @param {string} apiKey - the OS api key\n */\nexport async function searchByPostcode(postcode, apiKey) {\n const endpoint = 'postcode'\n const url = `https://api.os.uk/search/places/v1/${endpoint}?postcode=${encodeURIComponent(postcode.replaceAll(/\\s/g, ''))}&key=${apiKey}`\n\n return getAddressData(url, endpoint)\n}\n\n/**\n * OS UPRN search\n * @param {string} uprn - the unique property reference number\n * @param {string} apiKey - the OS api key\n */\nexport async function searchByUPRN(uprn, apiKey) {\n const endpoint = 'uprn'\n const url = `https://api.os.uk/search/places/v1/${endpoint}?uprn=${uprn}&key=${apiKey}`\n\n return getAddressData(url, endpoint)\n}\n\n/**\n * OS postcode and building name search\n * @param {string} postcodeQuery - the postcode query\n * @param {string} buildingNameQuery - the building name query\n * @param {string} apiKey - the OS api key\n */\nexport async function search(postcodeQuery, buildingNameQuery, apiKey) {\n let addresses = await searchByPostcode(postcodeQuery, apiKey)\n\n if (buildingNameQuery) {\n addresses = addresses.filter((item) =>\n item.address.includes(buildingNameQuery.toUpperCase())\n )\n }\n\n return addresses\n}\n\n/**\n * Converts a delivery point address to an address\n * Taken from http://github.com/dwp/find-an-address-plugin/blob/main/utils/getData.js\n * @param {DeliveryPointAddress} dpa\n */\nfunction formatAddress(dpa) {\n const addressLine1 = formatAddressLine1(dpa)\n const addressLine2 = formatAddressLine2(dpa)\n const town = titleCase(dpa.POST_TOWN || '')\n const postcode = dpa.POSTCODE || ''\n const lines = [addressLine1, addressLine2, town]\n const formatted = `${lines.filter((i) => !!i).join(', ')}, ${postcode}`\n\n /**\n * @type {Address}\n */\n const address = {\n uprn: dpa.UPRN,\n address: dpa.ADDRESS,\n addressLine1,\n addressLine2,\n town,\n county: '',\n postcode,\n formatted\n }\n\n return address\n}\n\n/**\n * @param {DeliveryPointAddress} dpa\n */\nfunction formatAddressLine1(dpa) {\n return titleCase(\n dpa.ORGANISATION_NAME ||\n dpa.SUB_BUILDING_NAME ||\n dpa.BUILDING_NAME ||\n dpa.BUILDING_NUMBER\n ? [\n dpa.ORGANISATION_NAME || '',\n dpa.SUB_BUILDING_NAME || '',\n dpa.BUILDING_NAME || '',\n dpa.BUILDING_NUMBER || ''\n ]\n .filter((item) => !!item)\n .join(' ')\n : ''\n )\n}\n\n/**\n * @param {DeliveryPointAddress} dpa\n */\nfunction formatAddressLine2(dpa) {\n return titleCase(\n dpa.THOROUGHFARE_NAME || dpa.DEPENDENT_LOCALITY\n ? [dpa.THOROUGHFARE_NAME || '', dpa.DEPENDENT_LOCALITY || '']\n .filter((item) => !!item)\n .join(', ')\n : ''\n )\n}\n\n/**\n * Title case address\n * @param {string} address\n */\nfunction titleCase(address) {\n return address\n .split(' ')\n .map((item) => item.charAt(0).toUpperCase() + item.slice(1).toLowerCase())\n .join(' ')\n}\n\n/**\n * @import { Address, DeliveryPointAddress, DeliveryPointAddressResult } from '~/src/server/plugins/postcode-lookup/types.js'\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ExB,MAAM,CAAEC,IAAI,IAAK,CAAC,CAACA,IAAI,CAAC,CACxBgB,IAAI,CAAC,GAAG,CAAC,GACZ,EACN,CAAC;AACH;;AAEA;AACA;AACA;AACA,SAASR,kBAAkBA,CAACJ,GAAG,EAAE;EAC/B,OAAOM,SAAS,CACdN,GAAG,CAACoB,iBAAiB,IAAIpB,GAAG,CAACqB,kBAAkB,GAC3C,CAACrB,GAAG,CAACoB,iBAAiB,IAAI,EAAE,EAAEpB,GAAG,CAACqB,kBAAkB,IAAI,EAAE,CAAC,CACxD1B,MAAM,CAAEC,IAAI,IAAK,CAAC,CAACA,IAAI,CAAC,CACxBgB,IAAI,CAAC,IAAI,CAAC,GACb,EACN,CAAC;AACH;;AAEA;AACA;AACA;AACA;AACA,SAASN,SAASA,CAACT,OAAO,EAAE;EAC1B,OAAOA,OAAO,CACXyB,KAAK,CAAC,GAAG,CAAC,CACV5C,GAAG,CAAEkB,IAAI,IAAKA,IAAI,CAAC2B,MAAM,CAAC,CAAC,CAAC,CAACxB,WAAW,CAAC,CAAC,GAAGH,IAAI,CAAC4B,KAAK,CAAC,CAAC,CAAC,CAACC,WAAW,CAAC,CAAC,CAAC,CACzEb,IAAI,CAAC,GAAG,CAAC;AACd;;AAEA;AACA;AACA","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"service.js","names":["getErrorMessage","Boom","logger","getJson","empty","logErrorAndReturnEmpty","err","endpoint","boomData","isBoom","data","msg","payload","error","message","getAddressData","url","getJsonByType","response","results","Array","isArray","map","result","formatAddress","DPA","searchByQuery","query","apiKey","encodeURIComponent","searchByPostcode","postcode","replaceAll","searchByUPRN","uprn","search","postcodeQuery","buildingNameQuery","addresses","filter","item","address","includes","toUpperCase","dpa","addressLine1","formatAddressLine1","addressLine2","formatAddressLine2","town","titleCase","POST_TOWN","POSTCODE","lines","formatted","i","join","UPRN","ADDRESS","county","ORGANISATION_NAME","SUB_BUILDING_NAME","BUILDING_NAME","BUILDING_NUMBER","THOROUGHFARE_NAME","DEPENDENT_LOCALITY","split","charAt","slice","toLowerCase"],"sources":["../../../../src/server/plugins/postcode-lookup/service.js"],"sourcesContent":["import { getErrorMessage } from '@defra/forms-model'\nimport Boom from '@hapi/boom'\n\nimport { logger } from '~/src/server/common/helpers/logging/logger.js'\nimport { getJson } from '~/src/server/services/httpService.js'\n\n/**\n * Returns an empty result set\n */\nfunction empty() {\n return []\n}\n\n/**\n * Logs OS places errors\n * @param {unknown} err - the error\n * @param {string} endpoint - the OS api endpoint\n */\nfunction logErrorAndReturnEmpty(err, endpoint) {\n /** @type {{ payload?: { error?: { message?: string } } } | false} */\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n const boomData = Boom.isBoom(err) && err.data\n const msg = `${getErrorMessage(err)} ${(boomData && boomData.payload?.error?.message) ?? ''}`\n\n logger.error(err, `Exception occured calling OS places ${endpoint} - ${msg}}`)\n\n return empty()\n}\n\n/**\n * Fetch data from OS API\n * @param {string} url - the url to get address json data from\n * @param {string} endpoint - the url endpoint description for logging\n */\nasync function getAddressData(url, endpoint) {\n const getJsonByType =\n /** @type {typeof getJson<DeliveryPointAddressResult>} */ (getJson)\n\n try {\n const response = await getJsonByType(url)\n\n if (response.error) {\n return logErrorAndReturnEmpty(response.error, endpoint)\n }\n\n const results = response.payload.results\n\n if (!Array.isArray(results)) {\n return empty()\n }\n\n return results.map((result) => formatAddress(result.DPA))\n } catch (err) {\n return logErrorAndReturnEmpty(err, endpoint)\n }\n}\n\n/**\n * OS places search\n * @param {string} query - the search term\n * @param {string} apiKey - the OS api key\n */\nexport async function searchByQuery(query, apiKey) {\n const endpoint = 'find'\n const url = `https://api.os.uk/search/places/v1/${endpoint}?query=${encodeURIComponent(query)}&key=${apiKey}`\n\n return getAddressData(url, endpoint)\n}\n\n/**\n * OS postcode search\n * @param {string} postcode - the postcode\n * @param {string} apiKey - the OS api key\n */\nexport async function searchByPostcode(postcode, apiKey) {\n const endpoint = 'postcode'\n const url = `https://api.os.uk/search/places/v1/${endpoint}?postcode=${encodeURIComponent(postcode.replaceAll(/\\s/g, ''))}&key=${apiKey}`\n\n return getAddressData(url, endpoint)\n}\n\n/**\n * OS UPRN search\n * @param {string} uprn - the unique property reference number\n * @param {string} apiKey - the OS api key\n */\nexport async function searchByUPRN(uprn, apiKey) {\n const endpoint = 'uprn'\n const url = `https://api.os.uk/search/places/v1/${endpoint}?uprn=${uprn}&key=${apiKey}`\n\n return getAddressData(url, endpoint)\n}\n\n/**\n * OS postcode and building name search\n * @param {string} postcodeQuery - the postcode query\n * @param {string} buildingNameQuery - the building name query\n * @param {string} apiKey - the OS api key\n */\nexport async function search(postcodeQuery, buildingNameQuery, apiKey) {\n let addresses = await searchByPostcode(postcodeQuery, apiKey)\n\n if (buildingNameQuery) {\n addresses = addresses.filter((item) =>\n item.address.includes(buildingNameQuery.toUpperCase())\n )\n }\n\n return addresses\n}\n\n/**\n * Converts a delivery point address to an address\n * Taken from http://github.com/dwp/find-an-address-plugin/blob/main/utils/getData.js\n * @param {DeliveryPointAddress} dpa\n */\nfunction formatAddress(dpa) {\n const addressLine1 = formatAddressLine1(dpa)\n const addressLine2 = formatAddressLine2(dpa)\n const town = titleCase(dpa.POST_TOWN || '')\n const postcode = dpa.POSTCODE || ''\n const lines = [addressLine1, addressLine2, town]\n const formatted = `${lines.filter((i) => !!i).join(', ')}, ${postcode}`\n\n /**\n * @type {Address}\n */\n const address = {\n uprn: dpa.UPRN,\n address: dpa.ADDRESS,\n addressLine1,\n addressLine2,\n town,\n county: '',\n postcode,\n formatted\n }\n\n return address\n}\n\n/**\n * @param {DeliveryPointAddress} dpa\n */\nfunction formatAddressLine1(dpa) {\n return titleCase(\n dpa.ORGANISATION_NAME ||\n dpa.SUB_BUILDING_NAME ||\n dpa.BUILDING_NAME ||\n dpa.BUILDING_NUMBER\n ? [\n dpa.ORGANISATION_NAME || '',\n dpa.SUB_BUILDING_NAME || '',\n dpa.BUILDING_NAME || '',\n dpa.BUILDING_NUMBER || ''\n ]\n .filter((item) => !!item)\n .join(' ')\n : ''\n )\n}\n\n/**\n * @param {DeliveryPointAddress} dpa\n */\nfunction formatAddressLine2(dpa) {\n return titleCase(\n dpa.THOROUGHFARE_NAME || dpa.DEPENDENT_LOCALITY\n ? [dpa.THOROUGHFARE_NAME || '', dpa.DEPENDENT_LOCALITY || '']\n .filter((item) => !!item)\n .join(', ')\n : ''\n )\n}\n\n/**\n * Title case address\n * @param {string} address\n */\nfunction titleCase(address) {\n return address\n .split(' ')\n .map((item) => item.charAt(0).toUpperCase() + item.slice(1).toLowerCase())\n .join(' ')\n}\n\n/**\n * @import { Address, DeliveryPointAddress, DeliveryPointAddressResult } from '~/src/server/plugins/postcode-lookup/types.js'\n */\n"],"mappings":"AAAA,SAASA,eAAe,QAAQ,oBAAoB;AACpD,OAAOC,IAAI,MAAM,YAAY;AAE7B,SAASC,MAAM;AACf,SAASC,OAAO;;AAEhB;AACA;AACA;AACA,SAASC,KAAKA,CAAA,EAAG;EACf,OAAO,EAAE;AACX;;AAEA;AACA;AACA;AACA;AACA;AACA,SAASC,sBAAsBA,CAACC,GAAG,EAAEC,QAAQ,EAAE;EAC7C;EACA;EACA,MAAMC,QAAQ,GAAGP,IAAI,CAACQ,MAAM,CAACH,GAAG,CAAC,IAAIA,GAAG,CAACI,IAAI;EAC7C,MAAMC,GAAG,GAAG,GAAGX,eAAe,CAACM,GAAG,CAAC,IAAI,CAACE,QAAQ,IAAIA,QAAQ,CAACI,OAAO,EAAEC,KAAK,EAAEC,OAAO,KAAK,EAAE,EAAE;EAE7FZ,MAAM,CAACW,KAAK,CAACP,GAAG,EAAE,uCAAuCC,QAAQ,MAAMI,GAAG,GAAG,CAAC;EAE9E,OAAOP,KAAK,CAAC,CAAC;AAChB;;AAEA;AACA;AACA;AACA;AACA;AACA,eAAeW,cAAcA,CAACC,GAAG,EAAET,QAAQ,EAAE;EAC3C,MAAMU,aAAa,GACjB,yDAA2Dd,OAAQ;EAErE,IAAI;IACF,MAAMe,QAAQ,GA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
1
1
|
{
|
|
2
2
|
"name": "@defra/forms-engine-plugin",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.11.0",
|
|
4
4
|
"description": "Defra forms engine",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -44,9 +44,10 @@
|
|
|
44
44
|
"dev:debug": "concurrently \"npm run client:watch\" \"npm run server:watch:debug\" --kill-others --names \"client,server\" --prefix-colors \"red.dim,blue.dim\"",
|
|
45
45
|
"format": "npm run format:check -- --write",
|
|
46
46
|
"format:check": "prettier --cache --cache-location .cache/prettier --cache-strategy content --check \"**/*.{cjs,js,json,md,mjs,scss,ts}\"",
|
|
47
|
+
"predocs:dev": "node scripts/generate-schema-docs.js && node scripts/generate-component-docs.js",
|
|
47
48
|
"docs:dev": "BROWSERSLIST_ENV=javascripts docusaurus start --host 0.0.0.0",
|
|
48
49
|
"docs:build": "BROWSERSLIST_ENV=javascripts docusaurus build",
|
|
49
|
-
"docs:build:all": "node scripts/generate-schema-docs.js && npm run docs:build",
|
|
50
|
+
"docs:build:all": "node scripts/generate-schema-docs.js && node scripts/generate-component-docs.js && npm run docs:build",
|
|
50
51
|
"docs:serve": "docusaurus serve --host 0.0.0.0",
|
|
51
52
|
"docs:clear": "docusaurus clear",
|
|
52
53
|
"generate-schema-docs": "node scripts/generate-schema-docs.js",
|
|
@@ -78,7 +79,7 @@
|
|
|
78
79
|
"minimatch": "3.1.5"
|
|
79
80
|
},
|
|
80
81
|
"serialize-javascript": ">=7.0.3",
|
|
81
|
-
"react-router": "^
|
|
82
|
+
"react-router": "^5.3.4"
|
|
82
83
|
},
|
|
83
84
|
"engines": {
|
|
84
85
|
"node": ">=22.11.0 <25.0.0",
|
|
@@ -86,9 +87,9 @@
|
|
|
86
87
|
},
|
|
87
88
|
"license": "SEE LICENSE IN LICENSE",
|
|
88
89
|
"dependencies": {
|
|
89
|
-
"@defra/forms-model": "^3.0.
|
|
90
|
+
"@defra/forms-model": "^3.0.655",
|
|
90
91
|
"@defra/hapi-tracing": "^1.29.0",
|
|
91
|
-
"@defra/interactive-map": "^0.0.
|
|
92
|
+
"@defra/interactive-map": "^0.0.22-alpha",
|
|
92
93
|
"@elastic/ecs-pino-format": "^1.5.0",
|
|
93
94
|
"@hapi/boom": "^10.0.1",
|
|
94
95
|
"@hapi/bourne": "^3.0.0",
|
|
@@ -104,6 +105,7 @@
|
|
|
104
105
|
"@hapi/wreck": "^18.1.0",
|
|
105
106
|
"@hapi/yar": "^11.0.3",
|
|
106
107
|
"@turf/bbox": "^7.3.4",
|
|
108
|
+
"@turf/boolean-within": "^7.3.5",
|
|
107
109
|
"@turf/centroid": "^7.3.4",
|
|
108
110
|
"@types/humanize-duration": "^3.27.4",
|
|
109
111
|
"accessible-autocomplete": "^3.0.1",
|
|
@@ -143,9 +145,9 @@
|
|
|
143
145
|
"@babel/plugin-syntax-import-attributes": "^7.27.1",
|
|
144
146
|
"@babel/preset-env": "^7.28.5",
|
|
145
147
|
"@babel/preset-typescript": "^7.28.5",
|
|
146
|
-
"@defra/docusaurus-theme-govuk": "^0.0.
|
|
147
|
-
"@docusaurus/core": "^3.
|
|
148
|
-
"@docusaurus/plugin-content-docs": "^3.
|
|
148
|
+
"@defra/docusaurus-theme-govuk": "^0.0.22-alpha",
|
|
149
|
+
"@docusaurus/core": "^3.10.1",
|
|
150
|
+
"@docusaurus/plugin-content-docs": "^3.10.1",
|
|
149
151
|
"@easyops-cn/docusaurus-search-local": "^0.55.0",
|
|
150
152
|
"@hapi/basic": "^7.0.2",
|
|
151
153
|
"@mdx-js/react": "^3.1.1",
|
|
@@ -39,8 +39,8 @@ const lineFeatureProperties = {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
const polygonFeatureProperties = {
|
|
42
|
-
stroke: '
|
|
43
|
-
fill: 'rgba(
|
|
42
|
+
stroke: 'rgb(0, 0, 0)',
|
|
43
|
+
fill: 'rgba(255, 221, 0, 0.2)',
|
|
44
44
|
strokeWidth: 2
|
|
45
45
|
}
|
|
46
46
|
|
|
@@ -111,11 +111,47 @@ export function processGeospatial(config, geospatial, index) {
|
|
|
111
111
|
const geojson = getGeoJSON(geospatialInput)
|
|
112
112
|
const bounds = geojson.features.length ? getBoundingBox(geojson) : undefined
|
|
113
113
|
const drawPlugin = defra.drawMLPlugin()
|
|
114
|
+
const plugins = [drawPlugin]
|
|
115
|
+
const country = geospatial.dataset.country
|
|
116
|
+
|
|
117
|
+
if (country) {
|
|
118
|
+
// Add the country bounds as a dataset plugin to show the valid area on the map
|
|
119
|
+
// and provide feedback to the user when they add features outside of the bounds.
|
|
120
|
+
const datasetsPlugin = defra.datasetsMaplibrePlugin({
|
|
121
|
+
datasets: [
|
|
122
|
+
{
|
|
123
|
+
id: 'invalid-area',
|
|
124
|
+
label: 'Invalid areas',
|
|
125
|
+
geojson: `${config.apiPath}/maps/countries.geojson?omit=${country}`,
|
|
126
|
+
showInKey: false,
|
|
127
|
+
showInMenu: false,
|
|
128
|
+
style: {
|
|
129
|
+
stroke: 'gray',
|
|
130
|
+
strokeWidth: 1,
|
|
131
|
+
fill: 'rgba(211,211,211,0.8)'
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
id: 'valid-area',
|
|
136
|
+
label: 'Valid areas',
|
|
137
|
+
geojson: `${config.apiPath}/maps/countries.geojson?only=${country}`,
|
|
138
|
+
showInKey: false,
|
|
139
|
+
showInMenu: false,
|
|
140
|
+
style: {
|
|
141
|
+
stroke: 'rgba(0,112,60,1)',
|
|
142
|
+
strokeWidth: 1
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
]
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
plugins.push(datasetsPlugin)
|
|
149
|
+
}
|
|
114
150
|
|
|
115
151
|
const initConfig = {
|
|
116
152
|
...defaultConfig,
|
|
117
153
|
bounds,
|
|
118
|
-
plugins
|
|
154
|
+
plugins
|
|
119
155
|
}
|
|
120
156
|
|
|
121
157
|
const { map, interactPlugin } = createMap(mapId, initConfig, config)
|
|
@@ -240,18 +240,6 @@ export function makeTileRequestTransformer(apiPath) {
|
|
|
240
240
|
}
|
|
241
241
|
}
|
|
242
242
|
|
|
243
|
-
/**
|
|
244
|
-
* Temporary transform request function to transform geocode requests. Fixed in v0.0.18 of interactive map so this is not needed when we upgrade.
|
|
245
|
-
* @param {object} request
|
|
246
|
-
* @param {string} request.url
|
|
247
|
-
* @param {{ method: 'get' }} request.options
|
|
248
|
-
* @returns {Request}
|
|
249
|
-
*/
|
|
250
|
-
export const transformGeocodeRequest = (request) => {
|
|
251
|
-
const url = new URL(request.url, window.location.origin)
|
|
252
|
-
return new Request(url.toString(), request.options)
|
|
253
|
-
}
|
|
254
|
-
|
|
255
243
|
/**
|
|
256
244
|
* Create a Defra map instance
|
|
257
245
|
* @param {string} mapId - the map id
|
|
@@ -267,7 +255,7 @@ export function createMap(mapId, initConfig, mapsConfig) {
|
|
|
267
255
|
|
|
268
256
|
const interactPlugin = defra.interactPlugin({
|
|
269
257
|
markerColor: { outdoor: '#ff0000', dark: '#00ff00' },
|
|
270
|
-
|
|
258
|
+
interactionModes: ['placeMarker'],
|
|
271
259
|
multiSelect: false
|
|
272
260
|
})
|
|
273
261
|
|
|
@@ -332,7 +320,6 @@ export function createMap(mapId, initConfig, mapsConfig) {
|
|
|
332
320
|
}),
|
|
333
321
|
interactPlugin,
|
|
334
322
|
defra.searchPlugin({
|
|
335
|
-
transformRequest: transformGeocodeRequest,
|
|
336
323
|
osNamesURL: `${apiPath}/geocode-proxy?query={query}`,
|
|
337
324
|
width: '300px',
|
|
338
325
|
showMarker: false
|
package/src/index.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import { getErrorMessage } from '@defra/forms-model'
|
|
2
2
|
|
|
3
3
|
import { config } from './config/index.js'
|
|
4
|
-
import {
|
|
4
|
+
import { logger } from './server/common/helpers/logging/logger.js'
|
|
5
5
|
import { createServer } from './server/index.js'
|
|
6
6
|
|
|
7
|
-
const logger = createLogger()
|
|
8
|
-
|
|
9
7
|
process.on('unhandledRejection', (error) => {
|
|
10
8
|
const err = getErrorMessage(error)
|
|
11
9
|
logger.info('Unhandled rejection')
|
|
@@ -2,6 +2,10 @@ import { pino } from 'pino'
|
|
|
2
2
|
|
|
3
3
|
import { loggerOptions } from './logger-options.js'
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
function createPinoLogger() {
|
|
6
6
|
return pino(loggerOptions)
|
|
7
7
|
}
|
|
8
|
+
|
|
9
|
+
// Singleton logger instance - pino adds 'exit' listeners to process,
|
|
10
|
+
// so we reuse a single instance to avoid MaxListenersExceededWarning
|
|
11
|
+
export const logger = createPinoLogger()
|
|
@@ -2,7 +2,7 @@ import { getErrorMessage } from '@defra/forms-model'
|
|
|
2
2
|
import { Cluster, Redis } from 'ioredis'
|
|
3
3
|
|
|
4
4
|
import { config } from '../../../config/index.js'
|
|
5
|
-
import {
|
|
5
|
+
import { logger } from './logging/logger.js'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Setup Redis and provide a redis client
|
|
@@ -11,8 +11,6 @@ import { createLogger } from './logging/logger.js'
|
|
|
11
11
|
* Out in the wild - Elasticache / Redis Cluster with username and password
|
|
12
12
|
*/
|
|
13
13
|
export function buildRedisClient() {
|
|
14
|
-
const logger = createLogger()
|
|
15
|
-
|
|
16
14
|
const port = 6379
|
|
17
15
|
const db = 0
|
|
18
16
|
const redisConfig = config.get('redis')
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
FormComponent,
|
|
7
7
|
isGeospatialState
|
|
8
8
|
} from './FormComponent.js'
|
|
9
|
-
import {
|
|
9
|
+
import { getGeospatialSchema } from './helpers/geospatial.js'
|
|
10
10
|
import { messageTemplate } from '../pageControllers/validationOptions.js'
|
|
11
11
|
import {
|
|
12
12
|
type ErrorMessageTemplateList,
|
|
@@ -31,7 +31,9 @@ export class GeospatialField extends FormComponent {
|
|
|
31
31
|
|
|
32
32
|
const { options } = def
|
|
33
33
|
|
|
34
|
-
let formSchema =
|
|
34
|
+
let formSchema = getGeospatialSchema(options.countries?.at(0))
|
|
35
|
+
.label(this.label)
|
|
36
|
+
.required()
|
|
35
37
|
|
|
36
38
|
formSchema = formSchema.max(50)
|
|
37
39
|
|
|
@@ -90,6 +92,7 @@ export class GeospatialField extends FormComponent {
|
|
|
90
92
|
|
|
91
93
|
return {
|
|
92
94
|
...viewModel,
|
|
95
|
+
country: this.options.countries?.at(0),
|
|
93
96
|
value
|
|
94
97
|
}
|
|
95
98
|
}
|
|
@@ -101,6 +104,9 @@ export class GeospatialField extends FormComponent {
|
|
|
101
104
|
if (err.name === 'description') {
|
|
102
105
|
err.href = `#description_${err.path[1]}`
|
|
103
106
|
err.text = `Enter description for location ${Number(err.path[1]) + 1}`
|
|
107
|
+
} else if (typeof err.name === 'number' && err.context?.country) {
|
|
108
|
+
err.href = `#description_${err.path[1]}`
|
|
109
|
+
err.text = `Location ${Number(err.path[1]) + 1} must be in ${err.context.country}`
|
|
104
110
|
}
|
|
105
111
|
})
|
|
106
112
|
|
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
import { StatusCodes } from 'http-status-codes'
|
|
8
8
|
import joi, { type ObjectSchema } from 'joi'
|
|
9
9
|
|
|
10
|
-
import {
|
|
10
|
+
import { logger } from '../../../common/helpers/logging/logger.js'
|
|
11
11
|
import { COMPONENT_STATE_ERROR } from '../../../constants.js'
|
|
12
12
|
import { FormComponent } from './FormComponent.js'
|
|
13
13
|
import { type PaymentState } from './PaymentField.types.js'
|
|
@@ -40,8 +40,6 @@ import {
|
|
|
40
40
|
formatCurrency
|
|
41
41
|
} from '../../payment/helper.js'
|
|
42
42
|
|
|
43
|
-
const logger = createLogger()
|
|
44
|
-
|
|
45
43
|
export class PaymentField extends FormComponent {
|
|
46
44
|
declare options: PaymentFieldComponent['options']
|
|
47
45
|
declare formSchema: ObjectSchema
|