2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
max-len,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2021-07-07 05:38:56 -04:00
|
|
|
const settings = require('@overleaf/settings')
|
2021-11-10 08:40:18 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2019-05-29 05:21:06 -04:00
|
|
|
const TeamInvitesHandler = require('./TeamInvitesHandler')
|
2021-07-28 04:51:20 -04:00
|
|
|
const SessionManager = require('../Authentication/SessionManager')
|
2019-05-29 05:21:06 -04:00
|
|
|
const SubscriptionLocator = require('./SubscriptionLocator')
|
|
|
|
const ErrorController = require('../Errors/ErrorController')
|
|
|
|
const EmailHelper = require('../Helpers/EmailHelper')
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
createInvite(req, res, next) {
|
2021-07-28 04:51:20 -04:00
|
|
|
const teamManagerId = SessionManager.getLoggedInUserId(req.session)
|
2019-05-29 05:21:06 -04:00
|
|
|
const subscription = req.entity
|
|
|
|
const email = EmailHelper.parseEmail(req.body.email)
|
|
|
|
if (email == null) {
|
|
|
|
return res.status(422).json({
|
|
|
|
error: {
|
|
|
|
code: 'invalid_email',
|
2021-04-27 03:52:58 -04:00
|
|
|
message: req.i18n.translate('invalid_email'),
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return TeamInvitesHandler.createInvite(
|
|
|
|
teamManagerId,
|
|
|
|
subscription,
|
|
|
|
email,
|
2021-04-14 09:17:21 -04:00
|
|
|
function (err, inviteUserData) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
2020-01-02 14:22:18 -05:00
|
|
|
if (err.alreadyInTeam) {
|
|
|
|
return res.status(400).json({
|
|
|
|
error: {
|
|
|
|
code: 'user_already_added',
|
2021-04-27 03:52:58 -04:00
|
|
|
message: req.i18n.translate('user_already_added'),
|
|
|
|
},
|
2020-01-02 14:22:18 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if (err.limitReached) {
|
|
|
|
return res.status(400).json({
|
|
|
|
error: {
|
|
|
|
code: 'group_full',
|
2021-04-27 03:52:58 -04:00
|
|
|
message: req.i18n.translate('group_full'),
|
|
|
|
},
|
2020-01-02 14:22:18 -05:00
|
|
|
})
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
return next(err)
|
|
|
|
}
|
2019-06-17 10:46:08 -04:00
|
|
|
return res.json({ user: inviteUserData })
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
viewInvite(req, res, next) {
|
|
|
|
const { token } = req.params
|
2021-07-28 04:51:20 -04:00
|
|
|
const userId = SessionManager.getLoggedInUserId(req.session)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
return TeamInvitesHandler.getInvite(
|
|
|
|
token,
|
|
|
|
function (err, invite, teamSubscription) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
if (!invite) {
|
|
|
|
return ErrorController.notFound(req, res, next)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
return SubscriptionLocator.getUsersSubscription(
|
|
|
|
userId,
|
|
|
|
function (err, personalSubscription) {
|
|
|
|
if (err != null) {
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasIndividualRecurlySubscription =
|
|
|
|
personalSubscription != null &&
|
|
|
|
personalSubscription.planCode.match(/(free|trial)/) == null &&
|
|
|
|
personalSubscription.groupPlan === false &&
|
|
|
|
personalSubscription.recurlySubscription_id != null &&
|
|
|
|
personalSubscription.recurlySubscription_id !== ''
|
|
|
|
|
|
|
|
return res.render('subscriptions/team/invite', {
|
|
|
|
inviterName: invite.inviterName,
|
|
|
|
inviteToken: invite.token,
|
|
|
|
hasIndividualRecurlySubscription,
|
|
|
|
appName: settings.appName,
|
2021-04-27 03:52:58 -04:00
|
|
|
expired: req.query.expired,
|
2021-04-14 09:17:21 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
acceptInvite(req, res, next) {
|
|
|
|
const { token } = req.params
|
2021-07-28 04:51:20 -04:00
|
|
|
const userId = SessionManager.getLoggedInUserId(req.session)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
return TeamInvitesHandler.acceptInvite(
|
|
|
|
token,
|
|
|
|
userId,
|
|
|
|
function (err, results) {
|
|
|
|
if (err != null) {
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
return res.sendStatus(204)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
2021-07-12 06:40:29 -04:00
|
|
|
revokeInvite(req, res, next) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const subscription = req.entity
|
|
|
|
const email = EmailHelper.parseEmail(req.params.email)
|
2021-07-28 04:51:20 -04:00
|
|
|
const teamManagerId = SessionManager.getLoggedInUserId(req.session)
|
2019-05-29 05:21:06 -04:00
|
|
|
if (email == null) {
|
|
|
|
return res.sendStatus(400)
|
|
|
|
}
|
|
|
|
|
|
|
|
return TeamInvitesHandler.revokeInvite(
|
|
|
|
teamManagerId,
|
|
|
|
subscription,
|
|
|
|
email,
|
2021-04-14 09:17:21 -04:00
|
|
|
function (err, results) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
return res.sendStatus(204)
|
|
|
|
}
|
|
|
|
)
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|