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 pathimportMembers.js
71 lines (63 loc) · 1.96 KB
/
importMembers.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
//
// This script imports our existing members
//
// usage
// cf_server cat script/examples/members.csv | node script/importMembers.js
//
require('dotenv').config()
const PgDb = require('../lib/pgdb')
const {dsvFormat} = require('d3-dsv')
const csvParse = dsvFormat(',').parse
const rw = require('rw')
const ABO_PRICE = 24000
PgDb.connect().then(async (pgdb) => {
// gather data
const pkg = await pgdb.public.packages.findOne({name: 'ABO'})
const pkgOption = await pgdb.public.packageOptions.findOne({packageId: pkg.id})
const membershipType = await pgdb.public.membershipTypes.findOne({name: 'ABO'})
const inputFile = rw.readFileSync('/dev/stdin', 'utf8')
const people = csvParse(inputFile)
for (let person of people) {
const {firstName, lastName, email} = person
console.log(firstName + ' ' + lastName)
let user = await pgdb.public.users.findOne({email})
if (!user) {
user = await pgdb.public.users.insertAndGet({
firstName,
lastName,
email,
verified: true
})
}
if (!(await pgdb.public.pledges.count({userId: user.id}))) {
const pledge = await pgdb.public.pledges.insertAndGet({
packageId: pkg.id,
userId: user.id,
status: 'SUCCESSFUL',
total: ABO_PRICE,
donation: 0,
sendConfirmMail: false
})
await pgdb.public.pledgeOptions.insert({
templateId: pkgOption.id,
pledgeId: pledge.id,
amount: 1,
price: ABO_PRICE
})
await pgdb.public.memberships.insert({
userId: user.id,
pledgeId: pledge.id,
membershipTypeId: membershipType.id,
beginDate: new Date()
})
}
}
console.log(`users: ${await pgdb.public.users.count()}`)
console.log(`memberships: ${await pgdb.public.memberships.count()}`)
console.log(`payments: ${await pgdb.public.payments.count()} (zero is ok)`)
}).then(() => {
process.exit()
}).catch(e => {
console.error(e)
process.exit(1)
})