import { ApolloError } from 'apollo-server-errors' ; import { Arg , Ctx , Mutation , Resolver } from 'type-graphql' ; import { ContextType } from '@root/server' ; import Question from '@entities/Question' ; import { hasPermission } from '@utils/common/hasPermission' ; import { Role } from '@enums/Roles' ; @ Resolver ( ) export default class DeleteQuestionResolver { @ Mutation ( ( ) => Boolean ) async deleteQuestion ( @ Arg ( 'questionId' ) questionId , @ Ctx ( ) { uid , getUser } ) { const question = await Question . findOne ( questionId ) ; if ( ! question ) throw new ApolloError ( 'Question not found' ) ; const author = await question . author ; if ( author . id !== uid && ! hasPermission ( ( await getUser ( ) ) . roles , Role . Moderator ) ) { throw new ApolloError ( 'You do not have permission to edit this question' ) ; } question . remove ( ) ; return true ; } }