diff --git a/README.md b/README.md index 700ef08..22c06cb 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Release v0.1 - [ ] Edit - [x] Delete - [x] Upload document -- [ ] Download document +- [x] Download document - [ ] Date filter ### Transaction label - [ ] Create diff --git a/app/src/lib/components/ui/data-table/transactions/data-table-row-actions.svelte b/app/src/lib/components/ui/data-table/transactions/data-table-row-actions.svelte index 5fed292..97f3882 100644 --- a/app/src/lib/components/ui/data-table/transactions/data-table-row-actions.svelte +++ b/app/src/lib/components/ui/data-table/transactions/data-table-row-actions.svelte @@ -7,6 +7,30 @@ export let row: Transactions; const transaction = transactionSchema.parse(row); + + async function openDocument(e: Event) { + e.preventDefault(); + const formData = new FormData(); + formData.append('download', transaction.id); + + try { + const response = await fetch('?/downloadDocument', { + method: 'POST', + body: formData + }); + const result = await response.json(); + const dataObject = JSON.parse(result.data); + const url = dataObject[3]; + + if (url) { + window.open(url, '_blank'); + } else { + console.error('URL not found in the response:', result); + } + } catch (error) { + console.error('Failed to fetch:', error); + } + } @@ -22,7 +46,12 @@ Edit - Download +
+ + +
Labels diff --git a/app/src/routes/(app)/admin/transactions/+page.server.ts b/app/src/routes/(app)/admin/transactions/+page.server.ts index 48b5201..471027d 100644 --- a/app/src/routes/(app)/admin/transactions/+page.server.ts +++ b/app/src/routes/(app)/admin/transactions/+page.server.ts @@ -76,4 +76,31 @@ export const actions: Actions = { } throw redirect(303, "/admin/transactions"); }, + downloadDocument: async ({ request, locals }) => { + const formData = await request.formData(); + const transactionId = formData.get('download'); + + if (typeof transactionId !== 'string' || !transactionId) { + return fail(400, { error: 'A valid transaction ID is required.' }); + } + + try { + // Check if authStore.model is not null + if (!locals.pb.authStore.model) { + console.log('No authenticated user found.'); + return fail(401, { message: 'No authenticated user found.' }); + } + + const fileToken = await locals.pb.files.getToken(); + const record = await locals.pb.collection('transactions').getOne(transactionId); + const url = locals.pb.files.getUrl(record, record.document, { 'token': fileToken }); + + return { + status: 200, + body: { url } + }; + } catch (error) { + console.error("Failed to download the transaction", error); + } + } } \ No newline at end of file diff --git a/backend/pb_migrations/1713140419_updated_transactions.js b/backend/pb_migrations/1713140419_updated_transactions.js new file mode 100644 index 0000000..06797a3 --- /dev/null +++ b/backend/pb_migrations/1713140419_updated_transactions.js @@ -0,0 +1,18 @@ +/// +migrate((db) => { + const dao = new Dao(db) + const collection = dao.findCollectionByNameOrId("rybgkg359i2537w") + + collection.viewRule = "" + collection.updateRule = "" + + return dao.saveCollection(collection) +}, (db) => { + const dao = new Dao(db) + const collection = dao.findCollectionByNameOrId("rybgkg359i2537w") + + collection.viewRule = null + collection.updateRule = null + + return dao.saveCollection(collection) +})