Add fetch endpoint

This commit is contained in:
Fred Boniface 2025-07-09 14:22:32 +01:00
parent 7ac03cb1fd
commit 2a6d6e0eb5
2 changed files with 20 additions and 1 deletions

View File

@ -45,6 +45,16 @@ export async function handleFormData(data: any) {
sendToDatabase(report);
}
export async function fetchReports(): Promise<Report[]> {
try {
const reports = await ReportSchema.find();
return reports;
} catch (error) {
console.error("Error fetching reports:", error);
throw error;
}
}
async function sendMail(report: Report): Promise<void> {
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,

View File

@ -1,5 +1,5 @@
import express, { Request, Response } from "express";
import { handleFormData } from "./formHandler.js";
import { handleFormData, fetchReports } from "./formHandler.js";
import { initDb } from "./database.js";
const app = express();
@ -20,6 +20,15 @@ app.post('/submit', (req, res) => {
res.status(200).json({ status: 'ok', message: 'Form received' });
});
app.get('/fetch', async (req, res) => {
try {
const data = await fetchReports();
res.status(200).json(data);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch reports' });
}
});
app.listen(port, () => {
console.log("Server running on port:", port);
});