From fc8ec65aa4ccbc4ae1bab1517eb3d677e8935bc0 Mon Sep 17 00:00:00 2001 From: sujithvg Date: Tue, 17 Sep 2024 13:49:59 +0100 Subject: [PATCH] Replace view licence contact details page (#1238) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Replace view licence contact details page https://eaflood.atlassian.net/browse/WATER-4559 We recently rebuilt the legacy view licence page, implementing the new version in our shiny new codebase water-abstraction-system. Substantial changes needed to be made to support the return requirements, so this was a great opportunity to also deal with the tech debt of the legacy view licence page.
 One of the pages that is linked to from the summary tab is the view licence contacts page. If we move that into our code base, we make another small dent in moving away from the legacy code. --- app/controllers/licences.controller.js | 18 +- .../licences/customer-contacts.presenter.js | 2 +- .../licences/licence-contacts.presenter.js | 2 +- .../view-licence-contact-details.presenter.js | 91 ++++++++ app/routes/licence.routes.js | 7 +- .../fetch-customer-contacts.service.js | 2 +- .../fetch-licence-contact-details.service.js | 42 ++++ .../fetch-licence-contacts.service.js | 2 +- .../view-licence-contact-details.service.js | 33 +-- .../licences/view-licence-contacts.service.js | 41 ++++ .../licences/licence-contact-details.njk | 54 +++++ app/views/licences/tabs/summary.njk | 2 +- test/controllers/licences.controller.test.js | 54 ++++- ...-licence-contact-details.presenter.test.js | 198 ++++++++++++++++++ ...ch-licence-contact-details.service.test.js | 81 +++++++ ...ew-licence-contact-details.service.test.js | 138 ++++++------ .../view-licence-contacts.service.test.js | 89 ++++++++ 17 files changed, 755 insertions(+), 101 deletions(-) create mode 100644 app/presenters/licences/view-licence-contact-details.presenter.js create mode 100644 app/services/licences/fetch-licence-contact-details.service.js create mode 100644 app/services/licences/view-licence-contacts.service.js create mode 100644 app/views/licences/licence-contact-details.njk create mode 100644 test/presenters/licences/view-licence-contact-details.presenter.test.js create mode 100644 test/services/licences/fetch-licence-contact-details.service.test.js create mode 100644 test/services/licences/view-licence-contacts.service.test.js diff --git a/app/controllers/licences.controller.js b/app/controllers/licences.controller.js index 94b150d73c..4f4d5f807e 100644 --- a/app/controllers/licences.controller.js +++ b/app/controllers/licences.controller.js @@ -13,6 +13,7 @@ const SubmitMarkForSupplementaryBillingService = require('../services/licences/s const ViewLicenceBillsService = require('../services/licences/view-licence-bills.service.js') const ViewLicenceCommunicationsService = require('../services/licences/view-licence-communications.service.js') const ViewLicenceContactDetailsService = require('../services/licences/view-licence-contact-details.service.js') +const ViewLicenceContactsService = require('../services/licences/view-licence-contacts.service.js') const ViewLicenceHistoryService = require('../services/licences/view-licence-history.service.js') const ViewLicenceReturnsService = require('../services/licences/view-licence-returns.service.js') const ViewLicenceSetUpService = require('../services/licences/view-licence-set-up.service.js') @@ -101,10 +102,20 @@ async function viewCommunications (request, h) { }) } -async function viewContacts (request, h) { +async function viewLicenceContactDetails (request, h) { + const { id } = request.params + + const pageData = await ViewLicenceContactDetailsService.go(id) + + return h.view('licences/licence-contact-details.njk', { + ...pageData + }) +} + +async function viewLicenceContacts (request, h) { const { params: { id }, auth } = request - const pageData = await ViewLicenceContactDetailsService.go(id, auth) + const pageData = await ViewLicenceContactsService.go(id, auth) return h.view(ViewLicencePage, { ...pageData @@ -160,8 +171,9 @@ module.exports = { supplementary, viewBills, viewCommunications, - viewContacts, + viewLicenceContacts, viewHistory, + viewLicenceContactDetails, viewReturns, viewSetUp, viewSummary diff --git a/app/presenters/licences/customer-contacts.presenter.js b/app/presenters/licences/customer-contacts.presenter.js index 322c46d86e..1d245204ab 100644 --- a/app/presenters/licences/customer-contacts.presenter.js +++ b/app/presenters/licences/customer-contacts.presenter.js @@ -2,7 +2,7 @@ /** * Formats data for the `/licences/{id}/contact-details` view customer contact details page - * @module CustomerContactDetailsPresenter + * @module CustomerContactsPresenter */ const ContactModel = require('../../models/contact.model.js') diff --git a/app/presenters/licences/licence-contacts.presenter.js b/app/presenters/licences/licence-contacts.presenter.js index 2c5f327765..6c0312d7fd 100644 --- a/app/presenters/licences/licence-contacts.presenter.js +++ b/app/presenters/licences/licence-contacts.presenter.js @@ -2,7 +2,7 @@ /** * Formats data for the `/licences/{id}/contact-details` view licence contact details page - * @module ViewLicenceContactDetailsPresenter + * @module ViewLicenceContactsPresenter */ /** diff --git a/app/presenters/licences/view-licence-contact-details.presenter.js b/app/presenters/licences/view-licence-contact-details.presenter.js new file mode 100644 index 0000000000..4af5d3c278 --- /dev/null +++ b/app/presenters/licences/view-licence-contact-details.presenter.js @@ -0,0 +1,91 @@ +'use strict' + +/** + * Formats data for the `/licences/{id}/licence-contact` view licence contact details link page + * @module ViewLicenceContactDetailsPresenter + */ + +/** + * Formats data for the `/licences/{id}/licence-contact` view licence contact details link page + * + * @param {module:LicenceModel} licence - The licence and related licenceDocumentHeader + * + * @returns {object} The data formatted for the view template + */ +function go (licence) { + const { id: licenceId, licenceDocumentHeader, licenceRef } = licence + + return { + licenceId, + licenceRef, + licenceContactDetails: _licenceContactDetails(licenceDocumentHeader), + pageTitle: 'Licence contact details' + } +} + +function _licenceContactAddress (contact) { + const contactAddressFields = [ + 'addressLine1', + 'addressLine2', + 'addressLine3', + 'addressLine4', + 'town', + 'county', + 'postcode', + 'country' + ] + + // NOTE: Maps over the `contactAddressFields` array to create an array of values from the `contact` object. Each + // `contactAddressField` corresponds to a property in the `contact` object, mapping and creating a contactAddress + // array. The `filter(Boolean)` function then removes falsy values from the `contactAddress` array. + const contactAddress = contactAddressFields.map((contactAddressField) => { + return contact[contactAddressField] + }).filter(Boolean) + + return contactAddress +} + +function _licenceContactName (contact) { + if (contact.type === 'Person') { + const { salutation, forename, initials, name } = contact + + // NOTE: Prioritise the initials and use the contact forename if initials is null + const initialsOrForename = initials || forename + + const nameComponents = [ + salutation, + initialsOrForename, + name + ] + + const filteredNameComponents = nameComponents.filter((item) => { + return item + }) + + return filteredNameComponents.join(' ') + } + + return contact.name +} + +function _licenceContactDetails (licenceDocumentHeader) { + const licenceContactDetailsData = licenceDocumentHeader.metadata.contacts + + const roles = ['Licence holder', 'Returns to', 'Licence contact'] + + const filteredContactDetails = licenceContactDetailsData.filter((licenceContactDetail) => { + return roles.includes(licenceContactDetail.role) + }) + + return filteredContactDetails.map((contact) => { + return { + address: _licenceContactAddress(contact), + role: contact.role, + name: _licenceContactName(contact) + } + }) +} + +module.exports = { + go +} diff --git a/app/routes/licence.routes.js b/app/routes/licence.routes.js index 983478e9bd..005ae38ef6 100644 --- a/app/routes/licence.routes.js +++ b/app/routes/licence.routes.js @@ -20,10 +20,15 @@ const routes = [ path: '/licences/{id}/communications', handler: LicencesController.viewCommunications }, + { + method: 'GET', + path: '/licences/{id}/licence-contact', + handler: LicencesController.viewLicenceContactDetails + }, { method: 'GET', path: '/licences/{id}/contact-details', - handler: LicencesController.viewContacts + handler: LicencesController.viewLicenceContacts }, { method: 'GET', diff --git a/app/services/licences/fetch-customer-contacts.service.js b/app/services/licences/fetch-customer-contacts.service.js index 236bb7b20e..68af9ca452 100644 --- a/app/services/licences/fetch-customer-contacts.service.js +++ b/app/services/licences/fetch-customer-contacts.service.js @@ -2,7 +2,7 @@ /** * Fetches all customer contacts for a licence which is needed for the view '/licences/{id}/contact-details` page - * @module FetchCustomerContactDetailsService + * @module FetchCustomerContactsService */ const { db } = require('../../../db/db.js') diff --git a/app/services/licences/fetch-licence-contact-details.service.js b/app/services/licences/fetch-licence-contact-details.service.js new file mode 100644 index 0000000000..543ed436ad --- /dev/null +++ b/app/services/licences/fetch-licence-contact-details.service.js @@ -0,0 +1,42 @@ +'use strict' + +/** + * Fetches data needed for the view '/licences/{id}/licence-contact` page + * @module FetchLicenceContactDetailsService + */ + +const LicenceModel = require('../../models/licence.model.js') + +/** + * Fetch the matching licence and return data needed for the licence contact details link page + * + * Was built to provide the data needed for the '/licences/{id}/licence-contact' page + * + * @param {string} licenceId - The UUID for the licence to fetch + * + * @returns {Promise} the matching `licenceModel` populated with the data needed for the view + * licence contact details page + */ +async function go (licenceId) { + return _fetch(licenceId) +} + +async function _fetch (licenceId) { + return LicenceModel.query() + .findById(licenceId) + .select([ + 'id', + 'licenceRef' + ]) + .withGraphFetched('licenceDocumentHeader') + .modifyGraph('licenceDocumentHeader', (builder) => { + builder.select([ + 'id', + 'metadata' + ]) + }) +} + +module.exports = { + go +} diff --git a/app/services/licences/fetch-licence-contacts.service.js b/app/services/licences/fetch-licence-contacts.service.js index 7dc434eb7f..a48fe210f8 100644 --- a/app/services/licences/fetch-licence-contacts.service.js +++ b/app/services/licences/fetch-licence-contacts.service.js @@ -2,7 +2,7 @@ /** * Fetches all return logs for a licence which is needed for the view '/licences/{id}/contact-details` page - * @module FetchLicenceContactDetailsService + * @module FetchLicenceContactService */ const { db } = require('../../../db/db.js') diff --git a/app/services/licences/view-licence-contact-details.service.js b/app/services/licences/view-licence-contact-details.service.js index 957245ab66..d785f413bb 100644 --- a/app/services/licences/view-licence-contact-details.service.js +++ b/app/services/licences/view-licence-contact-details.service.js @@ -1,40 +1,27 @@ 'use strict' /** - * Orchestrates fetching and presenting the data needed for the view licence contact details tab + * Orchestrates fetching and presenting the data needed for the view licence contact details link page * @module ViewLicenceContactDetailsService */ -const CustomerContactDetailsPresenter = require('../../presenters/licences/customer-contacts.presenter.js') -const FetchCustomerContactDetailsService = require('./fetch-customer-contacts.service.js') -const FetchLicenceContactsService = require('./fetch-licence-contacts.service.js') -const LicenceContactsPresenter = require('../../presenters/licences/licence-contacts.presenter.js') -const ViewLicenceService = require('./view-licence.service.js') +const ViewLicenceContactDetailsPresenter = require('../../presenters/licences/view-licence-contact-details.presenter.js') +const FetchLicenceContactDetailsService = require('./fetch-licence-contact-details.service.js') /** - * Orchestrates fetching and presenting the data needed for the licence contact details page + * Orchestrates fetching and presenting the data needed for the licence contact details link page * * @param {string} licenceId - The UUID of the licence - * @param {object} auth - The auth object taken from `request.auth` containing user details * - * @returns {Promise} an object representing the `pageData` needed by the licence contact details template. + * @returns {Promise} The view data for the licence contacts page */ -async function go (licenceId, auth) { - const commonData = await ViewLicenceService.go(licenceId, auth) - - // Licence contact details - const licenceContacts = await FetchLicenceContactsService.go(licenceId) - const licenceContactsData = LicenceContactsPresenter.go(licenceContacts) - - // Customer contacts details - const customerContacts = await FetchCustomerContactDetailsService.go(licenceId) - const customerContactsData = CustomerContactDetailsPresenter.go(customerContacts) +async function go (licenceId) { + const licence = await FetchLicenceContactDetailsService.go(licenceId) + const formattedData = await ViewLicenceContactDetailsPresenter.go(licence) return { - activeTab: 'contact-details', - ...commonData, - ...customerContactsData, - ...licenceContactsData + activeNavBar: 'search', + ...formattedData } } diff --git a/app/services/licences/view-licence-contacts.service.js b/app/services/licences/view-licence-contacts.service.js new file mode 100644 index 0000000000..3e875da087 --- /dev/null +++ b/app/services/licences/view-licence-contacts.service.js @@ -0,0 +1,41 @@ +'use strict' + +/** + * Orchestrates fetching and presenting the data needed for the view licence contact details tab + * @module ViewLicenceContactsService + */ + +const CustomerContactsPresenter = require('../../presenters/licences/customer-contacts.presenter.js') +const FetchCustomerContactsService = require('./fetch-customer-contacts.service.js') +const FetchLicenceContactsService = require('./fetch-licence-contacts.service.js') +const LicenceContactsPresenter = require('../../presenters/licences/licence-contacts.presenter.js') +const ViewLicenceService = require('./view-licence.service.js') + +/** + * Orchestrates fetching and presenting the data needed for the licence contact details page + * + * @param {string} licenceId - The UUID of the licence + * @param {object} auth - The auth object taken from `request.auth` containing user details + * + * @returns {Promise} an object representing the `pageData` needed by the licence contact details template. + */ +async function go (licenceId, auth) { + const commonData = await ViewLicenceService.go(licenceId, auth) + + const licenceContacts = await FetchLicenceContactsService.go(licenceId) + const licenceContactsData = LicenceContactsPresenter.go(licenceContacts) + + const customerContacts = await FetchCustomerContactsService.go(licenceId) + const customerContactsData = CustomerContactsPresenter.go(customerContacts) + + return { + activeTab: 'contact-details', + ...commonData, + ...customerContactsData, + ...licenceContactsData + } +} + +module.exports = { + go +} diff --git a/app/views/licences/licence-contact-details.njk b/app/views/licences/licence-contact-details.njk new file mode 100644 index 0000000000..1cc7ae98ba --- /dev/null +++ b/app/views/licences/licence-contact-details.njk @@ -0,0 +1,54 @@ +{% extends 'layout.njk' %} +{% from "govuk/components/back-link/macro.njk" import govukBackLink %} +{% from "govuk/components/summary-list/macro.njk" import govukSummaryList %} + +{% block breadcrumbs %} + {{ govukBackLink({ + text: 'Back to summary', + href: '/system/licences/' + licenceId + '/summary' + }) }} +{% endblock %} + +{% block content %} +

+ Licence {{ licenceRef }} + {{ pageTitle }} +

+ + {% macro displayAddress(address) %} + {% for item in address %} +

{{ item }}

+ {% endfor %} + {% endmacro %} + + {% for licenceContactDetail in licenceContactDetails %} + {{ govukSummaryList({ + card: { + title: { + text: licenceContactDetail.role + } + }, + rows: [ + { + key: { + text: "Name", + classes:"govuk-!-font-weight-regular" + }, + value: { + text: licenceContactDetail.name + } + }, + { + key: { + text: "Address", + classes:"govuk-!-font-weight-regular" + }, + value: { + html: displayAddress(licenceContactDetail.address) + } + } + ] + }) + }} + {% endfor %} +{% endblock %} diff --git a/app/views/licences/tabs/summary.njk b/app/views/licences/tabs/summary.njk index abff2c45a6..00b5dfd8b3 100644 --- a/app/views/licences/tabs/summary.njk +++ b/app/views/licences/tabs/summary.njk @@ -8,7 +8,7 @@
Licence Holder
{{ licenceHolder }} - View licence contact details + View licence contact details
diff --git a/test/controllers/licences.controller.test.js b/test/controllers/licences.controller.test.js index c87c275f8e..92acf3c1e1 100644 --- a/test/controllers/licences.controller.test.js +++ b/test/controllers/licences.controller.test.js @@ -20,6 +20,7 @@ const MarkForSupplementaryBillingService = require('../../app/services/licences/ const SubmitMarkForSupplementaryBillingService = require('../../app/services/licences/supplementary/submit-mark-for-supplementary-billing.service.js') const ViewLicenceBillsService = require('../../app/services/licences/view-licence-bills.service.js') const ViewLicenceCommunicationsService = require('../../app/services/licences/view-licence-communications.service.js') +const ViewLicenceContactsService = require('../../app/services/licences/view-licence-contacts.service.js') const ViewLicenceContactDetailsService = require('../../app/services/licences/view-licence-contact-details.service.js') const ViewLicenceHistoryService = require('../../app/services/licences/view-licence-history.service.js') const ViewLicenceReturnsService = require('../../app/services/licences/view-licence-returns.service.js') @@ -120,7 +121,7 @@ describe('Licences controller', () => { describe('when a request is valid and has contacts', () => { beforeEach(async () => { - Sinon.stub(ViewLicenceContactDetailsService, 'go').resolves(_viewLicenceContactDetails()) + Sinon.stub(ViewLicenceContactsService, 'go').resolves(_viewLicenceContacts()) }) it('returns the page successfully', async () => { @@ -161,6 +162,34 @@ describe('Licences controller', () => { }) }) + describe('/licences/{id}/licence-contact', () => { + describe('GET', () => { + beforeEach(async () => { + options = { + method: 'GET', + url: '/licences/7861814c-ca19-43f2-be11-3c612f0d744b/licence-contact', + auth: { + strategy: 'session', + credentials: { scope: [] } + } + } + }) + + describe('when a request is valid and has contacts', () => { + beforeEach(async () => { + Sinon.stub(ViewLicenceContactDetailsService, 'go').resolves(_viewLicenceContactDetails()) + }) + + it('returns the page successfully', async () => { + const response = await server.inject(options) + + expect(response.statusCode).to.equal(200) + expect(response.payload).to.contain('Licence contact details') + }) + }) + }) + }) + describe('/licences/{id}/no-returns-required', () => { describe('GET', () => { const session = { id: '1c265420-6a5e-4a4c-94e4-196d7799ed01' } @@ -520,7 +549,7 @@ function _viewLicenceCommunications () { } } -function _viewLicenceContactDetails () { +function _viewLicenceContacts () { const commonLicenceData = _viewLicence() return { @@ -540,6 +569,27 @@ function _viewLicenceHistory () { } } +function _viewLicenceContactDetails () { + const commonLicenceData = _viewLicence() + + commonLicenceData.pageTitle = null + + return { + ...commonLicenceData, + activeTab: 'search', + licenceContacts: [ + { + address: { + contactAddress: ['Address Line 1', 'Address Line 2', 'Address Line 3'] + }, + name: 'jimbo', + role: 'Licence holder' + } + ], + pageTitle: 'Licence contact details' + } +} + function _viewLicenceReturns () { const commonLicenceData = _viewLicence() diff --git a/test/presenters/licences/view-licence-contact-details.presenter.test.js b/test/presenters/licences/view-licence-contact-details.presenter.test.js new file mode 100644 index 0000000000..8e987bd4a0 --- /dev/null +++ b/test/presenters/licences/view-licence-contact-details.presenter.test.js @@ -0,0 +1,198 @@ +'use strict' + +// Test framework dependencies +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') + +const { describe, it, beforeEach } = exports.lab = Lab.script() +const { expect } = Code + +// Thing under test +const ViewLicenceContactDetailsPresenter = require('../../../app/presenters/licences/view-licence-contact-details.presenter.js') + +describe('View Licence Contact Details presenter', () => { + let licenceContactDetailsData + + beforeEach(() => { + licenceContactDetailsData = _testFetchLicenceContactDetailsData() + }) + + describe('when provided with populated licence contact details data', () => { + it('correctly presents the data', () => { + const result = ViewLicenceContactDetailsPresenter.go(licenceContactDetailsData) + + expect(result).to.equal({ + licenceId: '0a4ebb93-2e90-4e35-acd5-a5aa73466508', + licenceRef: '00/111/222', + pageTitle: 'Licence contact details', + licenceContactDetails: [ + { + address: [ + 'ENVIRONMENT AGENCY', + 'HORIZON HOUSE', + 'DEANERY ROAD', + 'BRISTOL', + 'BS1 5AH', + 'United Kingdom' + ], + role: 'Licence holder', + name: 'Acme ltd' + }, + { + address: [ + 'Furland', + 'Crewkerne', + 'Somerset', + 'TA18 7TT', + 'United Kingdom' + ], + role: 'Licence contact', + name: 'Furland Farm' + }, + { + address: [ + 'Crinkley Bottom', + 'Cricket St Thomas', + 'Somerset', + 'TA20 1KL', + 'United Kingdom' + ], + role: 'Returns to', + name: 'Mr N Edmonds' + }] + }) + }) + + describe('the "licenceContactDetails" property', () => { + describe('the "licenceContactDetails.address" property', () => { + it('returns the address of the property', () => { + const result = ViewLicenceContactDetailsPresenter.go(licenceContactDetailsData) + + expect(result.licenceContactDetails[0].address).to.equal([ + 'ENVIRONMENT AGENCY', + 'HORIZON HOUSE', + 'DEANERY ROAD', + 'BRISTOL', + 'BS1 5AH', + 'United Kingdom' + ]) + }) + }) + + describe('the "licenceContactDetails.role" property', () => { + describe('when one of the licence contact details has the role type of "Enforcement officer"', () => { + it('returns licenceContactDetails without the contact with the role type of "Enforcement officer"', () => { + const result = ViewLicenceContactDetailsPresenter.go(licenceContactDetailsData) + + const hasEnforcementOfficer = result.licenceContactDetails.some((contact) => { + return contact.role === 'Enforcement officer' + }) + + expect(hasEnforcementOfficer).to.be.false() + }) + }) + }) + + describe('the "licenceContacts.name" property', () => { + describe('when the initials are null', () => { + beforeEach(() => { + licenceContactDetailsData.licenceDocumentHeader.metadata.contacts[3].initials = null + }) + + it("returns the licence contact's forename and name", () => { + const result = ViewLicenceContactDetailsPresenter.go(licenceContactDetailsData) + + expect(result.licenceContactDetails[2].name).to.equal('Mr Noel Edmonds') + }) + }) + + describe('when the initials are not null', () => { + it("returns the licence contact's forename and name", () => { + const result = ViewLicenceContactDetailsPresenter.go(licenceContactDetailsData) + + expect(result.licenceContactDetails[2].name).to.equal('Mr N Edmonds') + }) + }) + }) + }) + }) +}) + +function _testFetchLicenceContactDetailsData () { + return { + id: '0a4ebb93-2e90-4e35-acd5-a5aa73466508', + licenceRef: '00/111/222', + licenceDocumentHeader: { + id: '0a4ebb93-12345-4e35-acd5-987654321abc', + metadata: { + contacts: [ + { + name: 'Acme ltd', + role: 'Licence holder', + town: 'BRISTOL', + type: 'Organisation', + county: null, + country: 'United Kingdom', + forename: null, + initials: null, + postcode: 'BS1 5AH', + salutation: null, + addressLine1: 'ENVIRONMENT AGENCY', + addressLine2: 'HORIZON HOUSE', + addressLine3: 'DEANERY ROAD', + addressLine4: null + }, + { + name: 'Acme ltd', + role: 'Enforcement officer', + town: 'BRISTOL', + type: 'Organisation', + county: null, + country: 'United Kingdom', + forename: null, + initials: null, + postcode: 'BS1 5AH', + salutation: null, + addressLine1: 'ENVIRONMENT AGENCY', + addressLine2: 'HORIZON HOUSE', + addressLine3: 'DEANERY ROAD', + addressLine4: null + }, + { + name: 'Furland Farm', + role: 'Licence contact', + town: 'Somerset', + type: 'Organisation', + county: null, + country: 'United Kingdom', + forename: null, + initials: null, + postcode: 'TA18 7TT', + salutation: null, + addressLine1: 'Furland', + addressLine2: 'Crewkerne', + addressLine3: null, + addressLine4: null + }, + { + name: 'Edmonds', + role: 'Returns to', + town: 'Somerset', + type: 'Person', + county: null, + country: 'United Kingdom', + forename: 'Noel', + initials: 'N', + postcode: 'TA20 1KL', + salutation: 'Mr', + addressLine1: 'Crinkley Bottom', + addressLine2: 'Cricket St Thomas', + addressLine3: null, + addressLine4: null + } + ], + IsCurrent: false + } + } + } +} diff --git a/test/services/licences/fetch-licence-contact-details.service.test.js b/test/services/licences/fetch-licence-contact-details.service.test.js new file mode 100644 index 0000000000..fa26f19150 --- /dev/null +++ b/test/services/licences/fetch-licence-contact-details.service.test.js @@ -0,0 +1,81 @@ +'use strict' + +// Test framework dependencies +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') + +const { describe, it, before } = exports.lab = Lab.script() +const { expect } = Code + +// Test helpers +const LicenceHelper = require('../../support/helpers/licence.helper.js') +const LicenceDocumentHeaderHelper = require('../../support/helpers/licence-document-header.helper.js') + +// Thing under test +const FetchLicenceContactDetailsService = require('../../../app/services/licences/fetch-licence-contact-details.service.js') + +describe('Fetch Licence Contact Details service', () => { + let licence + let licenceId + let licenceRef + let licenceDocumentHeader + let licenceDocumentHeaderId + + describe('when the licence has a licence document header', () => { + before(async () => { + licence = await LicenceHelper.add() + licenceId = licence.id + licenceRef = licence.licenceRef + + licenceDocumentHeader = await LicenceDocumentHeaderHelper.add({ licenceRef }) + licenceDocumentHeaderId = licenceDocumentHeader.id + }) + + it('returns the matching licence and licence document header', async () => { + const result = await FetchLicenceContactDetailsService.go(licenceId) + + expect(result).to.equal({ + id: licenceId, + licenceRef, + licenceDocumentHeader: { + id: licenceDocumentHeaderId, + metadata: { + Name: 'GUPTA', + Town: 'BRISTOL', + County: 'AVON', + Country: '', + Expires: null, + Forename: 'AMARA', + Initials: 'A', + Modified: '20080327', + Postcode: 'BS1 5AH', + contacts: [ + { + name: 'GUPTA', + role: 'Licence holder', + town: 'BRISTOL', + type: 'Person', + county: 'AVON', + country: null, + forename: 'AMARA', + initials: 'A', + postcode: 'BS1 5AH', + salutation: null, + addressLine1: 'ENVIRONMENT AGENCY', + addressLine2: 'HORIZON HOUSE', + addressLine3: 'DEANERY ROAD', + addressLine4: null + } + ], + IsCurrent: true, + Salutation: '', + AddressLine1: 'ENVIRONMENT AGENCY', + AddressLine2: 'HORIZON HOUSE', + AddressLine3: 'DEANERY ROAD', + AddressLine4: '' + } + } + }) + }) + }) +}) diff --git a/test/services/licences/view-licence-contact-details.service.test.js b/test/services/licences/view-licence-contact-details.service.test.js index 3de9638aec..92e7eb7f6b 100644 --- a/test/services/licences/view-licence-contact-details.service.test.js +++ b/test/services/licences/view-licence-contact-details.service.test.js @@ -5,87 +5,91 @@ const Lab = require('@hapi/lab') const Code = require('@hapi/code') const Sinon = require('sinon') -const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script() +const { describe, it, before } = exports.lab = Lab.script() const { expect } = Code // Things we need to stub -const FetchLicenceContactsService = - require('../../../app/services/licences/fetch-licence-contacts.service.js') -const FetchCustomerContactDetailsService = - require('../../../app/services/licences/fetch-customer-contacts.service.js') -const ViewLicenceService = require('../../../app/services/licences/view-licence.service.js') +const FetchLicenceContactDetailsService = require('../../../app/services/licences/fetch-licence-contact-details.service.js') // Thing under test const ViewLicenceContactDetailsService = require('../../../app/services/licences/view-licence-contact-details.service.js') describe('View Licence Contact Details service', () => { - const auth = {} - const testId = '2c80bd22-a005-4cf4-a2a2-73812a9861de' + const licenceId = 'fea88a95-d81f-4c5c-b497-00e5891a5861' - beforeEach(() => { - Sinon.stub(FetchLicenceContactsService, 'go').returns([{ - communicationType: 'Licence Holder', - companyId: 'ebe95a21-c6f6-4f15-8856-a48ffc737731', - companyName: 'Acme ltd', - contactId: null, - firstName: null, - lastName: null, - address1: '34 Eastgate', - address2: null, - address3: null, - address4: null, - address5: null, - address6: null, - postcode: 'CF71 7DG', - country: 'United Kingdom' - }]) - - Sinon.stub(FetchCustomerContactDetailsService, 'go').returns([{ - communicationType: 'Additional Contact', - email: 'dfd@email.com', - firstName: 'Donald', - initials: null, - lastName: 'Duck', - middleInitials: null, - salutation: null, - suffix: null - }]) - - Sinon.stub(ViewLicenceService, 'go').resolves({ licenceName: 'fake licence' }) + before(() => { + Sinon.stub(FetchLicenceContactDetailsService, 'go').returns(_testFetchLicenceContactDetailsData()) }) - afterEach(() => { - Sinon.restore() - }) - - describe('when called', () => { - it('returns page data for the view', async () => { - const result = await ViewLicenceContactDetailsService.go(testId, auth) + describe('when a licence with a matching ID exists', () => { + it('correctly presents the data', async () => { + const result = await ViewLicenceContactDetailsService.go(licenceId) expect(result).to.equal({ - activeTab: 'contact-details', - customerContacts: [{ - communicationType: 'Additional Contact', - email: 'dfd@email.com', - name: 'Donald Duck' - }], - licenceName: 'fake licence', - customerId: 'ebe95a21-c6f6-4f15-8856-a48ffc737731', - licenceContacts: [{ - address: { - address1: '34 Eastgate', - address2: null, - address3: null, - address4: null, - address5: null, - address6: null, - country: 'United Kingdom', - postcode: 'CF71 7DG' - }, - communicationType: 'Licence Holder', - name: 'Acme ltd' - }] + activeNavBar: 'search', + licenceContactDetails: [ + { + address: [ + 'ENVIRONMENT AGENCY', + 'HORIZON HOUSE', + 'DEANERY ROAD', + 'BRISTOL', + 'AVON', + 'BS1 5AH' + ], + name: 'A GUPTA', + role: 'Licence holder' + } + ], + licenceId: 'fea88a95-d81f-4c5c-b497-00e5891a5861', + licenceRef: '01/123', + pageTitle: 'Licence contact details' }) }) }) }) + +function _testFetchLicenceContactDetailsData () { + return { + id: 'fea88a95-d81f-4c5c-b497-00e5891a5861', + licenceRef: '01/123', + licenceDocumentHeader: { + id: 'e27682b4-8c28-4db0-bb0a-685380537bc5', + metadata: { + Name: 'GUPTA', + Town: 'BRISTOL', + County: 'AVON', + Country: '', + Expires: null, + Forename: 'AMARA', + Initials: 'A', + Modified: '20080327', + Postcode: 'BS1 5AH', + contacts: [ + { + name: 'GUPTA', + role: 'Licence holder', + town: 'BRISTOL', + type: 'Person', + county: 'AVON', + country: null, + forename: 'AMARA', + initials: 'A', + postcode: 'BS1 5AH', + salutation: null, + addressLine1: 'ENVIRONMENT AGENCY', + addressLine2: 'HORIZON HOUSE', + addressLine3: 'DEANERY ROAD', + addressLine4: null + } + ], + IsCurrent: true, + Salutation: '', + AddressLine1: 'ENVIRONMENT AGENCY', + AddressLine2: 'HORIZON HOUSE', + AddressLine3: 'DEANERY ROAD', + AddressLine4: '' + } + } + } +} diff --git a/test/services/licences/view-licence-contacts.service.test.js b/test/services/licences/view-licence-contacts.service.test.js new file mode 100644 index 0000000000..3982c6fd2d --- /dev/null +++ b/test/services/licences/view-licence-contacts.service.test.js @@ -0,0 +1,89 @@ +'use strict' + +// Test framework dependencies +const Lab = require('@hapi/lab') +const Code = require('@hapi/code') +const Sinon = require('sinon') + +const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script() +const { expect } = Code + +// Things we need to stub +const FetchLicenceContactsService = require('../../../app/services/licences/fetch-licence-contacts.service.js') +const FetchCustomerContactsService = require('../../../app/services/licences/fetch-customer-contacts.service.js') +const ViewLicenceService = require('../../../app/services/licences/view-licence.service.js') + +// Thing under test +const ViewLicenceContactsService = require('../../../app/services/licences/view-licence-contacts.service.js') + +describe('View Licence Contacts service', () => { + const auth = {} + const testId = '2c80bd22-a005-4cf4-a2a2-73812a9861de' + + beforeEach(() => { + Sinon.stub(FetchLicenceContactsService, 'go').returns([{ + communicationType: 'Licence Holder', + companyId: 'ebe95a21-c6f6-4f15-8856-a48ffc737731', + companyName: 'Acme ltd', + contactId: null, + firstName: null, + lastName: null, + address1: '34 Eastgate', + address2: null, + address3: null, + address4: null, + address5: null, + address6: null, + postcode: 'CF71 7DG', + country: 'United Kingdom' + }]) + + Sinon.stub(FetchCustomerContactsService, 'go').returns([{ + communicationType: 'Additional Contact', + email: 'dfd@email.com', + firstName: 'Donald', + initials: null, + lastName: 'Duck', + middleInitials: null, + salutation: null, + suffix: null + }]) + + Sinon.stub(ViewLicenceService, 'go').resolves({ licenceName: 'fake licence' }) + }) + + afterEach(() => { + Sinon.restore() + }) + + describe('when called', () => { + it('returns page data for the view', async () => { + const result = await ViewLicenceContactsService.go(testId, auth) + + expect(result).to.equal({ + activeTab: 'contact-details', + customerContacts: [{ + communicationType: 'Additional Contact', + email: 'dfd@email.com', + name: 'Donald Duck' + }], + licenceName: 'fake licence', + customerId: 'ebe95a21-c6f6-4f15-8856-a48ffc737731', + licenceContacts: [{ + address: { + address1: '34 Eastgate', + address2: null, + address3: null, + address4: null, + address5: null, + address6: null, + country: 'United Kingdom', + postcode: 'CF71 7DG' + }, + communicationType: 'Licence Holder', + name: 'Acme ltd' + }] + }) + }) + }) +})