Skip to content

Commit

Permalink
Replace view licence contact details page (#1238)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
sujithvg authored Sep 17, 2024
1 parent 34c0f05 commit fc8ec65
Show file tree
Hide file tree
Showing 17 changed files with 755 additions and 101 deletions.
18 changes: 15 additions & 3 deletions app/controllers/licences.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -160,8 +171,9 @@ module.exports = {
supplementary,
viewBills,
viewCommunications,
viewContacts,
viewLicenceContacts,
viewHistory,
viewLicenceContactDetails,
viewReturns,
viewSetUp,
viewSummary
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/licences/customer-contacts.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/licences/licence-contacts.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* Formats data for the `/licences/{id}/contact-details` view licence contact details page
* @module ViewLicenceContactDetailsPresenter
* @module ViewLicenceContactsPresenter
*/

/**
Expand Down
91 changes: 91 additions & 0 deletions app/presenters/licences/view-licence-contact-details.presenter.js
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 6 additions & 1 deletion app/routes/licence.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion app/services/licences/fetch-customer-contacts.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
42 changes: 42 additions & 0 deletions app/services/licences/fetch-licence-contact-details.service.js
Original file line number Diff line number Diff line change
@@ -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<module:LicenceModel>} 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
}
2 changes: 1 addition & 1 deletion app/services/licences/fetch-licence-contacts.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
33 changes: 10 additions & 23 deletions app/services/licences/view-licence-contact-details.service.js
Original file line number Diff line number Diff line change
@@ -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<object>} an object representing the `pageData` needed by the licence contact details template.
* @returns {Promise<object>} 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
}
}

Expand Down
41 changes: 41 additions & 0 deletions app/services/licences/view-licence-contacts.service.js
Original file line number Diff line number Diff line change
@@ -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<object>} 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
}
54 changes: 54 additions & 0 deletions app/views/licences/licence-contact-details.njk
Original file line number Diff line number Diff line change
@@ -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 %}
<h1 class="govuk-heading-l">
<span class="govuk-caption-l">Licence {{ licenceRef }}</span>
{{ pageTitle }}
</h1>

{% macro displayAddress(address) %}
{% for item in address %}
<p class="govuk-body govuk-!-margin-bottom-0"> {{ item }} </p>
{% 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 %}
2 changes: 1 addition & 1 deletion app/views/licences/tabs/summary.njk
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<dt class="govuk-summary-list__key">Licence Holder</dt>
<dd class="govuk-summary-list__value">
<span class="govuk-form-group">{{ licenceHolder }}</span>
<a class="govuk-form-group" href="/licences/{{ documentId }}/contact">View licence contact details</a>
<a class="govuk-form-group" href="/system/licences/{{ licenceId }}/licence-contact">View licence contact details</a>
</dd>
</div>

Expand Down
Loading

0 comments on commit fc8ec65

Please sign in to comment.