Creating a "reorderRules" mutation
This commit is contained in:
1 parent
50c9268942
commit
d71b24aa03
2 files changed
+52
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import {
|
||||
GraphQLNonNull,
|
||||
GraphQLID,
|
||||
GraphQLInt
|
||||
} from 'graphql'
|
||||
import { isBetween } from '../../utils/functions'
|
||||
import { CompetitionType } from '../types'
|
||||
import Competition from '../../models/competition'
|
||||
|
||||
const args = {
|
||||
competitionCodeName: { type: new GraphQLNonNull(GraphQLID) },
|
||||
fromNumber: { type: new GraphQLNonNull(GraphQLInt) },
|
||||
toNumber: { type: new GraphQLNonNull(GraphQLInt) }
|
||||
}
|
||||
|
||||
const resolve = (parent, { competitionCodeName, fromNumber, toNumber }, context) => {
|
||||
// let authIdOnSession = context.user.sub
|
||||
return new Promise((resolve, reject) => {
|
||||
Competition.findOne({ codeName: competitionCodeName }).exec((err, competition) => {
|
||||
if (err) reject(err)
|
||||
else {
|
||||
const delta = fromNumber > toNumber ? 1 : -1
|
||||
competition.rules.forEach((part) => {
|
||||
if (part.number === fromNumber) {
|
||||
part.number = toNumber
|
||||
} else if (isBetween(part.number, fromNumber, toNumber)) {
|
||||
part.number += delta
|
||||
}
|
||||
})
|
||||
|
||||
competition.save((err) => {
|
||||
if (err) reject(err)
|
||||
else {
|
||||
resolve(competition)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const mutation = {
|
||||
reorderRules: {
|
||||
type: CompetitionType,
|
||||
args,
|
||||
resolve
|
||||
}
|
||||
}
|
||||
|
||||
export default mutation
|
||||
@@ -8,6 +8,7 @@ import RegisterToCompetitionMutation from './RegisterToCompetition'
|
||||
import EditParticipantMutation from './EditParticipant'
|
||||
import AddRuleMutation from './AddRule'
|
||||
import EditRuleMutation from './EditRule'
|
||||
import ReorderRulesMutation from './ReorderRules'
|
||||
import AddPrizeMutation from './AddPrize'
|
||||
|
||||
const Mutation = new GraphQLObjectType({
|
||||
@@ -19,6 +20,7 @@ const Mutation = new GraphQLObjectType({
|
||||
EditParticipantMutation,
|
||||
AddRuleMutation,
|
||||
EditRuleMutation,
|
||||
ReorderRulesMutation,
|
||||
AddPrizeMutation
|
||||
)
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user