-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (141 loc) · 3.81 KB
/
index.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
const express = require('express')
const config = require('./config')
const bodyParser = require('body-parser');
const nodeMailer = require('nodemailer');
const BigCommerce = require('node-bigcommerce');
const generateEmailBody = require('./welcomeEmail');
const cors = require('cors');
const app = express()
const multer = require('multer');
app.use(cors({
origin: '*'
}));
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
const port = config.port;
const bigCommerce = new BigCommerce({
clientId: 's6u73s8wi9ksi329x0a6xr4c5wzcprb',
accessToken: 't525twricsuqvefl9ob4y2872phx3t3',
storeHash: '6at0uo0xax',
responseType: 'json',
apiVersion: 'v3' // Default is v2
});
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.post('/send-email', cors(), multer().single('image'), async function (req, res) {
const {
businessEmail,
password,
buyerFirstName,
buyerLastName,
companyName,
businessType,
phoneNumber,
businessAddress1,
businessAddress2,
city,
state,
zip,
country = 'United States',
retailCertification,
retailCertificationState
} = req.body;
const file = req.file;
const customer = [{
'email': businessEmail,
'first_name': buyerFirstName,
'last_name': buyerLastName,
"phone": phoneNumber,
"company": companyName,
"addresses": [
{
"address1": businessAddress1,
"address2": businessAddress2,
"city": city,
"company": companyName,
"country_code": "US",
"first_name": buyerFirstName,
"last_name": buyerLastName,
"phone": phoneNumber,
"postal_code": zip,
"state_or_province": 'California',
"address_type": "commercial",
}
],
"authentication": {
"force_password_reset": false,
"new_password": password
},
}];
try {
await bigCommerce.post('/customers', customer);
} catch (err) {
console.log(err);
res.send({
code: 422,
message: 'Something went wrong'
});
return;
}
const transporter = nodeMailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: config.senderEmail,
pass: config.senderPass
}
});
let mailOptions = {
from: config.senderEmail,
to: config.reciverEmail,
subject: 'New PPLF Wholesale Customer Application',
html: `<b>Buyer Name: ${buyerFirstName} ${buyerLastName}</b><br>
<b>Business Name: ${companyName}</b><br>
<b>Business Type: ${businessType}</b><br>
<b>Business address line 1: ${businessAddress1}</b><br>
<b>Business address line 2: ${businessAddress2}</b><br>
<b>Business email: ${businessEmail}</b><br>
<b>Phone number: ${phoneNumber}</b><br>
<b>Country: ${'United States'}</b><br>
<b>State: ${state}</b><br>
<b>City: ${city}</b><br>
<b>Zip: ${zip}</b><br>
<b>Retail Certification Number: ${retailCertification}</b><br>
<b>Retail Certification State: ${retailCertificationState}</b><br>`,
// attachments: [
// {
// filename: file.originalname,
// content: file.buffer
// }
// ]
};
try {
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
});
} catch (e) {
console.log(e)
}
let welcomeMailOptions = {
from: config.senderEmail,
to: businessEmail,
subject: 'Welcome to Our Site',
html: generateEmailBody()
};
try {
transporter.sendMail(welcomeMailOptions, (error, info) => {
if (error) {
return console.log(error);
}
});
} catch (e) {
console.log(e);
}
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})