ozserver
Version:
API for OZ
75 lines (62 loc) • 2.47 kB
JavaScript
var EventEmitter, Registration, User, colors, exports, jade, mongoose,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
mongoose = require('mongoose');
colors = require('colors');
jade = require('jade');
EventEmitter = require('events').EventEmitter;
User = mongoose.model('User', require(global.home + '/script/views/db/user'));
Registration = (function(_super) {
__extends(Registration, _super);
function Registration(req, res) {
var _this = this;
this.req = req;
this.res = res;
this.on('send', function() {
return _this.res.jsonp(_this.mdl.model);
});
this.on('success', function() {
var ObjectId, user;
ObjectId = mongoose.Types.ObjectId;
_this.mdl.model.id = new ObjectId;
user = new User(_this.mdl.model);
user.save();
return _this.mail();
});
this.on('check', function() {
var model, _ref;
model = ((_ref = _this.req.query) != null ? _ref.model : void 0) != null ? JSON.parse(_this.req.query.model) : {};
_this.mdl = require(global.home + '/script/models/registration/registration')(model);
if (_this.mdl.model.success === true) {
return User.findOne({
email: _this.mdl.model.email
}, function(err, exists) {
if (exists != null) {
_this.mdl.emailExists();
return _this.emit('send');
} else {
_this.mdl.genPwd();
_this.emit('success');
return _this.emit('send');
}
});
} else {
return _this.emit('send');
}
});
}
Registration.prototype.mail = function() {
var html, mailer;
html = jade.renderFile(global.home + '/view/mail/registration.jade', {
email: this.mdl.model.email,
password: this.mdl.model.password
});
mailer = require(global.home + '/script/controllers/mailer');
return mailer.send(this.mdl.model.email, 'Общий Заказ - поздравляем с успешной регистрацией!', html);
};
return Registration;
})(EventEmitter);
exports = module.exports = function(req, res) {
return new Registration(req, res);
};
exports.Registration = Registration;