Skip to content

Commit

Permalink
Merge pull request #7460 from uktrade/CPS-653-fix-investment-project-…
Browse files Browse the repository at this point in the history
…form-save

Fix investment form save
  • Loading branch information
bau123 authored Jan 13, 2025
2 parents 615252b + 1644867 commit f4239c5
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 13 deletions.
11 changes: 7 additions & 4 deletions src/apps/investments/client/projects/create/transformers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OPTION_NO } from '../../../../../common/constants'
import { OPTION_NO, OPTION_YES } from '../../../../../common/constants'
import {
FDI_TYPES,
INVESTOR_TYPES,
Expand All @@ -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,
Expand All @@ -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
Expand Down
15 changes: 6 additions & 9 deletions src/client/modules/Investments/Projects/Details/transformers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -135,7 +128,7 @@ export const transformProjectRequirementsForApi = ({ projectId, values }) => {

export const transformProjectSummaryForApi = ({
projectId,
currentAdviser,
currentAdviserId,
values,
}) => {
const {
Expand All @@ -151,6 +144,7 @@ export const transformProjectSummaryForApi = ({
investor_type,
level_of_involvement,
specific_programmes,
is_referral_source,
business_activities,
other_business_activity,
client_contacts,
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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',
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit f4239c5

Please sign in to comment.