2017-06-13 14:31:39 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Module dependencies
|
|
|
|
|
*/
|
|
|
|
|
var path = require('path'),
|
|
|
|
|
config = require(path.resolve('./config/config')),
|
2018-04-03 14:15:42 +08:00
|
|
|
common = require(path.resolve('./config/lib/common')),
|
2017-06-13 14:31:39 +08:00
|
|
|
mongoose = require('mongoose'),
|
|
|
|
|
errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller')),
|
2017-09-13 18:30:03 +08:00
|
|
|
validator = require('validator'),
|
2017-06-15 14:07:24 +08:00
|
|
|
nodemailer = require('nodemailer'),
|
2017-06-13 14:31:39 +08:00
|
|
|
User = mongoose.model('User'),
|
|
|
|
|
Invitation = mongoose.model('Invitation'),
|
2017-06-24 12:36:28 +08:00
|
|
|
async = require('async'),
|
2018-02-01 11:51:39 +08:00
|
|
|
scoreUpdate = require(path.resolve('./config/lib/score')).update,
|
2017-06-24 12:36:28 +08:00
|
|
|
traceLogCreate = require(path.resolve('./config/lib/tracelog')).create;
|
2017-06-13 14:31:39 +08:00
|
|
|
|
2017-06-15 14:07:24 +08:00
|
|
|
var smtpTransport = nodemailer.createTransport(config.mailer.options);
|
2017-06-24 12:36:28 +08:00
|
|
|
var traceConfig = config.meanTorrentConfig.trace;
|
2017-09-13 18:30:03 +08:00
|
|
|
var mtDebug = require(path.resolve('./config/lib/debug'));
|
2018-02-01 11:51:39 +08:00
|
|
|
var inviteConfig = config.meanTorrentConfig.invite;
|
|
|
|
|
var scoreConfig = config.meanTorrentConfig.score;
|
2018-03-16 17:41:51 +08:00
|
|
|
var appConfig = config.meanTorrentConfig.app;
|
2017-09-13 18:30:03 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A Validation function for local strategy email
|
|
|
|
|
*/
|
|
|
|
|
var validateEmail = function (email) {
|
|
|
|
|
return validator.isEmail(email, {require_tld: false});
|
|
|
|
|
};
|
2017-06-15 14:07:24 +08:00
|
|
|
|
2017-06-13 14:31:39 +08:00
|
|
|
/**
|
|
|
|
|
* create a Invitation
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.create = function (req, res) {
|
2018-06-16 22:43:19 +08:00
|
|
|
var user = req.user;
|
2017-06-13 14:31:39 +08:00
|
|
|
|
2018-06-16 22:43:19 +08:00
|
|
|
if (user.score >= inviteConfig.scoreExchange) {
|
|
|
|
|
var invitation = new Invitation();
|
|
|
|
|
invitation.expiresat = Date.now() + config.meanTorrentConfig.invite.expires;
|
|
|
|
|
invitation.user = req.user;
|
|
|
|
|
invitation.isOfficial = false;
|
|
|
|
|
invitation.token = req.user.randomAsciiString(32);
|
2017-06-24 12:36:28 +08:00
|
|
|
|
2018-06-16 22:43:19 +08:00
|
|
|
invitation.save(function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2018-05-24 18:42:01 +08:00
|
|
|
user.score -= inviteConfig.scoreExchange;
|
2018-02-01 11:51:39 +08:00
|
|
|
res.json(user);
|
2017-06-13 17:28:54 +08:00
|
|
|
|
2018-06-06 00:48:30 +08:00
|
|
|
//score update
|
|
|
|
|
scoreUpdate(req, user, scoreConfig.action.scoreExchangeInvitation, -(inviteConfig.scoreExchange));
|
|
|
|
|
|
2018-02-01 11:51:39 +08:00
|
|
|
//create trace log
|
|
|
|
|
traceLogCreate(req, traceConfig.action.userInvitationExchange, {
|
|
|
|
|
user: req.user._id,
|
|
|
|
|
token: invitation.token,
|
|
|
|
|
score: inviteConfig.scoreExchange
|
2017-11-30 17:50:20 +08:00
|
|
|
});
|
|
|
|
|
}
|
2018-06-16 22:43:19 +08:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
message: 'SERVER.SCORE_NOT_ENOUGH'
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-06-13 14:31:39 +08:00
|
|
|
};
|
|
|
|
|
|
2017-09-14 11:32:05 +08:00
|
|
|
/**
|
|
|
|
|
* listOfficial
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.listOfficial = function (req, res) {
|
|
|
|
|
Invitation.find({
|
|
|
|
|
isOfficial: true
|
|
|
|
|
})
|
|
|
|
|
.sort('-invitedat')
|
|
|
|
|
.populate('user', '-salt -password')
|
|
|
|
|
.populate('to_user', '-salt -password')
|
|
|
|
|
.exec(function (err, invitations) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.json(invitations);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
2017-06-13 14:31:39 +08:00
|
|
|
|
2017-12-05 15:47:17 +08:00
|
|
|
/**
|
|
|
|
|
* deleteExpiredOfficialInvitation
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.deleteExpiredOfficialInvitation = function (req, res) {
|
|
|
|
|
Invitation.remove({
|
|
|
|
|
isOfficial: true,
|
|
|
|
|
status: 1,
|
|
|
|
|
expiresat: {$lt: Date.now()}
|
|
|
|
|
}, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.json({
|
|
|
|
|
message: 'SERVER.DELETE_EXPIRED_OFFICIAL_INVITATION_OK'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2017-06-13 14:31:39 +08:00
|
|
|
/**
|
|
|
|
|
* List of Invitations
|
|
|
|
|
*/
|
|
|
|
|
exports.list = function (req, res) {
|
2017-06-14 17:28:40 +08:00
|
|
|
var findMyInvitations = function (callback) {
|
|
|
|
|
Invitation.find({
|
|
|
|
|
user: req.user._id,
|
|
|
|
|
status: 0,
|
|
|
|
|
expiresat: {$gt: Date.now()}
|
|
|
|
|
})
|
|
|
|
|
.sort('createdat')
|
2018-05-26 12:58:22 +08:00
|
|
|
.populate('user', '-salt -password -followers -following -leeched_ip -signed_ip -signature')
|
2017-06-14 17:28:40 +08:00
|
|
|
.exec(function (err, invitations) {
|
|
|
|
|
if (err) {
|
|
|
|
|
callback(err, null);
|
|
|
|
|
} else {
|
|
|
|
|
callback(null, invitations);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var findUsedInvitations = function (callback) {
|
|
|
|
|
Invitation.find({
|
2017-06-15 14:07:24 +08:00
|
|
|
user: req.user._id,
|
|
|
|
|
status: {$gt: 0}
|
2017-06-14 17:28:40 +08:00
|
|
|
})
|
2017-06-15 10:57:39 +08:00
|
|
|
.sort('invitedat')
|
2018-05-26 12:58:22 +08:00
|
|
|
.populate('user', '-salt -password -followers -following -leeched_ip -signed_ip -signature')
|
|
|
|
|
.populate('to_user', '-salt -password -followers -following -leeched_ip -signed_ip -signature')
|
2017-06-14 17:28:40 +08:00
|
|
|
.exec(function (err, invitations) {
|
|
|
|
|
if (err) {
|
|
|
|
|
callback(err, null);
|
|
|
|
|
} else {
|
|
|
|
|
callback(null, invitations);
|
|
|
|
|
}
|
2017-06-13 14:31:39 +08:00
|
|
|
});
|
2017-06-14 17:28:40 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async.parallel([findMyInvitations, findUsedInvitations], function (err, results) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send(err);
|
2017-06-13 14:31:39 +08:00
|
|
|
} else {
|
2017-06-14 17:28:40 +08:00
|
|
|
res.json({
|
|
|
|
|
my_invitations: results[0],
|
|
|
|
|
used_invitations: results[1]
|
|
|
|
|
});
|
2017-06-13 14:31:39 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2017-06-15 14:07:24 +08:00
|
|
|
/**
|
|
|
|
|
* Update an invitation
|
|
|
|
|
*/
|
|
|
|
|
exports.update = function (req, res) {
|
|
|
|
|
var invitation = req.invitation;
|
|
|
|
|
|
|
|
|
|
var countRegisteredEmail = function (callback) {
|
|
|
|
|
User.count({email: req.query.to_email}, function (err, count) {
|
|
|
|
|
if (err) {
|
|
|
|
|
callback(err, null);
|
|
|
|
|
} else {
|
|
|
|
|
callback(null, count);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
var countInvitedEmail = function (callback) {
|
|
|
|
|
Invitation.count({to_email: req.query.to_email}, function (err, count) {
|
|
|
|
|
if (err) {
|
|
|
|
|
callback(err, null);
|
|
|
|
|
} else {
|
|
|
|
|
callback(null, count);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async.parallel([countRegisteredEmail, countInvitedEmail], function (err, results) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send(err);
|
|
|
|
|
} else {
|
|
|
|
|
if (results[0] > 0) {
|
2018-03-21 14:16:20 +08:00
|
|
|
return res.status(422).send({message: 'SERVER.EMAIL_ALREADY_REGISTERED'});
|
2017-06-15 14:07:24 +08:00
|
|
|
} else if (results[1] > 0) {
|
2018-03-21 14:16:20 +08:00
|
|
|
return res.status(422).send({message: 'SERVER.EMAIL_ALREADY_INVITED'});
|
2018-04-18 13:12:55 +08:00
|
|
|
} else if (!common.emailIsAllowable(req.query.to_email)) {
|
|
|
|
|
return res.status(422).send({message: 'SERVER.EMAIL_ADDRESS_IS_NOT_ALLOW'});
|
2017-06-15 14:07:24 +08:00
|
|
|
} else {
|
2017-09-13 18:30:03 +08:00
|
|
|
//send invitation mail
|
2018-04-03 14:15:42 +08:00
|
|
|
var lang = common.getRequestLanguage(req);
|
|
|
|
|
res.render(path.resolve('modules/invitations/server/templates/invite-sign-up-email-' + lang), {
|
2017-06-15 14:07:24 +08:00
|
|
|
to_email: req.query.to_email,
|
|
|
|
|
name: req.user.displayName,
|
|
|
|
|
appName: config.app.title,
|
2018-03-16 17:41:51 +08:00
|
|
|
url: appConfig.domain + '/api/auth/invite/' + invitation.token,
|
2017-06-15 14:07:24 +08:00
|
|
|
hours: config.meanTorrentConfig.invite.expires / (60 * 60 * 1000)
|
|
|
|
|
}, function (err, emailHTML) {
|
|
|
|
|
if (err) {
|
2018-03-21 14:16:20 +08:00
|
|
|
return res.status(422).send({message: 'SERVER.INVITE_MAIL_RENDER_FAILED'});
|
2017-06-15 14:07:24 +08:00
|
|
|
} else {
|
|
|
|
|
var mailOptions = {
|
|
|
|
|
to: req.query.to_email,
|
|
|
|
|
from: config.mailer.from,
|
2018-03-21 12:40:53 +08:00
|
|
|
subject: config.app.title + ' - Invitation is here!',
|
2017-06-15 14:07:24 +08:00
|
|
|
html: emailHTML
|
|
|
|
|
};
|
|
|
|
|
smtpTransport.sendMail(mailOptions, function (err) {
|
|
|
|
|
if (err) {
|
2018-03-21 14:16:20 +08:00
|
|
|
return res.status(422).send({message: 'SERVER.INVITE_MAIL_SEND_FAILED'});
|
2017-06-15 14:07:24 +08:00
|
|
|
} else {
|
|
|
|
|
invitation.to_email = req.query.to_email;
|
|
|
|
|
invitation.status = 1;
|
|
|
|
|
invitation.invitedat = Date.now();
|
|
|
|
|
invitation.expiresat = Date.now() + config.meanTorrentConfig.invite.expires;
|
|
|
|
|
|
|
|
|
|
invitation.save(function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.json(invitation);
|
2017-09-13 18:46:53 +08:00
|
|
|
//create trace log
|
|
|
|
|
traceLogCreate(req, traceConfig.action.userSendInvitation, {
|
|
|
|
|
to: req.query.to_email,
|
|
|
|
|
token: invitation.token
|
|
|
|
|
});
|
2017-06-15 14:07:24 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
2017-06-13 14:58:57 +08:00
|
|
|
|
2017-09-13 18:30:03 +08:00
|
|
|
/**
|
2017-09-14 11:32:05 +08:00
|
|
|
* sendOfficial
|
2017-09-13 18:30:03 +08:00
|
|
|
* send official invitation
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
2017-09-14 11:32:05 +08:00
|
|
|
exports.sendOfficial = function (req, res) {
|
2017-09-13 18:30:03 +08:00
|
|
|
if (!validateEmail(req.body.email)) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
message: 'ERROR: invalid email address!'
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
var countRegisteredEmail = function (callback) {
|
|
|
|
|
User.count({email: req.body.email}, function (err, count) {
|
|
|
|
|
if (err) {
|
|
|
|
|
callback(err, null);
|
|
|
|
|
} else {
|
|
|
|
|
callback(null, count);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
var countInvitedEmail = function (callback) {
|
|
|
|
|
Invitation.count({to_email: req.body.email}, function (err, count) {
|
|
|
|
|
if (err) {
|
|
|
|
|
callback(err, null);
|
|
|
|
|
} else {
|
|
|
|
|
callback(null, count);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async.parallel([countRegisteredEmail, countInvitedEmail], function (err, results) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send(err);
|
|
|
|
|
} else {
|
|
|
|
|
if (results[0] > 0) {
|
2018-03-21 14:16:20 +08:00
|
|
|
return res.status(422).send({message: 'SERVER.EMAIL_ALREADY_REGISTERED'});
|
2017-09-13 18:30:03 +08:00
|
|
|
} else if (results[1] > 0) {
|
2018-03-21 14:16:20 +08:00
|
|
|
return res.status(422).send({message: 'SERVER.EMAIL_ALREADY_INVITED'});
|
2018-04-18 13:12:55 +08:00
|
|
|
} else if (!common.emailIsAllowable(req.body.email)) {
|
|
|
|
|
return res.status(422).send({message: 'SERVER.EMAIL_ADDRESS_IS_NOT_ALLOW'});
|
2017-09-13 18:30:03 +08:00
|
|
|
} else {
|
|
|
|
|
//write invitation data
|
|
|
|
|
var invitation = new Invitation();
|
|
|
|
|
invitation.user = req.user;
|
|
|
|
|
invitation.token = req.user.randomAsciiString(32);
|
|
|
|
|
invitation.to_email = req.body.email;
|
|
|
|
|
invitation.status = 1;
|
|
|
|
|
invitation.invitedat = Date.now();
|
|
|
|
|
invitation.expiresat = Date.now() + config.meanTorrentConfig.invite.expires;
|
|
|
|
|
invitation.isOfficial = true;
|
2018-05-26 12:58:22 +08:00
|
|
|
invitation.type = 'official';
|
2017-09-13 18:30:03 +08:00
|
|
|
|
|
|
|
|
//send invitation mail
|
2018-04-03 14:15:42 +08:00
|
|
|
var lang = common.getRequestLanguage(req);
|
|
|
|
|
res.render(path.resolve('modules/invitations/server/templates/invite-sign-up-email-' + lang), {
|
2017-09-13 18:30:03 +08:00
|
|
|
to_email: req.body.email,
|
|
|
|
|
name: req.user.displayName,
|
|
|
|
|
appName: config.app.title,
|
2018-03-16 17:41:51 +08:00
|
|
|
url: appConfig.domain + '/api/auth/invite/' + invitation.token,
|
2017-09-13 18:30:03 +08:00
|
|
|
hours: config.meanTorrentConfig.invite.expires / (60 * 60 * 1000)
|
|
|
|
|
}, function (err, emailHTML) {
|
|
|
|
|
if (err) {
|
2018-03-21 14:16:20 +08:00
|
|
|
return res.status(422).send({message: 'SERVER.INVITE_MAIL_RENDER_FAILED'});
|
2017-09-13 18:30:03 +08:00
|
|
|
} else {
|
|
|
|
|
var mailOptions = {
|
|
|
|
|
to: req.body.email,
|
|
|
|
|
from: config.mailer.from,
|
2018-03-21 12:40:53 +08:00
|
|
|
subject: config.app.title + ' - Official invitation is here!',
|
2017-09-13 18:30:03 +08:00
|
|
|
html: emailHTML
|
|
|
|
|
};
|
|
|
|
|
smtpTransport.sendMail(mailOptions, function (err) {
|
|
|
|
|
if (err) {
|
2018-03-21 14:16:20 +08:00
|
|
|
return res.status(422).send({message: 'SERVER.INVITE_MAIL_SEND_FAILED'});
|
2017-09-13 18:30:03 +08:00
|
|
|
} else {
|
2018-03-21 12:40:53 +08:00
|
|
|
//save invitation data
|
|
|
|
|
invitation.save(function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.json(invitation);
|
|
|
|
|
|
|
|
|
|
//create trace log
|
|
|
|
|
traceLogCreate(req, traceConfig.action.adminSendOfficialInvitation, {
|
|
|
|
|
to: req.body.email,
|
|
|
|
|
token: invitation.token
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-09-13 18:30:03 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-06-13 14:58:57 +08:00
|
|
|
/**
|
|
|
|
|
* Delete an invitation
|
|
|
|
|
*/
|
|
|
|
|
exports.delete = function (req, res) {
|
|
|
|
|
var invitation = req.invitation;
|
|
|
|
|
|
|
|
|
|
invitation.remove(function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.json(invitation);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2017-06-15 16:18:07 +08:00
|
|
|
/**
|
|
|
|
|
* verifyToken
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.verifyToken = function (req, res) {
|
|
|
|
|
var t = req.params.token;
|
|
|
|
|
|
|
|
|
|
Invitation.findOne({token: t}).exec(function (err, invitation) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
|
});
|
|
|
|
|
} else if (!invitation) {
|
|
|
|
|
return res.status(404).send({
|
|
|
|
|
message: 'No invitation with that token has been found'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
res.json(invitation);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
2017-06-13 14:58:57 +08:00
|
|
|
|
2017-06-16 10:04:52 +08:00
|
|
|
/**
|
|
|
|
|
* countInvitations
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.countInvitations = function (req, res) {
|
|
|
|
|
var countMyInvitations = function (callback) {
|
|
|
|
|
Invitation.count({
|
|
|
|
|
user: req.user._id,
|
|
|
|
|
status: 0,
|
|
|
|
|
expiresat: {$gt: Date.now()}
|
|
|
|
|
}, function (err, count) {
|
|
|
|
|
if (err) {
|
|
|
|
|
callback(err, null);
|
|
|
|
|
} else {
|
|
|
|
|
callback(null, count);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
var countUsedInvitations = function (callback) {
|
|
|
|
|
Invitation.count({
|
|
|
|
|
user: req.user._id,
|
|
|
|
|
status: 2
|
|
|
|
|
}, function (err, count) {
|
|
|
|
|
if (err) {
|
|
|
|
|
callback(err, null);
|
|
|
|
|
} else {
|
|
|
|
|
callback(null, count);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async.parallel([countMyInvitations, countUsedInvitations], function (err, results) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send(err);
|
|
|
|
|
} else {
|
|
|
|
|
res.json({
|
|
|
|
|
countMyInvitations: results[0],
|
|
|
|
|
countUsedInvitations: results[1]
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2017-06-13 14:58:57 +08:00
|
|
|
/**
|
|
|
|
|
* Invitation middleware
|
|
|
|
|
*/
|
|
|
|
|
exports.invitationByID = function (req, res, next, id) {
|
|
|
|
|
if (!mongoose.Types.ObjectId.isValid(id)) {
|
|
|
|
|
return res.status(400).send({
|
2018-01-12 16:53:24 +08:00
|
|
|
message: 'SERVER.INVALID_OBJECTID'
|
2017-06-13 14:58:57 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-14 11:32:05 +08:00
|
|
|
Invitation.findById(id).populate('user', '-salt -password').exec(function (err, invitation) {
|
2017-06-13 14:58:57 +08:00
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
} else if (!invitation) {
|
2018-01-21 16:27:17 +08:00
|
|
|
return res.status(404).send();
|
2017-06-13 14:58:57 +08:00
|
|
|
}
|
|
|
|
|
req.invitation = invitation;
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|