1
- import mongoose from 'mongoose' ;
2
- import _ from 'lodash' ;
3
- import logger from '../logs' ;
1
+ const mongoose = require ( 'mongoose' ) ;
2
+ const Handlebars = require ( 'handlebars' ) ;
4
3
5
- const { Schema } = mongoose ;
6
-
7
- const mongoSchema = new Schema ( {
4
+ const EmailTemplate = mongoose . model ( 'EmailTemplate' , {
8
5
name : {
9
6
type : String ,
10
7
required : true ,
@@ -20,69 +17,72 @@ const mongoSchema = new Schema({
20
17
} ,
21
18
} ) ;
22
19
23
- const EmailTemplate = mongoose . model ( 'EmailTemplate' , mongoSchema ) ;
24
-
25
- function insertTemplates ( ) {
20
+ async function insertTemplates ( ) {
26
21
const templates = [
27
22
{
28
23
name : 'welcome' ,
29
24
subject : 'Welcome to builderbook.org' ,
30
- message : `<%= userName %> ,
25
+ message : `{{ userName}} ,
31
26
<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!
33
28
</p>
34
29
<p>
35
- See list of available books here .
30
+ In our books, we teach you how to build production-ready web apps from scratch .
36
31
</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
40
37
` ,
41
38
} ,
42
39
{
43
40
name : 'purchase' ,
44
- subject : 'You purchased book at builderbook.org' ,
45
- message : `<%= userName %> ,
41
+ subject : 'You purchased "{{bookTitle}}" at builderbook.org' ,
42
+ message : `{{ userName}} ,
46
43
<p>
47
- Thank you for purchasing our book! You will get confirmation email from Stripe shortly.
44
+ Thank you for purchasing our book!
48
45
</p>
49
46
<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>
51
48
</p>
52
49
<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>.
56
53
</p>
57
-
54
+
58
55
Kelly & Timur, Team Builder Book
59
56
` ,
60
57
} ,
61
58
] ;
62
59
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 ( ) ;
67
65
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
+ }
72
72
}
73
73
74
74
insertTemplates ( ) ;
75
75
76
- export default async function getEmailTemplate ( name , params ) {
76
+ async function getEmailTemplate ( name , params ) {
77
77
const source = await EmailTemplate . findOne ( { name } ) ;
78
78
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' ) ;
82
80
}
83
81
84
82
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 ) ,
87
85
} ;
88
86
}
87
+
88
+ module . exports = getEmailTemplate ;
0 commit comments