-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconstants.js
163 lines (140 loc) · 5.89 KB
/
constants.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { addDoc, collection, getFirestore } from "@firebase/firestore";
export const PROJECT_NAME = 'test';
export const APP_TITLE = "";
export const APP_ICON = ''; // favicon_white.png
export const SITE_URL = '';
export const TWITTER_HANDLE = '@myaccount';
// Template ID is set in SendGrid and accepts substitution variables
export const EMAIL_API_SERVER = '';
export const SUPPORT_EMAIL = '[email protected]';
export const API_URL = '/'
export const cmsTemplates = {
// uniqueArray: [
// {
// type: "span",
// value: "title"
// },
// ]
}
export const sendData = (obj, cb, retryCount) => {
// console.log('SENDING EMAIL');
if(!retryCount) retryCount = 0;
var replyEmail = obj.filter(f => f.field === "Email address")[0].value;
var replyName = obj.filter(f => f.field === "Full name" || f.field === "First name")[0].value;
// console.log("tripName: ", tripName, ', replyName: ', replyName, ', obj: ', obj);
var subject = APP_TITLE + " Enquiry";
// console.log("replyEmail: ", replyEmail);
// console.log("replyName: ", replyName);
if (!EMAIL_API_SERVER.length) {
alert("Email server not set");
return;
}
fetch(EMAIL_API_SERVER,{
method: 'POST',
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json'
// 'Content-Type': 'application/x-www-form-urlencoded',
},
body: JSON.stringify({
subject: subject,
template_id: "d-ccc0a35ff1a5475199be661c43293995",
destination_name: '',
destination_email: SUPPORT_EMAIL,
title: "You Received A web enquiry",
items: obj,
from_name: "",
from_address: "[email protected]",
reply_email: replyEmail,
reply_subject: "",
reply_body: "Hi " + replyName + ", thank you for your enquiry.",
})
})
.then((response) => {
console.log("RESPONSE STATUS: ", response.status);
//Resend if status is not 200.
// TESTING
if(response.status !== 200 && retryCount < 2){
console.log('RESENDING AFTER FAIL');
sendData(obj, cb, ++retryCount);
}
else if(retryCount === 2){
// Log failure message to DB with all of the data.
logErrorEmail({...obj, business: 'Error collection', handled: false});
}
// console.log(response.text());
if(response.status === 200){
cb();
}
});
}
export const sendPlainData = (destinationEmail, attachment, attachmentPath, cb, retryCount) => {
console.log('SENDING EMAIL',destinationEmail);
if(!retryCount) retryCount = 0;
// console.log("tripName: ", tripName, ', replyName: ', replyName, ', obj: ', obj);
var subject = "A price estimate for your website";
// console.log("replyEmail: ", replyEmail);
// console.log("replyName: ", replyName);
fetch(EMAIL_API_SERVER,{
method: 'POST',
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json'
// 'Content-Type': 'application/x-www-form-urlencoded',
},
body: JSON.stringify({
subject: subject,
// template_id: "",
// template_id: "d-ccc0a35ff1a5475199be661c43293995",
text: `<p>Hi, <br/><br/>Please see a price estimate for your website as attached.</p><p>This was generated from Webbi's <a href="${'https://www.webbi.co.nz/pricing'}">pricing</a> page.</p>
<br/><br/>
<p style="font-size: .75rem; font-style: italic;">If you received this message by mistake, please reply to this message to let us know.
`,
destination_name: '',
destination_email: destinationEmail,
title: "Website Price Estimate - Webbi Digital Studio",
from_name: "Webbi Digital Studio",// (Account Management at Webbi.co.nz)",
from_address: "[email protected]",
reply_email: SUPPORT_EMAIL,
reply_subject: "My Response to Webbi's Website Price Estimate",
reply_body: "Hi, I would like to find out more about...",
attachment: attachment,
attachment_path: attachmentPath,
attachment_name: "Website Price Estimate - Webbi Digital Studio.pdf",
})
})
.then((response) => {
console.log("RESPONSE STATUS: ", response.status);
//Resend if status is not 200.
// TESTING
if(response.status !== 200 && retryCount < 2){
console.log('RESENDING AFTER FAIL');
sendPlainData(destinationEmail, attachment, attachmentPath, cb, ++retryCount);
}
else if(retryCount === 2){
// Log failure message to DB with all of the data.
//TODO enable this.
// logErrorEmail({name: 'Sending email quote attachment issue', email: destinationEmail, errStatus: response.status, business: 'webbi_co_nz', handled: false, createdDate: serverTimestamp()});
}
response.text().then(d => {
console.log('response text', d);
})
if(response.status === 200){
cb();
}
});
}
const logErrorEmail = async (obj) => {
console.log('Sending error email');
addDoc(collection(getFirestore(), 'Email_Errors'), obj)
.then((doc)=>{
console.log('Added new error email: ', doc.id);
})
.catch(err => {
console.error("Couldn't send email to admin: ", err);
});
}