Skip to content

Commit

Permalink
Merge pull request #3 from DEFRA/fix-sirp-smell
Browse files Browse the repository at this point in the history
fix: adding water smell sirp
  • Loading branch information
daniel78uk authored Nov 17, 2023
2 parents 45d4154 + 9023666 commit b7d8662
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 24 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ install:

start:
$(MAKE) backend-up

npm start

build-image:
Expand All @@ -22,7 +22,7 @@ build-image:
docker images | grep $(SERVICE_NAME)

backend-up:
docker-compose -f docker-compose-dev.yml up -d
docker-compose up -d

backend-down:
docker-compose down
Expand Down
7 changes: 5 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ services:
environment:
- REDIS_PASSWORD=foo
- REDIS_PORT=6379
- REDIS_DATABASES=1
- REDIS_ARGS=""
networks:
- default

volumes:
redisdata:
driver: local
driver: local



10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"build": "bin/build",
"debug": "nodemon --inspect index.js",
"watch:css": "nodemon -e scss -x 'npm run build:css'",
"watch:nodejs": "nodemon -r dotenv/config index.js",
"watch:nodejs": "nodemon --delay 1000ms --signal SIGTERM -r dotenv/config index.js",
"dev": "concurrently 'npm:watch:css' 'npm:watch:nodejs'",
"lint": "standard",
"unit-test": "npx jest --verbose",
Expand Down Expand Up @@ -83,5 +83,13 @@
"ignore": [
"**/public/js/"
]
},
"nodemonConfig": {
"ext": "html,css,js,cjs,mjs,json,pug,njk",
"ignore": [
"**/test/**",
"**/docs/**"
],
"delay": 3000
}
}
43 changes: 35 additions & 8 deletions server/routes/water-quality/smell-desc.route.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
'use strict'

const { Paths, Views, RedisKeys } = require('../../utils/constants')
const {
Paths,
Views,
RedisKeys,
WQSirpRedisKeys
} = require('../../utils/constants')

const RedisService = require('../../services/redis.service')

const handlers = {
Expand All @@ -12,21 +18,42 @@ const handlers = {
},
post: (request, h) => {
const context = _getContext()
const payload = request.payload

// const reported = request.payload.reported
//
// if (reported === 'yes') {
// return h.view(Views.WATER_TYPE_SMALL_DESC, { ...context })
// } else if (reported === 'no') {
// return h.view(Views.WATER_TYPE_SMALL_DESC, { ...context })
// }
_generateSirpData(request, payload)

return h.view(Views.WATER_TYPE_SOURCE, {
...context
})
}
}

const _generateSirpData = (request, payload) => {
const pollutionSource = payload['waterquality-source']
let pollutionSourceValue
if (pollutionSource === 'yes') {
pollutionSourceValue = true
} else if (pollutionSource === 'no') {
pollutionSourceValue = false
}

RedisService.set(
request,
WQSirpRedisKeys.WQ_SIRP_SMELL_SOURCE,
JSON.stringify(pollutionSourceValue)
)

const pollutionSourceOther = payload['smell-details']

if (pollutionSourceOther !== undefined && pollutionSourceOther !== ' ') {
RedisService.set(
request,
WQSirpRedisKeys.WQ_SIRP_SMELL_SOURCE_OTHER,
pollutionSourceOther
)
}
}

const _getContext = () => {
return {
pageTitle: 'Report an environmental incident',
Expand Down
26 changes: 15 additions & 11 deletions server/services/redis.service.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
'use strict'

const {
SI_SESSION_KEY
} = require('../utils/constants')
const { SI_SESSION_KEY } = require('../utils/constants')

const config = require('../utils/config')

const REDIS_TTL_IN_SECONDS = config.cookieTimeout / 1000

module.exports = class RedisService {
static async get (request, key) {
static async get(request, key) {
const client = request.redis.client
const redisValue = await client.get(
`${request.state[SI_SESSION_KEY]}.${key}`
Expand All @@ -29,19 +27,25 @@ module.exports = class RedisService {
return parsedValue
}

static set (request, key, value) {
static set(request, key, value) {
const client = request.redis.client
const keyWithSessionId = `${request.state[SI_SESSION_KEY]}.${key}`
return client.setex(keyWithSessionId, REDIS_TTL_IN_SECONDS, value)

try {
return client.setex(keyWithSessionId, REDIS_TTL_IN_SECONDS, value)
} catch (err) {
console.error(err.message)
return false
}
}

static delete (request, key) {
static delete(request, key) {
const client = request.redis.client
const keyWithSessionId = `${request.state[SI_SESSION_KEY]}.${key}`
client.del(keyWithSessionId)
}

static async deleteSessionData (request) {
static async deleteSessionData(request) {
const client = request.redis.client
const keys = await _getMatchingRedisKeys(request)
for (const key of keys) {
Expand All @@ -55,7 +59,7 @@ module.exports = class RedisService {
* @param {*} value The string value to be chekced
* @returns True if the string looks like a Json object, otherwise false
*/
const _isJsonString = value =>
const _isJsonString = (value) =>
value &&
value.length &&
((value.startsWith('{') && value.endsWith('}')) ||
Expand All @@ -67,7 +71,7 @@ const _isJsonString = value =>
* @returns True if the string contains a bolean, otherwise false
*/

const _isBooleanString = value =>
const _isBooleanString = (value) =>
value &&
value.length &&
(value.toLowerCase() === 'true' || value.toLowerCase() === 'false')
Expand All @@ -79,7 +83,7 @@ const _isBooleanString = value =>
* @param {*} request The request containing the Redis cache
* @returns An array of Redis keys that are prefixed with the session key
*/
const _getMatchingRedisKeys = async request => {
const _getMatchingRedisKeys = async (request) => {
const client = request.redis.client
const sessionKey = request.state[SI_SESSION_KEY]

Expand Down
4 changes: 4 additions & 0 deletions server/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ const WQSirpRedisKeys = {
WQ_SIRP_WHAT_CAN_YOU_SEE: 'WQ_sirp_Whatcanyouseeinoronthewater',
WQ_SIRP_WHAT_IS_IN_WATER: 'WQ_sirp_Whatdoyouthinkisinthewater',
WQ_SIRP_WHAT_IS_IN_WATER_OTHER: 'WQ_sirp_inWaterOther',

WQ_SIRP_SMELL_SOURCE: 'WQ_sirp_isthereasmell',
WQ_SIRP_SMELL_SOURCE_OTHER: 'WQ_sirp_isthereasmellother',

WQ_SIRP_POLLUTION_SOURCE: 'WQ_sirp_Doyouthinkyouknowwherethepollutioniscomin',
WQ_SIRP_POLLUTION_SOURCE_OTHER: 'WQ_sirp_pollutionSourceOther',
WQ_SIRP_HOW_FAR_ACROSS: 'WQ_sirp_Howfaracrossthewaterfeaturecanyouseethe',
Expand Down

0 comments on commit b7d8662

Please sign in to comment.