-
Notifications
You must be signed in to change notification settings - Fork 1
/
userUpdater.js
75 lines (68 loc) · 2.5 KB
/
userUpdater.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
process.env.AWS_PROFILE = 'fantasy-bach';
process.env.USERS_TABLE = 'fantasybach-dev-users';
var seasonId = 'season:HyfVRTasf';
var fs = require('fs');
var async = require('async');
var _ = require('lodash');
var AWS = require('aws-sdk');
AWS.config.update({ region : 'us-east-1' });
var dynamodbDoc = new AWS.DynamoDB.DocumentClient();
var scanUsers = function(callback) {
return _scanUsers(null, callback);
};
var _scanUsers = function(startKey, callback) {
console.log('Scanning with key: ' + startKey);
var params = startKey ? { ExclusiveStartKey : startKey } : {};
return dynamodbDoc.scan(_.assign(params, {
TableName : process.env.USERS_TABLE,
ProjectionExpression : '#id',
ExpressionAttributeNames : {
'#id' : 'id'
}
}), function(err, data) {
if (err) { callback(err); }
console.log('Last Evaluated Key: ' + data.LastEvaluatedKey);
if (data.LastEvaluatedKey) {
_scanUsers(data.LastEvaluatedKey, callback);
return callback(null, data.Items, false);
}
callback(null, data.Items, true);
});
};
var updateUser = function(userId, callback) {
var roundIds = ['round:BkrNPKWAjz', 'round:H1UNPFZAsM', 'round:ryo4DtZRjM', 'round:rk3NwFb0sM', 'round:B1aEPFZAoM', 'round:r1C4wtb0of', 'round:rJ1Swtb0of', 'round:ByerDtZAiz', 'round:rkZrvY-0sM', 'round:HJGSvt-RsG' ];
var scores = _.assign({ score : 0 }, _.reduce(roundIds, function(result, roundId) {
result[roundId] = 0;
return result;
}, {}));
var picks = _.reduce(roundIds, function(result, roundId) {
result[roundId] = {};
return result;
}, {});
var leagues = [];
dynamodbDoc.update({
TableName : process.env.USERS_TABLE,
Key : { id : userId },
UpdateExpression : 'SET #scores.#seasonId=:scores, #picks.#seasonId=:picks, #leagues.#seasonId=:leagues',
ExpressionAttributeNames : {
'#picks' : 'picks',
'#scores' : 'scores',
'#leagues' : 'leagues',
'#seasonId' : seasonId
},
ExpressionAttributeValues : {
':scores' : scores,
':picks' : picks,
':leagues' : leagues
}
}, callback);
};
scanUsers(function(err, users, lastGroup) {
if (err) { return done(err); }
console.log('Got ' + users.length + ' users');
async.eachLimit(users, 8, function(user, callback) {
updateUser(user.id, callback);
}, function(err) {
console.log(err);
});
});