Skip to content

Commit

Permalink
Merge pull request #160 from ministryofjustice/patch/person-formatting
Browse files Browse the repository at this point in the history
[P4-502] Fix replacing of identifiers
  • Loading branch information
teneightfive committed Jul 24, 2019
2 parents f9951f2 + 2ff2056 commit 47fff37
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 10 deletions.
28 changes: 18 additions & 10 deletions common/services/person.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
const { mapValues } = require('lodash')
const { mapValues, uniqBy } = require('lodash')

const apiClient = require('../lib/api-client')

function getFullname ({
first_names: firstNames,
last_name: lastName,
}) {
function getFullname ({ first_names: firstNames, last_name: lastName }) {
return `${lastName}, ${firstNames}`
}

function format (data) {
const relationships = ['gender', 'ethnicity']
const identifiers = [
const existingIdentifiers = data.identifiers || []
const relationshipKeys = ['gender', 'ethnicity']
const identifierKeys = [
'police_national_computer',
'criminal_records_office',
'prison_number',
Expand All @@ -21,21 +19,29 @@ function format (data) {

const formatted = mapValues(data, (value, key) => {
if (typeof value === 'string') {
if (relationships.includes(key)) {
if (relationshipKeys.includes(key)) {
return { id: value }
}

if (identifiers.includes(key)) {
if (identifierKeys.includes(key)) {
return { value, identifier_type: key }
}
}

return value
})

const identifiers = uniqBy(
[
...identifierKeys.map(key => formatted[key]).filter(Boolean),
...existingIdentifiers,
],
'identifier_type'
)

return {
...formatted,
identifiers: identifiers.map(key => formatted[key]).filter(Boolean),
identifiers,
}
}

Expand All @@ -50,6 +56,8 @@ function update (data) {
return
}

console.log(format(data))

return apiClient
.update('person', format(data))
.then(response => response.data)
Expand Down
79 changes: 79 additions & 0 deletions common/services/person.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,85 @@ describe('Person Service', function () {
expect(formatted.first_names).to.equal('Foo')
})
})

context('when existing identifiers are present', function () {
let formatted

context('when new identifiers are present', function () {
beforeEach(async function () {
formatted = await personService.format({
first_names: 'Foo',
police_national_computer: '67890',
identifiers: [
{
value: '12345',
identifier_type: 'police_national_computer',
},
{
value: 'Athena number',
identifier_type: 'athena_reference',
},
],
})
})

it('should remove duplicates', function () {
expect(formatted.identifiers.length).to.equal(2)
})

it('should return identifiers property', function () {
expect(formatted.identifiers).to.deep.equal([
{
value: '67890',
identifier_type: 'police_national_computer',
},
{
value: 'Athena number',
identifier_type: 'athena_reference',
},
])
})

it('should not affect non relationship fields', function () {
expect(formatted.first_names).to.equal('Foo')
})
})

context('when no new identifiers are present', function () {
beforeEach(async function () {
formatted = await personService.format({
first_names: 'Foo',
identifiers: [
{
value: 'PNC number',
identifier_type: 'police_national_computer',
},
{
value: 'Athena number',
identifier_type: 'athena_reference',
},
],
})
})

it('should return identifiers property', function () {
expect(formatted.identifiers).to.deep.equal([
{
value: 'PNC number',
identifier_type: 'police_national_computer',
},
{
value: 'Athena number',
identifier_type: 'athena_reference',
},
])
})

it('should not affect non relationship fields', function () {
expect(formatted.first_names).to.equal('Foo')
})
})
})
})

describe('#create()', function () {
Expand Down

0 comments on commit 47fff37

Please sign in to comment.