Skip to content

Commit

Permalink
Merge pull request #7357 from uktrade/fix/export-win-no-contact
Browse files Browse the repository at this point in the history
Fix a bug when export wins dashboard crashed when there were no company_contacts
  • Loading branch information
peterhudec authored Dec 3, 2024
2 parents 5bce3fc + 6153c57 commit 16db387
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const getAddress = (contact, companyAddress) => {
town: companyAddress.town,
region: companyAddress.county || null,
postcode: companyAddress.postcode,
country: companyAddress.country.name,
country: companyAddress.country?.name,
}
: {
line1: contact.address1,
Expand Down
14 changes: 14 additions & 0 deletions src/client/modules/ExportWins/Status/Contact.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react'
import Link from '@govuk-react/link'

import urls from '../../../../lib/urls'

const ContactLink = ({ name, id }) => (
<Link href={urls.contacts.details(id)}>{name}</Link>
)

const Contact = ({
win: { company_contacts: [contact] = [], customer_name },
}) => (contact ? <ContactLink {...contact} /> : customer_name || 'Not set')

export default Contact
8 changes: 2 additions & 6 deletions src/client/modules/ExportWins/Status/WinsConfirmedList.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react'
import Link from '@govuk-react/link'

import ExportWinsResource from '../../../components/Resource/ExportWins'
import { currencyGBP } from '../../../utils/number-utils'
Expand All @@ -9,6 +8,7 @@ import { sumExportValues, createRoleTags } from './utils'
import { SORT_OPTIONS, WIN_STATUS } from './constants'
import State from '../../../components/State'
import urls from '../../../../lib/urls'
import Contact from './Contact'

export const WinsConfirmedList = ({ exportWins = [], currentAdviserId }) => {
return exportWins.length === 0 ? null : (
Expand All @@ -27,11 +27,7 @@ export const WinsConfirmedList = ({ exportWins = [], currentAdviserId }) => {
metadata={[
{
label: 'Contact name:',
value: (
<Link href={urls.contacts.details(item.company_contacts[0].id)}>
{item.company_contacts[0].name}
</Link>
),
value: <Contact win={item} />,
},
{
label: 'Total value:',
Expand Down
8 changes: 2 additions & 6 deletions src/client/modules/ExportWins/Status/WinsRejectedList.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react'
import Link from '@govuk-react/link'

import ExportWinsResource from '../../../components/Resource/ExportWins'
import { currencyGBP } from '../../../utils/number-utils'
Expand All @@ -9,6 +8,7 @@ import { sumExportValues, createRoleTags } from './utils'
import { SORT_OPTIONS, WIN_STATUS } from './constants'
import State from '../../../components/State'
import urls from '../../../../lib/urls'
import Contact from './Contact'

export const WinsRejectedList = ({ exportWins, currentAdviserId }) => {
return exportWins.length === 0 ? null : (
Expand All @@ -27,11 +27,7 @@ export const WinsRejectedList = ({ exportWins, currentAdviserId }) => {
metadata={[
{
label: 'Contact name:',
value: (
<Link href={urls.contacts.details(item.company_contacts[0].id)}>
{item.company_contacts[0].name}
</Link>
),
value: <Contact win={item} />,
},
{
label: 'Total value:',
Expand Down
31 changes: 31 additions & 0 deletions test/component/cypress/specs/ExportWins/Contact.cy.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react'

import Contact from '../../../../../src/client/modules/ExportWins/Status/Contact'

describe('Contact', () => {
;['John Doe', 'Andy Pipkin', 'Lou Todd'].forEach((name) => {
it('should render a link to ${name} if company_contacts is not empty', () => {
const id = `id-of-${name}`

cy.mount(<Contact win={{ company_contacts: [{ name, id }] }} />)

cy.get('a')
.should('have.text', name)
.and('have.attr', 'href', `/contacts/${id}/details`)
})

it('should render customer_name if company_contacts empty', () => {
cy.mount(<Contact win={{ customer_name: name }} />)

cy.get('a').should('not.exist')
cy.contains(RegExp(`^${name}$`))
})
})

it("should render 'Not set' by default", () => {
cy.mount(<Contact win={{}} />)

cy.get('a').should('not.exist')
cy.contains(RegExp(`^Not set$`))
})
})

0 comments on commit 16db387

Please sign in to comment.