-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
982 lines (903 loc) · 33.8 KB
/
Copy pathmain.js
File metadata and controls
982 lines (903 loc) · 33.8 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
moment = require('cloud/moment');
_ = require('cloud/underscore-min');
/**
* The number of milliseconds in one day
* @type {number}
*/
MILLISECONDS_PER_DAY = 1000 * 60 * 60 * 24;
/**
* The parse ID for 'Bills' Category Object
* @type {string}
*/
BILLS_CATEGORY_ID = "93BaEoZPfo";
/**
* Debit ENUM value
* @type {number}
*/
DEBIT_ENUM = 1;
/**
* Credit ENUM value
* @type {number}
*/
CREDIT_ENUM = 2;
/**
* Goal Type ENUM values
* @type {number}
*/
GOAL_TYPE_LENDING_CIRCLE = 1;
GOAL_TYPE_PERSONAL = 2;
/**
* Goal Status ENUM values
*/
GOAL_STATUS_IN_PROGRESS = 1;
GOAL_STATUS_ACHIEVED = 2;
/**
* Payment Interval ENUM Values
*/
PAYMENT_INTERVAL_DAILY = 1;
PAYMENT_INTERVAL_WEEKLY = 7;
PAYMENT_INTERVAL_BIWEEKLY = 14;
PAYMENT_INTERVAL_MONTHLY = 30;
PAYMENT_INTERVAL_BIMONTHLY = 60;
logError = function(error) {
console.error('Error: ' + JSON.stringify(error));
};
handleError = function(error) {
logError(error);
return Parse.Promise.Error(error);
};
/**
* Calculate days between two dates
* @param {date} startDate
* @param {date} endDate
* @return {number} Number of days in between
*/
daysBetween = function(startDate, endDate) {
return Math.floor((endDate - startDate) / MILLISECONDS_PER_DAY);
};
getNextPaymentDate = function(currentPaymentDate, paymentInterval) {
var nextPaymentDate = moment(currentPaymentDate);
switch (paymentInterval) {
case PAYMENT_INTERVAL_DAILY:
nextPaymentDate = nextPaymentDate.add('days', 1);
break;
case PAYMENT_INTERVAL_WEEKLY:
nextPaymentDate = nextPaymentDate.add('weeks', 1);
break;
case PAYMENT_INTERVAL_BIWEEKLY:
nextPaymentDate = nextPaymentDate.add('weeks', 2);
break;
case PAYMENT_INTERVAL_MONTHLY:
nextPaymentDate = nextPaymentDate.add('months', 1);
break;
case PAYMENT_INTERVAL_BIMONTHLY:
nextPaymentDate = nextPaymentDate.add('months', 2);
break;
}
console.log('changing nextPaymentDate from: ' + currentPaymentDate + ' to: ' + nextPaymentDate.toDate());
return nextPaymentDate.toDate();
};
/**
* Method to run before saving the Goal
*/
Parse.Cloud.beforeSave("Goal", function(request, response) {
var goal = request.object;
// only calculate the number of payments if we don't have them already
if (goal.isNew() && !goal.get('numPayments')) {
var today = new Date();
var daysInBetween = daysBetween(today, goal.get("goalDate"));
var numPayments = Math.floor(daysInBetween / goal.get('paymentInterval'));
var paymentAmount = goal.get('amount') / numPayments;
console.log('today: ' + today + ' daysInBetween: ' + daysInBetween + ' numPayments: ' + numPayments + ' paymentAmount' + paymentAmount);
if (numPayments < 1) {
// Date selected is shorter than a successful first payment base on
// the payment interval
// TODO: improve error message
response.error("Date selected shorter than initial payment interval");
return;
}
goal.set("numPayments", numPayments);
goal.set("paymentAmount", paymentAmount);
goal.set("currentTotal", 0);
goal.set('numPaymentsMade', 0);
if (!goal.get('nextPaymentDate')) {
goal.set('nextPaymentDate', getNextPaymentDate(today, goal.get('paymentInterval')));
}
request.object = goal;
}
response.success();
});
/**
* Method to run after saving the Transaction. Will update User & Goal object
* accordingly.
*/
Parse.Cloud.afterSave("Transaction", function(request) {
transaction = request.object;
var promises = [];
queryUser = new Parse.Query(Parse.User);
queryUser.get(transaction.get("user").id).then(function(user) {
Parse.Cloud.useMasterKey();
if (transaction.get("type") == DEBIT_ENUM) {
// Debit transaction, increase cash
user.increment("totalCash", transaction.get("amount"));
} else {
console.log("totalCash: " + user.get("totalCash"));
// Credit transaction, decrease cash
if (!user.get("totalCash")) {
console.log("Empty totalCash!");
totalCash = 0;
} else {
totalCash = user.get("totalCash");
}
totalCash = totalCash - transaction.get("amount");
user.set("totalCash", totalCash);
}
return user.save();
}, function(error) {
console.error('Error fetching user');
logError(error);
}).then(function() {
console.log("User updated successfully!");
}, function(error) {
console.log('Error updating user');
logError(error);
});
promises.push(queryUser);
if (transaction.get("goal")) {
queryGoal = new Parse.Query("Goal");
queryGoal.get(transaction.get("goal").id).then(function(goal) {
if (transaction.get("type") == CREDIT_ENUM) {
// Is a payment event
goal.increment("currentTotal", transaction.get("amount"));
goal.increment("numPaymentsMade");
goal.set('nextPaymentDate', getNextPaymentDate(goal.get('nextPaymentDate'), goal.get('paymentInterval')));
} else {
// Is a cash out event
goal.set("paidOut", true);
}
return goal.save();
}, function(error) {
console.log('Error fetching goal');
logError(error);
}).then(function() {
console.log("Goal updated!");
}, function(error) {
console.log('Error updating goal');
logError(error);
});
promises.push(queryGoal);
}
return Parse.Promise.when(promises);
});
/**
* Method to run after saving the Post. will trigger notification to the
* Lending circle group.
*/
Parse.Cloud.afterSave("Post", function(request) {
// On successful post, broadcast a notification to the lending circle
post = request.object;
console.log('after save for post: ', JSON.stringify(post));
if (!post.existed()) {
console.log("Sending a push notification to the lending circle group after comments.");
var params = {
parentGoalId: post.get("goal").id,
message: request.user.get("name") + " just posted in the lending circle!",
currentUserId: post.get('user').id
};
Parse.Cloud.run('notifyLendingCircleGroup', params);
}
});
/**
* Method to run after saving a Comment. Will trigger notification to the
* Lending circle group.
*/
Parse.Cloud.afterSave('Comment', function(request) {
comment = request.object;
if (!comment.existed()) {
var query = new Parse.Query('Post');
query.equalTo('objectId', comment.get('post').id);
query.find(function(posts) {
// On successful post, broadcast a notification to the lending circle
console.log("Sending a push notification to the lending circle group after comments.");
var params = {
parentGoalId: posts[0].get("goal").id,
message: request.user.get("name") + " just replied to a post!",
currentUserId: post.get('user').id
};
Parse.Cloud.run('notifyLendingCircleGroup', params);
}, function(error) {
logError(error);
});
return query;
}
});
/**
* General Helpers
*/
createPostWithId = function(postId) {
var Post = Parse.Object.extend('Post');
var post = new Post();
post.id = postId;
return post;
};
createGoalWithId = function(goalId) {
var Goal = Parse.Object.extend('Goal');
var goal = new Goal();
goal.id = goalId;
return goal;
};
createUserWithId = function(userId) {
var User = Parse.Object.extend('User');
var user = new User();
user.id = userId;
return user;
};
getTransactions = function(user, type, internalResponse, includeCategory) {
internalResponse = internalResponse || {};
if (!internalResponse.transactions) {
var query = new Parse.Query('Transaction');
query.equalTo('user', user);
query.equalTo('type', type);
query.descending('transactionDate');
if (includeCategory) {
query.include('category');
}
return query.find().then(function(transactions) {
internalResponse.transactions = transactions;
return Parse.Promise.as(transactions);
});
} else {
return Parse.Promise.as(internalResponse.transactions);
}
};
getCategories = function() {
var query = new Parse.Query('Category');
return query.find();
};
addUserToRequest = function(request) {
if (!request.user) {
if (!request.params.userId) {
console.error('"userId" needs to be submitted in order to attach user to the request.');
return;
}
request.user = createUserWithId(request.params.userId);
}
};
addUserToRequestFetch = function(request) {
var promise = new Parse.Promise();
addUserToRequest(request);
getUser(request.user.id).then(function(user) {
request.user = user;
promise.resolve(user);
}, function(error) {
promise.reject(error);
});
return promise;
};
getUser = function(userId) {
var promise = new Parse.Promise();
var query = new Parse.Query('User');
query.equalTo('objectId', userId);
query.find().then(function(results) {
promise.resolve(results[0]);
}, function(error) {
promise.reject(error);
});
return promise;
};
getGoal = function(goalId) {
var promise = new Parse.Promise();
var query = new Parse.Query('Goal');
query.get(goalId, {
success: function(goal) {
promise.resolve(goal);
},
error: function(object, error) {
promise.reject(error);
}
});
return promise;
};
getStrippedDate = function(date) {
return moment(new Date(date.getFullYear(), date.getMonth(), date.getDate())).format('YYYY-MM-DD');
};
getLastSevenDays = function(year, month, day) {
var dates = [];
for (i = 0; i < 7; i++) {
dates.push(moment(new Date(year, month - 1, day - i)).format('YYYY-MM-DD'));
}
return dates;
};
/**
* Helpers for fetching the Stacked Bar Chart Detail View
*/
getTransactionsByDate = function(user, internalResponse) {
internalResponse = internalResponse || {};
var promise = new Parse.Promise();
var transactionsByDate = {};
getTransactions(user, CREDIT_ENUM, internalResponse, true).then(function(transactions) {
_.each(transactions, function(transaction) {
var strippedDate = getStrippedDate(transaction.get('transactionDate'));
var transactionsForDate = transactionsByDate[strippedDate] || [];
transactionsForDate.push(transaction);
transactionsByDate[strippedDate] = transactionsForDate;
});
promise.resolve(transactionsByDate);
});
return promise;
};
getTotalForTransactions = function(transactions, filterFunction) {
var total = 0;
_.each(transactions, function(transaction) {
if (filterFunction(transaction)) {
total += transaction.get('amount');
}
});
return total;
};
spentToday = function(today, transactions) {
return getTotalForTransactions(transactions, function(transaction) {
var transactionDate = moment(transaction.get('transactionDate'));
if (
transaction.get('type') == CREDIT_ENUM &&
transactionDate.year() == today.year() &&
transactionDate.month() == today.month() &&
transactionDate.date() == today.date()
) {
return true;
} else {
return false;
}
});
};
spentThisWeek = function(today, transactions) {
return getTotalForTransactions(transactions, function(transaction) {
var transactionDate = moment(transaction.get('transactionDate'));
if (transaction.get('type') == CREDIT_ENUM && transactionDate.week() == today.week()) {
return true;
} else {
return false;
}
});
};
transactionsTotalByCategoryByDate = function(request, internalResponse) {
var promise = new Parse.Promise();
internalResponse = internalResponse || {};
getTransactions(request.user, CREDIT_ENUM, internalResponse, true).then(function(transactions) {
var transactionsTotalByCategoryByDate = {};
_.each(transactions, function(transaction) {
var strippedDate = getStrippedDate(transaction.get('transactionDate'));
var categoriesForDate = transactionsTotalByCategoryByDate[strippedDate] || {};
var categoryName = transaction.get('category').get('name');
var categoryTotal = categoriesForDate[categoryName] || 0;
categoryTotal += parseFloat(transaction.get('amount'));
categoriesForDate[categoryName] = categoryTotal;
transactionsTotalByCategoryByDate[strippedDate] = categoriesForDate;
});
internalResponse.transactionsTotalByCategoryByDate = transactionsTotalByCategoryByDate;
promise.resolve(internalResponse);
}, function(error) {
logError(error);
promise.reject(error);
});
return promise;
};
stackedBarChart = function(request, internalResponse) {
var promise = new Parse.Promise();
internalResponse = internalResponse || {};
transactionsTotalByCategoryByDate(request, internalResponse).then(function() {
return getCategories();
}).then(function(categories) {
var maxValue = 0;
var hasData = false;
var dates = getLastSevenDays(request.params.year, request.params.month, request.params.day);
var xLabels = [];
var data = [];
internalResponse.dates = [];
_.each(dates, function(date) {
var dateItems = [];
var dateTotal = 0;
var categoriesForDate = internalResponse.transactionsTotalByCategoryByDate[date] || {};
_.each(categories, function(category) {
var categoryTotal = categoriesForDate[category.get('name')] || 0;
if (categoryTotal) {
dateTotal += categoryTotal;
dateItems.push({
categoryName: category.get('name'),
categoryTotal: categoryTotal,
categoryColor: category.get('color')
});
}
});
if (dateTotal) {
internalResponse.dates.push(date);
}
if (dateTotal > maxValue) {
if (!hasData) {
hasData = true;
}
maxValue = dateTotal;
}
data.unshift(dateItems);
xLabels.unshift(moment(date).format('ddd'));
});
internalResponse.stackedBarChart = {
maxValue: maxValue,
data: data,
xLabels: xLabels,
hasData: hasData,
};
promise.resolve(internalResponse);
});
return promise;
};
/**
* Helpers for populating the goal views
*/
getPostsForGoal = function(goalId, internalResponse) {
internalResponse = internalResponse || {};
// fetch posts related to the goals
var query = new Parse.Query('Post');
query.equalTo('goal', createGoalWithId(goalId));
query.descending('createdAt');
query.include('comments');
query.include('comments.user');
query.include('user');
return query.find().then(function(posts) {
internalResponse.posts = posts;
return Parse.Promise.as();
});
};
getParentGoalDetailView = function(request, internalResponse) {
internalResponse = internalResponse || {};
// fetch the parent goal
var query = new Parse.Query('Goal');
query.equalTo('objectId', request.params.parentGoalId);
return query.find().then(function(results) {
// fetch the child goals
var parentGoal = results[0];
internalResponse.parentGoal = parentGoal;
var childQuery = new Parse.Query('Goal');
childQuery.equalTo('parentGoal', parentGoal);
childQuery.include('user');
childQuery.ascending('cashOutDate');
return childQuery.find();
}).then(function(results) {
// collect child goal data
var userGoal,
cashOutSchedule = [],
currentCashOutUserName = '';
_.each(results, function(goal) {
// XXX alternatively this could be goalId
if (goal.get('user').id == request.user.id) {
userGoal = goal;
}
if (currentCashOutUserName === '' && !goal.get('paidOut')) {
currentCashOutUserName = goal.get('user').get('name');
}
cashOutSchedule.push({
userId: goal.get('user').id,
paidOut: goal.get('paidOut'),
profileImageId: goal.get('user').get('profileImageId')
});
});
internalResponse.cashOutSchedule = cashOutSchedule;
internalResponse.userGoal = userGoal;
internalResponse.currentCashOutUserName = currentCashOutUserName;
return Parse.Promise.as();
}).then(function() {
return getPostsForGoal(request.params.parentGoalId, internalResponse);
}).then(function() {
internalResponse.goalDetails = {
currentCashOutUserName: internalResponse.currentCashOutUserName,
cashOutSchedule: internalResponse.cashOutSchedule,
isLendingCircle: true
};
return Parse.Promise.as();
});
};
getGoalDetailView = function(request, internalResponse) {
internalResponse = internalResponse || {};
var query = new Parse.Query('Goal');
query.equalTo('objectId', request.params.goalId);
return query.find().then(function(results) {
internalResponse.userGoal = results[0];
return Parse.Promise();
}).then(function() {
return getPostsForGoal(request.params.goalId, internalResponse);
}).then(function() {
internalResponse.goalDetails = {
nextPaymentDue: moment(internalResponse.userGoal.get('nextPaymentDate')).fromNow(),
isLendingCircle: false
};
return Parse.Promise.as();
});
};
/**
* Helpers for creating lending circles
*/
createParentGoal = function(request, internalResponse) {
internalResponse = internalResponse || {};
var Goal = Parse.Object.extend('Goal');
var goal = new Goal();
users = _.map(request.params.users, function(userId) {
return createUserWithId(userId);
});
goal.set('users', users);
goal.set('name', request.params.name);
goal.set('type', GOAL_TYPE_LENDING_CIRCLE);
goal.set('status', GOAL_STATUS_IN_PROGRESS);
goal.set('amount', request.params.users.length * request.params.paymentAmount);
goal.set('paymentAmount', parseFloat(request.params.paymentAmount));
goal.set('numPayments', request.params.users.length);
return goal.save();
};
/**
* Cloud Functions
*/
/**
* Get the information needed to render the Goal Detail View
* @param {string} goalId The Goal ID you want the details for
* @param {string} parentGoalId The goal's parent id if available
* @param {string} userId The User ID of the user making the request
*/
Parse.Cloud.define('goalDetailView', function(request, response) {
var internalResponse = {};
addUserToRequest(request);
if (request.params.parentGoalId) {
goalDetails = getParentGoalDetailView(request, internalResponse);
} else {
goalDetails = getGoalDetailView(request, internalResponse);
}
goalDetails.then(function() {
response.success({
goal: internalResponse.userGoal,
goalDetails: internalResponse.goalDetails,
posts: internalResponse.posts
});
});
});
/**
* Get the information needed to render the Stacked Bar Chart Detail View
* @param {string} userId User ID making the request
* @param {date} today Date representing today's date
* @param {number} year Year of the latest transaction you want to display
* @param {number} month Month of the latest transaction you want to display
* @param {number} day Day of the latest transaction you want to display
*/
Parse.Cloud.define('stackedBarChartDetailView', function(request, response) {
var internalResponse = {};
getUser(request.params.userId).then(function(user) {
request.user = user;
return stackedBarChart(request, internalResponse);
}).then(function() {
return getTransactionsByDate(request.user, internalResponse);
}).then(function(transactionsByDate) {
response.success({
transactionsByDate: transactionsByDate,
stackedBarChart: internalResponse.stackedBarChart,
dates: internalResponse.dates,
spentThisWeek: spentThisWeek(moment(request.params.today), internalResponse.transactions),
spentToday: spentToday(moment(request.params.today), internalResponse.transactions),
totalCash: request.user.get('totalCash')
});
});
});
/**
* Create a lending circle for a list of users
* @param {list} users List of users in the lending circle (should be ordered by date they're going to get cashed out)
* @param {number} year Year of the first cash out date
* @param {number} month Month of the first cash out date
* @param {number} day Day of the first cash out date
* @param {string} name Name of the lending circle
* @param {string} paymentAmount Amount of the monthly payments
*/
Parse.Cloud.define('createLendingCircle', function(request, response) {
var internalResponse = {};
var firstPayment = moment([request.params.year, request.params.month - 1, request.params.day]);
var goalDate = moment([request.params.year, request.params.month -1, request.params.day]).add('months', request.params.users.length).toDate();
createParentGoal(request, internalResponse).then(function(parentGoal) {
internalResponse.parentGoal = parentGoal;
var goals = [];
var Goal = Parse.Object.extend('Goal');
var nextCashOutIncrement = 0;
_.each(request.params.users, function(userId) {
var goal = new Goal();
var nextCashOut = moment([request.params.year, request.params.month - 1, 20]).add('months', nextCashOutIncrement);
goal.set('user', createUserWithId(userId));
goal.set('name', parentGoal.get('name'));
goal.set('type', parentGoal.get('type'));
goal.set('status', parentGoal.get('status'));
goal.set('amount', parentGoal.get('amount'));
goal.set('paymentAmount', parentGoal.get('paymentAmount'));
goal.set('numPayments', parentGoal.get('numPayments'));
goal.set('numPaymentsMade', 0);
goal.set('currentTotal', 0);
goal.set('parentGoal', parentGoal);
goal.set('cashOutDate', nextCashOut.toDate());
goal.set('paidOut', false);
goal.set('nextPaymentDate', firstPayment.toDate());
goal.set('paymentInterval', PAYMENT_INTERVAL_MONTHLY);
goal.set('goalDate', goalDate);
goals.push(goal);
nextCashOutIncrement += 1;
});
return Parse.Object.saveAll(goals);
}).then(function(goals) {
response.success({
parentGoal: internalResponse.parentGoal,
userGoals: goals
});
}, function(error) {
logError(error);
response.error();
});
});
/**
* Create a post
* @param {string} userId User ID of the user making the post
* @param {string} goalId Goal ID the post is related to
* @param {string} content Content of the post
* @param {number} type Type of post
* @param {string} toUserId The user id of the recipient (only applicable for private posts)
*/
Parse.Cloud.define('createPost', function(request, response) {
var Post = Parse.Object.extend('Post');
var post = new Post();
addUserToRequest(request);
post.set('user', request.user);
post.set('goal', createGoalWithId(request.params.goalId));
post.set('content', request.params.content);
post.set('type', request.params.type);
if (request.params.toUserId) {
post.set('toUser', request.params.toUserId);
}
post.save().then(function(post) {
response.success({
success: true,
post: post,
});
}, function(error) {
logError(error);
response.error();
});
});
/**
* Create a comment for a post
* @param {string} postId The post the comment is associated with
* @param {string} userId The user posting the comment
* @param {string} content The contents of the commment
*/
Parse.Cloud.define('createComment', function(request, response) {
var Comment = Parse.Object.extend('Comment');
var comment = new Comment();
var post = createPostWithId(request.params.postId);
addUserToRequest(request);
comment.set('user', request.user);
comment.set('post', post);
comment.set('content', request.params.content);
post.add('comments', comment);
post.save().then(function(post) {
response.success({
success: true,
comment: post,
});
});
});
/**
* Fetch the data for the stacked bar chart.
* This will return the following structure:
* {
* 'maxValue': <maximum total amongst all the dates>,
* 'xLabels': <list of date strings to display as the xLabels>
* 'data': <list of arrays which represent data points for each bar>
* }
* @param {string} userId User ID making the request
* @param {number} year Year of the latest transaction you want to display
* @param {number} month Month of the latest transaction you want to display
* @param {number} day Day of the latest transaction you want to display
*/
Parse.Cloud.define('stackedBarChart', function(request, response) {
var internalResponse = {};
addUserToRequest(request);
stackedBarChart(request, internalResponse).then(function() {
response.success(internalResponse.stackedBarChart);
});
});
/**
* Method to record user payment
* @param {string} request.params.userId
* @param {string} request.params.goalId
* @return response.success or response.error
*/
Parse.Cloud.define("recordPayment", function(request, response) {
addUserToRequest(request);
getGoal(request.params.goalId).then(function(goal) {
var Transaction = Parse.Object.extend("Transaction");
var transaction = new Transaction();
// Category
var Category = Parse.Object.extend("Category");
var category = new Category();
category.id = BILLS_CATEGORY_ID;
transaction.set("user", request.user);
transaction.set("amount", goal.get("paymentAmount"));
transaction.set("name", "Lending circle Payment");
transaction.set("goal", goal);
transaction.set("transactionDate", new Date());
transaction.set("type", CREDIT_ENUM); // CREDIT
transaction.set("category", category);
return transaction.save();
}).then(function(transaction) {
// the save succeed, lets send a push notification to the user
var query = new Parse.Query(Parse.Installation);
query.equalTo('user', request.user);
console.log("Trying to send a push notification...");
Parse.Push.send({
where: query, // Set our Installation query
data: {
alert: "Payment received."
}
}, {
success: function() {
// Push was successful
console.log("Push notification sent!");
},
error: function(error) {
// Handle error
console.error("Push notification failed");
logError(error);
}
});
// Response will be sent regardless push outcome
response.success();
}, function(error) {
// The save failed
response.error("Oops! error recording payment.");
});
});
/**
* Method to record cash out event
* @param {string} request.params.userId
* @param {string} request.params.goalId
* @return response.success or response.error
*/
Parse.Cloud.define("recordCashOut", function(request, response) {
addUserToRequest(request);
getGoal(request.params.goalId).then(function(goal) {
if (!goal.get("paidOut")) {
var Transaction = Parse.Object.extend("Transaction");
var transaction = new Transaction();
// Category
var Category = Parse.Object.extend("Category");
var category = new Category();
category.id = BILLS_CATEGORY_ID;
transaction.set("user", request.user);
transaction.set("amount", goal.get("amount"));
transaction.set("name", "Lending circle Cash Out");
transaction.set("goal", goal);
transaction.set("transactionDate", new Date());
transaction.set("type", DEBIT_ENUM); // DEBIT
transaction.set("category", category);
return transaction.save();
} else {
return Parse.Promise.error("the goal for the user had already being cashed out.");
}
}).then(function(transaction) {
// the save succeed, lets send a push notification to the user
var query = new Parse.Query(Parse.Installation);
query.equalTo('user', request.user);
console.log("Trying to send a push notification...");
Parse.Push.send({
where: query, // Set our Installation query
data: {
alert: "Lending Circle loan successfully added to your account"
}
}, {
success: function() {
// Push was successful
console.log("Push notification sent!");
},
error: function(error) {
// Handle error
console.error("Push notification failed");
logError(error);
}
});
// Response will be sent regardless push outcome
response.success();
}, function(error) {
// The save failed
response.error("Oops! error recording a cash out. " + error);
});
});
/**
* Fetch the data for the main dashboard view
* @param {string} request.params.userId
*/
Parse.Cloud.define('dashboardView', function(request, response) {
var internalResponse = {};
addUserToRequestFetch(request).then(function() {
return getTransactions(request.user, CREDIT_ENUM, internalResponse).then(function(transactions) {
internalResponse.xLabels = [];
internalResponse.chartData = [];
internalResponse.transactionsTotalByWeek = {};
_.each(transactions, function(transaction) {
var week = moment(transaction.get('transactionDate')).endOf('week').format('MMM D');
if (_.indexOf(internalResponse.xLabels, week) === -1) {
// want the later dates to be at the end of the list
internalResponse.xLabels.unshift(week);
}
totalByWeek = internalResponse.transactionsTotalByWeek[week] || 0;
totalByWeek += transaction.get('amount');
internalResponse.transactionsTotalByWeek[week] = totalByWeek;
});
_.each(internalResponse.xLabels, function(week) {
internalResponse.chartData.push(internalResponse.transactionsTotalByWeek[week]);
});
return Parse.Promise.as();
});
}).then(function() {
var query = new Parse.Query('Goal');
query.equalTo('user', request.user);
query.ascending('type');
return query.find();
}).then(function(goals) {
internalResponse.goalToPrettyDueDate = {};
internalResponse.goals = goals;
internalResponse.goals = _.each(goals, function(goal) {
internalResponse.goalToPrettyDueDate[goal.id] = {
prettyDate: moment(goal.get('nextPaymentDate')).fromNow(),
warning: moment(goal.get('nextPaymentDate')).diff(new Date(), 'days') <= 7 ? true : false
};
});
return Parse.Promise.as();
}).then(function() {
response.success({
lineChart: {
data: internalResponse.chartData,
xLabels: internalResponse.xLabels
},
goals: internalResponse.goals,
goalToPrettyDueDate: internalResponse.goalToPrettyDueDate,
totalCash: request.user.get('totalCash')
});
});
});
/**
* Broadcast a message to all users participating on the Lending Circle
*
* @param {string} parentGoalId Parent Goal ID to broadcast message to
* @param {string} message Message to broadcast
*/
Parse.Cloud.define('notifyLendingCircleGroup', function(request, response) {
// get all users participating on the goal
var queryGoal = new Parse.Query('Goal');
queryGoal.include("users");
queryGoal.get(request.params.parentGoalId).then(function(goal) {
return goal.get("users");
}).then(function(users) {
// filter out current user
console.log('before users: ' + users.length);
users = _.filter(users, function(user) {
return user.id !== request.params.currentUserId;
});
console.log("after users: " + users.length);
// query all app installations containing those users id
var query = new Parse.Query(Parse.Installation);
query.containedIn('user', users);
// send out the message with push.send
console.log("Trying to send a push notification...");
return Parse.Push.send({
where: query, // Set our Installation query
data: {
alert: request.params.message
}
});
}).then(function() {
// push notification succeed
response.success();
}, function(error) {
// push notifications failed
response.error("Oops! error sending push notifications. e: " + error);
});
});