59 lines
1.8 KiB
JavaScript
59 lines
1.8 KiB
JavaScript
|
|
const express = require('express');
|
||
|
|
const router = express.Router();
|
||
|
|
const fs = require('fs');
|
||
|
|
const path = require('path');
|
||
|
|
const { getRecentLogs } = require('../db/audit');
|
||
|
|
const { getAllUsers } = require('../services/googleAdmin');
|
||
|
|
|
||
|
|
router.get('/logs', (req, res) => {
|
||
|
|
const logs = getRecentLogs(parseInt(req.query.limit) || 200);
|
||
|
|
res.json(logs);
|
||
|
|
});
|
||
|
|
|
||
|
|
router.get('/users', async (req, res) => {
|
||
|
|
try {
|
||
|
|
const users = await getAllUsers();
|
||
|
|
res.json(users);
|
||
|
|
} catch (err) {
|
||
|
|
res.status(500).json({ error: err.message });
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
router.get('/template', (req, res) => {
|
||
|
|
const templatePath = path.join(__dirname, '../../templates/default.hbs');
|
||
|
|
const content = fs.readFileSync(templatePath, 'utf8');
|
||
|
|
res.json({ content });
|
||
|
|
});
|
||
|
|
|
||
|
|
router.post('/template', (req, res) => {
|
||
|
|
const { content } = req.body;
|
||
|
|
if (!content) return res.status(400).json({ error: 'content required' });
|
||
|
|
const templatePath = path.join(__dirname, '../../templates/default.hbs');
|
||
|
|
fs.writeFileSync(templatePath + '.bak', fs.readFileSync(templatePath));
|
||
|
|
fs.writeFileSync(templatePath, content);
|
||
|
|
res.json({ ok: true });
|
||
|
|
});
|
||
|
|
|
||
|
|
router.post('/preview', (req, res) => {
|
||
|
|
const { templateHtml, userData } = req.body;
|
||
|
|
try {
|
||
|
|
const Handlebars = require('handlebars');
|
||
|
|
Handlebars.registerHelper('if_val', function(val, options) {
|
||
|
|
return val && val.trim() !== '' ? options.fn(this) : options.inverse(this);
|
||
|
|
});
|
||
|
|
const template = Handlebars.compile(templateHtml);
|
||
|
|
const rendered = template(userData || {
|
||
|
|
fullName: 'Jason Stedwell',
|
||
|
|
title: 'Director of Technical Services',
|
||
|
|
email: 'jstedwell@messagepointmedia.com',
|
||
|
|
phone: '334-707-2550',
|
||
|
|
cellPhone: ''
|
||
|
|
});
|
||
|
|
res.json({ html: rendered });
|
||
|
|
} catch (err) {
|
||
|
|
res.status(400).json({ error: err.message });
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
module.exports = router;
|