-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.js
257 lines (224 loc) · 6.58 KB
/
seed.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
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
'use strict'
const chalk = require('chalk');
const db = require('./server/db');
const Promise = require('sequelize').Promise;
const _ = require('lodash');
const R = require('Ramda');
const curry = R.curry;
const Account = db.model('account');
const Category = db.model('category');
const Transaction = db.model('transaction');
const Budget = db.model('budget');
const Merchant = db.model('merchant');
const Tag = db.model('tag');
const bluebird = require('bluebird');
const numAccounts = 6;
const numMerchants = 6;
const numCategories = 7;
const CreditAccountId = [2,6];
const NonCreditAccountId = [1,3,4,5];
const numberOfTransaction = 50;
// --------------------------------Account Seed--------------------------------------------//
function seedAccount() {
console.log(chalk.yellow('seeding account.'));
const accountObjs = [{
name: 'HSBC Simple',
type: 'Checking'
}, {
name: 'American Express Centurion Card',
type: 'Credit'
}, {
name: 'Chase',
type: 'Checking'
}, {
name: 'PNC',
type: 'Debit'
}, {
name: 'Bank of America',
type: 'Savings'
}, {
name: 'Chase Visa',
type: 'Credit'
}];
const creatingAccounts = accountObjs.map(accountObj => Account.create(accountObj))
return Promise.all(creatingAccounts)
}
// ---------------------------------Category Seed----------------------------------------------//
function seedCategory() {
console.log(chalk.yellow('Seeding Category'));
const categoryObjs = [{
name: 'Education'
}, {
name: 'Insurance'
}, {
name: 'Auto & Transport'
}, {
name: 'Food & Dining'
}, {
name: 'Housing'
}, {
name: 'Entertainment'
}, {
name: 'Income'
}
];
const creatingCategory = categoryObjs.map(categoryObj => Category.create(categoryObj));
return Promise.all(creatingCategory)
}
// -----------------------------------Merchant Seed-----------------------------------------------//
function seedMerchant() {
console.log(chalk.yellow('Seeding Merchant'));
const merchantObjs = [{
name: 'MIT',
categoryId: 1
}, {
name: 'MetLife',
categoryId: 2
}, {
name: 'Delta Air Lines',
categoryId: 3
}, {
name: 'Walmart',
categoryId: 4
}, {
name: 'American Mortgage Co',
categoryId: 5
}, {
name: 'Bestbuy',
categoryId: 6
}];
const creatingMerchant = merchantObjs.map((merchantObj) => Merchant.create(merchantObj));
return Promise.all(creatingMerchant)
}
// ------------------------------------Budget Seed-------------------------------------------------//
function seedBudget() {
console.log(chalk.yellow('Seeding Budget'));
const budgetObjs = [{
name: 'College Loan',
type: 'spending',
categoryId: 1
}, {
name: 'College textbook spending',
type: 'spending',
categoryId: 1
}, {
name: 'College Party',
type: 'spending',
categoryId: 1
}, {
name: 'Gas & Fuel',
type: 'spending',
categoryId: 3
}, {
name: 'Restaurants',
type: 'spending',
categoryId: 4
}, {
name: 'Groceries',
type: 'spending',
categoryId: 4
}];
const creatingBudget = budgetObjs.map(budgetObj => Budget.create(budgetObj));
return Promise.all(creatingBudget);
}
// ----------------------------------Transaction Seed-----------------------------------------------//
let randomType = [
'DEBIT',
'CREDIT',
'TRANSFER',
'DEPOSIT'
];
let randomDateGen = (monthsAway) =>
() => {
let currentDate = new Date(),
randomNum = _.random(-monthsAway, monthsAway);
return new Date(currentDate.getFullYear(), currentDate.getMonth() + randomNum, _.random(1, 27)).valueOf();
};
let randomDate2MonthsAway = randomDateGen(2);
let randomAmount = () => _.round(_.random(-1000, 2500, true), 2);
let randomAmountSpending = () => _.round(_.random(-100,-10, true),2)
let randomAmountSaving = () => _.round(_.random(100,300, true),2)
let randomTransaction = (accountId,status) => {
let amount;
if(status==="spending") amount = randomAmountSpending(); //it is negative amount for spending
if(status==="saving") amount = randomAmountSaving();
return {
amount: amount,
date: randomDate2MonthsAway(),
type: _.sample(randomType),
accountId: accountId,
categoryId: _.random(1, numCategories),
merchantId: _.random(1, numMerchants),
fitid: _.random(100000000, 900000000)
}
}
let randomTransactions = (numOfTransaction,savingDays,accountIdArray) => {
return accountIdArray.map(accountId => {
let range = new Array(numOfTransaction).fill(0);
let transactions = [];
for(let i=1;i<=numOfTransaction;i++) transactions.push(i);
let randomSavingDay = _.sampleSize(transactions, savingDays);
return transactions.map(day => {
if(randomSavingDay.indexOf(day)!==-1) return randomTransaction(accountId,'saving');
else return randomTransaction(accountId,'spending');
})
})
}
function promiseNester(promisifiedFunc, argsArray) {
if (argsArray.length === 1)
return promisifiedFunc(argsArray[0])
return promisifiedFunc(argsArray[0])
.then(function(resolvedContent) {
return promiseNester(promisifiedFunc, argsArray.slice(1))
});
}
function seedTransaction() {
console.log(chalk.yellow('seeding Transaction'));
let creatingTransactionCredit = randomTransactions(30,1,CreditAccountId);
let creatingTransactionNonCredit = randomTransactions(10,5,NonCreditAccountId);
let transactionObjs = creatingTransactionCredit.concat(creatingTransactionNonCredit);
transactionObjs = _.flatten(transactionObjs)
return promiseNester(Transaction.upsert.bind(Transaction),transactionObjs)
}
function setMerchant1ToCat1(updatedMerchant = { categoryId: 1 }) {
return Merchant.findById(1)
.then(merchant => {
return merchant.updateWithTransactions(updatedMerchant);
})
}
// ----------------------------------Datebase Sync-------------------------------------------------//
db.sync({
force: true
})
.then(() => {
console.log(chalk.green('Forcing Sync on DB Successful'));
return seedAccount();
})
.then(() => {
console.log(chalk.green('Account Seeding Successful'));
return seedCategory();
})
.then(() => {
console.log(chalk.green('Category Seeding Successful'));
return seedMerchant();
})
.then(() => {
console.log(chalk.green('Merchant Seeding Successful'));
return seedTransaction();
})
.then(() => {
console.log(chalk.green('Transaction Seeding Successful'));
return seedBudget();
})
.then(() => {
console.log(chalk.green('Set Merchant 1 To Cat 1'));
return setMerchant1ToCat1();
})
.then(() => {
console.log(chalk.blue('finish seeding'));
process.exit(0);
})
.catch(err => {
console.error(err);
process.exit(1)
});