-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (45 loc) · 2.02 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
import { mapFilterMap, getValidMonthFilter, fetchYahrzeitFormEntries, saveToCSV} from './lib/dataCollection.js'
import {saveToText} from './lib/weeklyList.js'
import {sendEmails} from './lib/mail.js'
import { MEMBER } from './lib/form_constants.js'
import promptInit from "prompt-sync"
const confirmPrompt = (savedCSV, savedTXT, emailCount) => {
const prompt = promptInit()
console.log('_________________________________________________________________')
console.log(`CSV of Yahrzeit forms saved to: ${savedCSV}`)
console.log(`Text for shabbat email saved to: ${savedTXT}`)
console.log(``)
console.log(`Please review both files.`)
console.log(`If everything looks right, we'll send the emails to the mourners.`)
console.log(`THERE ARE ${emailCount} EMAILS TO SEND.`)
const response = prompt(`Type "Send Emails" to confirm: `)
console.log(``)
if (response === 'Send Emails') {
return true
} else {
console.log('_________________________________________________________________')
console.log('Not sending emails.')
console.log('Re-run script or contact support to address an error.')
console.log('_________________________________________________________________')
console.log('')
}
}
const run = async () => {
const monthFilter = getValidMonthFilter()
// Get and filter forms
const json = await fetchYahrzeitFormEntries() // NEED ERROR HANDLING
const filteredForms = await mapFilterMap(monthFilter, json)
// dump to csv
const savedCSV = await saveToCSV(filteredForms)
// if monthFilter == 0, end here
if (!!monthFilter) {
// dump text for shabbat email
const savedTXT = saveToText(monthFilter, filteredForms)
// prompt user to review and continue with email or not
const justMembers = filteredForms.filter(form => form.member_status === MEMBER)
if (confirmPrompt(savedCSV, savedTXT, justMembers.length)) {
sendEmails(justMembers)
}
}
}
run()