-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate.js
94 lines (83 loc) · 2.77 KB
/
populate.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
// Mockaroo https://www.mockaroo.com/
import { readFile } from 'fs/promises';
import dotenv from 'dotenv';
dotenv.config();
import connectDB from './db/connection.js';
import Transaction from './models/TransactionModel.js';
const random = (length) => Math.floor(Math.random() * length);
const rationalizeData = (data) => {
const income_mapping = {
payment_mode: 'cash',
description: [
'Income from sale',
'Income from apartment rent',
'Income from stock market investment',
'Extra income from a game of bet'
],
category: ['salary', 'rent', 'stock', 'extra income']
};
const expense_mapping = {
description: [
'Cars and trucks used',
'Palmetto cheese and Mint julep',
'Peanuts in coke',
'Laundry and cleaning supplies',
'Expense for education',
'Muffulatte sandwich Mint julep',
'Pair of running shoes',
'Other vehicle expenses',
'Beauty care things'
],
category: [
'food',
'transportation',
'housing',
'clothing',
'education',
'shopping',
'trip'
]
};
const TRANSACTION_TYPES = {
expense: 'expense',
income: 'income'
};
Object.freeze(TRANSACTION_TYPES);
if (data !== undefined && data.length > 0) {
for (let i = 0; i < data.length; i++) {
if (data[i].type === TRANSACTION_TYPES['expense']) {
data[i].description =
expense_mapping.description[
random(expense_mapping.description.length)
];
data[i].category =
expense_mapping.category[random(expense_mapping.category.length)];
}
if (data[i].type === TRANSACTION_TYPES['income']) {
data[i].description =
income_mapping.description[random(income_mapping.description.length)];
data[i].category =
income_mapping.category[random(income_mapping.category.length)];
data[i].payment_mode = income_mapping.payment_mode;
}
}
console.log(data)
return data;
} else return null;
};
const run = async () => {
try {
await connectDB(process.env.MONGO_URI);
await Transaction.deleteMany();
const jsonProducts = JSON.parse(
await readFile(new URL('./mock-data.json', import.meta.url))
);
await Transaction.create(rationalizeData(jsonProducts));
console.log('Success!!!!');
process.exit(0);
} catch (error) {
console.log(error);
process.exit(1);
}
};
run();