Skip to content

Commit

Permalink
feat: add user's default categories
Browse files Browse the repository at this point in the history
在用戶創建的時候,也一併給予預設的類別
  • Loading branch information
JHIH-LEI committed Aug 14, 2021
1 parent 200b25c commit fe34a63
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
4 changes: 3 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ const methodOverride = require('method-override')
const bodyParser = require('body-parser')
const helpers = require('handlebars-helpers')
const comparison = helpers.comparison()
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
// 使用flash
require('dotenv').config()
const session = require('express-session')
const cookieParser = require('cookie-parser')
const flash = require('connect-flash')
Expand Down
4 changes: 3 additions & 1 deletion config/mongoose.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require('dotenv').config()
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const mongoose = require('mongoose')
const MONGODB_URI = process.env.MONGODB_URI
mongoose.connect(MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true })
Expand Down
2 changes: 2 additions & 0 deletions routes/modules/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const router = express.Router()
const passport = require('passport')
const bcrypt = require('bcryptjs')
const User = require('../../models/user')
const { generateCategory } = require('../../tools/helper')

router.get('/login', (req, res) => {
res.render('login')
Expand Down Expand Up @@ -41,6 +42,7 @@ router.post('/register', (req, res) => {
.then(salt => bcrypt.hash(password, salt))
.then(hash => {
User.create({ name, email, password: hash })
.then(user => generateCategory(user))
.then(() => res.redirect('/'))
.catch(err => console.log(err))
})
Expand Down
8 changes: 8 additions & 0 deletions tools/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const User = require('../models/user')
const Record = require('../models/record')
const Category = require('../models/category')
const bcrypt = require('bcryptjs')
const defaultCategoryList = require('../categories.json')
const functions = {
getIcon: function (recordCategory, categories) {
//返回該支出的類別物件
Expand All @@ -26,6 +27,9 @@ const functions = {
.then(salt => bcrypt.hash(randomPassword, salt))
.then(hash => {
User.create({ email, name, password: hash })
.then(user => {
return functions.generateCategory(user._id) //為帳戶附上預設類別選項
})
})
}
})
Expand Down Expand Up @@ -69,6 +73,10 @@ const functions = {
categories.forEach(category => categoryList.push(category.name))
})
return categoryList
},
generateCategory: function (userId) {
defaultCategoryList.forEach(category => category.userId = userId)
return Category.create(defaultCategoryList)
}
}

Expand Down

0 comments on commit fe34a63

Please sign in to comment.