From 98db0bede66f86d927d103cf2b83492dc61a44d3 Mon Sep 17 00:00:00 2001
From: Rob Booth <robin.booth@digital.justice.gov.uk>
Date: Mon, 9 Dec 2024 09:47:54 +0000
Subject: [PATCH] Add applicationId to Space Search Parameters Updated
 Cas1SpaceSearchParameters to include optional applicationId Mapped
 applicationId from UI to API parameters

---
 integration_tests/tests/match/match.cy.ts                     | 2 ++
 server/@types/shared/models/Cas1SpaceSearchParameters.ts      | 4 ++++
 server/@types/ui/index.d.ts                                   | 1 +
 server/controllers/match/search/spaceSearchController.test.ts | 2 ++
 server/testutils/factories/spaceSearchParameters.ts           | 1 +
 server/utils/match/index.test.ts                              | 1 +
 server/utils/match/index.ts                                   | 1 +
 server/utils/placementRequests/utils.test.ts                  | 1 +
 server/utils/placementRequests/utils.ts                       | 2 ++
 9 files changed, 15 insertions(+)

diff --git a/integration_tests/tests/match/match.cy.ts b/integration_tests/tests/match/match.cy.ts
index e2aa943ad7..b179551da3 100644
--- a/integration_tests/tests/match/match.cy.ts
+++ b/integration_tests/tests/match/match.cy.ts
@@ -78,6 +78,7 @@ context('Placement Requests', () => {
 
       // And the first request to the API should contain the criteria from the placement request
       expect(initialSearchRequestBody).to.deep.equal({
+        applicationId: placementRequest.applicationId,
         durationInDays: placementRequest.duration,
         startDate: placementRequest.expectedArrival,
         targetPostcodeDistrict: placementRequest.location,
@@ -91,6 +92,7 @@ context('Placement Requests', () => {
       // And the second request to the API should contain the new criteria I submitted
 
       expect(secondSearchRequestBody).to.contain({
+        applicationId: placementRequest.applicationId,
         durationInDays: placementRequest.duration,
         startDate: newSearchParameters.startDate,
         targetPostcodeDistrict: newSearchParameters.targetPostcodeDistrict,
diff --git a/server/@types/shared/models/Cas1SpaceSearchParameters.ts b/server/@types/shared/models/Cas1SpaceSearchParameters.ts
index a0d0fd2079..04564e80e0 100644
--- a/server/@types/shared/models/Cas1SpaceSearchParameters.ts
+++ b/server/@types/shared/models/Cas1SpaceSearchParameters.ts
@@ -4,6 +4,10 @@
 /* eslint-disable */
 import type { Cas1SpaceSearchRequirements } from './Cas1SpaceSearchRequirements';
 export type Cas1SpaceSearchParameters = {
+    /**
+     * The id of the application the space search is for
+     */
+    applicationId?: string;
     /**
      * The date the space is required from
      */
diff --git a/server/@types/ui/index.d.ts b/server/@types/ui/index.d.ts
index 7f98c98ba3..087d7f0829 100644
--- a/server/@types/ui/index.d.ts
+++ b/server/@types/ui/index.d.ts
@@ -389,6 +389,7 @@ type ContingencyPlanQuestion = {
 export type ContingencyPlanQuestionsRecord = Record<ContingencyPlanQuestionId, ContingencyPlanQuestion>
 
 export interface SpaceSearchParametersUi {
+  applicationId: string
   startDate: string
   targetPostcodeDistrict: string
   durationInDays: string
diff --git a/server/controllers/match/search/spaceSearchController.test.ts b/server/controllers/match/search/spaceSearchController.test.ts
index a3edc611e1..74cb71e367 100644
--- a/server/controllers/match/search/spaceSearchController.test.ts
+++ b/server/controllers/match/search/spaceSearchController.test.ts
@@ -55,6 +55,7 @@ describe('spaceSearchController', () => {
           spaceSearchResults,
           placementRequest: placementRequestDetail,
           tier: placementRequestDetail.risks.tier.value.level,
+          applicationId: placementRequestDetail.applicationId,
           startDate: placementRequestDetail.expectedArrival,
           formPath,
           ...query,
@@ -80,6 +81,7 @@ describe('spaceSearchController', () => {
           spaceSearchResults,
           placementRequest: placementRequestDetail,
           tier: placementRequestDetail.risks.tier.value.level,
+          applicationId: placementRequestDetail.applicationId,
           startDate: placementRequestDetail.expectedArrival,
           formPath,
           ...query,
diff --git a/server/testutils/factories/spaceSearchParameters.ts b/server/testutils/factories/spaceSearchParameters.ts
index 7bc4060c20..3ea028722b 100644
--- a/server/testutils/factories/spaceSearchParameters.ts
+++ b/server/testutils/factories/spaceSearchParameters.ts
@@ -28,6 +28,7 @@ export default Factory.define<Cas1SpaceSearchParameters>(() => {
 export const spaceSearchParametersUiFactory = Factory.define<SpaceSearchParametersUi>(() => {
   const startDateInputsValues = DateFormats.dateObjectToDateInputs(faker.date.soon(), 'startDate')
   return {
+    applicationId: faker.string.uuid(),
     startDate: startDateInputsValues.startDate,
     targetPostcodeDistrict: faker.helpers.arrayElement(postcodeAreas),
     durationInDays: faker.number.int({ max: 100, min: 1 }).toString(),
diff --git a/server/utils/match/index.test.ts b/server/utils/match/index.test.ts
index 898c034109..1da268bc6f 100644
--- a/server/utils/match/index.test.ts
+++ b/server/utils/match/index.test.ts
@@ -85,6 +85,7 @@ describe('matchUtils', () => {
           genders: [uiParams.requirements.gender],
           spaceCharacteristics: uiParams.requirements.spaceCharacteristics,
         },
+        applicationId: uiParams.applicationId,
         startDate: uiParams.startDate,
         targetPostcodeDistrict: uiParams.targetPostcodeDistrict,
       })
diff --git a/server/utils/match/index.ts b/server/utils/match/index.ts
index 71014c5637..3c651d3013 100644
--- a/server/utils/match/index.ts
+++ b/server/utils/match/index.ts
@@ -46,6 +46,7 @@ export type SearchFilterCategories = 'apType' | 'offenceAndRisk' | 'placementReq
 
 export const mapUiParamsForApi = (query: SpaceSearchParametersUi): SpaceSearchParameters => {
   return {
+    applicationId: query.applicationId,
     startDate: query.startDate,
     targetPostcodeDistrict: query.targetPostcodeDistrict,
     requirements: {
diff --git a/server/utils/placementRequests/utils.test.ts b/server/utils/placementRequests/utils.test.ts
index 2b1425d23c..8b0b55aa79 100644
--- a/server/utils/placementRequests/utils.test.ts
+++ b/server/utils/placementRequests/utils.test.ts
@@ -24,6 +24,7 @@ describe('utils', () => {
       })
 
       expect(mapPlacementRequestToSpaceSearchParams(placementRequest)).toEqual({
+        applicationId: placementRequest.applicationId,
         durationInDays: placementRequest.duration.toString(),
         startDate: placementRequest.expectedArrival,
         targetPostcodeDistrict: placementRequest.location,
diff --git a/server/utils/placementRequests/utils.ts b/server/utils/placementRequests/utils.ts
index df27c4a217..9a6f916288 100644
--- a/server/utils/placementRequests/utils.ts
+++ b/server/utils/placementRequests/utils.ts
@@ -11,6 +11,7 @@ import { DateFormats } from '../dateUtils'
 import { TabItem } from '../tasks/listTable'
 
 export const mapPlacementRequestToSpaceSearchParams = ({
+  applicationId,
   duration,
   expectedArrival,
   location,
@@ -20,6 +21,7 @@ export const mapPlacementRequestToSpaceSearchParams = ({
   gender,
 }: PlacementRequest): SpaceSearchParametersUi => {
   return {
+    applicationId,
     startDate: expectedArrival,
     targetPostcodeDistrict: location,
     durationInDays: duration.toString(),