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