Skip to content
Snippets Groups Projects
Commit d9e1baa1 authored by Sebastian Mihalache's avatar Sebastian Mihalache
Browse files

feat(component-manuscript-manager): create team for new fragment

parent 523738e0
No related branches found
No related tags found
1 merge request!13Sprint #14
......@@ -5,6 +5,10 @@ class Fragment {
this.fragment = fragment
}
set _fragment(newFragment) {
this.fragment = newFragment
}
async getFragmentData({ handlingEditor = {} }) {
const { fragment: { metadata = {}, recommendations = [], id } } = this
const heRecommendation = recommendations.find(
......
......@@ -136,6 +136,29 @@ class Team {
await user.save()
await collection.save()
}
createOrUpdateTeamForNewVersion({ role, iterable }) {
iterable.forEach(async obj => {
let { userId } = obj
if (role === 'author') {
userId = obj.id
}
let team = await this.getTeam({
role,
objectType: 'fragment',
})
if (team) {
team.members.push(userId)
team.save()
} else {
team = this.createTeam({
role,
userId,
objectType: 'fragment',
})
}
})
}
}
module.exports = Team
......@@ -61,6 +61,12 @@ class User {
const eic = users.find(user => user.editorInChief || user.admin)
return eic
}
async updateUserTeams({ userId, teamId }) {
const user = await this.UserModel.find(userId)
user.teams.push(teamId)
user.save()
}
}
module.exports = User
const {
Team,
User,
services,
Fragment,
Collection,
......@@ -30,6 +32,64 @@ module.exports = models => async (req, res) => {
const collectionHelper = new Collection({ collection })
const fragmentHelper = new Fragment({ fragment })
const teamHelper = new Team({
TeamModel: models.Team,
collectionId,
fragmentId,
})
fragment.authors = fragment.authors || []
teamHelper.createOrUpdateTeamForNewVersion({
role: 'author',
iterable: fragment.authors,
})
const authorsTeam = await teamHelper.getTeam({
role: 'author',
objectType: 'fragment',
})
const userHelper = new User({ UserModel: models.User })
if (authorsTeam) {
fragment.authors.forEach(author => {
userHelper.updateUserTeams({
userId: author.id,
teamId: authorsTeam.id,
})
})
}
fragment.invitations = fragment.invitations || []
teamHelper.createOrUpdateTeamForNewVersion({
role: 'reviewer',
iterable: fragment.invitations,
})
const reviewersTeam = await teamHelper.getTeam({
role: 'reviewer',
objectType: 'fragment',
})
if (reviewersTeam) {
fragment.invitations.forEach(inv => {
userHelper.updateUserTeams({
userId: inv.userId,
teamId: reviewersTeam.id,
})
})
}
const fragLength = collection.fragments.length
if (fragLength < 2) {
return res.status(400).json({
error: 'No previous version has been found.',
})
}
const previousFragment = await models.Fragment.find(
collection.fragments[fragLength - 2],
)
fragmentHelper.fragment = previousFragment
const heRecommendation = fragmentHelper.getHeRequestToRevision()
if (!heRecommendation) {
......
......@@ -278,7 +278,7 @@ const authsomeMode = async (userId, operation, object, context) => {
const user = await context.models.User.find(userId)
// Admins and editor in chiefs can do anything
// if (user && (user.admin || user.editorInChief)) return true
if (user && (user.admin || user.editorInChief)) return true
if (user) {
return authenticatedUser(user, operation, object, context)
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment