Newer
Older
const config = require('config')
const {
Team,
Sebastian Mihalache
committed
services,
authsome: authsomeHelper,
} = require('pubsweet-component-helper-service')
const {
deleteFilesS3,
} = require('pubsweet-component-mts-package/src/PackageManager')
const { last, get } = require('lodash')
const s3Config = get(config, 'pubsweet-component-aws-s3', {})
Sebastian Mihalache
committed
const notifications = require('./emails/notifications')
module.exports = models => async (req, res) => {
const { collectionId, invitationId } = req.params
const teamHelper = new Team({ TeamModel: models.Team, collectionId })
try {
const collection = await models.Collection.find(collectionId)
Sebastian Mihalache
committed
const authsome = authsomeHelper.getAuthsome(models)
const target = {
collection,
path: req.route.path,
}
const canDelete = await authsome.can(req.user, 'DELETE', target)
if (!canDelete)
return res.status(403).json({
error: 'Unauthorized.',
})
Sebastian Mihalache
committed
collection.invitations = collection.invitations || []
const invitation = collection.invitations.find(
invitation => invitation.id === invitationId,
)
Sebastian Mihalache
committed
if (!invitation)
return res.status(404).json({
error: `Invitation ${invitationId} not found`,
})
Sebastian Mihalache
committed
const team = await teamHelper.getTeam({
role: invitation.role,
Sebastian Mihalache
committed
objectType: 'collection',
collection.invitations = collection.invitations.filter(
inv => inv.id !== invitation.id,
)
Sebastian Mihalache
committed
collection.status = 'submitted'
delete collection.handlingEditor
Sebastian Mihalache
committed
await teamHelper.removeTeamMember({
teamId: team.id,
userId: invitation.userId,
})
Sebastian Mihalache
committed
const UserModel = models.User
const user = await UserModel.find(invitation.userId)
user.teams = user.teams.filter(userTeamId => team.id !== userTeamId)
await user.save()
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const FragmentModel = models.Fragment
const fragment = await FragmentModel.find(
last(get(collection, 'fragments', [])),
)
if (!fragment)
return res.status(404).json({
error: `Fragment ${fragment.id} not found`,
})
const fileKeys = []
fragment.recommendations &&
fragment.recommendations.forEach(recommendation => {
recommendation.comments.forEach(comment => {
comment.files &&
comment.files.forEach(file => {
fileKeys.push(file.id)
})
})
})
const responseToReviewersFileId = get(
fragment,
'responseToReviewers.file.id',
)
if (responseToReviewersFileId) fileKeys.push(responseToReviewersFileId)
if (fileKeys.length > 1) {
await deleteFilesS3({ fileKeys, s3Config })
}
fragment.invitations = []
fragment.recommendations = []
fragment.responseToReviewers && delete fragment.responseToReviewers
fragment.revision && delete fragment.revision
fragment.save()
notifications.sendInvitedHEEmail({
Sebastian Mihalache
committed
models,
collection,
invitedHE: user,
isCanceled: true,
baseUrl: services.getBaseUrl(req),
})
Sebastian Mihalache
committed
return res.status(200).json({})
Sebastian Mihalache
committed
const notFoundError = await services.handleNotFoundError(e, 'Collection')