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

refactor: restructure ahead of adding in multi-species routing. #419

Merged
merged 6 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/api-requests/application-service-api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Wreck = require('@hapi/wreck')
const config = require('../config')

async function getLatestApplicationsBySbi (sbi, logger) {
async function getAllApplicationsBySbi (sbi, logger) {
mattyod marked this conversation as resolved.
Show resolved Hide resolved
const endpoint = `${config.applicationApiUri}/applications/latest?sbi=${sbi}`

try {
Expand All @@ -21,5 +21,5 @@ async function getLatestApplicationsBySbi (sbi, logger) {
}

module.exports = {
getLatestApplicationsBySbi
getAllApplicationsBySbi
}
20 changes: 2 additions & 18 deletions app/api-requests/claim-service-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const config = require('../config')
const { livestockTypes, claimType, dateOfVetVisitExceptions } = require('../constants/claim')
const { REJECTED, READY_TO_PAY, PAID } = require('../constants/status')
const { getReviewType } = require('../lib/get-review-type')
const { isWithin10Months } = require('../lib/date-utils')

async function getClaimsByApplicationReference (applicationReference, logger) {
const endpoint = `${config.applicationApiUri}/claim/get-by-application-reference/${applicationReference}`
Expand Down Expand Up @@ -72,13 +73,6 @@ async function submitNewClaim (data, logger) {
}
}

const isWithin10Months = (a, b) => {
const [dateA, dateB] = [new Date(a), new Date(b)]
const [firstDate, secondDate] = dateA < dateB ? [dateA, dateB] : [dateB, dateA]
const firstDatePlus10Months = firstDate.setMonth(firstDate.getMonth() + 10)
return firstDatePlus10Months >= secondDate
}

const isWithIn4MonthsBeforeOrAfterDateOfVisit = (dateOfVisit, dateOfTesting) => {
const startDate = new Date(dateOfVisit)
const endDate = new Date(dateOfVisit)
Expand Down Expand Up @@ -171,14 +165,6 @@ const isValidDateOfVisit = (dateOfVisit, typeOfClaim, previousClaims, vetVisitRe
}
}

const lockedToSpecies = (previousEndemicClaims) => {
// any endemic (new-world) claims means they have missed their opportunity to switch species
if (previousEndemicClaims && previousEndemicClaims.length > 0) {
return true
}
return false
}

const isFirstTimeEndemicClaimForActiveOldWorldReviewClaim = (request) => {
const { latestVetVisitApplication, typeOfReview, previousClaims } = session.getEndemicsClaim(request)

Expand All @@ -194,13 +180,11 @@ module.exports = {
getAmount,
isURNUnique,
submitNewClaim,
isWithin10Months,
isValidDateOfVisit,
getReviewWithinLast10Months,
getClaimsByApplicationReference,
isDateOfTestingLessThanDateOfVisit,
getReviewTestResultWithinLast10Months,
isWithIn4MonthsBeforeOrAfterDateOfVisit,
isFirstTimeEndemicClaimForActiveOldWorldReviewClaim,
lockedToSpecies
isFirstTimeEndemicClaimForActiveOldWorldReviewClaim
}
12 changes: 0 additions & 12 deletions app/lib/check-date-validity.js

This file was deleted.

81 changes: 81 additions & 0 deletions app/lib/context-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const { getAllApplicationsBySbi } = require('../api-requests/application-service-api')
const { isWithin10Months } = require('./date-utils')
const session = require('../session')
const {
endemicsClaim: {
latestEndemicsApplication: latestEndemicsApplicationKey,
latestVetVisitApplication: latestVetVisitApplicationKey,
previousClaims: previousClaimsKey
}
} = require('../session/keys')
const { getClaimsByApplicationReference } = require('../api-requests/claim-service-api')
const { getEndemicsClaim } = require('../session')
const { claimType } = require('../constants/claim')

async function refreshApplications (request) {
const applications = await getAllApplicationsBySbi(request.query.sbi, request.logger)

// get latest new world
const latestEndemicsApplication = applications.find((application) => {
return application.type === 'EE'
})

// get latest old world - if there isn't one, or it's not within 10 months of the new world one, then we won't consider it,
// and thus return undefined
const latestVetVisitApplication = applications.find((application) => {
// endemics application must have been created within 10 months of vet-visit application visit date
return application.type === 'VV' && isWithin10Months(application.data?.visitDate, latestEndemicsApplication.createdAt)
})

session.setEndemicsClaim(request, latestVetVisitApplicationKey, latestVetVisitApplication)
session.setEndemicsClaim(request, latestEndemicsApplicationKey, latestEndemicsApplication)

return { latestEndemicsApplication, latestVetVisitApplication }
}

async function refreshClaims (request, applicationRef) {
// fetch all the claims (all species)
const claims = await getClaimsByApplicationReference(
applicationRef,
request.logger
)

session.setEndemicsClaim(request, previousClaimsKey, claims)

return claims
}

function getLatestClaimForContext (request) {
const { previousClaims, latestVetVisitApplication } = getEndemicsClaim(request)

// When we add the MS code we can layer in here filtering by species
// const { typeOfLivestock, previousClaims, latestVetVisitApplication } = getEndemicsClaim(request)
// const latestEEClaim = previousClaims.find(claim => claim.data.typeOfLivestock === typeOfLivestock)
const latestEEClaim = previousClaims?.find(claim => claim) // for now just latest

return latestEEClaim ?? latestVetVisitApplication
}

function getTypeOfLivestockFromLatestClaim (request) {
const claim = getLatestClaimForContext(request)

return claim.data?.typeOfLivestock ?? claim.data?.whichReview
}

function canChangeSpecies (request, typeOfReview) {
// for now we obey the following, we can manipulate this to consider MS
const { previousClaims } = getEndemicsClaim(request)
return claimType[typeOfReview] === claimType.review && !lockedToSpecies(previousClaims)
}

const lockedToSpecies = (previousEndemicClaims) => {
// any endemic (new-world) claims means they have missed their opportunity to switch species
return (previousEndemicClaims && previousEndemicClaims.length > 0)
}

module.exports = {
canChangeSpecies,
getTypeOfLivestockFromLatestClaim,
refreshApplications,
refreshClaims
}
20 changes: 20 additions & 0 deletions app/lib/date-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const isValidDate = (year, month, day) => {
const dateObject = new Date(year, month - 1, day)
return (
dateObject.getFullYear() === year &&
dateObject.getMonth() === month - 1 &&
dateObject.getDate() === day
)
}

const isWithin10Months = (a, b) => {
const [dateA, dateB] = [new Date(a), new Date(b)]
const [firstDate, secondDate] = dateA < dateB ? [dateA, dateB] : [dateB, dateA]
const firstDatePlus10Months = firstDate.setMonth(firstDate.getMonth() + 10)
return firstDatePlus10Months >= secondDate
}

module.exports = {
isValidDate,
isWithin10Months
}
82 changes: 50 additions & 32 deletions app/plugins/router.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,64 @@
const config = require('../config')

let routes = [].concat(
require('../routes/assets').handlers,
const alwaysOnRouteHandlers = [require('../routes/assets').handlers,
require('../routes/cookies').handlers,
require('../routes/healthy').handlers,
require('../routes/healthz').handlers,
require('../routes/index').handlers,
require('../routes/signin-oidc').handlers
)
require('../routes/signin-oidc').handlers].flat()

const endemicsSpecificRouteHandlers = [
require('../routes/endemics/index').handlers,
require('../routes/endemics/test-urn').handlers,
require('../routes/endemics/test-results').handlers,
require('../routes/endemics/date-of-visit').handlers,
require('../routes/endemics/number-of-fluid-oral-samples').handlers,
require('../routes/endemics/number-of-samples-tested').handlers,
require('../routes/endemics/species-numbers').handlers,
require('../routes/endemics/number-of-species-tested').handlers,
require('../routes/endemics/vet-name').handlers,
require('../routes/endemics/vet-rcvs').handlers,
require('../routes/endemics/check-answers').handlers,
require('../routes/endemics/confirmation').handlers,
require('../routes/endemics/date-of-testing').handlers,
require('../routes/endemics/vaccination').handlers,
require('../routes/endemics/disease-status').handlers,
require('../routes/endemics/sheep-endemics-package').handlers,
require('../routes/endemics/sheep-tests').handlers,
require('../routes/endemics/biosecurity').handlers,
require('../routes/endemics/vet-visits-review-test-results').handlers,
require('../routes/endemics/sheep-test-results').handlers,
require('../routes/endemics/pi-hunt').handlers,
require('../routes/endemics/pi-hunt-recommended').handlers,
require('../routes/endemics/pi-hunt-all-animals').handlers
].flat()

const endemicsWithMsOffHandlers = [
require('../routes/endemics/which-type-of-review').handlers,
require('../routes/endemics/which-species').handlers
].flat()

const endemicsWithMsOnHandlers = [
require('../routes/endemics/which-type-of-review-ms').handlers,
require('../routes/endemics/which-species-ms').handlers
].flat()

const devLoginHandlers = require('../routes/endemics/dev-sign-in').handlers

let routes = alwaysOnRouteHandlers

if (config.endemics.enabled) {
routes = routes.concat(
require('../routes/endemics/index').handlers,
require('../routes/endemics/test-urn').handlers,
require('../routes/endemics/test-results').handlers,
require('../routes/endemics/date-of-visit').handlers,
require('../routes/endemics/which-species').handlers,
require('../routes/endemics/number-of-fluid-oral-samples').handlers,
require('../routes/endemics/number-of-samples-tested').handlers,
require('../routes/endemics/species-numbers').handlers,
require('../routes/endemics/which-type-of-review').handlers,
require('../routes/endemics/number-of-species-tested').handlers,
require('../routes/endemics/vet-name').handlers,
require('../routes/endemics/vet-rcvs').handlers,
require('../routes/endemics/check-answers').handlers,
require('../routes/endemics/confirmation').handlers,
require('../routes/endemics/date-of-testing').handlers,
require('../routes/endemics/vaccination').handlers,
require('../routes/endemics/disease-status').handlers,
require('../routes/endemics/sheep-endemics-package').handlers,
require('../routes/endemics/sheep-tests').handlers,
require('../routes/endemics/biosecurity').handlers,
require('../routes/endemics/vet-visits-review-test-results').handlers,
require('../routes/endemics/sheep-test-results').handlers,
require('../routes/endemics/pi-hunt').handlers,
require('../routes/endemics/pi-hunt-recommended').handlers,
require('../routes/endemics/pi-hunt-all-animals').handlers
)
routes = routes.concat(endemicsSpecificRouteHandlers)

if (!config.multiSpecies.enabled) {
mattyod marked this conversation as resolved.
Show resolved Hide resolved
routes = routes.concat(endemicsWithMsOffHandlers)
} else {
routes = routes.concat(endemicsWithMsOnHandlers)
}
}

if (config.devLogin.enabled) {
routes = routes.concat(require('../routes/endemics/dev-sign-in').handlers)
routes = routes.concat(devLoginHandlers)
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion app/routes/endemics/date-of-testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const {
const raiseInvalidDataEvent = require('../../event/raise-invalid-data-event')
const { getReviewType } = require('../../lib/get-review-type')
const { getLivestockTypes } = require('../../lib/get-livestock-types')
const { isValidDate } = require('./../../lib/check-date-validity')
const { isValidDate } = require('../../lib/date-utils')

const pageUrl = `${urlPrefix}/${endemicsDateOfTesting}`
const backLink = (request) => {
Expand Down
69 changes: 19 additions & 50 deletions app/routes/endemics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,18 @@ const urlPrefix = require('../../config').urlPrefix
const { endemicsIndex } = require('../../config/routes')
const { requestAuthorizationCodeUrl } = require('../../auth')
const logout = require('../../lib/logout')
const {
getLatestApplicationsBySbi
} = require('../../api-requests/application-service-api')
const {
isWithin10Months,
getClaimsByApplicationReference
} = require('../../api-requests/claim-service-api')
const {
endemicsWhichSpecies,
endemicsWhichTypeOfReview
} = require('../../config/routes')
const {
endemicsClaim: {
landingPage: landingPageKey,
latestEndemicsApplication: latestEndemicsApplicationKey,
latestVetVisitApplication: latestVetVisitApplicationKey,
previousClaims: previousClaimsKey,
reference: referenceKey
}
} = require('../../session/keys')
const createClaimReference = require('../../lib/create-temp-claim-reference')
const { refreshApplications, refreshClaims } = require('../../lib/context-helper')

const endemicsWhichTypeOfReviewURI = `${urlPrefix}/${endemicsWhichTypeOfReview}`
const endemicsWhichSpeciesURI = `${urlPrefix}/${endemicsWhichSpecies}`
Expand All @@ -37,57 +28,35 @@ const getHandler = {
handler: async (request, h) => {
request.logger.setBindings({ sbi: request.query.sbi })
if (request.query?.from === 'dashboard' && request.query.sbi) {
const applications = await getLatestApplicationsBySbi(request.query.sbi, request.logger)
const latestEndemicsApplication = applications.find((application) => {
return application.type === 'EE'
})
const latestVetVisitApplication = applications.find((application) => {
// endemics application must have been created within 10 months of vetvisit application visit date
return (
application.type === 'VV' &&
isWithin10Months(
application.data?.visitDate,
latestEndemicsApplication.createdAt
)
)
})
const claims = await getClaimsByApplicationReference(
latestEndemicsApplication.reference,
request.logger
)
// fetch latest new world (always) and latest old world (if relevant) application
const { latestEndemicsApplication, latestVetVisitApplication } = await refreshApplications(request)

const claims = await refreshClaims(request, latestEndemicsApplication.reference)

const tempClaimId = createClaimReference()
session.setEndemicsClaim(
request,
latestVetVisitApplicationKey,
latestVetVisitApplication
)
session.setEndemicsClaim(
request,
latestEndemicsApplicationKey,
latestEndemicsApplication
)
session.setEndemicsClaim(request, previousClaimsKey, claims)
session.setEndemicsClaim(request, referenceKey, tempClaimId)

// new user
if (claims && (claims.length === 0) && latestVetVisitApplication === undefined) {
if (config.multiSpecies.enabled) {
// for MS we want to always go through same flow, so just redirect straight there
session.setEndemicsClaim(request, landingPageKey, endemicsWhichSpeciesURI)
return h.redirect(endemicsWhichSpeciesURI)
}

// new user (has no claims, and no relevant old world application)
if (claims.length === 0 && latestVetVisitApplication === undefined) {
session.setEndemicsClaim(request, landingPageKey, endemicsWhichSpeciesURI)
return h.redirect(endemicsWhichSpeciesURI)
}

// new claims
if (claims && claims.length > 0) {
// new claims (already made at least 1 claim in new world)
if (claims.length > 0) {
session.setEndemicsClaim(request, landingPageKey, endemicsWhichTypeOfReviewURI)
return h.redirect(endemicsWhichTypeOfReviewURI)
return h.redirect(endemicsWhichTypeOfReviewURI) // this was going straight to which type of review, skipping species
}

// old claims NO new claims
// old claim, but NO new world claims
if (latestVetVisitApplication) {
session.setEndemicsClaim(
request,
landingPageKey,
endemicsWhichTypeOfReviewURI
)
session.setEndemicsClaim(request, landingPageKey, endemicsWhichTypeOfReviewURI)
return h.redirect(endemicsWhichTypeOfReviewURI)
rossjarvisesynergy marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Down
Loading