-
Notifications
You must be signed in to change notification settings - Fork 14
/
dbUpdateScripts.js
84 lines (76 loc) · 2.28 KB
/
dbUpdateScripts.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
var _ = require('underscore')
var report = require('./report')
var admin = require('firebase-admin')
var serviceAccount = require('./serviceAccountKey.json')
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://whopays-522bd.firebaseio.com'
})
var db = admin.database()
function importFromMongoDump (filepath) {
var data = require(filepath)
data.forEach(function (d) {
db.ref('posts/' + d._id).set(d)
})
db.ref('posts').once('value', function (snapshot) {
var data = _.toArray(snapshot.val())
db.ref('events').set(_.uniq(_.pluck(data, 'event_name')))
db.ref('locations').set(_.uniq(_.pluck(data, 'event_location')))
})
}
function generateReports () {
var ref = db.ref('posts')
ref.once('value', function (snapshot) {
var data = snapshot.val()
for (var i in data) {
data[i].report = report.generate(data[i])
ref.child(i).set(data[i])
}
}, function () {
console.log('[generateReports] : Error getting posts')
})
}
function generateOneReport (id) {
var ref = db.ref('posts/' + id)
ref.once('value', function (snapshot) {
var data = snapshot.val()
data.report = report.generate(data)
ref.set(data)
}, function () {
console.log('[generateOneReport] : Error getting post')
})
}
function updateItem (id, key, val, reason, issueID) {
if (!id || !key || !val || !reason || !issueID) { console.log('please pass all 4 arg'); return }
var ref = db.ref('posts/' + id)
ref.once('value', function (snapshot) {
var data = snapshot.val()
data[key] = val
data.edited_by_admin = true
if (!data.edit_request_issues) {
data.edit_request_issues = []
}
data.edit_request_issues.push(issueID)
if (!data.edit_notes) {
data.edit_notes = []
}
data.edit_notes.push({
timestamp: new Date().toISOString(),
key: key,
value: val,
reason: reason,
issueID: issueID
})
ref.set(data)
}, function () {
console.log('[generateOneReport] : Error getting post')
})
}
function removeById (id) {
db.ref('posts').child(id).remove()
}
exports.importFromMongoDump = importFromMongoDump
exports.generateReports = generateReports
exports.generateOneReport = generateOneReport
exports.updateItem = updateItem
exports.removeById = removeById