From a715881c9b9364f03ecc7ef47285eed792b95420 Mon Sep 17 00:00:00 2001 From: flutistar Date: Mon, 13 Jan 2025 07:57:33 -0800 Subject: [PATCH] updated file download function --- .../HomeJurisdictionInformation.vue | 19 +---- auth-web/src/services/business.services.ts | 77 +++++-------------- .../HomeJurisdictionInformation.spec.ts | 5 +- 3 files changed, 25 insertions(+), 76 deletions(-) diff --git a/auth-web/src/components/auth/staff/continuation-application/HomeJurisdictionInformation.vue b/auth-web/src/components/auth/staff/continuation-application/HomeJurisdictionInformation.vue index 853e18d80..bcafbc707 100644 --- a/auth-web/src/components/auth/staff/continuation-application/HomeJurisdictionInformation.vue +++ b/auth-web/src/components/auth/staff/continuation-application/HomeJurisdictionInformation.vue @@ -315,27 +315,14 @@ export default defineComponent({ state.isDownloading = true const documentClass = 'CORP' - try { - const docUrl: string = await BusinessService.getDownloadUrl(documentKey, documentClass) - const link = document.createElement('a') - link.href = docUrl - link.download = documentName - link.target = '_blank' // This opens the link in a new browser tab - - // Append to the document and trigger the download - document.body.appendChild(link) - link.click() - - // Remove the link after the download is triggered - document.body.removeChild(link) - state.isDownloading = false - } catch (error) { + await BusinessService.downloadDocument(documentKey, documentName, documentClass).catch(error => { // eslint-disable-next-line no-console console.log('downloadDocument() error =', error) state.dialogTitle = 'Unable to download document' state.dialogText = 'An error occurred while downloading the document. Please try again.' const v = errorDialogComponent.value as any; v.open() - } + }) + state.isDownloading = false } /** diff --git a/auth-web/src/services/business.services.ts b/auth-web/src/services/business.services.ts index 258063f61..f4c0d1813 100644 --- a/auth-web/src/services/business.services.ts +++ b/auth-web/src/services/business.services.ts @@ -107,7 +107,7 @@ export default class BusinessService { static async fetchFiling (url: string): Promise { // safety check if (!url) throw new Error('Invalid parameters') - + return axios.get(url) .then(response => { const filing = response?.data?.filing @@ -158,73 +158,34 @@ export default class BusinessService { return axios.post(url, data) } - /** - * Downloads a Minio document from Legal API and prompts browser to open/save it. - * @param documentKey the document key - * @param documentName the document filename - * @returns a promise to return the axios response or the error response - * @see CommonUtils.fileDownload() for a similar method - */ - static async downloadDocument (documentKey: string, documentName: string): Promise { - // safety checks - if (!documentKey || !documentName) throw new Error('Invalid parameters') - - const url = `${ConfigHelper.getLegalAPIV2Url()}/documents/${documentKey}` - const config = { - headers: { 'Accept': 'application/pdf' }, - responseType: 'blob' as 'json' - } - - return axios.get(url, config).then(response => { - if (!response) throw new Error('Null response') - - /* solution below is from https://github.com/axios/axios/issues/1392 */ - - // it is necessary to create a new blob object with mime-type explicitly set - // otherwise only Chrome works like it should - const blob = new Blob([response.data], { type: 'application/pdf' }) - - // use Navigator.msSaveOrOpenBlob if available (possibly IE) - // warning: this is now deprecated - // ref: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/msSaveOrOpenBlob - if (window.navigator && window.navigator['msSaveOrOpenBlob']) { - window.navigator['msSaveOrOpenBlob'](blob, documentName) - } else { - // for other browsers, create a link pointing to the ObjectURL containing the blob - const url = window.URL.createObjectURL(blob) - const a = window.document.createElement('a') - window.document.body.appendChild(a) - a.setAttribute('style', 'display: none') - a.href = url - a.download = documentName - a.click() - window.URL.revokeObjectURL(url) - a.remove() - } - - return response - }) - } - /** * Get download url from Document Record Service * @param documentServiceId the unique id on Document Record Service + * @param documentName the file name to download. * @param documentClass the document class defined for the document service. e.g. 'CORP' - * @returns a promise to return the string of file download. + * @returns void */ - static async getDownloadUrl (documentKey: string, documentClass: string): Promise { + static async downloadDocument (documentServiceId: string, documentName: string, documentClass: string): Promise { // safety checks - if (!documentKey || !documentClass) throw new Error('Invalid parameters') + if (!documentServiceId || !documentClass) throw new Error('Invalid parameters') - const url = `${ConfigHelper.getLegalAPIV2Url()}/documents/drs/${documentClass}/${documentKey}` + const url = `${ConfigHelper.getLegalAPIV2Url()}/documents/drs/${documentClass}/${documentServiceId}` - return axios.get(url).then(response => { + axios.get(url).then(response => { if (!response) throw new Error('Null response') - return response.data.documentURL - }).catch(error => { - console.log(error) - return '' + const docUrl: string = response.data.documentURL + const link = document.createElement('a') + link.href = docUrl + link.download = documentName + link.target = '_blank' // This opens the link in a new browser tab + + // Append to the document and trigger the download + document.body.appendChild(link) + link.click() + + // Remove the link after the download is triggered + document.body.removeChild(link) }) } diff --git a/auth-web/tests/unit/components/HomeJurisdictionInformation.spec.ts b/auth-web/tests/unit/components/HomeJurisdictionInformation.spec.ts index 8a86293ab..b33d77496 100644 --- a/auth-web/tests/unit/components/HomeJurisdictionInformation.spec.ts +++ b/auth-web/tests/unit/components/HomeJurisdictionInformation.spec.ts @@ -120,12 +120,13 @@ describe('HomeJurisdictionInformation component', () => { }) it('rendered a functional authorization download button', () => { - BusinessService.getDownloadUrl = vi.fn().mockResolvedValue(null) + BusinessService.downloadDocument = vi.fn().mockResolvedValue(null) const button = wrapper.findAll('section').at(5).find('.download-authorization-btn') button.trigger('click') - expect(BusinessService.getDownloadUrl).toHaveBeenCalledWith( + expect(BusinessService.downloadDocument).toHaveBeenCalledWith( 'DS01000000', + 'My Authorization Document.pdf', documentClass )