Skip to content

Commit

Permalink
updated download component
Browse files Browse the repository at this point in the history
  • Loading branch information
flutistar committed Jan 10, 2025
1 parent 220720e commit 8fd778f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
/**
Expand Down
25 changes: 24 additions & 1 deletion auth-web/src/services/business.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default class BusinessService {
static async fetchFiling (url: string): Promise<any> {
// safety check
if (!url) throw new Error('Invalid parameters')

return axios.get(url)
.then(response => {
const filing = response?.data?.filing
Expand Down Expand Up @@ -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<string> {
// 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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()
})
Expand Down

0 comments on commit 8fd778f

Please sign in to comment.