-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbObjects.js
More file actions
114 lines (100 loc) · 4 KB
/
dbObjects.js
File metadata and controls
114 lines (100 loc) · 4 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
const Sequelize = require('sequelize');
const { dbPassword } = require('./config.json');
const sequelize = new Sequelize('foothill', 'manager', dbPassword, {
host: 'localhost',
dialect: 'sqlite',
logging: false,
storage: 'data.sqlite',
});
const Balance = require('./models/money/Balance.js')(sequelize, Sequelize.DataTypes);
const Category = require('./models/money/Category.js')(sequelize, Sequelize.DataTypes);
const MonthlyBudget = require('./models/money/MonthlyBudget.js')(sequelize, Sequelize.DataTypes);
const Status = require('./models/money/Status.js')(sequelize, Sequelize.DataTypes);
const Transaction = require('./models/money/Transaction.js')(sequelize, Sequelize.DataTypes);
const UserBalance = require('./models/money/UserBalance.js')(sequelize, Sequelize.DataTypes);
Balance.belongsTo(Category, { foreignKey: 'categoryId', onDelete: 'CASCADE' });
Balance.belongsTo(MonthlyBudget, { foreignKey: 'monthlyBudgetId', onDelete: 'CASCADE' });
Balance.hasMany(UserBalance, { as: 'userBalances', foreignKey: 'balanceId', onDelete: 'CASCADE' });
MonthlyBudget.hasMany(Balance, { as: 'balances', foreignKey: 'monthlyBudgetId', onDelete: 'CASCADE' });
Transaction.belongsTo(Category, { foreignKey: 'categoryId', onDelete: 'CASCADE' });
Transaction.belongsTo(UserBalance, { foreignKey: 'userBalanceId', onDelete: 'CASCADE' });
Transaction.belongsTo(Status, { foreignKey: 'statusId', onDelete: 'CASCADE' });
Transaction.belongsTo(MonthlyBudget, { foreignKey: 'monthlyBudgetId', onDelete: 'CASCADE' });
UserBalance.belongsTo(Balance, { foreignKey: 'balanceId', onDelete: 'CASCADE' });
// Reflect.defineProperty(Users.prototype, 'addItem', {
// value: async item => {
// const userItem = await UserItems.findOne({
// where: { user_id: this.user_id, item_id: item.id },
// });
// if (userItem) {
// userItem.amount += 1;
// return userItem.save();
// }
// return UserItems.create({ user_id: this.user_id, item_id: item.id, amount: 1 });
// },
// });
// // Reflect.defineProperty(Users.prototype, 'addItem', {
// // value: async item => {
// // const userItem = await UserItems.findOne({
// // where: { user_id: this.user_id, item_id: item.id },
// // });
// // if (userItem) {
// // userItem.amount += 1;
// // return userItem.save();
// // }
// // return UserItems.create({ user_id: this.user_id, item_id: item.id, amount: 1 });
// // },ff
// // });
Reflect.defineProperty(MonthlyBudget.prototype, 'getAllBalances', {
value: async budgetId => {
const monthlyBudget = await MonthlyBudget.findOne({
where: { id: budgetId },
});
let total_user_balances = [];
const budget_balances = await monthlyBudget.getBalances()
// console.log(budget_balances)
let count = 0;
for (const temp_budget_balance of budget_balances) {
const temp_user_balancessss = await temp_budget_balance.getUserBalances()
const temp_user_balances = temp_user_balancessss
count++;
if (count == 1) {
total_user_balances = temp_user_balances
} else {
temp_user_balances.forEach(b => {
total_user_balances.push(b)
})
}
}
return total_user_balances
// // return UserItems.findAll({
// // where: { user_id: this.user_id },
// // include: ['item'],
// // });
},
});
Reflect.defineProperty(MonthlyBudget.prototype, 'getAUserBalances', {
value: async (userId, budgetId) => {
// super
const monthlyBudget = await MonthlyBudget.findOne({
where: { id: budgetId },
});
let single_user_balances = [];
const budget_balances = await monthlyBudget.getBalances()
let count = 0;
for (const temp_budget_balance of budget_balances) {
const temp_user_balancessss = await temp_budget_balance.getUserBalances({ where: { userId: userId } })
const temp_user_balances = temp_user_balancessss
count++;
if (count == 1) {
single_user_balances = temp_user_balances
} else {
temp_user_balances.forEach(b => {
single_user_balances.push(b)
})
}
}
return single_user_balances
},
});
module.exports = { Balance, Category, MonthlyBudget, Status, Transaction, UserBalance };