mirror of
https://github.com/HackPlan/RootPanel.git
synced 2026-04-23 11:17:54 +08:00
refactor models
This commit is contained in:
@@ -25,6 +25,7 @@ app.libs =
|
||||
|
||||
ObjectId: (require 'mongoose').Schema.Types.ObjectId
|
||||
|
||||
async = require 'async'
|
||||
{cookieParser, copy, crypto, bodyParser, depd, express, fs, harp, middlewareInjector, mongoose} = exports.libs
|
||||
{morgan, nodemailer, path, redis, redisStore, expressSession} = exports.libs
|
||||
|
||||
@@ -73,11 +74,8 @@ app.config = config
|
||||
app.db = require './core/db'
|
||||
app.pluggable = require './core/pluggable'
|
||||
|
||||
app.schemas = {}
|
||||
app.models = {}
|
||||
|
||||
app.pluggable.initializePlugins()
|
||||
|
||||
require './core/model/account'
|
||||
require './core/model/balance_log'
|
||||
require './core/model/coupon_code'
|
||||
@@ -85,9 +83,7 @@ require './core/model/notification'
|
||||
require './core/model/security_log'
|
||||
require './core/model/ticket'
|
||||
|
||||
for name, schema of app.schemas
|
||||
model = mongoose.model name, schema
|
||||
app.models[name] = model
|
||||
app.pluggable.initializePlugins()
|
||||
|
||||
app.templates = require './core/templates'
|
||||
app.i18n = require './core/i18n'
|
||||
|
||||
@@ -8,9 +8,9 @@ if user and password
|
||||
else
|
||||
mongodb_uri = "mongodb://#{host}/#{name}"
|
||||
|
||||
module.exports = mongoose.createConnection mongodb_uri
|
||||
|
||||
exports.mongodb_uri = mongodb_uri
|
||||
mongoose.connect mongodb_uri
|
||||
|
||||
mongoose.connection.on 'error', (err) ->
|
||||
console.error err if err
|
||||
|
||||
module.exports = mongoose.connection
|
||||
|
||||
@@ -6,7 +6,7 @@ Token = mongoose.Schema
|
||||
type:
|
||||
required: true
|
||||
type: String
|
||||
enum: ['full_access'].concat selectModelEnum 'Token', 'type'
|
||||
enum: ['full_access']
|
||||
|
||||
token:
|
||||
required: true
|
||||
@@ -39,6 +39,10 @@ Account = mongoose.Schema
|
||||
password_salt:
|
||||
type: String
|
||||
|
||||
created_at:
|
||||
type: Date
|
||||
default: Date.now
|
||||
|
||||
groups:
|
||||
type: Array
|
||||
default: []
|
||||
@@ -80,50 +84,32 @@ Account = mongoose.Schema
|
||||
type: Object
|
||||
default: {}
|
||||
|
||||
_.extend app.schemas,
|
||||
Account: Account
|
||||
Token: Token
|
||||
|
||||
# @param account: username, email, password
|
||||
# @param callback(account)
|
||||
exports.register = (account, callback) ->
|
||||
Account.statics.register = (account, callback) ->
|
||||
password_salt = utils.randomSalt()
|
||||
|
||||
{username, email, password} = account
|
||||
|
||||
account =
|
||||
account = new Account
|
||||
username: username
|
||||
email: email
|
||||
password: utils.hashPassword(password, password_salt)
|
||||
password_salt: password_salt
|
||||
email: email
|
||||
created_at: new Date()
|
||||
|
||||
groups: []
|
||||
|
||||
preferences:
|
||||
avatar_url: "//ruby-china.org/avatar/#{utils.md5(email)}?s=58"
|
||||
language: 'auto'
|
||||
timezone: config.i18n.default_timezone
|
||||
|
||||
billing:
|
||||
services: []
|
||||
plans: []
|
||||
|
||||
balance: 0
|
||||
last_billing_at: {}
|
||||
arrears_at: null
|
||||
|
||||
pluggable: {}
|
||||
|
||||
resources_limit: {}
|
||||
|
||||
tokens: []
|
||||
|
||||
async.each pluggable.selectHook(account, 'account.before_register'), (hook, callback) ->
|
||||
hook.filter account, callback
|
||||
, ->
|
||||
exports.insert account, (err, result) ->
|
||||
callback _.first result
|
||||
account.save ->
|
||||
callback account
|
||||
|
||||
_.extend app.models,
|
||||
Account: mongoose.model 'Account', Account
|
||||
|
||||
exports.updatePassword = (account, password, callback) ->
|
||||
password_salt = utils.randomSalt()
|
||||
|
||||
@@ -11,7 +11,7 @@ BalanceLog = mongoose.Schema
|
||||
type:
|
||||
required: true
|
||||
type: String
|
||||
enum: ['deposit'].concat selectModelEnum 'BalanceLog', 'type'
|
||||
enum: ['deposit']
|
||||
|
||||
amount:
|
||||
required: true
|
||||
@@ -25,5 +25,5 @@ BalanceLog = mongoose.Schema
|
||||
type: Object
|
||||
default: {}
|
||||
|
||||
_.extend app.schemas,
|
||||
BalanceLog: BalanceLog
|
||||
_.extend app.models,
|
||||
BalanceLog: mongoose.model 'BalanceLog', BalanceLog
|
||||
|
||||
@@ -18,7 +18,7 @@ CouponCode = mongoose.Schema
|
||||
type:
|
||||
required: true
|
||||
type: String
|
||||
enum: ['amount'].concat selectModelEnum 'CouponCode', 'type'
|
||||
enum: ['amount']
|
||||
|
||||
meta:
|
||||
type: Object
|
||||
@@ -35,8 +35,8 @@ CouponCode = mongoose.Schema
|
||||
default: Date.now
|
||||
]
|
||||
|
||||
_.extend app.schemas,
|
||||
CouponCode: CouponCode
|
||||
_.extend app.models,
|
||||
CouponCode: mongoose.model 'CouponCode', CouponCode
|
||||
|
||||
exports.type_meta =
|
||||
amount:
|
||||
|
||||
@@ -15,12 +15,12 @@ Notification = mongoose.Schema
|
||||
type:
|
||||
required: true
|
||||
type: String
|
||||
enum: ['payment_success'].concat selectModelEnum 'Notification', 'type'
|
||||
enum: ['payment_success']
|
||||
|
||||
level:
|
||||
required: true
|
||||
type: String
|
||||
enum: ['notice', 'event', 'log'].concat selectModelEnum 'Notification', 'type'
|
||||
enum: ['notice', 'event', 'log']
|
||||
|
||||
created_at:
|
||||
type: Date
|
||||
@@ -30,8 +30,8 @@ Notification = mongoose.Schema
|
||||
type: Object
|
||||
default: {}
|
||||
|
||||
_.extend app.schemas,
|
||||
Notification: Notification
|
||||
_.extend app.models,
|
||||
Notification: mongoose.model 'Notification', Notification
|
||||
|
||||
exports.createNotice = (account, group_name, type, level, meta, callback) ->
|
||||
exports.insert
|
||||
|
||||
@@ -11,7 +11,7 @@ SecurityLog = mongoose.Schema
|
||||
type:
|
||||
required: true
|
||||
type: String
|
||||
enum: ['update_password', 'update_setting', 'update_email'].concat selectModelEnum 'SecurityLog', 'type'
|
||||
enum: ['update_password', 'update_setting', 'update_email']
|
||||
|
||||
created_at:
|
||||
type: Date
|
||||
@@ -26,8 +26,8 @@ SecurityLog = mongoose.Schema
|
||||
type: ObjectId
|
||||
ref: 'Token'
|
||||
|
||||
_.extend app.schemas,
|
||||
SecurityLog: SecurityLog
|
||||
_.extend app.models,
|
||||
SecurityLog: mongoose.model 'SecurityLog', SecurityLog
|
||||
|
||||
exports.create = (account, type, token, payload, callback) ->
|
||||
matched_token = _.findWhere account.tokens,
|
||||
|
||||
@@ -50,7 +50,7 @@ Ticket = mongoose.Schema
|
||||
|
||||
status:
|
||||
type: String
|
||||
enum: ['open', 'pending', 'finish', 'closed'].concat selectModelEnum 'Ticket', 'type'
|
||||
enum: ['open', 'pending', 'finish', 'closed']
|
||||
|
||||
option:
|
||||
type: Object
|
||||
@@ -64,8 +64,8 @@ Ticket = mongoose.Schema
|
||||
Reply
|
||||
]
|
||||
|
||||
_.extend app.schemas,
|
||||
Ticket: Ticket
|
||||
_.extend app.models,
|
||||
Ticket: mongoose.model 'Ticket', Ticket
|
||||
|
||||
exports.createTicket = (account, title, content, members, status, options, callback) ->
|
||||
exports.insert
|
||||
|
||||
@@ -132,15 +132,6 @@ exports.selectHook = (account, hook_name) ->
|
||||
else
|
||||
return false
|
||||
|
||||
exports.selectModelEnum = (model, field) ->
|
||||
result = []
|
||||
|
||||
for hook in exports.selectHook null, 'model.type_enum'
|
||||
if hook.model == model and hook.field == field
|
||||
result.push hook.type
|
||||
|
||||
return result
|
||||
|
||||
exports.initializePlugins = (callback) ->
|
||||
checkDependencies = ->
|
||||
all_plugins = _.union config.plugin.available_extensions, config.plugin.available_services
|
||||
|
||||
@@ -37,9 +37,6 @@ describe 'app', ->
|
||||
it 'session.key should exists', ->
|
||||
fs.existsSync("#{__dirname}/../../session.key").should.be.ok
|
||||
|
||||
it 'schemas should be created', ->
|
||||
app.schemas.should.not.be.empty
|
||||
|
||||
it 'models should be available', ->
|
||||
{Account, Ticket} = app.models
|
||||
Account.find.should.be.a 'function'
|
||||
|
||||
Reference in New Issue
Block a user