Commit afccdb16 authored by Yannis Barlas's avatar Yannis Barlas

chore(*): get teams working

parent 6aa7e32a
......@@ -118,9 +118,9 @@ const TeamManagerForm = props => {
key={team.id}
name={team.name}
setFieldValue={setFieldValue}
type={team.teamType}
type={team.role}
users={users}
value={values[team.teamType]}
value={values[team.role]}
/>
))}
......@@ -145,9 +145,9 @@ class TeamManager extends React.Component {
handleSubmit = (formValues, formikBag) => {
const { cleanUp, updateTeam, teams } = this.props
const data = keys(formValues).map(teamType => ({
id: teams.find(t => t.teamType === teamType).id,
members: formValues[teamType].map(item => {
const data = keys(formValues).map(role => ({
id: teams.find(t => t.role === role).id,
members: formValues[role].map(item => {
if (item.id) return item.id
return item.value
}),
......@@ -195,7 +195,7 @@ class TeamManager extends React.Component {
const initialValues = {}
globalTeams.forEach(team => {
initialValues[team.teamType] = team.members
initialValues[team.role] = team.members
})
globalTeams = sortBy(globalTeams, 'name')
......
......@@ -18,10 +18,10 @@ const mapper = {
}
const getAllEditors = globalTeams =>
globalTeams && globalTeams.find(t => t.teamType === 'editors').members
globalTeams && globalTeams.find(t => t.role === 'editors').members
const getEditorTeamForArticle = teamsForArticle =>
teamsForArticle && teamsForArticle.find(t => t.teamType === 'editor')
teamsForArticle && teamsForArticle.find(t => t.role === 'editor')
const getEditor = team => {
if (!team) return null
......
......@@ -39,7 +39,7 @@ const mapProps = args => {
)
const getTeam = type =>
allReviewerTeams && allReviewerTeams.find(t => t.teamType === type)
allReviewerTeams && allReviewerTeams.find(t => t.role === type)
const externalTeams = get(
args.getExternalTeamsForManuscript,
......@@ -47,11 +47,11 @@ const mapProps = args => {
)
const externalReviewers =
externalTeams && externalTeams.find(t => t.teamType === 'externalReviewers')
externalTeams && externalTeams.find(t => t.role === 'externalReviewers')
const externalReviewersInvited =
externalTeams &&
externalTeams.find(t => t.teamType === 'externalReviewersInvited')
externalTeams.find(t => t.role === 'externalReviewersInvited')
return {
addExternalReviewer: args.addExternalReviewer.addExternalReviewer,
......
......@@ -18,10 +18,10 @@ const mapper = {
}
const getAllScienceOfficers = globalTeams =>
globalTeams && globalTeams.find(t => t.teamType === 'scienceOfficers').members
globalTeams && globalTeams.find(t => t.role === 'scienceOfficers').members
const getScienceOfficerTeamForArticle = teamsForArticle =>
teamsForArticle && teamsForArticle.find(t => t.teamType === 'scienceOfficer')
teamsForArticle && teamsForArticle.find(t => t.role === 'scienceOfficer')
const getScienceOfficer = team => {
if (!team) return null
......
......@@ -28,11 +28,11 @@ const mapper = {
}
/* eslint-enable sort-keys */
const getTeamByType = (teams, type) =>
teams && teams.find(t => t.teamType === type)
const getTeamByType = (teams, type) => teams && teams.find(t => t.role === type)
/* eslint-disable-next-line arrow-body-style */
const mapProps = args => {
console.log(args)
return {
authorArticles: get(
args.getDashboardArticlesQuery,
......
......@@ -36,10 +36,10 @@ const mapper = {
}
const getAllScienceOfficers = globalTeams =>
globalTeams && globalTeams.find(t => t.teamType === 'scienceOfficers').members
globalTeams && globalTeams.find(t => t.role === 'scienceOfficers').members
const getAllEditors = globalTeams =>
globalTeams && globalTeams.find(t => t.teamType === 'editors').members
globalTeams && globalTeams.find(t => t.role === 'editors').members
const mapProps = args => {
const teams = get(args.getTeamsForArticle, 'data.teamsForArticle')
......
......@@ -13,7 +13,7 @@ const GET_EXTERNAL_TEAMS_FOR_MANUSCRIPT = gql`
email
name
}
teamType
role
}
}
`
......
......@@ -7,13 +7,13 @@ const GET_TEAMS = gql`
id
members {
id
username
# username
}
name
object {
objectId
}
teamType
role
}
}
`
......
......@@ -13,7 +13,7 @@ const GET_TEAMS_FOR_ARTICLE = gql`
username
}
name
teamType
role
}
}
`
......
......@@ -54,7 +54,7 @@ const getReviewerTeams = teams => {
const getReviewersTeamByType = (teams, type) => {
if (!teams) return null
return teams.find(t => t.teamType === type) || null
return teams.find(t => t.role === type) || null
}
// Get science officer assigned to article
......@@ -85,7 +85,7 @@ const getTeamMembers = team => {
}
const getOneTeamByType = (teams, type) =>
teams && teams.find(t => t.teamType === type)
teams && teams.find(t => t.role === type)
const isMember = (team, userId) => {
if (!team || !userId) return false
......
......@@ -5,10 +5,12 @@ const GET_GLOBAL_TEAMS = gql`
globalTeams {
id
members {
id
username
user {
id
# username
}
}
teamType
role
}
}
`
......
......@@ -6,32 +6,32 @@ const difference = require('lodash/difference')
const xor = require('lodash/xor')
// Helper functions
const isTeamMember = async (user, teamTypes, object, context) => {
const isTeamMember = async (user, roles, object, context) => {
const teams = await Promise.all(
user.teams.map(teamId => context.models.Team.find(getId(teamId))),
)
const teamTypesArray = Array.isArray(teamTypes) ? teamTypes : [teamTypes]
const rolesArray = Array.isArray(roles) ? roles : [roles]
const team = teams.find(
aTeam =>
!aTeam.global &&
object.id === aTeam.object.objectId &&
teamTypesArray.includes(aTeam.teamType),
rolesArray.includes(aTeam.role),
)
return !!team
}
const isGlobalTeamMember = async (user, teamTypes, context) => {
const isGlobalTeamMember = async (user, roles, context) => {
const teams = await Promise.all(
user.teams.map(teamId => context.models.Team.find(getId(teamId))),
)
const teamTypesArray = Array.isArray(teamTypes) ? teamTypes : [teamTypes]
const rolesArray = Array.isArray(roles) ? roles : [roles]
const team = teams.find(
aTeam => aTeam.global && teamTypesArray.includes(aTeam.teamType),
aTeam => aTeam.global && rolesArray.includes(aTeam.role),
)
return !!team
......@@ -233,7 +233,7 @@ const permissions = {
if (get(object, 'current.type') === 'team') {
const { current, update } = object
const teamType = get(object, 'current.teamType')
const role = get(object, 'current.role')
const changed = Object.keys(update)
// No one can update something other than members on existing teams
......@@ -242,12 +242,12 @@ const permissions = {
// Only global users can update the editor & SO team members for an object
const global = isGlobal(user, context)
if (teamType === 'editor' || teamType === 'scienceOfficer') return global
if (role === 'editor' || role === 'scienceOfficer') return global
// Only editors can update the reviewer teams
const editor = isEditor(user, context)
const editorAllow = ['reviewers', 'reviewersInvited']
if (editor && editorAllow.includes(teamType)) return true
if (editor && editorAllow.includes(role)) return true
// Only invited reviewers can alter accepted or rejected teams
// They can only apply changes that affect themselves and no one else
......@@ -258,7 +258,7 @@ const permissions = {
context,
)
const reviewerAllow = ['reviewersAccepted', 'reviewersRejected']
if (reviewerAllow.includes(teamType)) return reviewerInvited
if (reviewerAllow.includes(role)) return reviewerInvited
// Necessary, as any user needs to run the normalize team mutation
return true
......
......@@ -37,7 +37,7 @@ describe('Email', () => {
global: true,
members: [editor.id],
name: 'Editors Global',
teamType: 'editors',
role: 'editors',
}).save()
})
......
......@@ -19,7 +19,7 @@ const addExternalReviewer = async (_, variables, ctx) => {
const team = await ExternalTeam.query().findOne({
manuscriptId: articleId,
teamType: 'externalReviewers',
role: 'externalReviewers',
})
let updated
......@@ -32,7 +32,7 @@ const addExternalReviewer = async (_, variables, ctx) => {
updated = await ExternalTeam.query().insert({
manuscriptId: articleId,
members: [externalUser.id],
teamType: 'externalReviewers',
role: 'externalReviewers',
})
}
......
......@@ -2,7 +2,7 @@ type ExternalTeam {
id: ID!
manuscriptId: ID!
members: [ExternalUser!]!
teamType: String!
role: String!
}
extend type Query {
......
......@@ -22,14 +22,14 @@ const inviteExternalReviewer = async (_, { manuscriptId, reviewerId }, ctx) => {
externalInvitedReviewersTeam = await ExternalTeam.query().findOne({
manuscriptId,
teamType: 'externalReviewersInvited',
role: 'externalReviewersInvited',
})
if (!externalInvitedReviewersTeam) {
externalInvitedReviewersTeam = await ExternalTeam.query().insert({
manuscriptId,
members: [],
teamType: 'externalReviewersInvited',
role: 'externalReviewersInvited',
})
}
......@@ -67,16 +67,16 @@ const normalizeTeamMembership = async (_, variables, ctx) => {
const teams = await Team.all()
const normalizeOneTeamMembership = async externalTeam => {
const teamType = externalTeamMapper[externalTeam.teamType]
const role = externalTeamMapper[externalTeam.role]
const team = teams.find(
t =>
!t.global &&
t.object.objectId === externalTeam.manuscriptId &&
t.teamType === teamType,
t.role === role,
)
if (!team)
throw new Error(`Corresponding team of type ${teamType} not found!`)
throw new Error(`Corresponding team of type ${role} not found!`)
return Promise.all([
ctx.connectors.Team.update(
......
......@@ -42,7 +42,7 @@ const createManuscript = async (_, variables, ctx) => {
objectId: manuscript.id,
objectType: 'article',
},
teamType: 'author',
role: 'author',
},
// Editor team
......@@ -53,7 +53,7 @@ const createManuscript = async (_, variables, ctx) => {
objectId: manuscript.id,
objectType: 'article',
},
teamType: 'editor',
role: 'editor',
},
// Science officer team
......@@ -64,7 +64,7 @@ const createManuscript = async (_, variables, ctx) => {
objectId: manuscript.id,
objectType: 'article',
},
teamType: 'scienceOfficer',
role: 'scienceOfficer',
},
// Reviewer team
......@@ -75,7 +75,7 @@ const createManuscript = async (_, variables, ctx) => {
objectId: manuscript.id,
objectType: 'article',
},
teamType: 'reviewers',
role: 'reviewers',
},
// Invited reviewers team
......@@ -86,7 +86,7 @@ const createManuscript = async (_, variables, ctx) => {
objectId: manuscript.id,
objectType: 'article',
},
teamType: 'reviewersInvited',
role: 'reviewersInvited',
},
// Reviewers that accepted
......@@ -97,7 +97,7 @@ const createManuscript = async (_, variables, ctx) => {
objectId: manuscript.id,
objectType: 'article',
},
teamType: 'reviewersAccepted',
role: 'reviewersAccepted',
},
// Reviewers that rejected
......@@ -108,7 +108,7 @@ const createManuscript = async (_, variables, ctx) => {
objectId: manuscript.id,
objectType: 'article',
},
teamType: 'reviewersRejected',
role: 'reviewersRejected',
},
]
......@@ -127,14 +127,14 @@ const createManuscript = async (_, variables, ctx) => {
{
manuscriptId: manuscript.id,
members: [],
teamType: 'externalReviewers',
role: 'externalReviewers',
},
// Reviewers that are not users that have been invited to join the system
{
manuscriptId: manuscript.id,
members: [],
teamType: 'externalReviewersInvited',
role: 'externalReviewersInvited',
},
]
......
......@@ -2,7 +2,8 @@
const without = require('lodash/without')
// const { Team } = require('pubsweet-server/src/models')
const { model: Team } = require('@pubsweet/model-team')
// const { model: Team } = require('@pubsweet/model-team')
const { Team } = require('@pubsweet/models')
/*
This ensures that if there are any assigned editors or science officers
......@@ -11,14 +12,14 @@ const { model: Team } = require('@pubsweet/model-team')
*/
const cleanUpGlobalTeamMembership = async (_, __, ctx) => {
const globalTeams = await Team.findByField({ global: true })
const globalEditorTeam = globalTeams.find(t => t.teamType === 'editors')
const globalEditorTeam = globalTeams.find(t => t.role === 'editors')
const globalScienceOfficerTeam = globalTeams.find(
t => t.teamType === 'scienceOfficers',
t => t.role === 'scienceOfficers',
)
const teams = await Team.all()
const editorTeams = teams.filter(t => t.teamType === 'editor')
const scienceOfficerTeams = teams.filter(t => t.teamType === 'scienceOfficer')
const editorTeams = teams.filter(t => t.role === 'editor')
const scienceOfficerTeams = teams.filter(t => t.role === 'scienceOfficer')
editorTeams.forEach(t => {
t.members.forEach(m => {
......
......@@ -4,9 +4,10 @@ const isEqual = require('lodash/isEqual')
const merge = require('lodash/merge')
const union = require('lodash/union')
const { model: Team } = require('@pubsweet/model-team')
const { model: User } = require('@pubsweet/model-user')
// const { model: Team } = require('@pubsweet/model-team')
// const { model: User } = require('@pubsweet/model-user')
// const { Team, User } = require('pubsweet-server')
const { Team, User } = require('@pubsweet/models')
const Manuscript = require('./manuscript')
const Review = require('../../review/src/review')
......@@ -65,7 +66,7 @@ const resolvers = {
if (action === 'accept') {
let acceptTeams
try {
acceptTeams = await Team.findByField('teamType', 'reviewersAccepted')
acceptTeams = await Team.findByField('role', 'reviewersAccepted')
} catch (e) {
acceptTeams = []
}
......@@ -76,7 +77,7 @@ const resolvers = {
} else if (action === 'reject') {
let rejectTeams
try {
rejectTeams = await Team.findByField('teamType', 'reviewersRejected')
rejectTeams = await Team.findByField('role', 'reviewersRejected')
} catch (e) {
rejectTeams = []
}
......@@ -208,11 +209,11 @@ const resolvers = {
/* Create reviewer status */
const invitedTeam = articleTeams.find(
t => t.teamType === 'reviewersInvited',
t => t.role === 'reviewersInvited',
)
const acceptedTeam = articleTeams.find(
t => t.teamType === 'reviewersAccepted',
t => t.role === 'reviewersAccepted',
)
const submittedReview = reviews.find(
......@@ -236,7 +237,7 @@ const resolvers = {
}
// User is author of article
const authorTeam = articleTeams.find(t => t.teamType === 'author')
const authorTeam = articleTeams.find(t => t.role === 'author')
if (isMember(authorTeam, currentUserId)) {
data.author.push({ ...articleCopy })
......@@ -244,7 +245,7 @@ const resolvers = {
// User is an editor or science officer
if (isGlobal && article.status && article.status.submission.initial) {
const editorTeam = articleTeams.find(t => t.teamType === 'editor')
const editorTeam = articleTeams.find(t => t.role === 'editor')
const assignedEditorId = editorTeam.members[0]
let assignedEditor
......@@ -261,7 +262,10 @@ const resolvers = {
async globalTeams() {
let teams
try {
teams = await Team.findByField('global', true)
// teams = await Team.findByField('global', true)
teams = await Team.query()
.where('global', true)
.eager('[members.[user, alias]]')
} catch (e) {
teams = []
}
......
......@@ -10,9 +10,7 @@ jest.mock('../../../config/authsome.js', () => () => true)
const { model: Manuscript } = require('../src')
const { dbCleaner, api } = require('pubsweet-server/test')
// const { User, Team } = require('pubsweet-server')
const { model: Team } = require('@pubsweet/model-team')
const { model: User } = require('@pubsweet/model-user')
const { User, Team } = require('@pubsweet/models')
const fixtures = require('pubsweet-server/test/fixtures/fixtures')
const authentication = require('pubsweet-server/src/authentication')
......@@ -237,7 +235,7 @@ describe('Manuscript queries', () => {
// First try the accept path
const teamAccepted = await new Team({
name: 'Reviewers who accepted',
teamType: 'reviewersAccepted',
role: 'reviewersAccepted',
object: { objectId: manuscript.id, objectType: 'manuscript' },
}).save()
const { body } = await api.graphql.query(
......@@ -263,7 +261,7 @@ describe('Manuscript queries', () => {
// Then the reject path
const teamRejected = await new Team({
name: 'Reviewers who rejected',
teamType: 'reviewersRejected',
role: 'reviewersRejected',
object: { objectId: manuscript.id, objectType: 'manuscript' },
}).save()
......@@ -297,7 +295,7 @@ describe('Manuscript queries', () => {
it('can find global teams', async () => {
const globalTeam = await new Team({
name: 'Science Officers',
teamType: 'scienceOfficers',
role: 'scienceOfficers',
global: true,
}).save()
......@@ -305,6 +303,9 @@ describe('Manuscript queries', () => {
`query {
globalTeams {
id
members {
id
}
}
}`,
{},
......@@ -318,7 +319,7 @@ describe('Manuscript queries', () => {
const manuscript = await new Manuscript({ title: '1' }).save()
const team = await new Team({
name: 'Team for manuscript',
teamType: 'someType',
role: 'someType',
object: { objectId: manuscript.id, objectType: 'manuscript' },
}).save()
......@@ -343,7 +344,7 @@ describe('Manuscript queries', () => {
await new Team({
name: 'Author team for manuscript',
teamType: 'author',
role: 'author',
members: [user.id],
object: { objectId: authorManuscript.id, objectType: 'manuscript' },
}).save()
......@@ -378,7 +379,7 @@ describe('Manuscript queries', () => {
// Global editors team
await new Team({
name: 'Editors',
teamType: 'editors',
role: 'editors',
members: [user.id],
global: true,
}).save()
......@@ -386,7 +387,7 @@ describe('Manuscript queries', () => {
// An editor team with assigned editors
await new Team({
name: 'Editors',
teamType: 'editor',
role: 'editor',
members: [user.id],
object: { objectId: manuscript.id, objectType: 'manuscript' },
}).save()
......@@ -394,7 +395,7 @@ describe('Manuscript queries', () => {
// An empty editor team
await new Team({
name: 'Editors',
teamType: 'editor',
role: 'editor',
members: [],
object: { objectId: manuscript2.id, objectType: 'manuscript' },
}).save()
......
......@@ -26,11 +26,11 @@ class ExternalTeam extends BaseModel {
static get schema() {
return {
type: 'object',
required: ['manuscriptId', 'members', 'teamType'],
required: ['manuscriptId', 'members', 'role'],
properties: {
manuscriptId: id,
members: arrayOfIds,
teamType: stringNotEmpty,
role: stringNotEmpty,
},
}
}
......
......@@ -28,7 +28,7 @@ const inviteReviewer = async (_, args, context) => {
const { articleId, reviewerId } = args
const articleTeams = await Team.findByField('object.objectId', articleId)
const invitedTeam = articleTeams.find(t => t.teamType === 'reviewersInvited')
const invitedTeam = articleTeams.find(t => t.role === 'reviewersInvited')
const newMembers = union(invitedTeam.members, [reviewerId])
invitedTeam.members = union(invitedTeam.members, [reviewerId])
......@@ -129,15 +129,15 @@ const resolvers = {
// Is user a reviewer of article
const invitedReviewersTeam = articleTeams.find(
t => t.teamType === 'reviewersInvited',
t => t.role === 'reviewersInvited',
)
const acceptedReviewersTeam = articleTeams.find(
t => t.teamType === 'reviewersAccepted',
t => t.role === 'reviewersAccepted',
)
const rejectedReviewersTeam = articleTeams.find(
t => t.teamType === 'reviewersRejected',
t => t.role === 'reviewersRejected',
)
if (isMember(invitedReviewersTeam, ctx.user)) {
......
......@@ -188,7 +188,7 @@ describe('Review queries', () => {
// Global editors team
await new Team({
name: 'Editors',
teamType: 'editors',
role: 'editors',
members: [user.id],
global: true,
}).save()
......@@ -199,7 +199,7 @@ describe('Review queries', () => {
// An invited reviewer team
await new Team({
name: 'Invited reviewers',
teamType: 'reviewersInvited',
role: 'reviewersInvited',
members: [
(invited && invited.id) ||
(rejected && rejected.id) ||
......@@ -211,7 +211,7 @@ describe('Review queries', () => {
// A rejected reviewer team
await new Team({
name: 'Reviewers who rejected the invitation',
teamType: 'reviewersRejected',
role: 'reviewersRejected',
members: rejected ? [rejected.id] : [],
object: { objectId: manuscript.id, objectType: 'manuscript' },
}).save()
......@@ -219,35 +219,35 @@ describe('Review queries', () => {
// An accepted reviewer team
await new Team({
name: 'Reviewers who accepted the invitation',
teamType: 'reviewersAccepted',
role: 'reviewersAccepted',
members: accepted ? [accepted.id] : [],
object: { objectId: manuscript.id, objectType: 'manuscript' },
}).save()
await new Team({
name: 'Author',
teamType: 'author',
role: 'author',
members: [user.id],
object: { objectId: manuscript.id, objectType: 'manuscript' },
}).save()
await new Team({
name: 'Editor',
teamType: 'editor',
role: 'editor',
members: [],
object: { objectId: manuscript.id, objectType: 'manuscript' },
}).save()
await new Team({
name: 'Editor',
teamType: 'editor',
role: 'editor',
members: [scienceOfficer.id],
object: { objectId: manuscript.id, objectType: 'manuscript' },
}).save()