custom-scripts/mccs-it/thermal/app.js
2024-09-12 13:42:43 -04:00

104 lines
3.2 KiB
JavaScript

const express = require('express');
const Thermal = require('./modules/Thermal.js');
const app = express();
const port = 80;
// Middleware to serve static files (HTML)
app.use(express.static('public'));
// Middleware to parse JSON request bodies
app.use(express.json());
// Print credentials
app.post('/print-credentials', async (req, res) => {
try {
const { username, studentId } = req.body;
if (!username || !studentId) {
return res.status(400).json({ message: 'Username and Student ID are required.' });
}
// Generate the information string
const infoString = Thermal.generateInfo.credentials(username, studentId);
// Generate the PDF, saved to disk
Thermal.generatePdf(infoString).then(res => {
// Print the document
Thermal.print().then(res => {
res.send('PDF sent to printer successfully.');
}).catch(e => {
console.error('Failed to start printing process:', e);
res.status(500).json({ message: 'Failed to start printing process', error: e.message });
});
}).catch(e => {
console.error(e);
res.status(500).json({ message: 'Error writing PDF file', error: e.message });
});
} catch (e) {
console.error('Print Error:', e);
res.status(500).json({ message: 'Print Error', error: e.message });
}
});
// Print temp password
app.post('/print-temp-pw', async (req, res) => {
try {
// Grab the password from the request body
const { password } = req.body;
// Check that the password is actually there
if (!password) {
return res.status(400).json({ message: 'Password is required.' });
}
// Generate the information string
const infoString = Thermal.generateInfo.tempPassword(password);
// Generate the PDF, saved to disk
Thermal.generatePdf(infoString).then(res => {
// Print the document
Thermal.print().then(res => {
res.send('PDF sent to printer successfully.');
}).catch(e => {
console.error('Failed to start printing process:', err);
res.status(500).json({ message: 'Failed to start printing process', error: err.message });
});
}).catch(e => {
console.error(e);
res.status(500).json({ message: 'Error writing PDF file', error: err.message });
});
} catch (error) {
console.error('Print Error:', error);
res.status(500).json({ message: 'Print Error', error: error.message });
}
});
// Print Student MaineCC Info
app.post('/print-mcc', async (req, res) => {
try {
// Generate the information string
const infoString = Thermal.generateInfo.maineCC();
// Generate the PDF, saved to disk
Thermal.generatePdf(infoString).then(res => {
// Print the document
Thermal.print().then(res => {
res.send('PDF sent to printer successfully.');
}).catch(e => {
console.error('Failed to start printing process:', err);
res.status(500).json({ message: 'Failed to start printing process', error: err.message });
});
}).catch(e => {
console.error(e);
res.status(500).json({ message: 'Error writing PDF file', error: err.message });
});
} catch (error) {
console.error('Print Error:', error);
res.status(500).json({ message: 'Print Error', error: error.message });
}
});
// Start the server
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});