diff --git a/app/controllers/licences.controller.js b/app/controllers/licences.controller.js index 44f41e6d54..21cae31473 100644 --- a/app/controllers/licences.controller.js +++ b/app/controllers/licences.controller.js @@ -6,10 +6,10 @@ */ const InitiateSessionService = require('../services/return-requirements/initiate-session.service.js') -const ViewLicenceBillsService = require('../services/licences/view-licence-bills.service') -const ViewLicenceContactDetailsService = require('../services/licences/view-licence-contact-details.service') -const ViewLicenceReturnsService = require('../services/licences/view-licence-returns.service') -const ViewLicenceSummaryService = require('../services/licences/view-licence-summary.service') +const ViewLicenceBillsService = require('../services/licences/view-licence-bills.service.js') +const ViewLicenceContactDetailsService = require('../services/licences/view-licence-contact-details.service.js') +const ViewLicenceReturnsService = require('../services/licences/view-licence-returns.service.js') +const ViewLicenceSummaryService = require('../services/licences/view-licence-summary.service.js') const ViewLicencePage = 'licences/view.njk' @@ -40,9 +40,9 @@ async function viewBills (request, h) { } async function viewContacts (request, h) { - const { params: { id }, auth, query: { page = 1 } } = request + const { params: { id }, auth } = request - const data = await ViewLicenceContactDetailsService.go(id, auth, page) + const data = await ViewLicenceContactDetailsService.go(id, auth) return h.view(ViewLicencePage, { ...data diff --git a/app/presenters/licences/licence-contacts.presenter.js b/app/presenters/licences/licence-contacts.presenter.js new file mode 100644 index 0000000000..c3ee6dc0f9 --- /dev/null +++ b/app/presenters/licences/licence-contacts.presenter.js @@ -0,0 +1,61 @@ +'use strict' + +/** + * Formats data for the `/licences/{id}/contact-details` view licence contact details page + * @module ViewLicenceContactDetailsPresenter + */ + +/** + * Formats data for the `/licences/{id}/contact-details` view licence contact details page + * + * @returns {Object} The data formatted for the view template + */ +function go (contacts) { + return { + customerId: _findCustomerId(contacts), + licenceContacts: _licenceContacts(contacts) + } +} + +function _findCustomerId (contacts) { + const customerContact = contacts.find((contact) => { + return contact.communicationType === 'Licence Holder' + }) + + if (customerContact) { + return customerContact.companyId + } + + return null +} + +function _licenceContactName (contact) { + if (contact.contactId) { + return `${contact.firstName || ''} ${contact.lastName}`.trim() + } + + return contact.companyName +} + +function _licenceContacts (contacts) { + return contacts.map((contact) => { + return { + address: { + address1: contact.address1, + address2: contact.address2, + address3: contact.address3, + address4: contact.address4, + address5: contact.address5, + address6: contact.address6, + country: contact.country, + postcode: contact.postcode + }, + communicationType: contact.communicationType, + name: _licenceContactName(contact) + } + }) +} + +module.exports = { + go +} diff --git a/app/presenters/licences/view-licence-contact-details.presenter.js b/app/presenters/licences/view-licence-contact-details.presenter.js deleted file mode 100644 index 90708bac14..0000000000 --- a/app/presenters/licences/view-licence-contact-details.presenter.js +++ /dev/null @@ -1,55 +0,0 @@ -'use strict' - -/** - * Formats data for the `/licences/{id}/contact-details` view licence contact details page - * @module ViewLicenceContactDetailsPresenter - */ - -/** - * Formats data for the `/licences/{id}/contact-details` view licence contact details page - * - * @returns {Object} The data formatted for the view template - */ -function go (contactDetails) { - return { - activeTab: 'contact-details', - customerId: _findCustomerId(contactDetails.licenceContacts), - licenceContacts: _mapLicenceContacts(contactDetails.licenceContacts) - } -} - -function _findCustomerId (licenceContacts) { - const customer = licenceContacts.find(con => con.licenceRole.name === 'licenceHolder') - - if (customer?.company) { - return customer.company.id - } - - return null -} - -function _formatLicenceContactName (company, contact) { - if (contact?.firstName && contact.lastName) { - return `${contact.firstName} ${contact.lastName}` - } - - if (company) { - return company.name - } - - return null -} - -function _mapLicenceContacts (licenceContacts) { - return licenceContacts.map(contact => { - return { - address: contact.address, - communicationType: contact.licenceRole.label, - name: _formatLicenceContactName(contact.company, contact.contact) - } - }) -} - -module.exports = { - go -} diff --git a/app/services/licences/fetch-licence-contact-details.service.js b/app/services/licences/fetch-licence-contact-details.service.js deleted file mode 100644 index c11c642298..0000000000 --- a/app/services/licences/fetch-licence-contact-details.service.js +++ /dev/null @@ -1,62 +0,0 @@ -'use strict' - -/** - * Fetches all return logs for a licence which is needed for the view '/licences/{id}/contact-details` page - * @module FetchLicenceContactDetailsService - */ - -const LicenceDocumentModel = require('../../models/licence-document.model') - -/** - * Fetches all contact details for a licence which is needed for the view '/licences/{id}/contact-details` page - * - * @param {string} licenceId - The UUID for the licence to fetch - * - * @returns {Promise} the data needed to populate the view licence page's contact details tab - */ -async function go (licenceId) { - const { licenceContacts } = await _fetch(licenceId) - - return { licenceContacts: licenceContacts.licenceDocumentRoles } -} - -async function _fetch (licenceId) { - const licenceContacts = await LicenceDocumentModel.query() - .first() - .select('') - .innerJoinRelated('licence') - .where('licence.id', licenceId) - .withGraphFetched('licenceDocumentRoles') - .modifyGraph('licenceDocumentRoles', (builder) => { - builder.where(function () { - this.where('end_date', '>', new Date()).orWhere({ end_date: null }) - }).select('') - }) - .withGraphFetched('licenceDocumentRoles.licenceRole') - .modifyGraph('licenceDocumentRoles.licenceRole', (builder) => { - builder.select(['name', 'label']) - }) - .withGraphFetched('licenceDocumentRoles.address') - .modifyGraph('licenceDocumentRoles.address', (builder) => { - builder.select([ - 'address1', 'address2', 'address3', - 'address4', 'address5', 'address6', - 'country', 'postcode']) - }) - .withGraphFetched('licenceDocumentRoles.contact') - .modifyGraph('contact', (builder) => { - builder.select(['first_name', 'last_name']) - }) - .withGraphFetched('licenceDocumentRoles.company') - .modifyGraph('company', (builder) => { - builder.select(['id', 'name']) - }) - - return { - licenceContacts - } -} - -module.exports = { - go -} diff --git a/app/services/licences/fetch-licence-contacts.service.js b/app/services/licences/fetch-licence-contacts.service.js new file mode 100644 index 0000000000..30c6f454e1 --- /dev/null +++ b/app/services/licences/fetch-licence-contacts.service.js @@ -0,0 +1,55 @@ +'use strict' + +/** + * Fetches all return logs for a licence which is needed for the view '/licences/{id}/contact-details` page + * @module FetchLicenceContactDetailsService + */ + +const { db } = require('../../../db/db.js') + +/** + * Fetches all contact details for a licence which is needed for the view '/licences/{id}/contact-details` page + * + * @param {string} licenceId - The UUID for the licence to fetch + * + * @returns {Promise} the data needed to populate the view licence page's contact details tab + */ +async function go (licenceId) { + return _fetch(licenceId) +} + +async function _fetch (licenceId) { + return db.withSchema('public') + .select([ + 'lr.label AS communicationType', + 'cmp.id AS companyId', + 'cmp.name AS companyName', + 'con.id AS contactId', + 'con.firstName', + 'con.lastName', + 'a.address1', + 'a.address2', + 'a.address3', + 'a.address4', + 'a.address5', + 'a.address6', + 'a.postcode', + 'a.country' + ]) + .from('licenceDocuments AS ld') + .innerJoin('licences AS l', 'l.licenceRef', '=', 'ld.licenceRef') + .innerJoin('licenceDocumentRoles AS ldr', 'ldr.licenceDocumentId', '=', 'ld.id') + .innerJoin('licenceRoles AS lr', 'lr.id', '=', 'ldr.licenceRoleId') + .innerJoin('companies AS cmp', 'cmp.id', '=', 'ldr.companyId') + .leftJoin('contacts AS con', 'con.id', '=', 'ldr.contactId') + .innerJoin('addresses AS a', 'a.id', '=', 'ldr.addressId') + .where('l.id', '=', licenceId) + .andWhere((builder) => { + builder.whereNull('ldr.end_date').orWhere('ldr.end_date', '>', db.raw('NOW()')) + }) + .orderBy('lr.label', 'desc') +} + +module.exports = { + go +} diff --git a/app/services/licences/view-licence-contact-details.service.js b/app/services/licences/view-licence-contact-details.service.js index ef6c59c557..bb5f4ce89c 100644 --- a/app/services/licences/view-licence-contact-details.service.js +++ b/app/services/licences/view-licence-contact-details.service.js @@ -1,30 +1,33 @@ 'use strict' /** - * Orchestrates fetching and presenting the data needed for the licence contact page + * Orchestrates fetching and presenting the data needed for the view licence contact details tab * @module ViewLicenceContactDetailsService */ -const FetchLicenceContactDetailsService = require('./fetch-licence-contact-details.service') -const ViewLicenceContactDetailsPresenter = require('../../presenters/licences/view-licence-contact-details.presenter') -const ViewLicenceService = require('./view-licence.service') +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. + * @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 contactsData = await FetchLicenceContactDetailsService.go(licenceId) - const data = ViewLicenceContactDetailsPresenter.go(contactsData) + // Licence contact details + const licenceContacts = await FetchLicenceContactsService.go(licenceId) + const licenceContactsData = LicenceContactsPresenter.go(licenceContacts) return { + activeTab: 'contact-details', ...commonData, - ...data + ...licenceContactsData } } diff --git a/app/views/licences/tabs/contact-details.njk b/app/views/licences/tabs/contact-details.njk index 2e6ca0f8ab..af9215f9e5 100644 --- a/app/views/licences/tabs/contact-details.njk +++ b/app/views/licences/tabs/contact-details.njk @@ -1,7 +1,7 @@

Contact Details

{% if customerId %} -

Go to customer contacts

+

Go to customer contacts

{% endif %} {% if licenceContacts.length > 0 %} @@ -15,34 +15,34 @@ - {% for contact in licenceContacts %} + {% for licenceContact in licenceContacts %} - {{ contact.name }} - {{ contact.communicationType }} + {{ licenceContact.name }} + {{ licenceContact.communicationType }} - {% if contact.address.address1 %} -

{{ contact.address.address1 }}

+ {% if licenceContact.address.address1 %} +

{{ licenceContact.address.address1 }}

{% endif %} - {% if contact.address.address2 %} -

{{ contact.address.address2 }}

+ {% if licenceContact.address.address2 %} +

{{ licenceContact.address.address2 }}

{% endif %} - {% if contact.address.address3 %} -

{{ contact.address.address3 }}

+ {% if licenceContact.address.address3 %} +

{{ licenceContact.address.address3 }}

{% endif %} - {% if contact.address.address4 %} -

{{ contact.address.address4 }}

+ {% if licenceContact.address.address4 %} +

{{ licenceContact.address.address4 }}

{% endif %} - {% if contact.address.address5 %} -

{{ contact.address.address5 }}

+ {% if licenceContact.address.address5 %} +

{{ licenceContact.address.address5 }}

{% endif %} - {% if contact.address.address6 %} -

{{ contact.address.address6 }}

+ {% if licenceContact.address.address6 %} +

{{ licenceContact.address.address6 }}

{% endif %} - {% if contact.address.country %} -

{{ contact.address.country }}

+ {% if licenceContact.address.country %} +

{{ licenceContact.address.country }}

{% endif %} - {% if contact.address.postcode %} -

{{ contact.address.postcode }}

+ {% if licenceContact.address.postcode %} +

{{ licenceContact.address.postcode }}

{% endif %} {% endfor %} diff --git a/test/presenters/licences/licence-contacts.presenter.test.js b/test/presenters/licences/licence-contacts.presenter.test.js new file mode 100644 index 0000000000..e8704df2d9 --- /dev/null +++ b/test/presenters/licences/licence-contacts.presenter.test.js @@ -0,0 +1,119 @@ +'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 LicenceContactsPresenter = require('../../../app/presenters/licences/licence-contacts.presenter.js') + +describe('Licence Contacts presenter', () => { + let licenceContacts + + beforeEach(() => { + licenceContacts = [ + { + 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' + } + ] + }) + + describe('when provided with populated licence contacts data', () => { + it('correctly presents the data', () => { + const result = LicenceContactsPresenter.go(licenceContacts) + + expect(result).to.equal({ + 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' + }] + }) + }) + + describe("the 'customerId' property", () => { + describe("when one of the licence contacts has the communication type of 'Licence Holder'", () => { + it("returns that licence contact's company Id", () => { + const result = LicenceContactsPresenter.go(licenceContacts) + + expect(result.customerId).to.equal('ebe95a21-c6f6-4f15-8856-a48ffc737731') + }) + }) + + describe("when none of the licence contacts has the communication type of 'Licence Holder'", () => { + beforeEach(() => { + licenceContacts[0].communicationType = 'Returns' + }) + + it('returns null', () => { + const result = LicenceContactsPresenter.go(licenceContacts) + + expect(result.customerId).to.be.null() + }) + }) + }) + + describe("the 'licenceContacts.name' property", () => { + describe('when the licence contact does not have a contact', () => { + it("returns the licence contact's company name", () => { + const result = LicenceContactsPresenter.go(licenceContacts) + + expect(result.licenceContacts[0].name).to.equal('Acme ltd') + }) + }) + + describe('when the licence contact has a contact with a last name', () => { + beforeEach(() => { + licenceContacts[0].contactId = '0a4ebb93-2e90-4e35-acd5-a5aa73466508' + licenceContacts[0].lastName = 'Flow' + }) + + describe('but no first name', () => { + it("returns just the licence contact's last name", () => { + const result = LicenceContactsPresenter.go(licenceContacts) + + expect(result.licenceContacts[0].name).to.equal('Flow') + }) + }) + + describe('and a first name', () => { + beforeEach(() => { + licenceContacts[0].firstName = 'Jackie' + }) + + it("returns the licence contact's first and last name", () => { + const result = LicenceContactsPresenter.go(licenceContacts) + + expect(result.licenceContacts[0].name).to.equal('Jackie Flow') + }) + }) + }) + }) + }) +}) diff --git a/test/presenters/licences/view-licence-contact-details-presenter.test.js b/test/presenters/licences/view-licence-contact-details-presenter.test.js deleted file mode 100644 index 1c789aeb73..0000000000 --- a/test/presenters/licences/view-licence-contact-details-presenter.test.js +++ /dev/null @@ -1,139 +0,0 @@ -'use strict' - -// Test framework dependencies -const Lab = require('@hapi/lab') -const Code = require('@hapi/code') - -const { describe, it } = exports.lab = Lab.script() -const { expect } = Code - -// Thing under test -const ViewLicenceContactDetailsPresenter = - require('../../../app/presenters/licences/view-licence-contact-details.presenter') - -describe('View Licence contact details presenter', () => { - describe('when provided with a licence contacts data', () => { - it('correctly presents the data', () => { - const result = ViewLicenceContactDetailsPresenter.go(_LicenceContacts()) - - expect(result).to.equal({ - activeTab: 'contact-details', - customerId: '562e7ec4-eb9f-4d4b-9f4b-8005020fb50a', - licenceContacts: [ - { - address: { - address1: 'Air House', - address2: 'Open Road', - address3: 'Farmers yard', - address4: null, - address5: null, - address6: null, - country: null, - postcode: 'BS1 5TL' - }, - communicationType: 'Returns', - name: 'Jackie Flow' - }, - { - address: { - address1: 'Water House', - address2: 'Liquid Road', - address3: null, - address4: null, - address5: null, - address6: null, - country: null, - postcode: 'BS1 5TL' - }, - communicationType: 'Licence Holder', - name: 'Water Services Ltd' - } - ] - } - ) - }) - - it('uses the contact first and last name if available', () => { - const result = ViewLicenceContactDetailsPresenter.go(_LicenceContacts()) - - const [licenceWithFirstAndLastName] = result.licenceContacts - - expect(licenceWithFirstAndLastName.name).to.equal('Jackie Flow') - }) - - it('uses the contact company name if no contact names are available', () => { - const result = ViewLicenceContactDetailsPresenter.go(_LicenceContacts()) - - const [, licenceWithCompanyName] = result.licenceContacts - - expect(licenceWithCompanyName.name).to.equal('Water Services Ltd') - }) - - it('finds the customerId', () => { - const result = ViewLicenceContactDetailsPresenter.go(_LicenceContacts()) - - expect(result.customerId).to.equal('562e7ec4-eb9f-4d4b-9f4b-8005020fb50a') - }) - - it('handles not finding a customerId', () => { - const contacts = _LicenceContacts() - contacts.licenceContacts[1].company = null - - const result = ViewLicenceContactDetailsPresenter.go(contacts) - - expect(result.customerId).to.be.null() - }) - }) -}) - -function _LicenceContacts () { - return { - licenceContacts: [ - { - contact: { - firstName: 'Jackie', - lastName: 'Flow' - }, - licenceRole: { - name: 'returnsTo', - label: 'Returns' - }, - company: { - id: '6d861a7a-d639-4851-acf7-04481dcdafe5', - name: 'Ms Jackie Flow' - }, - address: { - address1: 'Air House', - address2: 'Open Road', - address3: 'Farmers yard', - address4: null, - address5: null, - address6: null, - country: null, - postcode: 'BS1 5TL' - } - }, - { - contact: null, - licenceRole: { - name: 'licenceHolder', - label: 'Licence Holder' - }, - company: { - id: '562e7ec4-eb9f-4d4b-9f4b-8005020fb50a', - name: 'Water Services Ltd' - }, - address: { - address1: 'Water House', - address2: 'Liquid Road', - address3: null, - address4: null, - address5: null, - address6: null, - country: null, - postcode: 'BS1 5TL' - } - } - ] - } -} diff --git a/test/services/licences/fetch-licence-contact-details.service.test.js b/test/services/licences/fetch-licence-contact-details.service.test.js deleted file mode 100644 index c47a378214..0000000000 --- a/test/services/licences/fetch-licence-contact-details.service.test.js +++ /dev/null @@ -1,101 +0,0 @@ -'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 - -// Test helpers -const AddressHelper = require('../../support/helpers/address.helper') -const CompanyHelper = require('../../support/helpers/company.helper') -const ContactModel = require('../../support/helpers/contact.helper') -const DatabaseSupport = require('../../support/database.js') -const LicenceDocumentHelper = require('../../support/helpers/licence-document.helper') -const LicenceDocumentRolesHelper = require('../../support/helpers/licence-document-role.helper') -const LicenceHelper = require('../../support/helpers/licence.helper') -const LicenceRoleHelper = require('../../support/helpers/licence-role.helper') - -// Thing under test -const FetchLicenceContactDetailsService = - require('../../../app/services/licences/fetch-licence-contact-details.service') - -describe('Fetch licence contact details service', () => { - beforeEach(async () => { - await DatabaseSupport.clean() - }) - - describe('when the licence has contact details', () => { - const addressId = '3eef7d72-9f8d-4f17-9b50-3739cb7f4aed' - const companyId = 'd3398615-f613-46be-a9ce-d452cf702271' - const contactId = '47fd9240-0a7b-4412-9db4-9ac4a94f675c' - const documentId = '93540ffd-fd4f-4a03-84b3-dba3b297a752' - const licenceId = '96d97293-1a62-4ad0-bcb6-24f68a203e6b' - const licenceRef = '21/06' - const licenceRoleId = 'ee59a1ce-272a-4f73-aa08-2d62dec4bbe1' - - beforeEach(async () => { - await AddressHelper.add({ - id: addressId, - address5: 'Earth', - address6: 'Planet' - }) - - await ContactModel.add({ - id: contactId - }) - - await CompanyHelper.add({ - id: companyId - }) - - await LicenceDocumentRolesHelper.add({ - endDate: null, - licenceDocumentId: documentId, - addressId, - licenceRoleId, - contactId, - companyId - }) - - await LicenceDocumentHelper.add({ - licenceRef, - id: documentId - }) - - await LicenceHelper.add({ - id: licenceId, - licenceRef - }) - - await LicenceRoleHelper.add({ - id: licenceRoleId - }) - }) - - it('returns licenceContacts', async () => { - const result = await FetchLicenceContactDetailsService.go(licenceId) - const [licenceUnderTest] = result.licenceContacts - - // Address - expect(licenceUnderTest.address.address1).to.equal('ENVIRONMENT AGENCY') - expect(licenceUnderTest.address.address2).to.equal('HORIZON HOUSE') - expect(licenceUnderTest.address.address3).to.equal('DEANERY ROAD') - expect(licenceUnderTest.address.address4).to.equal('BRISTOL') - expect(licenceUnderTest.address.address5).to.equal('Earth') - expect(licenceUnderTest.address.address6).to.equal('Planet') - expect(licenceUnderTest.address.country).to.equal('United Kingdom') - expect(licenceUnderTest.address.postcode).to.equal('BS1 5AH') - // Company - expect(licenceUnderTest.company.id).to.equal(companyId) - expect(licenceUnderTest.company.name).to.equal('Example Trading Ltd') - // Contact - expect(licenceUnderTest.contact.firstName).to.equal('Amara') - expect(licenceUnderTest.contact.lastName).to.equal('Gupta') - // Licence Role - expect(licenceUnderTest.licenceRole.label).to.equal('Licence Holder') - expect(licenceUnderTest.licenceRole.name).to.equal('licenceHolder') - }) - }) -}) diff --git a/test/services/licences/fetch-licence-contacts.service.test.js b/test/services/licences/fetch-licence-contacts.service.test.js new file mode 100644 index 0000000000..49ea736171 --- /dev/null +++ b/test/services/licences/fetch-licence-contacts.service.test.js @@ -0,0 +1,77 @@ +'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 + +// Test helpers +const AddressHelper = require('../../support/helpers/address.helper.js') +const CompanyHelper = require('../../support/helpers/company.helper.js') +const ContactHelper = require('../../support/helpers/contact.helper.js') +const DatabaseSupport = require('../../support/database.js') +const LicenceDocumentHelper = require('../../support/helpers/licence-document.helper.js') +const LicenceDocumentRolesHelper = require('../../support/helpers/licence-document-role.helper.js') +const LicenceHelper = require('../../support/helpers/licence.helper.js') +const LicenceRoleHelper = require('../../support/helpers/licence-role.helper.js') + +// Thing under test +const FetchLicenceContactsService = require('../../../app/services/licences/fetch-licence-contacts.service.js') + +describe('Fetch Licence Contacts service', () => { + let licenceId + let companyId + let contactId + + beforeEach(async () => { + await DatabaseSupport.clean() + }) + + describe('when the licence has contact details', () => { + beforeEach(async () => { + const licence = await LicenceHelper.add() + licenceId = licence.id + + const company = await CompanyHelper.add() + companyId = company.id + + const contact = await ContactHelper.add() + contactId = contact.id + + const { id: licenceDocumentId } = await LicenceDocumentHelper.add({ licenceRef: licence.licenceRef }) + const { id: licenceRoleId } = await LicenceRoleHelper.add() + const { id: addressId } = await AddressHelper.add() + await LicenceDocumentRolesHelper.add({ + endDate: null, + licenceDocumentId, + addressId, + licenceRoleId, + contactId, + companyId + }) + }) + + it('returns the matching licence contacts', async () => { + const results = await FetchLicenceContactsService.go(licenceId) + + expect(results[0]).to.equal({ + communicationType: 'Licence Holder', + companyId, + companyName: 'Example Trading Ltd', + contactId, + firstName: 'Amara', + lastName: 'Gupta', + address1: 'ENVIRONMENT AGENCY', + address2: 'HORIZON HOUSE', + address3: 'DEANERY ROAD', + address4: 'BRISTOL', + address5: null, + address6: null, + postcode: 'BS1 5AH', + country: 'United Kingdom' + }) + }) + }) +}) 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 5a44ca5c78..6d4ea0685d 100644 --- a/test/services/licences/view-licence-contact-details.service.test.js +++ b/test/services/licences/view-licence-contact-details.service.test.js @@ -9,54 +9,65 @@ const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script() const { expect } = Code // Things we need to stub -const FetchLicenceContactDetailsService = - require('../../../app/services/licences/fetch-licence-contact-details.service') -const ViewLicenceContactDetailsPresenter = - require('../../../app/presenters/licences/view-licence-contact-details.presenter') -const ViewLicenceService = require('../../../app/services/licences/view-licence.service') +const FetchLicenceContactsService = + require('../../../app/services/licences/fetch-licence-contacts.service.js') +const ViewLicenceService = require('../../../app/services/licences/view-licence.service.js') // Thing under test -const ViewLicenceContactDetailsService = require('../../../app/services/licences/view-licence-contact-details.service') +const ViewLicenceContactDetailsService = require('../../../app/services/licences/view-licence-contact-details.service.js') -describe('View Licence service contact details', () => { +describe('View Licence Contact Details service', () => { const auth = {} const testId = '2c80bd22-a005-4cf4-a2a2-73812a9861de' beforeEach(() => { - Sinon.stub(FetchLicenceContactDetailsService, 'go').returns(_contactDetailsFetchService()) - Sinon.stub(ViewLicenceContactDetailsPresenter, 'go').returns(_contactDetails()) - Sinon.stub(ViewLicenceService, 'go').resolves(_licence()) + 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(ViewLicenceService, 'go').resolves({ licenceName: 'fake licence' }) }) afterEach(() => { Sinon.restore() }) - describe('when a contact details', () => { - describe('and it has no optional fields', () => { - it('will return all the mandatory data and default values for use in the licence contact details page', - async () => { - const result = await ViewLicenceContactDetailsService.go(testId, auth) - - expect(result).to.equal({ - activeTab: 'contact-details', - licenceName: 'fake licence' - }) - }) + describe('when called', () => { + it('returns page data for the view', async () => { + const result = await ViewLicenceContactDetailsService.go(testId, auth) + + expect(result).to.equal({ + activeTab: 'contact-details', + 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' + }] + }) }) }) }) - -function _contactDetails () { - return { - activeTab: 'contact-details' - } -} - -function _contactDetailsFetchService () { - return {} -} - -function _licence () { - return { licenceName: 'fake licence' } -}