From 8fd778fffd699f6271952e864073475fcc7dd9e9 Mon Sep 17 00:00:00 2001 From: flutistar Date: Fri, 10 Jan 2025 07:24:54 -0800 Subject: [PATCH] updated download component --- .../HomeJurisdictionInformation.vue | 20 ++++++++++++--- auth-web/src/services/business.services.ts | 25 ++++++++++++++++++- .../HomeJurisdictionInformation.spec.ts | 14 ++++++----- 3 files changed, 49 insertions(+), 10 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 b9bf3f9ff2..853e18d80e 100644 --- a/auth-web/src/components/auth/staff/continuation-application/HomeJurisdictionInformation.vue +++ b/auth-web/src/components/auth/staff/continuation-application/HomeJurisdictionInformation.vue @@ -314,14 +314,28 @@ export default defineComponent({ if (!documentKey || !documentName) return // safety check state.isDownloading = true - await BusinessService.downloadDocument(documentKey, documentName).catch(error => { + 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) { // 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 012567b805..907981c08b 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 @@ -206,6 +206,29 @@ export default class BusinessService { }) } + /** + * Downloads a document from Legal API and prompts browser to open/save it. + * @param documentServiceId the unique id on Document Record Service + * @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 getDownloadUrl (documentKey: string, documentClass: string): Promise { + // safety checks + if (!documentKey || !documentClass) throw new Error('Invalid parameters') + + const url = `${ConfigHelper.getLegalAPIV2Url()}/documents/drs/${documentClass}/${documentKey}` + + return axios.get(url).then(response => { + if (!response) throw new Error('Null response') + + return response.data.documentURL + }).catch(error => { + console.log(error) + return '' + }) + } + static async searchReviews (reviewFilter: ReviewFilterParams) { const params = new URLSearchParams() for (const key in reviewFilter) { diff --git a/auth-web/tests/unit/components/HomeJurisdictionInformation.spec.ts b/auth-web/tests/unit/components/HomeJurisdictionInformation.spec.ts index 88443e23ca..8a86293ab6 100644 --- a/auth-web/tests/unit/components/HomeJurisdictionInformation.spec.ts +++ b/auth-web/tests/unit/components/HomeJurisdictionInformation.spec.ts @@ -12,20 +12,20 @@ const localVue = createLocalVue() const vuetify = new Vuetify({}) const review = {} - +const documentClass = 'CORP' const filing = { continuationIn: { authorization: { date: '2024-07-01', files: [ { - fileKey: '0071dbd6-6095-46f6-b5e4-cc859b0ebf27.pdf', + fileKey: 'DS01000000', fileName: 'My Authorization Document.pdf' } ] }, foreignJurisdiction: { - affidavitFileKey: '007bd7bd-d421-49a9-9925-03ce561d044f.pdf', + affidavitFileKey: 'DS99999999', affidavitFileName: 'My Director Affidavit.pdf', country: 'CA', identifier: 'AB-5444', @@ -120,12 +120,14 @@ describe('HomeJurisdictionInformation component', () => { }) it('rendered a functional authorization download button', () => { - BusinessService.downloadDocument = vi.fn().mockResolvedValue(null) + BusinessService.getDownloadUrl = vi.fn().mockResolvedValue(null) const button = wrapper.findAll('section').at(5).find('.download-authorization-btn') button.trigger('click') - expect(BusinessService.downloadDocument).toHaveBeenCalledWith('0071dbd6-6095-46f6-b5e4-cc859b0ebf27.pdf', - 'My Authorization Document.pdf') + expect(BusinessService.getDownloadUrl).toHaveBeenCalledWith( + 'DS01000000', + documentClass + ) vi.clearAllMocks() })