Skip to content

Commit

Permalink
SDI-265: ♻️ Minor improvements (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
petergphillips authored Jul 11, 2022
1 parent a6ac4cb commit a71978d
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 42 deletions.
2 changes: 1 addition & 1 deletion server/middleware/authorisationMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import asyncMiddleware from './asyncMiddleware'

export default function authorisationMiddleware(authorisedRoles: string[] = []): RequestHandler {
return asyncMiddleware((req, res, next) => {
if (res.locals && res.locals.user && res.locals.user.token) {
if (res.locals?.user?.token) {
const { authorities: roles = [] } = jwtDecode(res.locals.user.token) as { authorities?: string[] }

if (authorisedRoles.length && !roles.some(role => authorisedRoles.includes(role))) {
Expand Down
2 changes: 1 addition & 1 deletion server/services/userService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import convertToTitleCase from '../utils/utils'
import { convertToTitleCase } from '../utils/utils'
import type HmppsAuthClient from '../data/hmppsAuthClient'

interface UserDetails {
Expand Down
10 changes: 2 additions & 8 deletions server/utils/nunjucksSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import nunjucks from 'nunjucks'
import express from 'express'
import * as pathModule from 'path'
import { initialiseName } from './utils'

const production = process.env.NODE_ENV === 'production'

Expand Down Expand Up @@ -37,12 +38,5 @@ export default function nunjucksSetup(app: express.Express, path: pathModule.Pla
},
)

njkEnv.addFilter('initialiseName', (fullName: string) => {
// this check is for the authError page
if (!fullName) {
return null
}
const array = fullName.split(' ')
return `${array[0][0]}. ${array.reverse()[0]}`
})
njkEnv.addFilter('initialiseName', initialiseName)
}
53 changes: 26 additions & 27 deletions server/utils/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
import convertToTitleCase from './utils'
import { convertToTitleCase, initialiseName } from './utils'

describe('Convert to title case', () => {
it('null string', () => {
expect(convertToTitleCase(null)).toEqual('')
describe('convert to title case', () => {
it.each([
[null, null, ''],
['empty string', '', ''],
['Lower case', 'robert', 'Robert'],
['Upper case', 'ROBERT', 'Robert'],
['Mixed case', 'RoBErT', 'Robert'],
['Multiple words', 'RobeRT SMiTH', 'Robert Smith'],
['Leading spaces', ' RobeRT', ' Robert'],
['Trailing spaces', 'RobeRT ', 'Robert '],
['Hyphenated', 'Robert-John SmiTH-jONes-WILSON', 'Robert-John Smith-Jones-Wilson'],
])('%s convertToTitleCase(%s, %s)', (_: string, a: string, expected: string) => {
expect(convertToTitleCase(a)).toEqual(expected)
})
it('empty string', () => {
expect(convertToTitleCase('')).toEqual('')
})
it('Lower Case', () => {
expect(convertToTitleCase('robert')).toEqual('Robert')
})
it('Upper Case', () => {
expect(convertToTitleCase('ROBERT')).toEqual('Robert')
})
it('Mixed Case', () => {
expect(convertToTitleCase('RoBErT')).toEqual('Robert')
})
it('Multiple words', () => {
expect(convertToTitleCase('RobeRT SMiTH')).toEqual('Robert Smith')
})
it('Leading spaces', () => {
expect(convertToTitleCase(' RobeRT')).toEqual(' Robert')
})
it('Trailing spaces', () => {
expect(convertToTitleCase('RobeRT ')).toEqual('Robert ')
})
it('Hyphenated', () => {
expect(convertToTitleCase('Robert-John SmiTH-jONes-WILSON')).toEqual('Robert-John Smith-Jones-Wilson')
})

describe('initialise name', () => {
it.each([
[null, null, null],
['Empty string', '', null],
['One word', 'robert', 'r. robert'],
['Two words', 'Robert James', 'R. James'],
['Three words', 'Robert James Smith', 'R. Smith'],
['Double barrelled', 'Robert-John Smith-Jones-Wilson', 'R. Smith-Jones-Wilson'],
])('%s initialiseName(%s, %s)', (_: string, a: string, expected: string) => {
expect(initialiseName(a)).toEqual(expected)
})
})
10 changes: 8 additions & 2 deletions server/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ const isBlank = (str: string): boolean => !str || /^\s*$/.test(str)
*/
const properCaseName = (name: string): string => (isBlank(name) ? '' : name.split('-').map(properCase).join('-'))

const convertToTitleCase = (sentence: string): string =>
export const convertToTitleCase = (sentence: string): string =>
isBlank(sentence) ? '' : sentence.split(' ').map(properCaseName).join(' ')

export default convertToTitleCase
export const initialiseName = (fullName?: string): string | null => {
// this check is for the authError page
if (!fullName) return null

const array = fullName.split(' ')
return `${array[0][0]}. ${array.reverse()[0]}`
}
2 changes: 0 additions & 2 deletions server/views/partials/header.njk
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{% from "govuk/components/tag/macro.njk" import govukTag %}

<header class="hmpps-header" role="banner">
<div class="hmpps-header__container">
<div class="hmpps-header__title">
Expand Down
1 change: 0 additions & 1 deletion server/views/partials/layout.njk
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{% extends "govuk/template.njk" %}
{% from "govuk/components/phase-banner/macro.njk" import govukPhaseBanner %}

{% block head %}
<!--[if !IE 8]><!-->
Expand Down

0 comments on commit a71978d

Please sign in to comment.