diff --git a/package.json b/package.json index cc3a83e97..1ecdaa8c2 100644 --- a/package.json +++ b/package.json @@ -110,6 +110,7 @@ "p-queue": "^6.6.2", "pino": "^5.14.0", "regex-to-strings": "^2.1.0", + "short-uid": "^0.1.0", "short-uuid": "^4.1.0", "ts-node": "^10.9.1", "tsconfig-paths": "^3.8.0", diff --git a/src/api/tracking-id/handler.ts b/src/api/tracking-id/handler.ts new file mode 100644 index 000000000..7d36cc757 --- /dev/null +++ b/src/api/tracking-id/handler.ts @@ -0,0 +1,10 @@ +import * as Hapi from '@hapi/hapi' +import { generateTrackingId } from './service' + +export function trackingIDHandler( + request: Hapi.Request, + h: Hapi.ResponseToolkit +) { + const trackingId = generateTrackingId(request.params.eventType) + return h.response(trackingId) +} diff --git a/src/api/tracking-id/service.ts b/src/api/tracking-id/service.ts new file mode 100644 index 000000000..764b79c18 --- /dev/null +++ b/src/api/tracking-id/service.ts @@ -0,0 +1,8 @@ +import * as ShortUIDGen from 'short-uid' + +export function generateTrackingId(eventType: string): string { + // using first letter of eventType for prefix + // TODO: for divorce, need to think about prefix as Death & Divorce prefix is same 'D' + const prefix = eventType.charAt(0) + return prefix.concat(new ShortUIDGen().randomUUID()).toUpperCase() +} diff --git a/src/index.ts b/src/index.ts index a37f40183..b6a0ba08b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -51,6 +51,7 @@ import { validatorsHandler } from './form/common/custom-validation-conditionals/ import { conditionalsHandler } from './form/common/custom-validation-conditionals/conditionals-handler' import { COUNTRY_WIDE_CRUDE_DEATH_RATE } from './api/application/application-config-default' import { handlebarsHandler } from './form/common/custom-validation-conditionals/handlebars-handler' +import { trackingIDHandler } from './api/tracking-id/handler' export interface ITokenPayload { sub: string @@ -469,6 +470,16 @@ export async function createServer() { } }) + server.route({ + method: 'GET', + path: '/tracking-id/{eventType}', + handler: trackingIDHandler, + options: { + tags: ['api'], + description: 'Provides a tracking id' + } + }) + server.ext({ type: 'onRequest', method(request: Hapi.Request & { sentryScope?: any }, h) { diff --git a/typings/short-uid.d.ts b/typings/short-uid.d.ts new file mode 100644 index 000000000..af761fc73 --- /dev/null +++ b/typings/short-uid.d.ts @@ -0,0 +1 @@ +declare module 'short-uid' diff --git a/yarn.lock b/yarn.lock index d4faf6ba1..0df0a1330 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9173,6 +9173,11 @@ shellwords@^0.1.1: resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b" integrity sha512-vFwSUfQvqybiICwZY5+DAWIPLKsWO31Q91JSKl3UYv+K5c2QRPzn0qzec6QPu1Qc9eHYItiP3NdJqNVqetYAww== +short-uid@^0.1.0: + version "0.1.0" + resolved "https://registry.yarnpkg.com/short-uid/-/short-uid-0.1.0.tgz#069d94d044fc498afb0ea7bb22c5e634979434c8" + integrity sha512-FcrUzrOPh7gZcJ9piR15qs3bTSOyZWzpPFeKYK37A4Mr20wKnwskWFdYRj+mk8AWSO1wsflM+HgyeNTbgBecPg== + short-uuid@^4.1.0: version "4.2.2" resolved "https://registry.yarnpkg.com/short-uuid/-/short-uuid-4.2.2.tgz#4bb3d926da04a4a5f34420d17b5551fd6d9d535c"