Skip to content

Commit 63d7ae9

Browse files
Merge pull request #435 from bcgov/ofmcc-6773-fix-document-link
Create a way for business to be able to send attachments to clients
2 parents e999c08 + 5307b4e commit 63d7ae9

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

backend/src/components/files.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,24 @@ const HttpStatus = require('http-status-codes')
44

55
async function getFile(req, res) {
66
try {
7-
let operation = `msdyn_richtextfiles(${req.params.fileId})/`
8-
if (req.query.image) {
9-
operation += 'msdyn_imageblob/$value?size=full'
7+
let fileDataOperation = `msdyn_richtextfiles(${req.params.fileId})/`
8+
9+
if (req.query.image === 'true') {
10+
fileDataOperation += 'msdyn_imageblob/$value?size=full'
1011
} else {
11-
operation += 'msdyn_fileblob'
12+
fileDataOperation += 'msdyn_fileblob'
1213
}
13-
const response = await getOperation(operation)
14-
return res.status(HttpStatus.OK).json(response?.value)
14+
const fileDataResponse = await getOperation(fileDataOperation)
15+
16+
const fileDetailsOperation = `fileattachments?$filter=(_objectid_value eq ${req.params.fileId})`
17+
const fileDetailsResponse = await getOperation(fileDetailsOperation)
18+
const fileDetails = fileDetailsResponse?.value[0] || { mimetype: undefined, filename: undefined }
19+
20+
return res.status(HttpStatus.OK).json({
21+
fileData: fileDataResponse?.value,
22+
mimetype: fileDetails.mimetype,
23+
filename: fileDetails.filename,
24+
})
1525
} catch (e) {
1626
handleError(res, e)
1727
}

frontend/src/components/messages/RequestConversations.vue

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
<span class="font-weight-bold">Sent:</span>
6868
{{ format.formatDate(item.sentDate) }}
6969
</div>
70-
<div class="pt-1" v-html="item.message" />
70+
<div class="pt-1" @click.capture="handlePdf" v-html="item.message" />
7171
</v-col>
7272
</v-row>
7373
</template>
@@ -209,6 +209,16 @@ export default {
209209
deriveFromDisplay(item) {
210210
return item.ofmSourceSystem ? `${this.userInfo?.firstName ?? ''} ${this.userInfo?.lastName}` : OFM_PROGRAM
211211
},
212+
handlePdf(event) {
213+
if (event.target.classList.contains('conversation-pdf')) {
214+
fetch(event.target.dataset.link)
215+
.then((res) => res.blob())
216+
.then((blob) => {
217+
const blobUrl = URL.createObjectURL(blob)
218+
window.open(blobUrl)
219+
})
220+
}
221+
},
212222
async formatConversation() {
213223
const parser = new DOMParser()
214224
for (const conversation of this.assistanceRequestConversation) {
@@ -218,15 +228,36 @@ export default {
218228
const matches = ID_REGEX.exec(img.getAttribute('src'))
219229
if (matches) {
220230
const fileId = matches[1]
221-
const image = await FileService.getFile(fileId)
222-
const imageType = deriveImageType(image)
223-
img.setAttribute('src', `data:image/${imageType};base64,${image}`)
231+
const imageFile = await FileService.getFile(fileId, true)
232+
const imageType = deriveImageType(imageFile.fileData)
233+
img.setAttribute('src', `data:image/${imageType};base64,${imageFile.fileData}`)
224234
} else {
225235
img.remove()
226236
}
227237
}
238+
228239
// Remove CRM links for now until we can support them
229-
document.querySelectorAll('a[href*="/api/data"]').forEach((link) => link.remove())
240+
const fileLinks = document.querySelectorAll('a[href*="/api/data"]')
241+
242+
for (const fileLink of fileLinks) {
243+
const matches = ID_REGEX.exec(fileLink.dataset.value)
244+
if (matches) {
245+
const fileId = matches[1]
246+
const file = await FileService.getFile(fileId)
247+
const dataLink = `data:${file.mimetype};base64,${file.fileData}`
248+
249+
if (file.mimetype === 'application/pdf') {
250+
fileLink.setAttribute('href', '#')
251+
fileLink.classList.add('conversation-pdf')
252+
fileLink.setAttribute('data-link', dataLink)
253+
} else {
254+
fileLink.setAttribute('href', dataLink)
255+
fileLink.setAttribute('download', file.filename)
256+
}
257+
} else {
258+
fileLink.remove()
259+
}
260+
}
230261
231262
// Update the message content
232263
conversation.message = document.documentElement.innerHTML

0 commit comments

Comments
 (0)