implemente attachemtn download route

This commit is contained in:
2024-01-05 12:39:22 +01:00
parent 8b7cd45087
commit eea2ca1492
6 changed files with 82 additions and 33 deletions

View File

@@ -46,13 +46,14 @@ const serializeAttachment = async (billAttachment: File | null) => {
return null;
}
// convert the file contents to a base64 string
// convert the billAttachment file contents to format that can be stored in the database
const fileContents = await billAttachment.arrayBuffer();
const fileContentsBase64 = Buffer.from(fileContents).toString('base64');
// create an object to store the file in the database
return({
fileName: decodeURIComponent(fileName),
fileName,
fileSize,
fileType,
fileLastModified,

31
app/lib/fetchBillById.ts Normal file
View File

@@ -0,0 +1,31 @@
import { PlainBill, MongoLocation } from '@/app/lib/db-types';
import clientPromise from '@/app/lib/mongodb';
export const fetchBillById = async (locationID:string, billID:string) => {
const client = await clientPromise;
const db = client.db("rezije");
// find a location with the given locationID
const billLocation = await db.collection<MongoLocation>("lokacije").findOne({ _id: locationID })
if(!billLocation) {
console.log(`Location ${locationID} not found`);
return(null);
}
// find a bill with the given billID
const mongoBill = billLocation?.bills.find(({ _id }) => _id.toString() === billID);
if(!mongoBill) {
console.log('Bill not found');
return(null);
}
const { _id, ...billBase } = mongoBill;
return({
id: _id.toString(),
...billBase
} as PlainBill);
}