From 560501c838fd556470a2aa11fa27d13ceea9c806 Mon Sep 17 00:00:00 2001 From: Bau Nguyen Date: Fri, 10 Jan 2025 11:24:44 +0000 Subject: [PATCH 1/2] Fix investment form save --- .../client/projects/create/transformers.js | 11 +++++++---- .../Investments/Projects/Details/transformers.js | 15 ++++++--------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/apps/investments/client/projects/create/transformers.js b/src/apps/investments/client/projects/create/transformers.js index 22ae6bec309..b510cd01146 100644 --- a/src/apps/investments/client/projects/create/transformers.js +++ b/src/apps/investments/client/projects/create/transformers.js @@ -1,4 +1,4 @@ -import { OPTION_NO } from '../../../../../common/constants' +import { OPTION_NO, OPTION_YES } from '../../../../../common/constants' import { FDI_TYPES, INVESTOR_TYPES, @@ -23,12 +23,14 @@ export const transformFormValuesToPayload = (values, csrfToken) => { client_contacts, other_business_activity, client_relationship_manager, + is_referral_source, referral_source_adviser, referral_source_activity, referral_source_activity_event, referral_source_activity_marketing, referral_source_activity_website, estimated_land_date, + likelihood_to_land, actual_land_date, investor_type, level_of_involvement, @@ -54,14 +56,15 @@ export const transformFormValuesToPayload = (values, csrfToken) => { ? client_relationship_manager.value : adviser.id, referral_source_adviser: - values.referralSourceAdviser === OPTION_NO - ? referral_source_adviser.value - : adviser.id, + is_referral_source === OPTION_YES + ? adviser.id + : referral_source_adviser.value, referral_source_activity: referral_source_activity, referral_source_activity_event: referral_source_activity_event, referral_source_activity_marketing: referral_source_activity_marketing, referral_source_activity_website: referral_source_activity_website, estimated_land_date: formatEstimatedLandDate(estimated_land_date), + likelihood_to_land: likelihood_to_land?.value, actual_land_date: formatActualLandDate(actual_land_date), investor_type: fdi_type?.value === FDI_TYPES.expansionOfExistingSiteOrActivity.value diff --git a/src/client/modules/Investments/Projects/Details/transformers.js b/src/client/modules/Investments/Projects/Details/transformers.js index 9ac693a1ad0..780aa2c9fe6 100644 --- a/src/client/modules/Investments/Projects/Details/transformers.js +++ b/src/client/modules/Investments/Projects/Details/transformers.js @@ -58,13 +58,6 @@ const setReferralSourceEvent = (values) => { : '' } -const setReferralSourceAdviser = (currentAdviser, values) => { - const { is_referral_source, referral_source_adviser } = values - return is_referral_source === 'yes' - ? currentAdviser - : checkIfItemHasValue(referral_source_adviser?.value) -} - const transformRadioOptionToInvertedBool = (radioOption) => radioOption === null ? null : radioOption === OPTION_NO @@ -135,7 +128,7 @@ export const transformProjectRequirementsForApi = ({ projectId, values }) => { export const transformProjectSummaryForApi = ({ projectId, - currentAdviser, + currentAdviserId, values, }) => { const { @@ -151,6 +144,7 @@ export const transformProjectSummaryForApi = ({ investor_type, level_of_involvement, specific_programmes, + is_referral_source, business_activities, other_business_activity, client_contacts, @@ -186,7 +180,10 @@ export const transformProjectSummaryForApi = ({ referral_source_activity_website ), referral_source_activity_event: setReferralSourceEvent(values), - referral_source_adviser: setReferralSourceAdviser(currentAdviser, values), + referral_source_adviser: + is_referral_source === OPTION_YES + ? currentAdviserId + : values.referral_source_adviser.value, } if (Array.isArray(specific_programmes)) { From 16448673c76ccac23ad2ab7d1262df7bd835e014 Mon Sep 17 00:00:00 2001 From: Bau Nguyen Date: Fri, 10 Jan 2025 16:24:16 +0000 Subject: [PATCH 2/2] test for referral adviser in payload --- .../project-add-investment-spec.js | 57 +++++++++++++++++++ .../investments/project-edit-details-spec.js | 35 ++++++++++++ 2 files changed, 92 insertions(+) diff --git a/test/functional/cypress/specs/investments/project-add-investment-spec.js b/test/functional/cypress/specs/investments/project-add-investment-spec.js index beb7aebc7a2..3e2f48bfef0 100644 --- a/test/functional/cypress/specs/investments/project-add-investment-spec.js +++ b/test/functional/cypress/specs/investments/project-add-investment-spec.js @@ -1,7 +1,13 @@ +import { + clickButton, + selectFirstMockedTypeaheadOption, +} from '../../support/actions' + const { expect } = require('chai') const urls = require('../../../../../src/lib/urls') const { company } = require('../../fixtures') + const { assertSummaryTable, assertFieldTypeahead, @@ -496,3 +502,54 @@ describe('Validation error messages', () => { cy.contains('Actual land date cannot be in the future') }) }) + +describe('When creating investment project check payload is transformed for API', () => { + beforeEach(() => { + cy.visit(urls.investments.projects.index()) + cy.get('[data-test="add-collection-item-button"]').click() + cy.intercept('POST', '/api-proxy/v3/investment').as('createInvestment') + + cy.get('input[data-test="company-name"]').clear() + cy.get('input[data-test="company-name"]').type('alphabet') + cy.get('form button').click() + cy.get('form ol li:nth-child(1)').click() + cy.get('[data-test="investment-type-non-fdi"]').click() + cy.get('[data-test="continue"]').click() + + cy.get('input[data-test="name-input"]').type('project name') + cy.get('textarea[name="description"]').type('project description') + cy.get('#sector').parent().selectTypeaheadOption('Advanced Engineering') + cy.get('#business_activities').parent().selectTypeaheadOption('Assembly') + cy.get('#client_contacts').parent().selectTypeaheadOption('Joseph Woof') + cy.get('input[data-test="client-relationship-manager-yes"]').click() + cy.get('#referral_source_activity').select('LEP') + cy.get('input[data-test="estimated_land_date-month"').type(10) + cy.get('input[data-test="estimated_land_date-year"').type(2024) + + cy.intercept('POST', '/api-proxy/v3/investment').as('createInvestment') + }) + + it('should pass referral source as the user if they are the referral source', () => { + cy.get('[data-test="field-is_referral_source"]').contains('Yes').click() + clickButton('Submit') + + cy.wait('@createInvestment').its('request.body').should('include', { + referral_source_adviser: '7d19d407-9aec-4d06-b190-d3f404627f21', + }) + }) + + it('should pass referral source as given adviser if they are the referral source', () => { + cy.get('[data-test="field-is_referral_source"]').contains('No').click() + + selectFirstMockedTypeaheadOption({ + element: '[data-test="field-referral_source_adviser"]', + input: 'Puck Head', + mockAdviserResponse: false, + }) + + clickButton('Submit') + cy.wait('@createInvestment').its('request.body').should('include', { + referral_source_adviser: 'e83a608e-84a4-11e6-ae22-56b6b6499611', + }) + }) +}) diff --git a/test/functional/cypress/specs/investments/project-edit-details-spec.js b/test/functional/cypress/specs/investments/project-edit-details-spec.js index 18871541cf9..8ed817c74a9 100644 --- a/test/functional/cypress/specs/investments/project-edit-details-spec.js +++ b/test/functional/cypress/specs/investments/project-edit-details-spec.js @@ -495,6 +495,41 @@ describe('Editing the project summary', () => { } ) + context('When submitting data check payload is transformmed for API', () => { + const project = setupProjectFaker({}) + beforeEach(() => { + setup(project) + cy.intercept('PATCH', `/api-proxy/v3/investment/*`).as( + 'editDetailsRequest' + ) + cy.get('[data-test="submit"]').should('exist') + }) + it('should pass referral source as the user if they are the referral source', () => { + cy.get('[data-test="field-is_referral_source"]').contains('Yes').click() + clickButton('Submit') + cy.wait('@editDetailsRequest').its('request.body').should('include', { + referral_source_adviser: '7d19d407-9aec-4d06-b190-d3f404627f21', + }) + }) + + it('should pass referral source as given adviser if they are the referral source', () => { + cy.get('[data-test="field-is_referral_source"]').contains('No') + cy.get('[data-test="field-referral_source_adviser"]').then((element) => { + assertFieldTypeahead({ + element, + label: 'Referral source adviser', + placeholder: 'Choose a referral source adviser', + value: 'Puck Head', + isMulti: false, + }) + }) + clickButton('Submit') + cy.wait('@editDetailsRequest').its('request.body').should('include', { + referral_source_adviser: 'e83a608e-84a4-11e6-ae22-56b6b6499611', + }) + }) + }) + context('When changing the project FDI type from other to Expansion', () => { beforeEach(() => { const project = setupProjectFaker({