This repository was archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrestoreFAQs.js
55 lines (51 loc) · 1.61 KB
/
restoreFAQs.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
require('dotenv').config()
const fetch = require('isomorphic-unfetch')
const sendMail = require('../lib/sendMail')
const {MANDRILL_API_KEY} = process.env
Promise.resolve().then(async () => {
const messages = await (await fetch('https://mandrillapp.com/api/1.0/messages/search.json', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
key: process.env.MANDRILL_API_KEY,
subject: 'Danke für Ihre Frage',
date_to: '2017-04-27',
senders: [
'faq@republik.ch'
],
limit: 1000
})
})).json()
for (let message of messages) {
const content = await (await fetch('https://mandrillapp.com/api/1.0/messages/content.json', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
key: MANDRILL_API_KEY,
id: message._id
})
})).json()
if (content.subject === 'Danke für Ihre Frage') {
const questionRegex = /Herzlichen Dank für die folgende Frage:(.*?)Eine Antwort finden Sie/g
const question = questionRegex.exec(content.text.replace(/(?:\r\n|\r|\n)/g, ' '))[1].trim()
const text = `${content.to.email} hat folgende Frage gestellt:\n\n${question}`
await sendMail({
to: 'faq@republik.ch',
fromEmail: 'faq@republik.ch',
fromName: 'Republik',
subject: `restored question of: ${content.to.email}`,
text
})
console.log(`sent restored question from: ${content.to.email}`)
}
}
}).then(() => {
process.exit()
}).catch(e => {
console.error(e)
process.exit(1)
})