Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: feature/change-consent-detail-to-read-only #7277

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/client/modules/Contacts/ContactDetails/ConsentDetails.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import React from 'react'
import { isNil } from 'lodash'

import { SectionHeader } from '../../../components'
import { transformContactConsents } from './transformers'

const ConsentText = ({ consent }) => {
const message = (domain, topicNames, consentGiven) => {
let consentedMessage = `This contact has ${consentGiven ? 'given' : 'not given'} consent to ${domain}`
if (!topicNames) {
return ''
} else if (topicNames.length == 1) {
consentedMessage += ` and topic ${topicNames[0]}`

Check warning on line 13 in src/client/modules/Contacts/ContactDetails/ConsentDetails.jsx

View check run for this annotation

Codecov / codecov/patch

src/client/modules/Contacts/ContactDetails/ConsentDetails.jsx#L13

Added line #L13 was not covered by tests
} else if (topicNames.length > 1) {
consentedMessage += ` and topics: ${topicNames.join(', ')}`

Check warning on line 15 in src/client/modules/Contacts/ContactDetails/ConsentDetails.jsx

View check run for this annotation

Codecov / codecov/patch

src/client/modules/Contacts/ContactDetails/ConsentDetails.jsx#L15

Added line #L15 was not covered by tests
}
return consentedMessage + '.'
}

const topicsWithNames = (topics) =>
topics.length > 0
? topics.filter((topic) => topic.name).map((topic) => topic.name)
: undefined

const consentedTopicNames = topicsWithNames(consent.consentedTopics)
const notConsentedTopicNames = topicsWithNames(consent.notConsentedTopics)

const consentedMessage = message(consent.domain, consentedTopicNames, true)
const notConsentedMessage = message(
consent.domain,
notConsentedTopicNames,
false
)

const finalMessage = []
if (consentedMessage) {
finalMessage.push(consentedMessage)

Check warning on line 37 in src/client/modules/Contacts/ContactDetails/ConsentDetails.jsx

View check run for this annotation

Codecov / codecov/patch

src/client/modules/Contacts/ContactDetails/ConsentDetails.jsx#L37

Added line #L37 was not covered by tests
}
if (notConsentedMessage) {
if (consentedMessage) {
finalMessage.push(' ')

Check warning on line 41 in src/client/modules/Contacts/ContactDetails/ConsentDetails.jsx

View check run for this annotation

Codecov / codecov/patch

src/client/modules/Contacts/ContactDetails/ConsentDetails.jsx#L41

Added line #L41 was not covered by tests
}
finalMessage.push(notConsentedMessage)
}

return <p>{finalMessage.join('')}</p>
}

const ConsentDetails = ({ contact }) => {
const consents = transformContactConsents(contact)
return (
<div>
<SectionHeader type="contact-consent">Contact consents</SectionHeader>
{isNil(consents) ? (
<p data-test="no-contact-consents">
There is no consent data available for this contact
</p>
) : (
<>
{consents.map((consent) => {
return <ConsentText consent={consent} />
})}
</>
)}
</div>
)
}

export default ConsentDetails
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { ID, TASK_ARCHIVE_CONTACT } from './state'
import ArchiveForm from '../../../components/ArchiveForm'
import ContactLayout from '../../../components/Layout/ContactLayout'
import ConsentDetails from './ConsentDetails'

const getAddress = (contact, companyAddress) => {
const address = contact.addressSameAsCompany
Expand Down Expand Up @@ -103,6 +104,8 @@ const ContactDetails = ({ contactId, companyAddress, permissions }) => (
</Button>
) : null}

<ConsentDetails contact={contact} />

<ArchiveForm
id={ID}
submissionTaskName={TASK_ARCHIVE_CONTACT}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import 'core-js/proposals/array-grouping-v2'
import { transformContactConsents } from '../transformers'

describe('transformContactConsents', () => {
context('When a falsey contact is passed', () => {
it('Should return undefined', () => {
expect(transformContactConsents(undefined)).to.be.undefined
})
})

context('When a contact has no consent data', () => {
it('Should return undefined', () => {
expect(transformContactConsents({})).to.be.undefined
})
})

context('When a contact with a single unique value for domain', () => {
it('Should return a single domain with a single topic', () => {
expect(
transformContactConsents({
consentData: [
{
topic: 'Topic 1',
sourceSystem: 'System A',
consentDomain: 'International',
emailContactConsent: false,
telephoneContactConsent: false,
},
],
})
).to.deep.equal([
{
domain: 'International',
consentedTopics: [],
notConsentedTopics: [{ consent: false, name: 'Topic 1' }],
},
])
})
})

context('When a contact has multiple values for domain', () => {
it('Should return a single domain with multiple topics', () => {
expect(
transformContactConsents({
consentData: [
{
topic: 'Topic 1',
sourceSystem: 'System A',
consentDomain: 'International',
emailContactConsent: false,
telephoneContactConsent: false,
},
{
topic: 'Topic 2',
sourceSystem: 'System A',
consentDomain: 'International',
emailContactConsent: false,
telephoneContactConsent: true,
},
],
})
).to.deep.equal([
{
domain: 'International',
consentedTopics: [
{
consent: true,
name: 'Topic 2',
},
],
notConsentedTopics: [{ consent: false, name: 'Topic 1' }],
},
])
})
})
})
30 changes: 30 additions & 0 deletions src/client/modules/Contacts/ContactDetails/transformers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export const transformContactConsents = (contact) => {
if (!contact || !contact.consentData) {
return undefined
}

const domainGroupedConsent = Object.groupBy(
contact.consentData,
({ consentDomain }) => consentDomain
)

return Object.entries(domainGroupedConsent).map((domain) => ({
domain: domain[0],
consentedTopics: domain[1]
.filter(
(topic) => topic.emailContactConsent || topic.telephoneContactConsent
)
.map((topic) => ({
consent: true,
name: topic.topic,
})),
notConsentedTopics: domain[1]
.filter(
(topic) => !topic.emailContactConsent && !topic.telephoneContactConsent
)
.map((topic) => ({
consent: false,
name: topic.topic,
})),
}))
}
Loading
Loading