overleaf/services/web/app/coffee/Features/Subscription/TeamInvitesController.coffee

64 lines
2.1 KiB
CoffeeScript
Raw Normal View History

2018-05-31 06:54:50 -04:00
settings = require "settings-sharelatex"
logger = require("logger-sharelatex")
TeamInvitesHandler = require('./TeamInvitesHandler')
AuthenticationController = require("../Authentication/AuthenticationController")
SubscriptionLocator = require("./SubscriptionLocator")
2018-05-30 08:06:27 -04:00
ErrorController = require("../Errors/ErrorController")
2018-06-11 10:22:42 -04:00
EmailHelper = require("../Helpers/EmailHelper")
module.exports =
createInvite: (req, res, next) ->
2018-05-31 06:54:50 -04:00
teamManagerId = AuthenticationController.getLoggedInUserId(req)
subscription = req.entity
2018-06-11 10:22:42 -04:00
email = EmailHelper.parseEmail(req.body.email)
if !email?
return res.status(422).json error:
code: 'invalid_email'
message: req.i18n.translate('invalid_email')
TeamInvitesHandler.createInvite teamManagerId, subscription, email, (err, invite) ->
return next(err) if err?
inviteView = { user:
{ email: invite.email, sentAt: invite.sentAt, invite: true }
}
res.json inviteView
2018-05-30 08:06:27 -04:00
viewInvite: (req, res, next) ->
token = req.params.token
userId = AuthenticationController.getLoggedInUserId(req)
TeamInvitesHandler.getInvite token, (err, invite, teamSubscription) ->
return next(err) if err?
2018-05-31 11:42:09 -04:00
if !invite
2018-05-30 08:06:27 -04:00
return ErrorController.notFound(req, res, next)
SubscriptionLocator.getUsersSubscription userId, (err, personalSubscription) ->
return next(err) if err?
res.render "subscriptions/team/invite",
inviterName: invite.inviterName
inviteToken: invite.token
hasPersonalSubscription: personalSubscription?
appName: settings.appName
2018-05-30 08:06:27 -04:00
2018-05-31 06:54:50 -04:00
acceptInvite: (req, res, next) ->
token = req.params.token
userId = AuthenticationController.getLoggedInUserId(req)
TeamInvitesHandler.acceptInvite token, userId, (err, results) ->
return next(err) if err?
2018-06-07 10:05:04 -04:00
res.sendStatus 204
revokeInvite: (req, res) ->
subscription = req.entity
2018-06-11 10:22:42 -04:00
email = EmailHelper.parseEmail(req.params.email)
2018-05-31 06:54:50 -04:00
teamManagerId = AuthenticationController.getLoggedInUserId(req)
2018-06-11 10:22:42 -04:00
if !email?
return res.sendStatus(400)
2018-05-31 06:54:50 -04:00
TeamInvitesHandler.revokeInvite teamManagerId, subscription, email, (err, results) ->
return next(err) if err?
2018-05-31 06:54:50 -04:00
res.sendStatus 204