diff --git a/.gitignore b/.gitignore index 3c3629e..37d7e73 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +.env diff --git a/src/database.ts b/src/database.ts new file mode 100644 index 0000000..b8c7c7f --- /dev/null +++ b/src/database.ts @@ -0,0 +1,30 @@ +import mongoose from "mongoose"; + +let isConnected = false; + +export async function initDb(): Promise { + if (isConnected || mongoose.connection.readyState >= 1) return; + + const { + MONGO_HOST = 'localhost', + MONGO_USER, + MONGO_PASS, + MONGO_NAME = 'tracreport', + MONGO_OPTS = '', + } = process.env + + const credentials = MONGO_USER && MONGO_PASS + ? `${encodeURIComponent(MONGO_USER)}:${encodeURIComponent(MONGO_PASS)}@` + : ''; + + const MONGO_URI = `mongodb://${credentials}${MONGO_HOST}/${MONGO_NAME}${MONGO_OPTS ? '?' + MONGO_OPTS : ''}`; + + try { + await mongoose.connect(MONGO_URI); + isConnected = true; + console.log('✅ MongoDB connected'); + } catch (err) { + console.error('❌ MongoDB connection error:', err) + process.exit(1); + } +} diff --git a/src/formHandler.ts b/src/formHandler.ts index 4a4b90b..fe6db97 100644 --- a/src/formHandler.ts +++ b/src/formHandler.ts @@ -1,12 +1,13 @@ import nodemailer from 'nodemailer'; import type { Transporter, SendMailOptions } from 'nodemailer'; +import { Report, Report as ReportSchema } from './models/report'; -interface Fault { +export interface Fault { coach: string; zone: string; } -interface Report { +export interface Report { unitNumber: string; reported: string; comments: string; @@ -40,21 +41,15 @@ export async function handleFormData(data) { } } - submit(report); sendMail(report); -} - -async function submit(report: Report): Promise { - console.log(report); - // Send to database - // Send to email + sendToDatabase(report); } async function sendMail(report: Report): Promise { const transporter = nodemailer.createTransport({ host: process.env.SMTP_HOST, port: parseInt(process.env.SMTP_PORT || '587'), - secure: true, + secure: false, auth: { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS, @@ -70,8 +65,8 @@ async function sendMail(report: Report): Promise { 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'` + `Reported to Maintenance: ${report.reported}\n\n` + + `Comments:\n${report.comments || 'None'}` }; try { @@ -80,4 +75,18 @@ async function sendMail(report: Report): Promise { } catch (err) { console.error('❌ Failed to send email:', err); } +} + +async function sendToDatabase(report: Report) { + const newReport = new ReportSchema({ + unitNumber: report.unitNumber, + reported: report.reported, + comments: report.comments, + utcTimestamp: report.utcTimestamp, + faults: report.faults + }); + + await newReport.save(); + + console.log("Report saved to Database"); } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 1f7b16a..21ea8a8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,12 @@ import express, { Request, Response } from "express"; import { handleFormData } from "./formHandler"; +import { initDb } from "./database"; const app = express(); const port = process.env.port || 3000; +initDb(); + app.use(express.static('static')); app.use(express.json()); diff --git a/src/models/report.ts b/src/models/report.ts new file mode 100644 index 0000000..44dd27c --- /dev/null +++ b/src/models/report.ts @@ -0,0 +1,19 @@ +import { Schema, model, Document } from "mongoose"; +import type { Report as ReportType, Fault as FaultType } from "../formHandler"; + +interface ReportDocument extends ReportType, Document {} + +const FaultSchema = new Schema({ + coach: { type: String, required: true }, + zone: { type: String, required: true }, +}, { _id: false }); // Prevents Mongoose from auto-generating _id for each fault + +const ReportSchema = new Schema({ + unitNumber: { type: String, required: true }, + reported: { type: String, required: true }, + comments: { type: String, required: true }, + utcTimestamp: { type: String, required: true }, + faults: { type: [FaultSchema], required: true }, +}); + +export const Report = model('Report', ReportSchema); \ No newline at end of file