Add saved signature presets
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
+52
-1
@@ -5,7 +5,7 @@ const archiver = require('archiver');
|
||||
const PDFDocument = require('pdfkit');
|
||||
const QRCode = require('qrcode');
|
||||
const { pool, initializeDatabase } = require('./db');
|
||||
const { normalizeInvoiceData } = require('./invoice-data');
|
||||
const { isValidSignatureDataUrl, normalizeInvoiceData } = require('./invoice-data');
|
||||
|
||||
const app = express();
|
||||
const sessionDurationMs = 1000 * 60 * 60 * 24 * 30;
|
||||
@@ -362,6 +362,57 @@ app.delete('/api/payment-presets/:id', requireAuth, async (req, res, next) => {
|
||||
}
|
||||
});
|
||||
|
||||
const signaturePresetResponse = (row) => ({
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
dataUrl: row.data_url
|
||||
});
|
||||
|
||||
app.get('/api/signature-presets', requireAuth, async (req, res, next) => {
|
||||
try {
|
||||
const result = await pool.query(
|
||||
'SELECT * FROM signature_presets WHERE user_id = $1 ORDER BY name',
|
||||
[req.userId]
|
||||
);
|
||||
res.json({ presets: result.rows.map(signaturePresetResponse) });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/signature-presets', requireAuth, async (req, res, next) => {
|
||||
try {
|
||||
const name = String(req.body.name || '').trim();
|
||||
const dataUrl = req.body.dataUrl;
|
||||
if (!name) {
|
||||
res.status(400).json({ error: 'Zadejte název podpisu.' });
|
||||
return;
|
||||
}
|
||||
if (!isValidSignatureDataUrl(dataUrl)) {
|
||||
res.status(400).json({ error: 'Podpis musí být platný obrázek JPG nebo PNG.' });
|
||||
return;
|
||||
}
|
||||
const result = await pool.query(`
|
||||
INSERT INTO signature_presets (user_id, name, data_url)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (user_id, name) DO UPDATE SET data_url = EXCLUDED.data_url
|
||||
RETURNING *
|
||||
`, [req.userId, name, dataUrl]);
|
||||
res.status(201).json({ preset: signaturePresetResponse(result.rows[0]) });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
app.delete('/api/signature-presets/:id', requireAuth, async (req, res, next) => {
|
||||
try {
|
||||
await pool.query('DELETE FROM signature_presets WHERE id = $1 AND user_id = $2', [req.params.id, req.userId]);
|
||||
res.status(204).end();
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
const invoiceResponse = (row) => ({
|
||||
id: row.id,
|
||||
invoiceNumber: row.invoice_number,
|
||||
|
||||
Reference in New Issue
Block a user