File size: 2,978 Bytes
92a138e
f4623fe
92a138e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const { getDb } = require('../config/database');
const { generatePreviewHtml } = require('../services/previewService');
const logger = require('../config/logger');

/**
 * GET /api/export/:resumeId/preview
 * Renders an HTML preview by reading from the database.
 */
async function previewResume(req, res) {
  const { resumeId } = req.params;
  const db = await getDb();

  const resume = await db.get(
    `SELECT r.*, t.html_content, t.css_content 
     FROM resumes r 
     LEFT JOIN templates t ON r.template_id = t.id 
     WHERE r.id = ?`,
    resumeId
  );

  if (!resume) return res.status(404).send('Resume not found');

  if (!resume.html_content) {
    return res.send(`
      <div style="font-family: sans-serif; display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; color: #64748b; background: #f8fafc; text-align: center; padding: 2rem;">
        <svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="margin-bottom: 1rem; opacity: 0.5;"><rect width="18" height="18" x="3" y="3" rx="2"/><path d="M7 7h10"/><path d="M7 12h10"/><path d="M7 17h10"/></svg>
        <h2 style="margin: 0 0 0.5rem 0; color: #1e293b;">No Template Selected</h2>
        <p style="margin: 0;">Please select a template in the design sidebar to preview your resume.</p>
      </div>
    `);
  }

  const content = JSON.parse(resume.content || '{}');
  const design = JSON.parse(resume.design || '{}');
  const previewHtml = generatePreviewHtml(content, design, resume.html_content, resume.css_content);
  res.send(previewHtml);
}

/**
 * POST /api/export/preview
 * Renders a live preview directly from posted content+design+templateId.
 * DOES NOT read from or write to the database (except to fetch template HTML/CSS).
 * Body: { content, design, templateId }
 */
async function previewFromData(req, res) {
  const { content, design, templateId } = req.body;

  if (!templateId) {
    return res.status(400).json({ success: false, message: 'templateId is required' });
  }

  const db = await getDb();
  const template = await db.get(
    'SELECT html_content, css_content FROM templates WHERE id = ?',
    templateId
  );

  if (!template || !template.html_content) {
    return res.send(`
      <div style="font-family: sans-serif; display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; color: #64748b; background: #f8fafc; text-align: center; padding: 2rem;">
        <h2 style="margin: 0 0 0.5rem 0; color: #1e293b;">Template not found</h2>
        <p style="margin: 0;">The selected template could not be loaded.</p>
      </div>
    `);
  }

  const previewHtml = generatePreviewHtml(
    content || {},
    design || {},
    template.html_content,
    template.css_content
  );
  res.send(previewHtml);
}

module.exports = { previewResume, previewFromData };