Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sonarcloud fixes in progress #111

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions app/closure/process-closure-data.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const parsedSchema = require('../routes/schemas/parsed-closure')

const maxClosureDataLength = 3
const processClosureData = async (data) => {
const uploadData = []
const splitData = data.split(/\r?\n|\r|\n/g)
const closureLines = splitData.filter((str) => str !== '')
for (const closureLine of closureLines) {
const clData = closureLine.split(',')
if (clData.length !== 3) {
if (clData.length !== maxClosureDataLength) {
return {
errors: { details: [{ message: 'The file is not in the expected format' }] }
}
Expand Down
11 changes: 8 additions & 3 deletions app/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ const joi = require('joi')
const authConfig = require('./auth')
const storageConfig = require('./storage')
const messageConfig = require('./message')
const portNumber = 3007
const staticCacheTimeout = 604800000

// Define config schema
const schema = joi.object({
serviceName: joi.string().default('Payment management'),
port: joi.number().default(3007),
env: joi.string().valid('development', 'test', 'production').default('development'),
staticCacheTimeoutMillis: joi.number().default(7 * 24 * 60 * 60 * 1000),
port: joi.number().default(portNumber),
env: joi
.string()
.valid('development', 'test', 'production')
.default('development'),
staticCacheTimeoutMillis: joi.number().default(staticCacheTimeout),
googleTagManagerKey: joi.string().default(''),
paymentsEndpoint: joi.string().uri().required(),
trackingEndpoint: joi.string().uri().required(),
Expand Down
3 changes: 2 additions & 1 deletion app/helpers/render-error-page.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { getSchemes } = require('./get-schemes')
const HTTP_BAD_REQUEST = 400

const renderErrorPage = async (view, request, h, err) => {
request.log(['error', 'validation'], err)
Expand All @@ -11,7 +12,7 @@ const renderErrorPage = async (view, request, h, err) => {
})
: []
const schemes = await getSchemes()
return h.view(view, { schemes, errors }).code(400).takeover()
return h.view(view, { schemes, errors }).code(HTTP_BAD_REQUEST).takeover()
}

module.exports = {
Expand Down
5 changes: 3 additions & 2 deletions app/hold/handle-bulk-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ const { post } = require('../api')
const { getHoldCategories } = require('../holds')
const { readFileContent } = require('./read-file-content')
const { processHoldData } = require('./process-hold-data')
const HTTP_BAD_REQUEST = 400

const handleBulkPost = async (request, h) => {
const data = readFileContent(request.payload.file.path)
if (!data) {
const { schemes, paymentHoldCategories } = await getHoldCategories()
return h.view('payment-holds/bulk', { schemes, paymentHoldCategories, errors: { details: [{ message: 'An error occurred whilst reading the file' }] } }).code(400).takeover()
return h.view('payment-holds/bulk', { schemes, paymentHoldCategories, errors: { details: [{ message: 'An error occurred whilst reading the file' }] } }).code(HTTP_BAD_REQUEST).takeover()
}
const { uploadData, errors } = await processHoldData(data)
if (errors) {
const { schemes, paymentHoldCategories } = await getHoldCategories()
return h.view('payment-holds/bulk', { schemes, paymentHoldCategories, errors }).code(400).takeover()
return h.view('payment-holds/bulk', { schemes, paymentHoldCategories, errors }).code(HTTP_BAD_REQUEST).takeover()
}
if (request.payload.remove) {
await post('/payment-holds/bulk/remove', { data: uploadData, holdCategoryId: request.payload.holdCategoryId }, null)
Expand Down
6 changes: 3 additions & 3 deletions app/hold/process-hold-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ const parsedSchema = require('../routes/schemas/parsed-hold')
const processHoldData = async (data) => {
const uploadData = []
const splitData = data.split(',')
for (const data of splitData) {
const result = parsedSchema.validate({ frn: data }, {
for (const frn of splitData) {
const result = parsedSchema.validate({ frn }, {
abortEarly: false
})
if (result.error) {
return {
errors: result.error
}
} else {
uploadData.push(data)
uploadData.push(frn)
}
}
return {
Expand Down
38 changes: 27 additions & 11 deletions app/payments/get-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,36 @@ const { sendMessage, receiveMessage } = require('../messaging')
const getData = async (category, value) => {
const messageId = uuidv4()
const request = { category, value }
await sendMessage(request, TYPE, config.messageConfig.dataTopic, { messageId })

await sendMessage(request, TYPE, config.messageConfig.dataTopic, {
messageId
})
console.info('Data request sent:', util.inspect(request, false, null, true))
const response = await receiveMessage(messageId, config.messageConfig.dataQueue)
if (response) {
console.info('Data response received:', util.inspect(response, false, null, true))
if (Array.isArray(response.data)) {
for (let i = 0; i < response.data.length; i++) {
if (response.data[i].scheme === 'SFI') {
response.data[i].scheme = 'SFI22'
}
}
}

const response = await receiveMessage(
messageId,
config.messageConfig.dataQueue
)

if (!response) {
return null
}

console.info(
'Data response received:',
util.inspect(response, false, null, true)
)

if (!Array.isArray(response.data)) {
return response.data
}

const transformedData = response.data.map(item => ({
...item,
scheme: item.scheme === 'SFI' ? 'SFI22' : item.scheme
}))

return transformedData
}

module.exports = {
Expand Down
15 changes: 10 additions & 5 deletions app/plugins/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ const config = require('../config')
const authCookie = require('@hapi/cookie')
const auth = require('../auth')

const SESSION_AUTH = 'session-auth'

module.exports = {
plugin: {
name: 'auth',
register: async (server) => {
register: async server => {
await server.register(authCookie)

server.auth.strategy('session-auth', 'cookie', {
server.auth.strategy(SESSION_AUTH, 'cookie', {
cookie: {
name: 'session-auth',
name: SESSION_AUTH,
password: config.authConfig.cookie.password,
ttl: config.authConfig.cookie.ttl,
path: '/',
Expand All @@ -21,11 +23,14 @@ module.exports = {
redirectTo: '/login'
})

server.auth.default('session-auth')
server.auth.default(SESSION_AUTH)

server.ext('onPreAuth', async (request, h) => {
if (request.auth.credentials) {
await auth.refresh(request.auth.credentials.account, request.cookieAuth)
await auth.refresh(
request.auth.credentials.account,
request.cookieAuth
)
}
return h.continue
})
Expand Down
26 changes: 14 additions & 12 deletions app/plugins/error-pages.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Add an `onPreResponse` listener to return error pages
*/
const HTTP_NOT_AUTHORIZED = 401
const HTTP_FORBIDDEN = 403
const HTTP_NOT_FOUND = 404
const HTTP_SERVER_ERROR = 500

module.exports = {
plugin: {
Expand All @@ -10,18 +11,16 @@ module.exports = {
const response = request.response

if (response.isBoom) {
// An error was raised during
// processing the request
const statusCode = response.output.statusCode

// if not authorised then request login
if (statusCode === 401 || statusCode === 403) {
if (
statusCode === HTTP_NOT_AUTHORIZED ||
statusCode === HTTP_FORBIDDEN
) {
return h.view('unauthorized').code(statusCode)
}

// In the event of 404
// return the `404` view
if (statusCode === 404) {
if (statusCode === HTTP_NOT_FOUND) {
return h.view('404').code(statusCode)
}

Expand All @@ -31,8 +30,11 @@ module.exports = {
message: response.message
})

// The return the `500` view
return h.view('500').code(statusCode)
if (statusCode >= HTTP_SERVER_ERROR) {
return h.view('500').code(statusCode)
}

return h.continue
}
return h.continue
})
Expand Down
9 changes: 8 additions & 1 deletion app/plugins/view-context.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
const { mapAuth, getUser } = require('../auth')
const HTTP_NOT_FOUND = 404
const HTTP_INTERNAL_SERVER_ERROR = 500

module.exports = {
plugin: {
name: 'view-context',
register: (server, _options) => {
server.ext('onPreResponse', (request, h) => {
const statusCode = request.response.statusCode
if (request.response.variety === 'view' && statusCode !== 404 && statusCode !== 500 && request.response.source.context) {
if (
request.response.variety === 'view' &&
statusCode !== HTTP_NOT_FOUND &&
statusCode !== HTTP_INTERNAL_SERVER_ERROR &&
request.response.source.context
) {
request.response.source.context.auth = mapAuth(request)
request.response.source.context.user = getUser(request)
}
Expand Down
Loading