Skip to content

Commit

Permalink
feature/BNGP-4400-land-ownership-multiple-upload-issue (#663)
Browse files Browse the repository at this point in the history
* feature/BNGP-4400-land-ownership-multiple-upload-issue

* feature/BNGP-4400-land-ownership-multiple-upload-issue

* feature/BNGP-4400-land-ownership-multiple-upload-issue

* feature/BNGP-4400-land-ownership-multiple-upload-issue

* feature/BNGP-4400-land-ownership-multiple-upload-issue

---------

Co-authored-by: Kiran Varma <varma.ap@gmail.com>
  • Loading branch information
soorajvdefra and KiranVarma02 committed Apr 10, 2024
1 parent 06dbab3 commit 70f1dd6
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ describe(url, () => {
})

it('should show correct land ownership proofs', async () => {
redisMap.set(constants.redisKeys.TEMP_LAND_OWNERSHIP_PROOF, {
fileName: 'file-1.doc',
fileLocation: '800376c7-8652-4906-8848-70a774578dfe/land-ownership/file-1.doc',
fileSize: 0.01,
fileType: 'application/msword',
id: '1'
})
const request = {
yar: redisMap,
query: { id: '1' }
Expand Down Expand Up @@ -131,7 +138,14 @@ describe(url, () => {
session.set(constants.redisKeys.LAND_OWNERSHIP_LOCATION, 'test/test.doc')
session.set(constants.redisKeys.LAND_OWNERSHIP_FILE_SIZE, '2.5')
session.set(constants.redisKeys.LAND_OWNERSHIP_PROOFS, [])

session.set(constants.redisKeys.TEMP_LAND_OWNERSHIP_PROOF, {
fileName: 'file-1.doc',
fileLocation: '800376c7-8652-4906-8848-70a774578dfe/land-ownership/file-1.doc',
fileSize: 0.01,
fileType: 'application/msword',
id: '1',
confirmed: false
})
let viewArgs = ''
let redirectArgs = ''
const h = {
Expand All @@ -142,12 +156,15 @@ describe(url, () => {
redirectArgs = args
}
}

const payload = {
checkLandOwnership: 'yes'
}

await postHandler({ yar: session, payload }, h)
const request = {
yar: session,
query: { id: '1' },
payload
}
await postHandler(request, h)
expect(viewArgs).toEqual('')
expect(redirectArgs).toEqual([constants.routes.LAND_OWNERSHIP_PROOF_LIST])
done()
Expand All @@ -165,7 +182,6 @@ describe(url, () => {
session.set(constants.redisKeys.ROLE_KEY, 'Landowner')
session.set(constants.redisKeys.LAND_OWNERSHIP_LOCATION, 'test/test.doc')
session.set(constants.redisKeys.LAND_OWNERSHIP_FILE_SIZE, '2.5')

let viewArgs = ''
let redirectArgs = ''
const h = {
Expand All @@ -180,8 +196,12 @@ describe(url, () => {
const payload = {
checkLandOwnership: 'yes'
}

await postHandler({ yar: session, payload }, h)
const request = {
yar: session,
query: { id: '1' },
payload
}
await postHandler(request, h)
expect(viewArgs).toEqual('')
expect(redirectArgs).toEqual([constants.routes.CHECK_AND_SUBMIT])
done()
Expand All @@ -198,7 +218,21 @@ describe(url, () => {
session.set(constants.redisKeys.ROLE_KEY, 'Other')
session.set(constants.redisKeys.LAND_OWNERSHIP_LOCATION, 'test/test.doc')
session.set(constants.redisKeys.LAND_OWNERSHIP_FILE_SIZE, '2.5')

session.set(constants.redisKeys.TEMP_LAND_OWNERSHIP_PROOF, {
fileName: 'file-3.doc',
fileLocation: '800376c7-8652-4906-8848-70a774578dfe/land-ownership/file-1.doc',
fileSize: 0.01,
fileType: 'application/msword',
id: '1',
confirmed: false
})
session.set(constants.redisKeys.LAND_OWNERSHIP_PROOFS, [{
fileName: 'file-3.doc',
fileLocation: '800376c7-8652-4906-8848-70a774578dfe/land-ownership/file-1.doc',
fileSize: 0.01,
fileType: 'application/msword',
id: '1'
}])
let viewArgs = ''
let redirectArgs = ''
const h = {
Expand All @@ -213,8 +247,12 @@ describe(url, () => {
const payload = {
checkLandOwnership: 'yes'
}

await postHandler({ yar: session, payload }, h)
const request = {
yar: session,
query: { id: '1' },
payload
}
await postHandler(request, h)
expect(viewArgs).toEqual('')
expect(redirectArgs).toEqual([constants.routes.LAND_OWNERSHIP_PROOF_LIST])
done()
Expand Down Expand Up @@ -247,8 +285,12 @@ describe(url, () => {
const payload = {
checkLandOwnership: 'yes'
}

await postHandler({ yar: session, payload }, h)
const request = {
yar: session,
query: { id: '1' },
payload
}
await postHandler(request, h)
expect(viewArgs).toEqual('')
expect(redirectArgs).toEqual([constants.routes.CHECK_AND_SUBMIT])
done()
Expand Down Expand Up @@ -281,8 +323,12 @@ describe(url, () => {
const payload = {
checkLandOwnership: 'yes'
}

await postHandler({ yar: session, payload }, h)
const request = {
yar: session,
query: { id: '1' },
payload
}
await postHandler(request, h)
expect(viewArgs).toEqual('')
expect(redirectArgs).toEqual([constants.routes.LAND_OWNERSHIP_PROOF_LIST])
done()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ describe('Proof of ownership upload controller tests', () => {
fileType: 'application/pdf',
id: '1'
}]
uploadConfig.sessionData[`${constants.redisKeys.TEMP_LAND_OWNERSHIP_PROOF}`] = [{
fileName: 'file-1.doc',
fileLocation: '800376c7-8652-4906-8848-70a774578dfe/land-ownership/file-1.doc',
fileSize: 0.01,
fileType: 'application/msword',
id: '1',
confirmed: false
}]
await uploadFile(uploadConfig)
setImmediate(() => {
done()
Expand Down
53 changes: 30 additions & 23 deletions packages/webapp/src/routes/land/check-ownership-proof-file.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import constants from '../../utils/constants.js'
import path from 'path'
import { getHumanReadableFileSize, processRegistrationTask } from '../../utils/helpers.js'
import { deleteBlobFromContainers } from '../../utils/azure-storage.js'
import path from 'path'

const handlers = {
get: async (request, h) => {
Expand All @@ -20,17 +20,30 @@ const handlers = {
},
post: async (request, h) => {
const checkLandOwnership = request.payload.checkLandOwnership
const { id } = request.query
const context = getContext(request)
request.yar.set(constants.redisKeys.LAND_OWNERSHIP_CHECKED, checkLandOwnership)

if (checkLandOwnership === 'no') {
const { id } = request.query
const lopFiles = request.yar.get(constants.redisKeys.LAND_OWNERSHIP_PROOFS) || []
await deleteBlobFromContainers(context.fileLocation)
const updatedLopFiles = lopFiles.filter(item => item.id !== id)
request.yar.set(constants.redisKeys.LAND_OWNERSHIP_PROOFS, updatedLopFiles)
return h.redirect(constants.routes.UPLOAD_LAND_OWNERSHIP)
} else if (checkLandOwnership === 'yes') {
const tempFile = request.yar.get(constants.redisKeys.TEMP_LAND_OWNERSHIP_PROOF)
if (tempFile && tempFile.id === id) {
const lopFiles = request.yar.get(constants.redisKeys.LAND_OWNERSHIP_PROOFS) || []
const duplicateIndex = lopFiles.findIndex(file => path.basename(file.fileLocation) === path.basename(tempFile.fileLocation))
tempFile.confirmed = true
if (duplicateIndex === -1) {
tempFile.confirmed = true
const { confirmed, ...fileToAdd } = tempFile
lopFiles.push(fileToAdd)
request.yar.set(constants.redisKeys.LAND_OWNERSHIP_PROOFS, lopFiles)
}
request.yar.set(constants.redisKeys.LAND_OWNERSHIP_PROOFS, lopFiles)
request.yar.clear(constants.redisKeys.TEMP_LAND_OWNERSHIP_PROOF)
}
return h.redirect(request.yar.get(constants.redisKeys.REFERER, true) || constants.routes.LAND_OWNERSHIP_PROOF_LIST)
} else {
context.err = [{
Expand All @@ -43,28 +56,22 @@ const handlers = {
}

const getContext = request => {
const id = request.query?.id
let lopFile
let fileLocation = ''
let fileName = ''
let fileSize = null
let humanReadableFileSize = ''
const lopFiles = request.yar.get(constants.redisKeys.LAND_OWNERSHIP_PROOFS)
if (id) {
lopFile = lopFiles.find(item => item.id === id)
if (lopFile) {
fileLocation = lopFile.fileLocation
fileName = fileLocation === null ? '' : path.parse(fileLocation).base
fileSize = lopFile.fileSize
humanReadableFileSize = getHumanReadableFileSize(fileSize)
}
const tempFile = request.yar.get(constants.redisKeys.TEMP_LAND_OWNERSHIP_PROOF)
let fileDetails = {
fileName: '',
fileSize: '',
fileLocation: '',
fileId: ''
}
return {
fileName,
fileSize: humanReadableFileSize,
fileLocation,
fileId: id
if (tempFile) {
fileDetails = {
fileName: tempFile.fileName || '',
fileSize: getHumanReadableFileSize(tempFile.fileSize),
fileLocation: tempFile.fileLocation || '',
fileId: tempFile.id || ''
}
}
return fileDetails
}

export default [{
Expand Down
34 changes: 16 additions & 18 deletions packages/webapp/src/routes/land/upload-ownership-proof.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
import { buildConfig } from '../../utils/build-upload-config.js'
import constants from '../../utils/constants.js'
import { uploadFile } from '../../utils/upload.js'
import { deleteBlobFromContainers } from '../../utils/azure-storage.js'
import { generateUniqueId, getMaximumFileSizeExceededView, processRegistrationTask } from '../../utils/helpers.js'
import { ThreatScreeningError, MalwareDetectedError } from '@defra/bng-errors-lib'
import path from 'path'

const LAND_OWNERSHIP_ID = '#landOwnership'

const processSuccessfulUpload = (result, request, h) => {
const lopFiles = request.yar.get(constants.redisKeys.LAND_OWNERSHIP_PROOFS) || []
const location = result.config.blobConfig.blobName
const fileName = path.parse(location).base
let id = lopFiles.length > 0 && lopFiles.find(file => path.basename(file.fileLocation) === path.basename(location))?.id
if (!id) {
id = generateUniqueId()
lopFiles.push({
fileName,
fileLocation: location,
fileSize: result.fileSize,
fileType: constants.uploadTypes.LAND_OWNERSHIP_UPLOAD_TYPE,
contentMediaType: result.fileType,
id
})
const processSuccessfulUpload = async (result, request, h) => {
const tempFile = request.yar.get(constants.redisKeys.TEMP_LAND_OWNERSHIP_PROOF)
if (tempFile && !tempFile.confirmed) {
await deleteBlobFromContainers(tempFile.fileLocation)
}
const tempFileDetails = {
id: generateUniqueId(),
fileName: path.parse(result.config.blobConfig.blobName).base,
fileLocation: result.config.blobConfig.blobName,
fileSize: result.fileSize,
fileType: constants.uploadTypes.LAND_OWNERSHIP_UPLOAD_TYPE,
contentMediaType: result.fileType,
confirmed: false
}
request.logger.info(`${new Date().toUTCString()} Received land ownership data for ${location.substring(location.lastIndexOf('/') + 1)}`)
request.yar.set(constants.redisKeys.LAND_OWNERSHIP_PROOFS, lopFiles)
return h.redirect(`${constants.routes.CHECK_PROOF_OF_OWNERSHIP}?id=${id}`)
request.yar.set(constants.redisKeys.TEMP_LAND_OWNERSHIP_PROOF, tempFileDetails)
return h.redirect(`${constants.routes.CHECK_PROOF_OF_OWNERSHIP}?id=${tempFileDetails.id}`)
}

const processErrorUpload = (err, h) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/webapp/src/utils/loj-constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const GEOSPATIAL_FILE_NAME = 'geospatial_filename'
const GEOSPATIAL_FILE_SIZE = 'geospatial-file-size'
const LOCAL_LAND_CHARGE_FILE_SIZE = 'local-land-charge-file-size'
const HABITAT_PLAN_FILE_SIZE = 'habitat-plan-file-size'
const TEMP_LAND_OWNERSHIP_PROOF = 'temp_land_ownership_proof'
const HABITAT_PLAN_LEGAL_AGREEMENT_DOCUMENT_INCLUDED_YES_NO = 'habitat-plan-legal-agreement-document-included-yes-no'
const REPROJECTED_GEOSPATIAL_FILE_SIZE = 'reprojected-geospatial-file-size'
const GEOSPATIAL_FILE_TYPE = 'geospatial-file-type'
Expand Down Expand Up @@ -206,6 +207,7 @@ export default {
REPROJECTED_GEOSPATIAL_FILE_SIZE,
LOCAL_LAND_CHARGE_FILE_SIZE,
HABITAT_PLAN_FILE_SIZE,
TEMP_LAND_OWNERSHIP_PROOF,
HABITAT_PLAN_LEGAL_AGREEMENT_DOCUMENT_INCLUDED_YES_NO,
GEOSPATIAL_FILE_TYPE,
GEOSPATIAL_HECTARES,
Expand Down

0 comments on commit 70f1dd6

Please sign in to comment.