diff --git a/actions/azurestorage.js b/actions/azurestorage.js index e4cedfe4..ea104db1 100644 --- a/actions/azurestorage.js +++ b/actions/azurestorage.js @@ -361,7 +361,9 @@ export const createAppealPDFBlob = async ( console.log("blobName:", blobName); const blockBlobClient = containerClient.getBlockBlobClient(blobName); - const uploadBlobResponse = await blockBlobClient.uploadStream(content); + const uploadBlobResponse = Buffer.isBuffer(content) + ? await blockBlobClient.uploadData(content) + : await blockBlobClient.uploadStream(content); console.log(uploadBlobResponse); let whichLocation = "846040000"; // Default value diff --git a/actions/services/documentDirectService.js b/actions/services/documentDirectService.js index d867ce35..567ebc7b 100644 --- a/actions/services/documentDirectService.js +++ b/actions/services/documentDirectService.js @@ -265,20 +265,25 @@ export const generateAppealPDF = async ( containerID, casefolderID, appealType, - fileList + fileList, + options = {} ) => { Object.assign(formValues, { "filesList": fileList }); - var queryUrl = "/api/file/generateappealpdf?appealType=" + appealType; + var queryUrl = + "/api/file/generateappealpdf?appealType=" + + appealType + + (options.download ? "&download=true" : ""); const hashedUrl = await buildHashedQueryUrl(queryUrl); const config = { method: "post", url: hashedUrl, - data: formValues + data: formValues, + ...(options.download ? { responseType: "blob" } : {}) }; try { diff --git a/components/case/representation/representationCompleteSubmit.js b/components/case/representation/representationCompleteSubmit.js index f471dfca..fc819e03 100644 --- a/components/case/representation/representationCompleteSubmit.js +++ b/components/case/representation/representationCompleteSubmit.js @@ -292,7 +292,8 @@ const RepCompleteSubmit = (props) => { const pdfBlob = await generateRepPDF( repFormData, containerID, - caseRef + caseRef, + { download: true } ); if (pdfBlob) { diff --git a/components/newappeal/buildchecksection.js b/components/newappeal/buildchecksection.js index 5c30d0ff..6285306f 100644 --- a/components/newappeal/buildchecksection.js +++ b/components/newappeal/buildchecksection.js @@ -124,6 +124,8 @@ let BuildCheckSection = (props) => { const [confirmSections, setConfirmSections] = useState(false); const [finaliseAppealProcess, setFinaliseAppealProcess] = useState(false); + const [downloadAppealForm, setDownloadAppealForm] = useState(false); + const [downloadInProgress, setDownloadInProgress] = useState(false); const FieldsTranslations = (label) => { const router = useRouter(); @@ -183,6 +185,65 @@ let BuildCheckSection = (props) => { : setConfirmSections(false); }; + const triggerAppealDownload = async () => { + if (downloadInProgress) { + return; + } + + try { + setDownloadInProgress(true); + + const uniqueArray = pdfObj.filesList.filter( + (value, index, self) => + index === self.findIndex((t) => t.name === value.name) + ); + + pdfObj.filesList = uniqueArray; + + console.log(pdfObj); + + const containerID = + props.accountDetails?.containerID || + props.props?.accountDetails?.containerID || + props.props?.props?.accountDetails?.containerID; + const caseRef = + props?.currentView?.caseReference?.ticketnumber || + props?.appealType?.caseReference?.ticketnumber; + + if (!containerID || !caseRef) { + console.error( + "Appeal Form download skipped: missing containerID or case reference", + { containerID, caseRef } + ); + return; + } + + const pdfBlob = await generateAppealPDF( + pdfObj, + containerID, + caseRef, + router.query.appealtypes, + pdfObj.filesList, + { download: true } + ); + + if (pdfBlob) { + const blobUrl = window.URL.createObjectURL(pdfBlob); + const anchor = document.createElement("a"); + anchor.href = blobUrl; + anchor.download = `${"appealForm"}.pdf`; + document.body.appendChild(anchor); + anchor.click(); + anchor.remove(); + window.URL.revokeObjectURL(blobUrl); + } + } catch (error) { + console.error("Appeal Form download failed", error); + } finally { + setDownloadInProgress(false); + } + }; + let ShowDocLinks = getDocLink(docsOffline); return ( @@ -280,6 +341,30 @@ let BuildCheckSection = (props) => { <>