Skip to content

Commit

Permalink
Merge pull request #16 from Divyateja04/deletepost
Browse files Browse the repository at this point in the history
deletePost
  • Loading branch information
thenicekat authored Mar 28, 2024
2 parents 5cbd256 + 552d80b commit 30ee0bc
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 17 deletions.
32 changes: 21 additions & 11 deletions backend/src/service/posts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export const editPost = async (post: {
if (user?.karmaPoints < post.costInPoints)
return {
error: true,
data: "Karma points not enough to create a post."
data: "Karma points not enough to edit a post."
}

if (post.status == "closed")
Expand All @@ -186,7 +186,7 @@ export const editPost = async (post: {
data: "Post has already been closed."
}

let editPost = await prisma.post.update({
let editedPost = await prisma.post.update({
where: { id: post.id },
data: {
source: post.source,
Expand All @@ -198,7 +198,7 @@ export const editPost = async (post: {

return {
error: false,
data: editPost
data: editedPost
};
} catch (err: any) {
logger.error(JSON.stringify({
Expand All @@ -213,11 +213,15 @@ export const editPost = async (post: {
}

export const deletePost = async (post: {
id: string,
id: string
authorEmail: string,
source: string,
destination: string
costInPoints: number,
status: string,
service: string
}): Promise<CustomReturn<Post>> => {
}
): Promise<CustomReturn<Post>> => {
if (!post.authorEmail) return {
error: true,
data: "Author email is required."
Expand All @@ -229,7 +233,6 @@ export const deletePost = async (post: {
email: post.authorEmail
}
});

if (!user) return {
error: true,
data: "User does not exist."
Expand All @@ -241,13 +244,20 @@ export const deletePost = async (post: {
data: "Post has already been closed."
}

let deletePost = await prisma.post.delete({
where: { id: post.id },
})
// Delete post requests and then delete the post
// Make this a transaction
let [_returnedRequests, returnedPost] = await prisma.$transaction([
prisma.request.deleteMany({
where: { postId: post.id },
}),
prisma.post.delete({
where: { id: post.id },
})
]);

return {
error: false,
data: deletePost
data: returnedPost
};
} catch (err: any) {
logger.error(JSON.stringify({
Expand All @@ -256,7 +266,7 @@ export const deletePost = async (post: {
}));
return {
error: true,
data: "Some error occurred while creating the post"
data: "Some error occurred while deleting the post"
}
}
}
102 changes: 97 additions & 5 deletions backend/tests/posts.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect } from "@jest/globals";
import { prismaMock } from "./_mockdb";
import { createPost, getAllPosts, getMyPosts, getPostDetails, editPost } from "../src/service/posts.service";
import { Post, User } from ".prisma/client";
import { createPost, getAllPosts, getMyPosts, getPostDetails, editPost, deletePost } from "../src/service/posts.service";
import { Post, User, Request } from ".prisma/client";

const userWith10KarmaPoints: User = {
id: "1",
Expand Down Expand Up @@ -34,6 +34,13 @@ const post: Post & {
status: "open",
}

const request: Request = {
id: "1",
postId: "1",
senderEmail: "[email protected]",
status: "open"
}

describe("Create a new post", () => {
it("should create a new post", () => {

Expand Down Expand Up @@ -135,7 +142,7 @@ describe("Get my posts", () => {
})

describe("Get post details", () => {
it("should get post details", () => {
it("should get post details successfully", () => {
prismaMock.post.findUnique.mockResolvedValue(post);

expect(getPostDetails(
Expand Down Expand Up @@ -170,7 +177,7 @@ describe("Get post details", () => {
})

describe("Update post", () => {
it("should update post", () => {
it("should update the post successfully", () => {
prismaMock.user.findUnique.mockResolvedValue(userWith10KarmaPoints);
prismaMock.post.update.mockResolvedValue(post);

Expand All @@ -188,6 +195,23 @@ describe("Update post", () => {
});
});

it("should throw an error if user does not exist", () => {
prismaMock.user.findUnique.mockResolvedValue(null);

expect(editPost({
id: post.id,
source: post.source,
destination: post.destination,
costInPoints: post.costInPoints,
service: post.service,
status: post.status,
authorEmail: post.authorEmail
})).resolves.toEqual({
error: true,
data: "User does not exist."
});
})

it("should throw an error if new karma higher than user karma", () => {
prismaMock.user.findUnique.mockResolvedValue(userWith0KarmaPoints);

Expand All @@ -201,7 +225,25 @@ describe("Update post", () => {
authorEmail: post.authorEmail
})).resolves.toEqual({
error: true,
data: "Karma points not enough to create a post."
data: "Karma points not enough to edit a post."
});

})

it("should throw an error if post has already been closed", () => {
prismaMock.user.findUnique.mockResolvedValue(userWith10KarmaPoints);

expect(editPost({
id: post.id,
source: post.source,
destination: post.destination,
costInPoints: post.costInPoints,
service: post.service,
status: "closed",
authorEmail: post.authorEmail
})).resolves.toEqual({
error: true,
data: "Post has already been closed."
});

})
Expand All @@ -222,4 +264,54 @@ describe("Update post", () => {
data: "Some error occurred while updating the post"
});
});
})

describe("Delete post", () => {
it("should throw an error if user does not exist", () => {
prismaMock.post.findUnique.mockResolvedValue(post);
expect(deletePost(post)).resolves.toEqual({
error: true,
data: "User does not exist."
});
});

it("should throw an error if post has already been closed", () => {
prismaMock.user.findUnique.mockResolvedValue(userWith10KarmaPoints);
const post: Post & {
authorEmail: string
} = {
id: "1",
authorId: "1",
authorEmail: "[email protected]",
source: "source",
destination: "destination",
costInPoints: 10,
service: "service",
status: "closed",
}
prismaMock.post.findUnique.mockResolvedValue(post);
expect(deletePost(post)).resolves.toEqual({
error: true,
data: "Post has already been closed."
});
});

it("should catch any error occurred", () => {
prismaMock.user.findUnique.mockResolvedValue(userWith10KarmaPoints);
prismaMock.post.findUnique.mockRejectedValue(new Error("Some Error ocurred"));
expect(deletePost(post)).resolves.toEqual({
error: true,
data: "Some error occurred while deleting the post"
});
});

it("should delete the post successfully", () => {
prismaMock.user.findUnique.mockResolvedValue(userWith10KarmaPoints);
prismaMock.$transaction.mockResolvedValue([[request], post]);

expect(deletePost(post)).resolves.toEqual({
error: false,
data: post
});
});
})
4 changes: 3 additions & 1 deletion frontend/app/post/details/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ const PostDetailsPage = ({ params }: Props) => {
if (res.status == HttpCodes.OK) {
setError("")
setMessage("Post deleted successfully.")
window.location.href = ('/user/dashboard')
setTimeout(function () {
window.location.href = "/";
}, 1000);
} else if (res.status == HttpCodes.UNAUTHORIZED) {
window.location.href = ('/')
}
Expand Down

0 comments on commit 30ee0bc

Please sign in to comment.