Skip to content

Commit ad34a98

Browse files
committed
keep EmailTemplate.js in main repo the same for now
1 parent 790ea4b commit ad34a98

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

server/models/EmailTemplate.js

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import mongoose from 'mongoose';
2-
import _ from 'lodash';
3-
import logger from '../logs';
1+
const mongoose = require('mongoose');
2+
const Handlebars = require('handlebars');
43

5-
const { Schema } = mongoose;
6-
7-
const mongoSchema = new Schema({
4+
const EmailTemplate = mongoose.model('EmailTemplate', {
85
name: {
96
type: String,
107
required: true,
@@ -20,69 +17,72 @@ const mongoSchema = new Schema({
2017
},
2118
});
2219

23-
const EmailTemplate = mongoose.model('EmailTemplate', mongoSchema);
24-
25-
function insertTemplates() {
20+
async function insertTemplates() {
2621
const templates = [
2722
{
2823
name: 'welcome',
2924
subject: 'Welcome to builderbook.org',
30-
message: `<%= userName %>,
25+
message: `{{userName}},
3126
<p>
32-
At Builder Book, we are excited to help you build useful, production-ready web apps from scratch.
27+
Thank you for signing up for Builder Book!
3328
</p>
3429
<p>
35-
See list of available books here.
30+
In our books, we teach you how to build production-ready web apps from scratch.
3631
</p>
37-
38-
Kelly & Timur,
39-
Team BB
32+
<p>
33+
The code for our books will always be free and open source.
34+
</p>
35+
36+
Kelly & Timur, Team Builder Book
4037
`,
4138
},
4239
{
4340
name: 'purchase',
44-
subject: 'You purchased book at builderbook.org',
45-
message: `<%= userName %>,
41+
subject: 'You purchased "{{bookTitle}}" at builderbook.org',
42+
message: `{{userName}},
4643
<p>
47-
Thank you for purchasing our book! You will get confirmation email from Stripe shortly.
44+
Thank you for purchasing our book!
4845
</p>
4946
<p>
50-
Start reading your book: <a href="<%= bookUrl %>" target="_blank"><%= bookTitle %></a>
47+
Start reading your book: <a href="{{bookUrl}}" target="_blank">{{bookTitle}}</a>
5148
</p>
5249
<p>
53-
If you have any questions while reading the book,
54-
please fill out an issue on
55-
<a href="https://github.com/builderbook/builderbook/issues" target="blank">Github</a>.
50+
If you have any questions while reading the book,
51+
please fill out an issue on
52+
<a href="https://github.com/builderbook/builderbook target="blank">Github</a>.
5653
</p>
57-
54+
5855
Kelly & Timur, Team Builder Book
5956
`,
6057
},
6158
];
6259

63-
templates.forEach(async (template) => {
64-
if ((await EmailTemplate.find({ name: template.name }).count()) > 0) {
65-
return;
66-
}
60+
for (let i = 0; i < templates.length; i += 1) {
61+
const t = templates[i];
62+
63+
// eslint-disable-next-line no-await-in-loop
64+
const count = await EmailTemplate.find({ name: t.name }).count();
6765

68-
EmailTemplate.create(template).catch((error) => {
69-
logger.error('EmailTemplate insertion error:', error);
70-
});
71-
});
66+
if (count === 0) {
67+
EmailTemplate.create(Object.assign({}, t, {
68+
message: t.message.replace(/\n/g, '').replace(/[ ]+/g, ' '),
69+
}));
70+
}
71+
}
7272
}
7373

7474
insertTemplates();
7575

76-
export default async function getEmailTemplate(name, params) {
76+
async function getEmailTemplate(name, params) {
7777
const source = await EmailTemplate.findOne({ name });
7878
if (!source) {
79-
throw new Error(`No EmailTemplates found.
80-
Please check that at least one is generated at server startup,
81-
restart your server and try again.`);
79+
throw new Error('not found');
8280
}
8381

8482
return {
85-
message: _.template.compile(source.message)(params),
86-
subject: _.template.compile(source.subject)(params),
83+
message: Handlebars.compile(source.message)(params),
84+
subject: Handlebars.compile(source.subject)(params),
8785
};
8886
}
87+
88+
module.exports = getEmailTemplate;

0 commit comments

Comments
 (0)