Add fetch endpoint
This commit is contained in:
parent
7ac03cb1fd
commit
2a6d6e0eb5
@ -45,6 +45,16 @@ export async function handleFormData(data: any) {
|
|||||||
sendToDatabase(report);
|
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> {
|
async function sendMail(report: Report): Promise<void> {
|
||||||
const transporter = nodemailer.createTransport({
|
const transporter = nodemailer.createTransport({
|
||||||
host: process.env.SMTP_HOST,
|
host: process.env.SMTP_HOST,
|
||||||
|
11
src/index.ts
11
src/index.ts
@ -1,5 +1,5 @@
|
|||||||
import express, { Request, Response } from "express";
|
import express, { Request, Response } from "express";
|
||||||
import { handleFormData } from "./formHandler.js";
|
import { handleFormData, fetchReports } from "./formHandler.js";
|
||||||
import { initDb } from "./database.js";
|
import { initDb } from "./database.js";
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
@ -20,6 +20,15 @@ app.post('/submit', (req, res) => {
|
|||||||
res.status(200).json({ status: 'ok', message: 'Form received' });
|
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, () => {
|
app.listen(port, () => {
|
||||||
console.log("Server running on port:", port);
|
console.log("Server running on port:", port);
|
||||||
});
|
});
|
Loading…
x
Reference in New Issue
Block a user