@defra/forms-engine-plugin 4.9.1 → 4.10.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.
Files changed (38) hide show
  1. package/.server/index.js +1 -2
  2. package/.server/index.js.map +1 -1
  3. package/.server/server/common/helpers/logging/logger.d.ts +1 -1
  4. package/.server/server/common/helpers/logging/logger.js +5 -1
  5. package/.server/server/common/helpers/logging/logger.js.map +1 -1
  6. package/.server/server/common/helpers/redis-client.js +1 -2
  7. package/.server/server/common/helpers/redis-client.js.map +1 -1
  8. package/.server/server/plugins/engine/components/PaymentField.js +4 -11
  9. package/.server/server/plugins/engine/components/PaymentField.js.map +1 -1
  10. package/.server/server/plugins/engine/helpers.js +1 -2
  11. package/.server/server/plugins/engine/helpers.js.map +1 -1
  12. package/.server/server/plugins/engine/models/FormModel.js +1 -2
  13. package/.server/server/plugins/engine/models/FormModel.js.map +1 -1
  14. package/.server/server/plugins/engine/options.js +1 -2
  15. package/.server/server/plugins/engine/options.js.map +1 -1
  16. package/.server/server/plugins/engine/routes/payment.js +1 -2
  17. package/.server/server/plugins/engine/routes/payment.js.map +1 -1
  18. package/.server/server/plugins/map/service.js +1 -2
  19. package/.server/server/plugins/map/service.js.map +1 -1
  20. package/.server/server/plugins/nunjucks/context.js +1 -2
  21. package/.server/server/plugins/nunjucks/context.js.map +1 -1
  22. package/.server/server/plugins/payment/service.js +1 -2
  23. package/.server/server/plugins/payment/service.js.map +1 -1
  24. package/.server/server/plugins/postcode-lookup/service.js +1 -2
  25. package/.server/server/plugins/postcode-lookup/service.js.map +1 -1
  26. package/package.json +1 -1
  27. package/src/index.ts +1 -3
  28. package/src/server/common/helpers/logging/logger.ts +5 -1
  29. package/src/server/common/helpers/redis-client.js +1 -3
  30. package/src/server/plugins/engine/components/PaymentField.ts +4 -11
  31. package/src/server/plugins/engine/helpers.ts +1 -3
  32. package/src/server/plugins/engine/models/FormModel.ts +1 -3
  33. package/src/server/plugins/engine/options.js +1 -3
  34. package/src/server/plugins/engine/routes/payment.js +1 -3
  35. package/src/server/plugins/map/service.js +1 -3
  36. package/src/server/plugins/nunjucks/context.js +1 -3
  37. package/src/server/plugins/payment/service.js +1 -3
  38. package/src/server/plugins/postcode-lookup/service.js +1 -3
@@ -1 +1 @@
1
- {"version":3,"file":"options.js","names":["getErrorMessage","Joi","createLogger","CacheService","logger","pluginRegistrationOptionsSchema","object","model","optional","services","controllers","pattern","string","any","cache","alternatives","try","instance","globals","filters","pluginPath","nunjucks","baseLayoutPath","required","paths","array","items","viewContext","function","preparePageEventRequestOptions","onRequest","baseUrl","uri","saveAndExit","ordnanceSurveyApiKey","ordnanceSurveyApiSecret","validatePluginOptions","options","result","validate","abortEarly","error","Error","value"],"sources":["../../../../src/server/plugins/engine/options.js"],"sourcesContent":["import { getErrorMessage } from '@defra/forms-model'\nimport Joi from 'joi'\n\nimport { createLogger } from '~/src/server/common/helpers/logging/logger.js'\nimport { CacheService } from '~/src/server/services/index.js'\n\nconst logger = createLogger()\n\nconst pluginRegistrationOptionsSchema = Joi.object({\n model: Joi.object().optional(),\n services: Joi.object().optional(),\n controllers: Joi.object().pattern(Joi.string(), Joi.any()).optional(),\n cache: Joi.alternatives().try(\n Joi.object().instance(CacheService),\n Joi.string()\n ),\n globals: Joi.object().pattern(Joi.string(), Joi.any()).optional(),\n filters: Joi.object().pattern(Joi.string(), Joi.any()).optional(),\n pluginPath: Joi.string().optional(),\n nunjucks: Joi.object({\n baseLayoutPath: Joi.string().required(),\n paths: Joi.array().items(Joi.string()).required()\n }).required(),\n viewContext: Joi.function().required(),\n preparePageEventRequestOptions: Joi.function().optional(),\n onRequest: Joi.function().optional(),\n baseUrl: Joi.string().uri().required(),\n saveAndExit: Joi.function().optional(),\n ordnanceSurveyApiKey: Joi.string().optional(),\n ordnanceSurveyApiSecret: Joi.string().optional()\n})\n\n/**\n * Validates the plugin options against the schema and returns the validated value.\n * @param {PluginOptions} options\n * @returns {PluginOptions}\n */\nexport function validatePluginOptions(options) {\n const result = pluginRegistrationOptionsSchema.validate(options, {\n abortEarly: false\n })\n\n if (result.error) {\n logger.error(\n result.error,\n `Missing required properties in plugin options: ${getErrorMessage(result.error)}`\n )\n throw new Error('Invalid plugin options', result.error)\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return result.value\n}\n\n/**\n * @import { PluginOptions } from '~/src/server/plugins/engine/types.js'\n */\n"],"mappings":"AAAA,SAASA,eAAe,QAAQ,oBAAoB;AACpD,OAAOC,GAAG,MAAM,KAAK;AAErB,SAASC,YAAY;AACrB,SAASC,YAAY;AAErB,MAAMC,MAAM,GAAGF,YAAY,CAAC,CAAC;AAE7B,MAAMG,+BAA+B,GAAGJ,GAAG,CAACK,MAAM,CAAC;EACjDC,KAAK,EAAEN,GAAG,CAACK,MAAM,CAAC,CAAC,CAACE,QAAQ,CAAC,CAAC;EAC9BC,QAAQ,EAAER,GAAG,CAACK,MAAM,CAAC,CAAC,CAACE,QAAQ,CAAC,CAAC;EACjCE,WAAW,EAAET,GAAG,CAACK,MAAM,CAAC,CAAC,CAACK,OAAO,CAACV,GAAG,CAACW,MAAM,CAAC,CAAC,EAAEX,GAAG,CAACY,GAAG,CAAC,CAAC,CAAC,CAACL,QAAQ,CAAC,CAAC;EACrEM,KAAK,EAAEb,GAAG,CAACc,YAAY,CAAC,CAAC,CAACC,GAAG,CAC3Bf,GAAG,CAACK,MAAM,CAAC,CAAC,CAACW,QAAQ,CAACd,YAAY,CAAC,EACnCF,GAAG,CAACW,MAAM,CAAC,CACb,CAAC;EACDM,OAAO,EAAEjB,GAAG,CAACK,MAAM,CAAC,CAAC,CAACK,OAAO,CAACV,GAAG,CAACW,MAAM,CAAC,CAAC,EAAEX,GAAG,CAACY,GAAG,CAAC,CAAC,CAAC,CAACL,QAAQ,CAAC,CAAC;EACjEW,OAAO,EAAElB,GAAG,CAACK,MAAM,CAAC,CAAC,CAACK,OAAO,CAACV,GAAG,CAACW,MAAM,CAAC,CAAC,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EF,IAAIF,MAAM,CAACG,KAAK,EAAE;IAChBrC,MAAM,CAACqC,KAAK,CACVH,MAAM,CAACG,KAAK,EACZ,kDAAkDzC,eAAe,CAACsC,MAAM,CAACG,KAAK,CAAC,EACjF,CAAC;IACD,MAAM,IAAIC,KAAK,CAAC,wBAAwB,EAAEJ,MAAM,CAACG,KAAK,CAAC;EACzD;;EAEA;EACA,OAAOH,MAAM,CAACK,KAAK;AACrB;;AAEA;AACA;AACA","ignoreList":[]}
1
+ {"version":3,"file":"options.js","names":["getErrorMessage","Joi","logger","CacheService","pluginRegistrationOptionsSchema","object","model","optional","services","controllers","pattern","string","any","cache","alternatives","try","instance","globals","filters","pluginPath","nunjucks","baseLayoutPath","required","paths","array","items","viewContext","function","preparePageEventRequestOptions","onRequest","baseUrl","uri","saveAndExit","ordnanceSurveyApiKey","ordnanceSurveyApiSecret","validatePluginOptions","options","result","validate","abortEarly","error","Error","value"],"sources":["../../../../src/server/plugins/engine/options.js"],"sourcesContent":["import { getErrorMessage } from '@defra/forms-model'\nimport Joi from 'joi'\n\nimport { logger } from '~/src/server/common/helpers/logging/logger.js'\nimport { CacheService } from '~/src/server/services/index.js'\n\nconst pluginRegistrationOptionsSchema = Joi.object({\n model: Joi.object().optional(),\n services: Joi.object().optional(),\n controllers: Joi.object().pattern(Joi.string(), Joi.any()).optional(),\n cache: Joi.alternatives().try(\n Joi.object().instance(CacheService),\n Joi.string()\n ),\n globals: Joi.object().pattern(Joi.string(), Joi.any()).optional(),\n filters: Joi.object().pattern(Joi.string(), Joi.any()).optional(),\n pluginPath: Joi.string().optional(),\n nunjucks: Joi.object({\n baseLayoutPath: Joi.string().required(),\n paths: Joi.array().items(Joi.string()).required()\n }).required(),\n viewContext: Joi.function().required(),\n preparePageEventRequestOptions: Joi.function().optional(),\n onRequest: Joi.function().optional(),\n baseUrl: Joi.string().uri().required(),\n saveAndExit: Joi.function().optional(),\n ordnanceSurveyApiKey: Joi.string().optional(),\n ordnanceSurveyApiSecret: Joi.string().optional()\n})\n\n/**\n * Validates the plugin options against the schema and returns the validated value.\n * @param {PluginOptions} options\n * @returns {PluginOptions}\n */\nexport function validatePluginOptions(options) {\n const result = pluginRegistrationOptionsSchema.validate(options, {\n abortEarly: false\n })\n\n if (result.error) {\n logger.error(\n result.error,\n `Missing required properties in plugin options: ${getErrorMessage(result.error)}`\n )\n throw new Error('Invalid plugin options', result.error)\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return result.value\n}\n\n/**\n * @import { PluginOptions } from '~/src/server/plugins/engine/types.js'\n */\n"],"mappings":"AAAA,SAASA,eAAe,QAAQ,oBAAoB;AACpD,OAAOC,GAAG,MAAM,KAAK;AAErB,SAASC,MAAM;AACf,SAASC,YAAY;AAErB,MAAMC,+BAA+B,GAAGH,GAAG,CAACI,MAAM,CAAC;EACjDC,KAAK,EAAEL,GAAG,CAACI,MAAM,CAAC,CAAC,CAACE,QAAQ,CAAC,CAAC;EAC9BC,QAAQ,EAAEP,GAAG,CAACI,MAAM,CAAC,CAAC,CAACE,QAAQ,CAAC,CAAC;EACjCE,WAAW,EAAER,GAAG,CAACI,MAAM,CAAC,CAAC,CAACK,OAAO,CAACT,GAAG,CAACU,MAAM,CAAC,CAAC,EAAEV,GAAG,CAACW,GAAG,CAAC,CAAC,CAAC,CAACL,QAAQ,CAAC,CAAC;EACrEM,KAAK,EAAEZ,GAAG,CAACa,YAAY,CAAC,CAAC,CAACC,GAAG,CAC3Bd,GAAG,CAACI,MAAM,CAAC,CAAC,CAACW,QAAQ,CAACb,YAAY,CAAC,EACnCF,GAAG,CAACU,MAAM,CAAC,CACb,CAAC;EACDM,OAAO,EAAEhB,GAAG,CAACI,MAAM,CAAC,CAAC,CAACK,OAAO,CAACT,GAAG,CAACU,MAAM,CAAC,CAAC,EAAEV,GAAG,CAACW,GAAG,CAAC,CAAC,CAAC,CAACL,QAAQ,CAAC,CAAC;EACjEW,OAAO,EAAEjB,GAAG,CAACI,MAAM,CAAC,CAAC,C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O,EAAE;IAC/DG,UAAU,EAAE;EACd,CAAC,CAAC;EAEF,IAAIF,MAAM,CAACG,KAAK,EAAE;IAChBtC,MAAM,CAACsC,KAAK,CACVH,MAAM,CAACG,KAAK,EACZ,kDAAkDxC,eAAe,CAACqC,MAAM,CAACG,KAAK,CAAC,EACjF,CAAC;IACD,MAAM,IAAIC,KAAK,CAAC,wBAAwB,EAAEJ,MAAM,CAACG,KAAK,CAAC;EACzD;;EAEA;EACA,OAAOH,MAAM,CAACK,KAAK;AACrB;;AAEA;AACA;AACA","ignoreList":[]}
@@ -1,13 +1,12 @@
1
1
  import Boom from '@hapi/boom';
2
2
  import { StatusCodes } from 'http-status-codes';
3
3
  import Joi from 'joi';
4
- import { createLogger } from "../../../common/helpers/logging/logger.js";
4
+ import { logger } from "../../../common/helpers/logging/logger.js";
5
5
  import { EXTERNAL_STATE_APPENDAGE } from "../../../constants.js";
6
6
  import { getPluginOptions } from "../helpers.js";
7
7
  import { buildPaymentInfo, convertPenceToPounds, getPaymentContext } from "./payment-helper.js";
8
8
  export const PAYMENT_RETURN_PATH = '/payment-callback';
9
9
  export const PAYMENT_SESSION_PREFIX = 'payment-';
10
- const logger = createLogger();
11
10
 
12
11
  /**
13
12
  * Flash form component state after successful payment
@@ -1 +1 @@
1
- {"version":3,"file":"payment.js","names":["Boom","StatusCodes","Joi","createLogger","EXTERNAL_STATE_APPENDAGE","getPluginOptions","buildPaymentInfo","convertPenceToPounds","getPaymentContext","PAYMENT_RETURN_PATH","PAYMENT_SESSION_PREFIX","logger","flashComponentState","request","session","paymentStatus","paymentState","paymentId","reference","amount","description","uuid","formId","isLivePayment","payerEmail","email","preAuth","status","createdAt","Date","toISOString","appendage","component","componentName","data","yar","flash","getRoutes","getReturnRoute","logPaymentSuccess","info","state","logPaymentFailure","handlePaymentSuccess","h","sessionKey","clear","separator","returnUrl","includes","redirect","code","SEE_OTHER","handlePaymentFailure","failureUrl","method","path","handler","query","services","server","formsService","nextUrl","_links","next_url","href","badRequest","unknownStatus","internal","options","validate","object","keys","string","required"],"sources":["../../../../../src/server/plugins/engine/routes/payment.js"],"sourcesContent":["import Boom from '@hapi/boom'\nimport { StatusCodes } from 'http-status-codes'\nimport Joi from 'joi'\n\nimport { createLogger } from '~/src/server/common/helpers/logging/logger.js'\nimport { EXTERNAL_STATE_APPENDAGE } from '~/src/server/constants.js'\nimport { getPluginOptions } from '~/src/server/plugins/engine/helpers.js'\nimport {\n buildPaymentInfo,\n convertPenceToPounds,\n getPaymentContext\n} from '~/src/server/plugins/engine/routes/payment-helper.js'\n\nexport const PAYMENT_RETURN_PATH = '/payment-callback'\nexport const PAYMENT_SESSION_PREFIX = 'payment-'\n\nconst logger = createLogger()\n\n/**\n * Flash form component state after successful payment\n * @param {Request} request - the request\n * @param {PaymentSessionData} session - the session data containing payment state\n * @param {GetPaymentResponse} paymentStatus - the payment status response from GOV.UK Pay\n */\nfunction flashComponentState(request, session, paymentStatus) {\n /** @type {PaymentState} */\n const paymentState = {\n paymentId: paymentStatus.paymentId,\n reference: session.reference,\n amount: session.amount,\n description: session.description,\n uuid: session.uuid,\n formId: session.formId,\n isLivePayment: session.isLivePayment,\n payerEmail: paymentStatus.email,\n preAuth: {\n status: 'success',\n createdAt: new Date().toISOString()\n }\n }\n\n /** @type {ExternalStateAppendage} */\n const appendage = {\n component: session.componentName,\n data: /** @type {FormState} */ (/** @type {unknown} */ (paymentState))\n }\n\n request.yar.flash(EXTERNAL_STATE_APPENDAGE, appendage, true)\n}\n\n/**\n * Gets the payment routes for handling GOV.UK Pay callbacks\n * @returns {ServerRoute[]}\n */\nexport function getRoutes() {\n return [getReturnRoute()]\n}\n\n/**\n * Logs successful payment\n * @param {PaymentSessionData} session - the session data\n * @param {GetPaymentResponse} paymentStatus - the payment status from GOV.UK Pay\n */\nfunction logPaymentSuccess(session, paymentStatus) {\n logger.info(\n buildPaymentInfo(\n 'pre-auth',\n 'success',\n `${paymentStatus.state.status} amount=${convertPenceToPounds(paymentStatus.amount)}`,\n session.isLivePayment,\n paymentStatus.paymentId\n ),\n `[payment] Successful pre-auth for paymentId=${paymentStatus.paymentId}`\n )\n}\n\n/**\n * Logs failed/cancelled payment\n * @param {PaymentSessionData} session - the session data\n * @param {GetPaymentResponse} paymentStatus - the payment status from GOV.UK Pay\n */\nfunction logPaymentFailure(session, paymentStatus) {\n logger.info(\n buildPaymentInfo(\n 'pre-auth',\n 'failed/cancelled',\n `${paymentStatus.state.status} amount=${convertPenceToPounds(paymentStatus.amount)}`,\n session.isLivePayment,\n paymentStatus.paymentId\n ),\n `[payment] Failed/cancelled pre-auth for paymentId=${paymentStatus.paymentId}`\n )\n}\n\n/**\n * Handles successful payment states (capturable/success)\n * @param {Request} request - the request\n * @param {ResponseToolkit} h - the response toolkit\n * @param {PaymentSessionData} session - the session data\n * @param {string} sessionKey - the session key\n * @param {GetPaymentResponse} paymentStatus - the payment status from GOV.UK Pay\n */\nfunction handlePaymentSuccess(request, h, session, sessionKey, paymentStatus) {\n flashComponentState(request, session, paymentStatus)\n request.yar.clear(sessionKey)\n\n // Append paymentComplete flag so the summary page auto-submits\n // instead of showing CYA again\n const separator = session.returnUrl.includes('?') ? '&' : '?'\n const returnUrl = `${session.returnUrl}${separator}paymentComplete=true`\n\n return h.redirect(returnUrl).code(StatusCodes.SEE_OTHER)\n}\n\n/**\n * Handles failed/cancelled/error payment states\n * @param {Request} request - the request\n * @param {ResponseToolkit} h - the response toolkit\n * @param {PaymentSessionData} session - the session data\n * @param {string} sessionKey - the session key\n */\nfunction handlePaymentFailure(request, h, session, sessionKey) {\n request.yar.clear(sessionKey)\n return h.redirect(session.failureUrl).code(StatusCodes.SEE_OTHER)\n}\n\n/**\n * Route handler for payment return URL\n * This is called when GOV.UK Pay redirects the user back after payment\n * @returns {ServerRoute}\n */\nfunction getReturnRoute() {\n return {\n method: 'GET',\n path: PAYMENT_RETURN_PATH,\n async handler(request, h) {\n const { uuid } = /** @type {{ uuid: string }} */ (request.query)\n\n const { services } = getPluginOptions(request.server)\n\n const { session, sessionKey, paymentStatus } = await getPaymentContext(\n request,\n uuid,\n /** @type {FormsService} */ (services?.formsService)\n )\n\n /**\n * @see https://docs.payments.service.gov.uk/api_reference/#payment-status-lifecycle\n */\n const { status } = paymentStatus.state\n\n switch (status) {\n case 'capturable':\n case 'success':\n logPaymentSuccess(session, paymentStatus)\n return handlePaymentSuccess(\n request,\n h,\n session,\n sessionKey,\n paymentStatus\n )\n\n case 'cancelled':\n case 'failed':\n case 'error':\n logPaymentFailure(session, paymentStatus)\n return handlePaymentFailure(request, h, session, sessionKey)\n\n case 'created':\n case 'started':\n case 'submitted': {\n const nextUrl = paymentStatus._links.next_url?.href\n\n if (!nextUrl) {\n throw Boom.badRequest(\n `Payment in state '${status}' but no next_url available`\n )\n }\n\n return h.redirect(nextUrl).code(StatusCodes.SEE_OTHER)\n }\n\n default: {\n const unknownStatus = /** @type {string} */ (status)\n throw Boom.internal(`Unknown payment status: ${unknownStatus}`)\n }\n }\n },\n options: {\n validate: {\n query: Joi.object()\n .keys({\n uuid: Joi.string().uuid().required()\n })\n .required()\n }\n }\n }\n}\n\n/**\n * @import { Request, ResponseToolkit, ServerRoute } from '@hapi/hapi'\n * @import { GetPaymentResponse, PaymentSessionData } from '~/src/server/plugins/payment/types.js'\n * @import { PaymentState } from '~/src/server/plugins/engine/components/PaymentField.types.js'\n * @import { ExternalStateAppendage, FormState } from '~/src/server/plugins/engine/types.js'\n * @import { PluginOptions } from '~/src/server/plugins/engine/types.js'\n * @import { FormsService } from '~/src/server/types.js'\n */\n"],"mappings":"AAAA,OAAOA,IAAI,MAAM,YAAY;AAC7B,SAASC,WAAW,QAAQ,mBAAmB;AAC/C,OAAOC,GAAG,MAAM,KAAK;AAErB,SAASC,YAAY;AACrB,SAASC,wBAAwB;AACjC,SAASC,gBAAgB;AACzB,SACEC,gBAAgB,EAChBC,oBAAoB,EACpBC,iBAAiB;AAGnB,OAAO,MAAMC,mBAAmB,GAAG,mBAAmB;AACtD,OAAO,M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ACH;YAEA,OAAOiB,CAAC,CAACM,QAAQ,CAACY,OAAO,CAAC,CAACX,IAAI,CAAClD,WAAW,CAACmD,SAAS,CAAC;UACxD;QAEA;UAAS;YACP,MAAMe,aAAa,GAAG,qBAAuBxC,MAAO;YACpD,MAAM3B,IAAI,CAACoE,QAAQ,CAAC,2BAA2BD,aAAa,EAAE,CAAC;UACjE;MACF;IACF,CAAC;IACDE,OAAO,EAAE;MACPC,QAAQ,EAAE;QACRZ,KAAK,EAAExD,GAAG,CAACqE,MAAM,CAAC,CAAC,CAChBC,IAAI,CAAC;UACJnD,IAAI,EAAEnB,GAAG,CAACuE,MAAM,CAAC,CAAC,CAACpD,IAAI,CAAC,CAAC,CAACqD,QAAQ,CAAC;QACrC,CAAC,CAAC,CACDA,QAAQ,CAAC;MACd;IACF;EACF,CAAC;AACH;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","ignoreList":[]}
1
+ {"version":3,"file":"payment.js","names":["Boom","StatusCodes","Joi","logger","EXTERNAL_STATE_APPENDAGE","getPluginOptions","buildPaymentInfo","convertPenceToPounds","getPaymentContext","PAYMENT_RETURN_PATH","PAYMENT_SESSION_PREFIX","flashComponentState","request","session","paymentStatus","paymentState","paymentId","reference","amount","description","uuid","formId","isLivePayment","payerEmail","email","preAuth","status","createdAt","Date","toISOString","appendage","component","componentName","data","yar","flash","getRoutes","getReturnRoute","logPaymentSuccess","info","state","logPaymentFailure","handlePaymentSuccess","h","sessionKey","clear","separator","returnUrl","includes","redirect","code","SEE_OTHER","handlePaymentFailure","failureUrl","method","path","handler","query","services","server","formsService","nextUrl","_links","next_url","href","badRequest","unknownStatus","internal","options","validate","object","keys","string","required"],"sources":["../../../../../src/server/plugins/engine/routes/payment.js"],"sourcesContent":["import Boom from '@hapi/boom'\nimport { StatusCodes } from 'http-status-codes'\nimport Joi from 'joi'\n\nimport { logger } from '~/src/server/common/helpers/logging/logger.js'\nimport { EXTERNAL_STATE_APPENDAGE } from '~/src/server/constants.js'\nimport { getPluginOptions } from '~/src/server/plugins/engine/helpers.js'\nimport {\n buildPaymentInfo,\n convertPenceToPounds,\n getPaymentContext\n} from '~/src/server/plugins/engine/routes/payment-helper.js'\n\nexport const PAYMENT_RETURN_PATH = '/payment-callback'\nexport const PAYMENT_SESSION_PREFIX = 'payment-'\n\n/**\n * Flash form component state after successful payment\n * @param {Request} request - the request\n * @param {PaymentSessionData} session - the session data containing payment state\n * @param {GetPaymentResponse} paymentStatus - the payment status response from GOV.UK Pay\n */\nfunction flashComponentState(request, session, paymentStatus) {\n /** @type {PaymentState} */\n const paymentState = {\n paymentId: paymentStatus.paymentId,\n reference: session.reference,\n amount: session.amount,\n description: session.description,\n uuid: session.uuid,\n formId: session.formId,\n isLivePayment: session.isLivePayment,\n payerEmail: paymentStatus.email,\n preAuth: {\n status: 'success',\n createdAt: new Date().toISOString()\n }\n }\n\n /** @type {ExternalStateAppendage} */\n const appendage = {\n component: session.componentName,\n data: /** @type {FormState} */ (/** @type {unknown} */ (paymentState))\n }\n\n request.yar.flash(EXTERNAL_STATE_APPENDAGE, appendage, true)\n}\n\n/**\n * Gets the payment routes for handling GOV.UK Pay callbacks\n * @returns {ServerRoute[]}\n */\nexport function getRoutes() {\n return [getReturnRoute()]\n}\n\n/**\n * Logs successful payment\n * @param {PaymentSessionData} session - the session data\n * @param {GetPaymentResponse} paymentStatus - the payment status from GOV.UK Pay\n */\nfunction logPaymentSuccess(session, paymentStatus) {\n logger.info(\n buildPaymentInfo(\n 'pre-auth',\n 'success',\n `${paymentStatus.state.status} amount=${convertPenceToPounds(paymentStatus.amount)}`,\n session.isLivePayment,\n paymentStatus.paymentId\n ),\n `[payment] Successful pre-auth for paymentId=${paymentStatus.paymentId}`\n )\n}\n\n/**\n * Logs failed/cancelled payment\n * @param {PaymentSessionData} session - the session data\n * @param {GetPaymentResponse} paymentStatus - the payment status from GOV.UK Pay\n */\nfunction logPaymentFailure(session, paymentStatus) {\n logger.info(\n buildPaymentInfo(\n 'pre-auth',\n 'failed/cancelled',\n `${paymentStatus.state.status} amount=${convertPenceToPounds(paymentStatus.amount)}`,\n session.isLivePayment,\n paymentStatus.paymentId\n ),\n `[payment] Failed/cancelled pre-auth for paymentId=${paymentStatus.paymentId}`\n )\n}\n\n/**\n * Handles successful payment states (capturable/success)\n * @param {Request} request - the request\n * @param {ResponseToolkit} h - the response toolkit\n * @param {PaymentSessionData} session - the session data\n * @param {string} sessionKey - the session key\n * @param {GetPaymentResponse} paymentStatus - the payment status from GOV.UK Pay\n */\nfunction handlePaymentSuccess(request, h, session, sessionKey, paymentStatus) {\n flashComponentState(request, session, paymentStatus)\n request.yar.clear(sessionKey)\n\n // Append paymentComplete flag so the summary page auto-submits\n // instead of showing CYA again\n const separator = session.returnUrl.includes('?') ? '&' : '?'\n const returnUrl = `${session.returnUrl}${separator}paymentComplete=true`\n\n return h.redirect(returnUrl).code(StatusCodes.SEE_OTHER)\n}\n\n/**\n * Handles failed/cancelled/error payment states\n * @param {Request} request - the request\n * @param {ResponseToolkit} h - the response toolkit\n * @param {PaymentSessionData} session - the session data\n * @param {string} sessionKey - the session key\n */\nfunction handlePaymentFailure(request, h, session, sessionKey) {\n request.yar.clear(sessionKey)\n return h.redirect(session.failureUrl).code(StatusCodes.SEE_OTHER)\n}\n\n/**\n * Route handler for payment return URL\n * This is called when GOV.UK Pay redirects the user back after payment\n * @returns {ServerRoute}\n */\nfunction getReturnRoute() {\n return {\n method: 'GET',\n path: PAYMENT_RETURN_PATH,\n async handler(request, h) {\n const { uuid } = /** @type {{ uuid: string }} */ (request.query)\n\n const { services } = getPluginOptions(request.server)\n\n const { session, sessionKey, paymentStatus } = await getPaymentContext(\n request,\n uuid,\n /** @type {FormsService} */ (services?.formsService)\n )\n\n /**\n * @see https://docs.payments.service.gov.uk/api_reference/#payment-status-lifecycle\n */\n const { status } = paymentStatus.state\n\n switch (status) {\n case 'capturable':\n case 'success':\n logPaymentSuccess(session, paymentStatus)\n return handlePaymentSuccess(\n request,\n h,\n session,\n sessionKey,\n paymentStatus\n )\n\n case 'cancelled':\n case 'failed':\n case 'error':\n logPaymentFailure(session, paymentStatus)\n return handlePaymentFailure(request, h, session, sessionKey)\n\n case 'created':\n case 'started':\n case 'submitted': {\n const nextUrl = paymentStatus._links.next_url?.href\n\n if (!nextUrl) {\n throw Boom.badRequest(\n `Payment in state '${status}' but no next_url available`\n )\n }\n\n return h.redirect(nextUrl).code(StatusCodes.SEE_OTHER)\n }\n\n default: {\n const unknownStatus = /** @type {string} */ (status)\n throw Boom.internal(`Unknown payment status: ${unknownStatus}`)\n }\n }\n },\n options: {\n validate: {\n query: Joi.object()\n .keys({\n uuid: Joi.string().uuid().required()\n })\n .required()\n }\n }\n }\n}\n\n/**\n * @import { Request, ResponseToolkit, ServerRoute } from '@hapi/hapi'\n * @import { GetPaymentResponse, PaymentSessionData } from '~/src/server/plugins/payment/types.js'\n * @import { PaymentState } from '~/src/server/plugins/engine/components/PaymentField.types.js'\n * @import { ExternalStateAppendage, FormState } from '~/src/server/plugins/engine/types.js'\n * @import { PluginOptions } from '~/src/server/plugins/engine/types.js'\n * @import { FormsService } from '~/src/server/types.js'\n */\n"],"mappings":"AAAA,OAAOA,IAAI,MAAM,YAAY;AAC7B,SAASC,WAAW,QAAQ,mBAAmB;AAC/C,OAAOC,GAAG,MAAM,KAAK;AAErB,SAASC,MAAM;AACf,SAASC,wBAAwB;AACjC,SAASC,gBAAgB;AACzB,SACEC,gBAAgB,EAChBC,oBAAoB,EACpBC,iBAAiB;AAGnB,OAAO,MAAMC,mBAAmB,GAAG,mBAAmB;AACtD,OAAO,MAAMC,sBAAsB,GAAG,UAAU;;AAEhD;AACA;AACA;AACA;AACA;AACA;AACA,SAASC,mBAAmBA,CAACC,OAAO,EAAEC,OAAO,EAAEC,aAAa,EAAE;EAC5D;EACA,MAAMC,YAAY,GAAG;IACnBC,SAAS,EAAEF,aAAa,CAACE,SAAS;IAClCC,SAAS,EAAEJ,OAAO,CAACI,SAAS;IAC5BC,MAA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AK,EAAEvD,GAAG,CAACoE,MAAM,CAAC,CAAC,CAChBC,IAAI,CAAC;UACJnD,IAAI,EAAElB,GAAG,CAACsE,MAAM,CAAC,CAAC,CAACpD,IAAI,CAAC,CAAC,CAACqD,QAAQ,CAAC;QACrC,CAAC,CAAC,CACDA,QAAQ,CAAC;MACd;IACF;EACF,CAAC;AACH;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","ignoreList":[]}
@@ -1,8 +1,7 @@
1
1
  import { getErrorMessage } from '@defra/forms-model';
2
2
  import Boom from '@hapi/boom';
3
- import { createLogger } from "../../common/helpers/logging/logger.js";
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","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 { 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 /** @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,SAASA,eAAe,QAAQ,oBAAoB;AACpD,OAAOC,IAAI,MAAM,YAAY;AAE7B,SAASC,YAAY;AACrB,SAASC,OAAO;AAEhB,MAAMC,MAAM,GAAGF,YAAY,CAAC,CAAC;;AAE7B;AACA;AACA;AACA,SAASG,KAAKA,CAAA,EAAG;EACf;EACA,MAAMC,OAAO,GAAG,EAAE;EAElB,OAAO,kCAAoC;IAAEC,MAAM,EAAE,CAAC,CAAC;IAAED;EAAQ,CAAC;AACpE;;AAEA;AACA;AACA;AACA;AACA;AACA,SAASE,sBAAsBA,CAACC,GAAG,EAAEC,QAAQ,EAAE;EAC7C;EACA;EACA,MAAMC,QAAQ,GAAGV,IAAI,CAACW,MAAM,CAACH,GAAG,CAAC,IAAIA,GAAG,CAACI,IAAI;EAC7C,MAAMC,GAAG,GAAG,GAAGd,eAAe,CAACS,GAAG,CAAC,IAAI,CAACE,QAAQ,IAAIA,QAAQ,CAACI,OAAO,EAAEC,KAAK,EAAEC,OAAO,KAAK,EAAE,EAAE;EAE7Fb,MAAM,CAACY,KAAK,CAACP,GAAG,EAAE,sCAAsCC,QAAQ,MAAMI,GAAG,GAAG,CAAC;EAE7E,OAAOT,KAAK,CAAC,CAAC;AAChB;;AAEA;AACA;AACA;AACA;AACA;AACA,eAAea,OAAOA,CAACC,GAAG,EAAET,QAAQ,EAAE;EACpC,MAAMU,aAAa,GAAG;EACpBjB,OACD;EAED,IAAI;IACF,MAAMkB,QAAQ,GAAG,MAAMD,aAAa,CAACD,GAAG,CAAC;IAEzC,IAAIE,QAAQ,CAACL,KAAK,EAAE;MAClB,OAAOR,sBAAsB,CAACa,QAAQ,CAACL,KAAK,EAAEN,QAAQ,CAAC;IACzD;IAEA,MAAMJ,OAAO,GAAGe,QAAQ,CAACN,OAAO;IAEhC,OAAOT,OAAO;EAChB,CAAC,CAAC,OAAOG,GAAG,EAAE;IACZ,OAAOD,sBAAsB,CAACC,GAAG,EAAEC,QAAQ,CAAC;EAC9C;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA,OAAO,eAAeY,IAAIA,CAACC,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":[]}
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,SAASA,eAAe,QAAQ,oBAAoB;AACpD,OAAOC,IAAI,MAAM,YAAY;AAE7B,SAASC,MAAM;AACf,SAASC,OAAO;;AAEhB;AACA;AACA;AACA,SAASC,KAAKA,CAAA,EAAG;EACf;EACA,MAAMC,OAAO,GAAG,EAAE;EAElB,OAAO,kCAAoC;IAAEC,MAAM,EAAE,CAAC,CAAC;IAAED;EAAQ,CAAC;AACpE;;AAEA;AACA;AACA;AACA;AACA;AACA,SAASE,sBAAsBA,CAACC,GAAG,EAAEC,QAAQ,EAAE;EAC7C;EACA;EACA,MAAMC,QAAQ,GAAGT,IAAI,CAACU,MAAM,CAACH,GAAG,CAAC,IAAIA,GAAG,CAACI,IAAI;EAC7C,MAAMC,GAAG,GAAG,GAAGb,eAAe,CAACQ,GAAG,CAAC,IAAI,CAACE,QAAQ,IAAIA,QAAQ,CAACI,OAAO,EAAEC,KAAK,EAAEC,OAAO,KAAK,EAAE,EAAE;EAE7Fd,MAAM,CAACa,KAAK,CAACP,GAAG,EAAE,sCAAsCC,QAAQ,MAAMI,GAAG,GAAG,CAAC;EAE7E,OAAOT,KAAK,CAAC,CAAC;AAChB;;AAEA;AACA;AACA;AACA;AACA;AACA,eAAea,OAAOA,CAACC,GAAG,EAAET,QAAQ,EAAE;EACpC,MAAMU,aAAa,GAAG;EACpBhB,OACD;EAED,IAAI;IACF,MAAMiB,QAAQ,GAAG,MAAMD,aAAa,CAACD,GAAG,CAAC;IAEzC,IAAIE,QAAQ,CAACL,KAAK,EAAE;MAClB,OAAOR,sBAAsB,CAACa,QAAQ,CAACL,KAAK,EAAEN,QAAQ,CAAC;IACzD;IAEA,MAAMJ,OAAO,GAAGe,QAAQ,CAACN,OAAO;IAEhC,OAAOT,OAAO;EAChB,CAAC,CAAC,OAAOG,GAAG,EAAE;IACZ,OAAOD,sBAAsB,CAACC,GAAG,EAAEC,QAAQ,CAAC;EAC9C;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA,OAAO,eAAeY,IAAIA,CAACC,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":[]}
@@ -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 { createLogger } from "../../common/helpers/logging/logger.js";
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","createLogger","checkFormStatus","encodeUrl","logger","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 { createLogger } from '~/src/server/common/helpers/logging/logger.js'\nimport {\n checkFormStatus,\n encodeUrl\n} from '~/src/server/plugins/engine/helpers.js'\n\nconst logger = createLogger()\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,YAAY;AACrB,SACEC,eAAe,EACfC,SAAS;AAGX,MAAMC,MAAM,GAAGH,YAAY,CAAC,CAAC;;AAE7B;AACA,IAAII,eAAe;;AAEnB;AACA;AACA;AACA,OAAO,eAAeC,OAAOA,CAACC,OAAO,EAAE;EACrC,MAAM;IAAE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E,IAAI,CAAC/B,eAAe,EAAE;IACpB,IAAI;MACF;MACAA,eAAe,GAAGgC,IAAI,CAACC,KAAK,CAAC3C,YAAY,CAACwC,YAAY,EAAE,OAAO,CAAC,CAAC;IACnE,CAAC,CAAC,MAAM;MACN/B,MAAM,CAACmC,IAAI,CACT,oCAAoC3C,QAAQ,CAACuC,YAAY,CAAC,6CAC5D,CAAC;IACH;EACF;EAEA,OAAO;IACLnC,MAAM,EAAE;MACNwC,cAAc,EAAExC,MAAM,CAACoC,GAAG,CAAC,gBAAgB,CAAC;MAC5CK,WAAW,EAAEzC,MAAM,CAACoC,GAAG,CAAC,aAAa,CAAC;MACtCM,YAAY,EAAEvC,SAAS,CAACH,MAAM,CAACoC,GAAG,CAAC,cAAc,CAAC,CAAC;MACnDO,QAAQ,EAAE3C,MAAM,CAACoC,GAAG,CAAC,UAAU,CAAC;MAChCQ,WAAW,EAAE5C,MAAM,CAACoC,GAAG,CAAC,aAAa,CAAC;MACtCS,cAAc,EAAE7C,MAAM,CAACoC,GAAG,CAAC,gBAAgB;IAC7C,CAAC;IACDU,SAAS,EAAE,SAAS;IACpBC,eAAe,EAAEA,CAACC,KAAK,GAAG,EAAE,KAAK;MAC/B,OAAO,IAAI3C,eAAe,GAAG2C,KAAK,CAAC,IAAIA,KAAK,EAAE;IAChD;EACF,CAAC;AACH;;AAEA;AACA;AACA;AACA","ignoreList":[]}
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,mDAA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AACmC,GAAG,CAAC,aAAa,CAAC;MACtCS,cAAc,EAAE5C,MAAM,CAACmC,GAAG,CAAC,gBAAgB;IAC7C,CAAC;IACDU,SAAS,EAAE,SAAS;IACpBC,eAAe,EAAEA,CAACC,KAAK,GAAG,EAAE,KAAK;MAC/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 { createLogger } from "../../common/helpers/logging/logger.js";
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,O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AM,IAAIJ,KAAK,CACb,0CAA0CtB,OAAO,CAACJ,SAAS,EAC7D,CAAC;MACH;MAEA,OAAOO,QAAQ,CAACH,OAAO;IACzB,CAAC,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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AK,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,8 +1,7 @@
1
1
  import { getErrorMessage } from '@defra/forms-model';
2
2
  import Boom from '@hapi/boom';
3
- import { createLogger } from "../../common/helpers/logging/logger.js";
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, DeliveryPointA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EACvBlB,GAAG,CAACmB,eAAe,IAAI,EAAE,CAC1B,CACExB,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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package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/forms-engine-plugin",
3
- "version": "4.9.1",
3
+ "version": "4.10.0",
4
4
  "description": "Defra forms engine",
5
5
  "type": "module",
6
6
  "files": [
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 { createLogger } from './server/common/helpers/logging/logger.js'
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
- export function createLogger() {
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 { createLogger } from './logging/logger.js'
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')
@@ -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 { createLogger } from '../../../common/helpers/logging/logger.js'
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
@@ -270,15 +268,10 @@ export class PaymentField extends FormComponent {
270
268
  const payCallbackUrl = `${baseUrl}/payment-callback?uuid=${uuid}`
271
269
  const paymentPageUrl = args.sourceUrl
272
270
 
273
- // Prepopulate GOV.UK Pay email if emailField is configured.
274
- // The referenced EmailAddressField validates with joi.string().email()
275
- // at input time, so the value in state is already validated.
276
271
  let prefilledEmail: string | undefined
277
- if (options.emailField) {
278
- const emailValue = state[options.emailField]
279
- if (typeof emailValue === 'string' && emailValue) {
280
- prefilledEmail = emailValue
281
- }
272
+ const userConfirmationEmail = state.userConfirmationEmailAddress
273
+ if (typeof userConfirmationEmail === 'string' && userConfirmationEmail) {
274
+ prefilledEmail = userConfirmationEmail
282
275
  }
283
276
 
284
277
  const amountInPence = Math.round(resolvedAmount * 100)
@@ -15,7 +15,7 @@ import { StatusCodes } from 'http-status-codes'
15
15
  import { type Schema, type ValidationErrorItem } from 'joi'
16
16
  import { Liquid } from 'liquidjs'
17
17
 
18
- import { createLogger } from '../../common/helpers/logging/logger.js'
18
+ import { logger } from '../../common/helpers/logging/logger.js'
19
19
  import { FORM_VERSION_METADATA_KEY } from '../../constants.js'
20
20
  import {
21
21
  getAnswer,
@@ -37,8 +37,6 @@ import {
37
37
  type FormResponseToolkit
38
38
  } from '../../routes/types.js'
39
39
 
40
- const logger = createLogger()
41
-
42
40
  export const engine = new Liquid({
43
41
  outputEscape: 'escape',
44
42
  jsTruthy: true,
@@ -28,7 +28,7 @@ import { add, format } from 'date-fns'
28
28
  import { Parser, type Value } from 'expr-eval-fork'
29
29
  import joi from 'joi'
30
30
 
31
- import { createLogger } from '../../../common/helpers/logging/logger.js'
31
+ import { logger } from '../../../common/helpers/logging/logger.js'
32
32
  import { type ListFormComponent } from '../components/ListFormComponent.js'
33
33
  import {} from '../components/YesNoField.js'
34
34
  import {
@@ -61,8 +61,6 @@ import { FormAction } from '../../../routes/types.js'
61
61
  import { merge } from '../../../services/cacheService.js'
62
62
  import { type Services } from '../../../types.js'
63
63
 
64
- const logger = createLogger()
65
-
66
64
  export class FormModel {
67
65
  /** The runtime engine that should be used */
68
66
  engine?: Engine
@@ -1,11 +1,9 @@
1
1
  import { getErrorMessage } from '@defra/forms-model'
2
2
  import Joi from 'joi'
3
3
 
4
- import { createLogger } from '../../common/helpers/logging/logger.js'
4
+ import { logger } from '../../common/helpers/logging/logger.js'
5
5
  import { CacheService } from '../../services/index.js'
6
6
 
7
- const logger = createLogger()
8
-
9
7
  const pluginRegistrationOptionsSchema = Joi.object({
10
8
  model: Joi.object().optional(),
11
9
  services: Joi.object().optional(),
@@ -2,7 +2,7 @@ import Boom from '@hapi/boom'
2
2
  import { StatusCodes } from 'http-status-codes'
3
3
  import Joi from 'joi'
4
4
 
5
- import { createLogger } from '../../../common/helpers/logging/logger.js'
5
+ import { logger } from '../../../common/helpers/logging/logger.js'
6
6
  import { EXTERNAL_STATE_APPENDAGE } from '../../../constants.js'
7
7
  import { getPluginOptions } from '../helpers.js'
8
8
  import {
@@ -14,8 +14,6 @@ import {
14
14
  export const PAYMENT_RETURN_PATH = '/payment-callback'
15
15
  export const PAYMENT_SESSION_PREFIX = 'payment-'
16
16
 
17
- const logger = createLogger()
18
-
19
17
  /**
20
18
  * Flash form component state after successful payment
21
19
  * @param {Request} request - the request
@@ -1,11 +1,9 @@
1
1
  import { getErrorMessage } from '@defra/forms-model'
2
2
  import Boom from '@hapi/boom'
3
3
 
4
- import { createLogger } from '../../common/helpers/logging/logger.js'
4
+ import { logger } from '../../common/helpers/logging/logger.js'
5
5
  import { getJson } from '../../services/httpService.js'
6
6
 
7
- const logger = createLogger()
8
-
9
7
  /**
10
8
  * Returns an empty result set
11
9
  */
@@ -5,14 +5,12 @@ import Boom from '@hapi/boom'
5
5
  import { StatusCodes } from 'http-status-codes'
6
6
 
7
7
  import { config } from '../../../config/index.js'
8
- import { createLogger } from '../../common/helpers/logging/logger.js'
8
+ import { logger } from '../../common/helpers/logging/logger.js'
9
9
  import {
10
10
  checkFormStatus,
11
11
  encodeUrl
12
12
  } from '../engine/helpers.js'
13
13
 
14
- const logger = createLogger()
15
-
16
14
  /** @type {Record<string, string> | undefined} */
17
15
  let webpackManifest
18
16
 
@@ -1,6 +1,6 @@
1
1
  import { StatusCodes } from 'http-status-codes'
2
2
 
3
- import { createLogger } from '../../common/helpers/logging/logger.js'
3
+ import { logger } from '../../common/helpers/logging/logger.js'
4
4
  import {
5
5
  buildPaymentInfo,
6
6
  convertPenceToPounds
@@ -10,8 +10,6 @@ import { get, post, postJson } from '../../services/httpService.js'
10
10
  const PAYMENT_BASE_URL = 'https://publicapi.payments.service.gov.uk'
11
11
  const PAYMENT_ENDPOINT = '/v1/payments'
12
12
 
13
- const logger = createLogger()
14
-
15
13
  /**
16
14
  * @param {string} apiKey
17
15
  * @returns {{ Authorization: string }}
@@ -1,11 +1,9 @@
1
1
  import { getErrorMessage } from '@defra/forms-model'
2
2
  import Boom from '@hapi/boom'
3
3
 
4
- import { createLogger } from '../../common/helpers/logging/logger.js'
4
+ import { logger } from '../../common/helpers/logging/logger.js'
5
5
  import { getJson } from '../../services/httpService.js'
6
6
 
7
- const logger = createLogger()
8
-
9
7
  /**
10
8
  * Returns an empty result set
11
9
  */