Initial
This commit is contained in:
83
src/formHandler.ts
Normal file
83
src/formHandler.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import nodemailer from 'nodemailer';
|
||||
import type { Transporter, SendMailOptions } from 'nodemailer';
|
||||
|
||||
interface Fault {
|
||||
coach: string;
|
||||
zone: string;
|
||||
}
|
||||
|
||||
interface Report {
|
||||
unitNumber: string;
|
||||
reported: string;
|
||||
comments: string;
|
||||
utcTimestamp: string;
|
||||
faults: Fault[];
|
||||
}
|
||||
|
||||
export async function handleFormData(data) {
|
||||
// Uploads form data to database and emails
|
||||
// to defined subscribers.
|
||||
|
||||
const knownKeys = ['unitNumber', 'reported', 'comments', 'utcTimestamp'];
|
||||
|
||||
const faults: Fault[] = [];
|
||||
|
||||
const report: Report = {
|
||||
unitNumber: data.unitNumber,
|
||||
reported: data.reported,
|
||||
comments: data.comments || '',
|
||||
utcTimestamp: data.utcTimestamp,
|
||||
faults: []
|
||||
};
|
||||
|
||||
for (const key in data) {
|
||||
if (!knownKeys.includes(key) && data[key] === 'on') {
|
||||
const match = key.match(/^(.+)-(.+)$/);
|
||||
if (match) {
|
||||
const [_, zone, coach] = match;
|
||||
report.faults.push({ coach, zone });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
submit(report);
|
||||
sendMail(report);
|
||||
}
|
||||
|
||||
async function submit(report: Report): Promise<void> {
|
||||
console.log(report);
|
||||
// Send to database
|
||||
// Send to email
|
||||
}
|
||||
|
||||
async function sendMail(report: Report): Promise<void> {
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: process.env.SMTP_HOST,
|
||||
port: parseInt(process.env.SMTP_PORT || '587'),
|
||||
secure: true,
|
||||
auth: {
|
||||
user: process.env.SMTP_USER,
|
||||
pass: process.env.SMTP_PASS,
|
||||
}
|
||||
});
|
||||
|
||||
const faultList = report.faults.map(f => `- ${f.zone} (${f.coach})`).join('\n');
|
||||
|
||||
const mailOptions: SendMailOptions = {
|
||||
from: `"AC Reporter ${process.env.SMTP_USER}"`,
|
||||
to: process.env.MAIL_RECIPIENTS,
|
||||
subject: `New A/C Report: ${report.unitNumber}`,
|
||||
text: `A report has been submitted for unit ${report.unitNumber}.\n\n` +
|
||||
`Timestamp: ${report.utcTimestamp}\n\n` +
|
||||
`Faults:\n${faultList}\n\n` +
|
||||
`Reported to Maintenance: ${report.reported}` +
|
||||
`Comments:\n${report.comments} || 'None'`
|
||||
};
|
||||
|
||||
try {
|
||||
const info = await transporter.sendMail(mailOptions);
|
||||
console.log('📧 Report sent:', info.messageId);
|
||||
} catch (err) {
|
||||
console.error('❌ Failed to send email:', err);
|
||||
}
|
||||
}
|
||||
27
src/index.ts
Normal file
27
src/index.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import express, { Request, Response } from "express";
|
||||
import { handleFormData } from "./formHandler";
|
||||
|
||||
const app = express();
|
||||
const port = process.env.port || 3000;
|
||||
|
||||
app.use(express.static('static'));
|
||||
app.use(express.json());
|
||||
|
||||
app.get("/test", (req: Request, res: Response) => {
|
||||
res.json({message: "TrACreport is running"});
|
||||
});
|
||||
|
||||
app.post('/submit', (req, res) => {
|
||||
const report = req.body;
|
||||
|
||||
// For now, just log it
|
||||
console.log('📥 New form submission:');
|
||||
|
||||
// TODO: Validate and write to MongoDB later
|
||||
handleFormData(report);
|
||||
res.status(200).json({ status: 'ok', message: 'Form received' });
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log("Server running on port:", port);
|
||||
});
|
||||
Reference in New Issue
Block a user