cms icon indicating copy to clipboard operation
cms copied to clipboard

bug: Most videos are bookmarked, without explicitly being done.

Open Zafeeruddin opened this issue 1 year ago • 9 comments

Describe the bug Videos till week-12 are bookmarked by default, and errors out if bookmark being removed To Reproduce Steps to reproduce the behavior:

  1. Uncheck bookmarks, which are previously bookmarked
  2. See error

Expected behavior Expected: a) No previous bookmarks, which I didn't do. b) No error if uncheck it. Screenshots or GIFs

image Works fine for new bookmarks

Zafeeruddin avatar May 14 '24 16:05 Zafeeruddin

This is a pretty weird bug. we can't reproduce it in development. We could speculate why this error is happening.

  1. There is no Bookmark data in the database image

  2. but on the client side we are manual assigning some bookmark data like

{
    id: 5,
    userId: '1',
    contentId: 3,
    createdAt: new Date(),
}
  1. when you click on bookMark button, the deleteBookmarkHandler gets triggered and tries to run the following code to delete the bookmark data from db. since you don't have a bookmark record with id:5 and userId:'1' we are getting the prisma error.
  const userId = session.user.id;
  const { id } = data;
  console.log(id);
  try {
    const deletedBookmark = await db.bookmark.delete({
      where: { id, userId },
    });

victorchrollo14 avatar May 15 '24 06:05 victorchrollo14

The only way to reproduce this bug in dev would be to modify the addedBookmark state with wrong data that doesn't exist in database.

! Not sure why this bug would appear in production, we'll probably have to look through the data in prod. Right now in dev we are using less example data and in production we have more data so maybe the the current code which works for a single data item, might be producing this bug, when used with a lot of data.

export const useBookmark = (bookmark: Bookmark | null, contentId: number) => {
  const newBookmark = {
    id: 5,
    userId: '1',
    contentId: 3,
    createdAt: new Date(),
  };
  const [addedBookmark, setAddedBookmark] = useState<Bookmark | null>(
    newBookmark,
  );

Screenshot from 2024-05-15 11-17-34

victorchrollo14 avatar May 15 '24 06:05 victorchrollo14

Were you able to get the cause of this problem? If you haven't started working on it, I can pick it up.

uttamsutariya avatar May 15 '24 18:05 uttamsutariya

Were you able to get the cause of this problem? If you haven't started working on it, I can pick it up.

I don't see this problem in development, it probably has something to do with the production database.

Go ahead and work on it, if you'd like to.

victorchrollo14 avatar May 16 '24 01:05 victorchrollo14

Picking up this issue

uttamsutariya avatar May 16 '24 08:05 uttamsutariya

image

So the issue here is even my userId is 40541, the server action fetches the bookmark with any userId (exists in db) as you can see in below screenshots.

image image image image

This is because there isn't any constraint while fetching all the contents.

image

There must be a constraint that makes sure we are fetching the bookmarks of the currently logged-in user only.

image

uttamsutariya avatar May 16 '24 09:05 uttamsutariya

cool. nice job bro. Did you insert more data into the database to test it out?

victorchrollo14 avatar May 16 '24 17:05 victorchrollo14

@uttamsutariya how did you found out that issue is with bookmark:true by adding more videos or by creating a diff user?

Prakhar788 avatar May 17 '24 09:05 Prakhar788

Initially, I didn't do anything in my local cms.

I saw the prisma error message, then I tried to see the network request to remove the bookmark.

While doing this I saw that for each request the userId is not unique & not mine. So I felt that there is some issue with all the the fetched bookmarks.

Then I saw all the bookmarks have different different userId.

Now at that point I understood the problem that while fetching the content, the bookmarks are not being fetched of only mine, it's being fetched without any constraint of currently logged-in user.

So this way then I tried to create few users in my local db & resolved this issue.

So this kinda debugging skills you will eventually learn while working with prod databases & stuffs.

@hkirat Can you please review this PR as many people are struggling with the unexpected behaviour of bookmarks that they haven't added.

uttamsutariya avatar May 17 '24 10:05 uttamsutariya