-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
49 lines (43 loc) · 1.76 KB
/
server.js
File metadata and controls
49 lines (43 loc) · 1.76 KB
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
require('dotenv').config()
const { Resend } = require('resend');
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
const { EMAIL_KEY, EMAIL_DEST, SEC_EMAIL_DEST } = process.env;
const resend = new Resend(EMAIL_KEY);
app.use(express.json());
app.post('/data', async (req, res) => {
const { name, email, message, authenticated, phone, notes, cartItems, total, order_source } = req.body;
let candleOrderTemplate = '';
if (order_source === 'gdlglow') {
candleOrderTemplate = `
<h1>New Candle Order</h1>
<p>name: <strong>${name}</strong></p>
<p>email: <strong>${email}</strong></p>
<p>phone: <strong>${phone}</strong></p>
<p>notes: <strong>${notes}</strong></p>
<p>cartItems: <strong>${cartItems.map(item => {
const optionsText = item.selectedOptions ? Object.entries(item.selectedOptions).map(([key, value]) => `${key}: ${value}`).join(', ') : '';
return `<li>${item.name} - ${item.quantity}${optionsText ? ` (${optionsText})` : ''}</li>`;
}).join('')}</strong></p>
<p>total: <strong>${total}</strong></p>
`;
}
const otherTemplate = `
<p>name: <strong>${name}</strong></p>
<p>email: <strong>${email}</strong></p>
<p>message: <strong>${message}</strong></p>
`;
const { data, error } = await resend.emails.send({
from: order_source === 'gdlglow' ? "GDL Glow <no-reply@gdlglow.com>" : "Portfolio <no-reply@gdlglow.com>",
to: order_source === 'gdlglow' ? [EMAIL_DEST, SEC_EMAIL_DEST] : [EMAIL_DEST],
subject: authenticated ? "Important message!!!" : "View alert!",
html: order_source === 'gdlglow' ? candleOrderTemplate : otherTemplate,
});
if (error) {
return res.status(400).json({ error });
}
res.status(200).json({ data });
})
app.listen(3000);