| Hash | Commit message | Author | Date | Files | + | - |
1 | commit fb699c69d02749ab83a426c80eb41dc4db550532 |
2 | Author: Connor Etherington <[email protected]> |
3 | Date: Fri Apr 14 07:48:08 2023 +0200 |
4 | |
5 | Auto-Commit Update 14.04.2023 - 07:48:08 |
6 | --- |
7 | .../inputs => .bkp}/affiliate-register.json | 0 |
8 | .../freepaid/templates => .bkp}/postPayment.html | 0 |
9 | .env | 2 +- |
10 | clients/freepaid/templates/welcomeMail.js | 23 -------- |
11 | methods/main.js | 18 +++--- |
12 | methods/mkMail.js | 6 +- |
13 | methods/updateDB.js | 25 +++++--- |
14 | models/template.js | 4 +- |
15 | routes/api.js | 66 +++++++++++++++++++--- |
16 | 9 files changed, 95 insertions(+), 49 deletions(-) |
17 | |
18 | diff --git a/clients/freepaid/inputs/affiliate-register.json b/.bkp/affiliate-register.json |
19 | similarity index 100% |
20 | rename from clients/freepaid/inputs/affiliate-register.json |
21 | rename to .bkp/affiliate-register.json |
22 | diff --git a/clients/freepaid/templates/postPayment.html b/.bkp/postPayment.html |
23 | similarity index 100% |
24 | rename from clients/freepaid/templates/postPayment.html |
25 | rename to .bkp/postPayment.html |
26 | diff --git a/.env b/.env |
27 | index 9b30338..befb26d 100644 |
28 | --- a/.env |
29 | +++ b/.env |
30 | @@ -1,4 +1,4 @@ |
31 | |
32 | URI="mongodb+srv://a4to:c1b120d56bb97eb37fbc45f009e33f03f2840@mycondb.bt5st8l.mongodb.net/mailer?retryWrites=true" |
33 | -token='oiouoiok' |
34 | +TOKEN='oiouoiok' |
35 | |
36 | diff --git a/clients/freepaid/templates/welcomeMail.js b/clients/freepaid/templates/welcomeMail.js |
37 | deleted file mode 100644 |
38 | index a752a4f..0000000 |
39 | --- a/clients/freepaid/templates/welcomeMail.js |
40 | +++ /dev/null |
41 | @@ -1,23 +0,0 @@ |
42 | - |
43 | - |
44 | -module.exports = async (recipient, link, html) => { |
45 | - |
46 | - let baseName = recipient.agency.replace(/^/, 'https://').replace(/https?:\/\//g, '').replace(/\/$/, ''); |
47 | - let from = baseName.slice(0, 1).toString().toUpperCase() + baseName.slice(1).split('.')[0].replace('data', ' Data').replace('gbservices', 'GB Services'); |
48 | - let subject = `Welcome to the ${from} family!`; |
49 | - let { name, surname } = recipient; |
50 | - |
51 | - const baseUrl = recipient.agency.replace(/^/, 'https://'); |
52 | - let logo; |
53 | - |
54 | - logo = baseName + '.png'; |
55 | - logo = `${baseUrl}/img/logos/${logo}`; |
56 | - |
57 | - |
58 | - |
59 | -const mailOptions = { |
60 | - from: `"${from}" <[email protected]>`, |
61 | - to: recipient.email, |
62 | - subject, |
63 | - html |
64 | -}; |
65 | diff --git a/methods/main.js b/methods/main.js |
66 | index ed25cda..dfde0a0 100644 |
67 | --- a/methods/main.js |
68 | +++ b/methods/main.js |
69 | @@ -3,7 +3,7 @@ const Client = require('../models/client'); |
70 | const Template = require('../models/template'); |
71 | const nodemailer = require('nodemailer'); |
72 | const { mkConfig, mkMail } = require('./mkMail'); |
73 | -const { updateCliets, updateTemplates } = require('./updateDB'); |
74 | +const { updateClients, updateTemplates, singleClient } = require('./updateDB'); |
75 | require('colors'); |
76 | |
77 | const getClient = (name, token) => { |
78 | @@ -66,23 +66,25 @@ const mail = async (recipient, link, name, token, type) => { |
79 | |
80 | const test = async (recipient, link, name, token, type) => { |
81 | |
82 | + console.log('TESTING'.cyan + ` ${type}`.yellow + ' EMAIL'.cyan); |
83 | + |
84 | const { html, requires } = await getTemplate(name, token, type); |
85 | - if(!html || !requires) return false; |
86 | + if(!html || !requires) return console.log('No template found'.red); |
87 | |
88 | const client = await getClient(name, token); |
89 | - if(!client) return false; |
90 | + if(!client) return console.log('No client found'.red); |
91 | |
92 | const transporter = nodemailer.createTransport(client.config); |
93 | - if(!transporter) return false; |
94 | + if(!transporter) return console.log('No transporter found'.red); |
95 | |
96 | const inputs = mkConfig(client, recipient, link); |
97 | - if(!inputs) return false; |
98 | + if(!inputs) return console.log('No inputs found'.red); |
99 | |
100 | const mail = mkMail(client, type, inputs); |
101 | - if(!mail) return false; |
102 | + if(!mail) return console.log('No mail found'.red); |
103 | |
104 | const { from, to, subject, Html } = mail; |
105 | - if(!from || !to || !subject || !Html) return false; |
106 | + if(!from || !to || !subject || !Html) return console.log('No mail found'.red); |
107 | |
108 | console.log('From: '.green + from); |
109 | console.log('To: '.green + to); |
110 | @@ -93,5 +95,5 @@ const test = async (recipient, link, name, token, type) => { |
111 | |
112 | } |
113 | |
114 | -module.exports = { getClient, getTemplate, mail, test, updateCliets, updateTemplates }; |
115 | +module.exports = { getClient, getTemplate, mail, test, updateClients, updateTemplates, singleClient }; |
116 | |
117 | diff --git a/methods/mkMail.js b/methods/mkMail.js |
118 | index 857af98..49cab08 100644 |
119 | --- a/methods/mkMail.js |
120 | +++ b/methods/mkMail.js |
121 | @@ -3,6 +3,8 @@ const Template = require('../models/template'); |
122 | |
123 | const mkConfig = (client, recipient, link) => { |
124 | |
125 | + console.log(`\nMKCONFIG STARTED FOR ${client.name}\n`); |
126 | + |
127 | try { |
128 | if (client.name == 'FREEPAID') { |
129 | let { name, surname, agency } = recipient, |
130 | @@ -27,7 +29,9 @@ const mkConfig = (client, recipient, link) => { |
131 | } |
132 | |
133 | |
134 | -const mkMail = (client, type, inputs) => { |
135 | +const mkMail = async (client, type, inputs) => { |
136 | + |
137 | + console.log(`\nMKMAIL STARTED FOR ${client.name}\nTYPE:${type}\n`); |
138 | |
139 | try { |
140 | if (client.name == 'FREEPAID') { |
141 | diff --git a/methods/updateDB.js b/methods/updateDB.js |
142 | index 6daaa4f..113fb18 100644 |
143 | --- a/methods/updateDB.js |
144 | +++ b/methods/updateDB.js |
145 | @@ -7,6 +7,8 @@ require('colors'); |
146 | |
147 | const getClients = async () => { |
148 | |
149 | + let TS = []; |
150 | + |
151 | await fs.readdir(path.join(__dirname, '../clients'), (err, files) => { |
152 | if (err) { console.log(err); } |
153 | for (let i = 0; i < files.length; i++) { |
154 | @@ -22,6 +24,8 @@ const getClients = async () => { |
155 | |
156 | const singleClient = async (clientName) => { |
157 | |
158 | + console.log(clientName); |
159 | + |
160 | const CLIENTS = Array.from(await getClients()); |
161 | const client = await Client.findOne({ name: clientName.toUpperCase() }); |
162 | |
163 | @@ -42,7 +46,7 @@ const singleClient = async (clientName) => { |
164 | const templateInputs = path.join(__dirname, `../clients/${clientName.toLowerCase()}/inputs/${file.replace(/.html/, '')}.json`); |
165 | const templateContent = fs.readFileSync(templatePath, 'utf8'); |
166 | |
167 | - const template = await Template.findOne({ client: client._id, }); |
168 | + const template = await Template.findOne({ name: client.clientName }); |
169 | |
170 | if (!template) { |
171 | const newTemplate = new Template({ |
172 | @@ -62,8 +66,7 @@ const singleClient = async (clientName) => { |
173 | } else { |
174 | |
175 | if (template.emails.filter(email => email.name === templateName).length === 0) { |
176 | - template.emails.push({ name: templateName, html: templateContent, requires: JSON.parse(fs.readFileSync(templateInputs, 'utf8')) }); |
177 | - await template.save().then(() => { |
178 | + await Template.findOneAndUpdate({ client: client._id }, { $push: { emails: { name: templateName, html: templateContent, requires: JSON.parse(fs.readFileSync(templateInputs, 'utf8')) } } }).then(() => { |
179 | console.log(`[+] `.green + `${templateName.yellow} template added to ${clientName.green} client`); |
180 | }); |
181 | |
182 | @@ -84,8 +87,16 @@ const updateTemplates = async () => { |
183 | |
184 | console.log(`\nAdding templates to all clients...`); |
185 | console.log(''); |
186 | - await singleClient('CONCISE'); |
187 | - await singleClient('FREEPAID'); |
188 | + await singleClient('CONCISE').then(() => { |
189 | + console.log('Concise client updated'); |
190 | + }).finally(async () => { |
191 | + await singleClient('FREEPAID').then(() => { |
192 | + console.log('Freepaid client updated'); |
193 | + }) |
194 | + }); |
195 | + |
196 | + |
197 | + return true; |
198 | |
199 | } |
200 | |
201 | @@ -98,11 +109,11 @@ const updateClients = async () => { |
202 | await Client.deleteMany(); |
203 | await Client.insertMany(clients); |
204 | console.log('Clients imported...'.green.inverse); |
205 | - process.exit(); |
206 | + return true; |
207 | } catch (error) { |
208 | console.error(error); |
209 | } |
210 | |
211 | } |
212 | |
213 | -module.exports = { updateTemplates, updateClients }; |
214 | +module.exports = { updateTemplates, updateClients, singleClient }; |
215 | diff --git a/models/template.js b/models/template.js |
216 | index 035d32e..ee517bf 100644 |
217 | --- a/models/template.js |
218 | +++ b/models/template.js |
219 | @@ -2,8 +2,8 @@ |
220 | const mongoose = require('mongoose'); |
221 | |
222 | const TemplateSchema = new mongoose.Schema({ |
223 | - client: { type: mongoose.Schema.Types.ObjectId, ref: 'Client', unique: true }, |
224 | - clientName: { type: String, unique: true }, |
225 | + client: { type: mongoose.Schema.Types.ObjectId, ref: 'Client' }, |
226 | + clientName: { type: String }, |
227 | emails: [Object], |
228 | }); |
229 | |
230 | diff --git a/routes/api.js b/routes/api.js |
231 | index d438c25..471e699 100644 |
232 | --- a/routes/api.js |
233 | +++ b/routes/api.js |
234 | @@ -1,6 +1,7 @@ |
235 | |
236 | const router = require('express').Router(); |
237 | -const { getClient, getTemplate, mail, test, updateCliets, updateTemplates } = require('../methods/main'); |
238 | +const { getClient, getTemplate, mail, test, updateClients, updateTemplates, singleClient } = require('../methods/main'); |
239 | + |
240 | |
241 | // getClient (name, token) |
242 | // getTemplate (name, token, type) |
243 | @@ -13,32 +14,83 @@ router.post('/sendmail', async (req, res) => { |
244 | const { recipient, link, name, token, type } = req.body; |
245 | |
246 | if (!recipient || !link || !name || !token || !type) { |
247 | + console.log(`Unauthorized mail attempt from ${req.headers['x-forwarded-for'] || req.connection.remoteAddress}`); |
248 | + return res.status(400).json({ error: 'Missing parameters' }); |
249 | + } else { |
250 | + await mail(recipient, link, name, token, type); |
251 | + res.status(200).json({ status: 'success', message: 'Mail sent' }); |
252 | + } |
253 | + |
254 | +}); |
255 | + |
256 | +router.post('/test', async (req, res) => { |
257 | + |
258 | + const { recipient, link, name, token, type } = req.body; |
259 | + |
260 | + if (!recipient || !link || !name || !token || !type) { |
261 | + console.log(`Unauthorized test attempt from ${req.headers['x-forwarded-for'] || req.connection.remoteAddress}`); |
262 | return res.status(400).json({ error: 'Missing parameters' }); |
263 | } else { |
264 | - await test(recipient, link, name, token, type); |
265 | - res.send('\nMail sent\n'); |
266 | + await test(recipient, link, name, token, type).then(() => { |
267 | + res.status(200).json({ status: 'success', message: 'Mail sent' }); |
268 | + }).catch((err) => { |
269 | + res.status(500).json({ status: 'error', message: err }); |
270 | + }); |
271 | + |
272 | } |
273 | |
274 | }); |
275 | |
276 | -router.post('/update', (req, res) => { |
277 | +router.post('/updateC', async (req, res) => { |
278 | |
279 | const { token } = req.body; |
280 | |
281 | if (!token) { |
282 | return res.status(400).json({ error: 'Missing parameters' }); |
283 | } else if (token !== process.env.TOKEN) { |
284 | + console.log(`Unauthorized update attempt from ${req.headers['x-forwarded-for'] || req.connection.remoteAddress}`); |
285 | return res.status(401).json({ error: 'Unauthorized' }); |
286 | } |
287 | |
288 | - updateCliets(); |
289 | - updateTemplates(); |
290 | + await updateClients(); |
291 | |
292 | - res.status(200).send(`\nClients and templates updated successfully\n`); |
293 | + res.status(200).json({ status: 'success', message: 'Clients and templates updated successfully'}); |
294 | |
295 | }); |
296 | |
297 | +router.post('/updateT', async (req, res) => { |
298 | + |
299 | + const { token } = req.body; |
300 | |
301 | + if (!token) { |
302 | + return res.status(400).json({ error: 'Missing parameters' }); |
303 | + } else if (token !== process.env.TOKEN) { |
304 | + console.log(`Unauthorized update attempt from ${req.headers['x-forwarded-for'] || req.connection.remoteAddress}`); |
305 | + return res.status(401).json({ error: 'Unauthorized' }); |
306 | + } |
307 | + |
308 | + await updateTemplates(); |
309 | + |
310 | + res.status(200).json({ status: 'success', message: 'Clients and templates updated successfully'}); |
311 | + |
312 | +}); |
313 | + |
314 | +router.post('/updateS', async (req, res) => { |
315 | + |
316 | + const { token, client } = req.body; |
317 | + |
318 | + if (!token) { |
319 | + return res.status(400).json({ error: 'Missing parameters' }); |
320 | + } else if (token !== process.env.TOKEN) { |
321 | + console.log(`Unauthorized update attempt from ${req.headers['x-forwarded-for'] || req.connection.remoteAddress}`); |
322 | + return res.status(401).json({ error: 'Unauthorized' }); |
323 | + } |
324 | + |
325 | + await singleClient(client); |
326 | + |
327 | + res.status(200).json({ status: 'success', message: 'Clients and templates updated successfully'}); |
328 | + |
329 | +}); |
330 | |
331 | module.exports = router; |
332 | |