import { ApolloError } from 'apollo-server-errors' ; import { Arg , Ctx , Mutation , Resolver } from 'type-graphql' ; import { ContextType } from '@root/server' ; import Comment from '@entities/Comment' ; import { hasPermission } from '@utils/common/hasPermission' ; import { Role } from '@enums/Roles' ; import EditCommentInput from './EditCommentInput' ; @ Resolver ( ) export default class EditCommentResolver { @ Mutation ( ( ) => Boolean ) async editComment ( @ Arg ( 'commentId' ) commentId , @ Arg ( 'data' ) data , @ Ctx ( ) { uid , getUser } ) { const comment = await Comment . findOne ( commentId ) ; if ( ! comment ) throw new ApolloError ( 'Comment not found' ) ; const author = await comment . author ; if ( author . id !== uid && ! hasPermission ( ( await getUser ( ) ) . roles , Role . Moderator ) ) { throw new ApolloError ( 'You do not have permission to edit this comment!' ) ; } Object . assign ( comment , data ) ; comment . lastEdited = String ( Date . now ( ) ) ; await comment . save ( ) ; return true ; } }