add delete post graphql api and tests

This commit is contained in:
Hongbo Wu 2024-06-18 16:23:34 +08:00
parent 16f67c854c
commit 72a477c25a
4 changed files with 110 additions and 8 deletions

View file

@ -153,6 +153,7 @@ import {
} from './index'
import {
createPostResolver,
deletePostResolver,
postResolver,
postsResolver,
updatePostResolver,
@ -325,6 +326,7 @@ export const functionResolvers = {
deleteFolderPolicy: deleteFolderPolicyResolver,
createPost: createPostResolver,
updatePost: updatePostResolver,
deletePost: deletePostResolver,
},
Query: {
me: getMeUserResolver,
@ -913,4 +915,5 @@ export const functionResolvers = {
...resultResolveTypeResolver('Post'),
...resultResolveTypeResolver('CreatePost'),
...resultResolveTypeResolver('UpdatePost'),
...resultResolveTypeResolver('DeletePost'),
}

View file

@ -3,7 +3,11 @@ import {
CreatePostError,
CreatePostErrorCode,
CreatePostSuccess,
DeletePostError,
DeletePostErrorCode,
DeletePostSuccess,
MutationCreatePostArgs,
MutationDeletePostArgs,
MutationUpdatePostArgs,
PostEdge,
PostErrorCode,
@ -19,6 +23,7 @@ import {
} from '../../generated/graphql'
import {
createPublicPost,
deletePosts,
findPublicPostById,
findPublicPostsByUserId,
updatePost,
@ -193,3 +198,31 @@ export const updatePostResolver = authorized<
post,
}
})
export const deletePostResolver = authorized<
DeletePostSuccess,
DeletePostError,
MutationDeletePostArgs
>(async (_, { id }, { uid, log }) => {
if (!id) {
log.error('Invalid args', { id })
return {
errorCodes: [DeletePostErrorCode.BadRequest],
}
}
const result = await deletePosts(uid, [id])
if (!result.affected) {
log.error('Failed to delete post', { id })
return {
errorCodes: [DeletePostErrorCode.Unauthorized],
}
}
return {
success: true,
}
})

View file

@ -61,14 +61,9 @@ export const createPosts = async (
}
export const deletePosts = async (userId: string, postIds: string[]) => {
return authTrx(
async (trx) => {
await trx.getRepository(Post).delete(postIds)
},
{
uid: userId,
}
)
return authTrx(async (trx) => trx.getRepository(Post).delete(postIds), {
uid: userId,
})
}
export const findPublicPostById = async (id: string) => {

View file

@ -383,4 +383,75 @@ describe('Post Resolvers', () => {
expect(post?.title).to.eql('Updated Post')
})
})
describe('deletePostResolver', () => {
const mutation = `
mutation DeletePost($id: ID!) {
deletePost(id: $id) {
... on DeletePostSuccess {
success
}
... on DeletePostError {
errorCodes
}
}
}
`
let postId: string
before(async () => {
const post = {
title: 'Post',
content: 'Content',
user: loginUser,
}
const newPost = await createPosts(loginUser.id, [post])
postId = newPost[0].id
})
it('should return an error if the args are invalid', async () => {
const response = await graphqlRequest(mutation, authToken, {
id: '',
})
expect(response.body.data.deletePost.errorCodes).to.eql(['BAD_REQUEST'])
})
it('should return an error if the post is not found', async () => {
const response = await graphqlRequest(mutation, authToken, {
id: generateFakeUuid(),
})
expect(response.body.data.deletePost.errorCodes).to.eql(['UNAUTHORIZED'])
})
it('should return an error if the user is not the owner of the post', async () => {
const notOwner = await createTestUser('notOwner')
const notOwnerToken = await loginAndGetAuthToken(notOwner.email)
const response = await graphqlRequest(mutation, notOwnerToken, {
id: postId,
})
expect(response.body.data.deletePost.errorCodes).to.eql(['UNAUTHORIZED'])
const post = await findPublicPostById(postId)
expect(post).to.exist
await deleteUser(notOwner.id)
})
it('should delete the post', async () => {
const response = await graphqlRequest(mutation, authToken, {
id: postId,
})
expect(response.body.data.deletePost.success).to.be.true
const post = await findPublicPostById(postId)
expect(post).to.not.exist
})
})
})