diff --git a/prisma/migrations/20231206165259_soft_delete/migration.sql b/prisma/migrations/20231206165259_soft_delete/migration.sql new file mode 100644 index 0000000..7aef5e2 --- /dev/null +++ b/prisma/migrations/20231206165259_soft_delete/migration.sql @@ -0,0 +1,42 @@ +-- RedefineTables +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_Group" ( + "id" TEXT NOT NULL PRIMARY KEY, + "title" TEXT NOT NULL, + "createdAt" DATETIME NOT NULL, + "theme" TEXT, + "removed" BOOLEAN NOT NULL DEFAULT false, + "userId" TEXT NOT NULL, + CONSTRAINT "Group_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); +INSERT INTO "new_Group" ("createdAt", "id", "theme", "title", "userId") SELECT "createdAt", "id", "theme", "title", "userId" FROM "Group"; +DROP TABLE "Group"; +ALTER TABLE "new_Group" RENAME TO "Group"; +CREATE TABLE "new_WishlistItem" ( + "id" TEXT NOT NULL PRIMARY KEY, + "title" TEXT NOT NULL, + "url" TEXT, + "createdAt" DATETIME NOT NULL, + "removed" BOOLEAN NOT NULL DEFAULT false, + "userId" TEXT, + "wishlistId" TEXT NOT NULL, + CONSTRAINT "WishlistItem_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE, + CONSTRAINT "WishlistItem_wishlistId_fkey" FOREIGN KEY ("wishlistId") REFERENCES "Wishlist" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); +INSERT INTO "new_WishlistItem" ("createdAt", "id", "title", "url", "userId", "wishlistId") SELECT "createdAt", "id", "title", "url", "userId", "wishlistId" FROM "WishlistItem"; +DROP TABLE "WishlistItem"; +ALTER TABLE "new_WishlistItem" RENAME TO "WishlistItem"; +CREATE TABLE "new_Wishlist" ( + "id" TEXT NOT NULL PRIMARY KEY, + "title" TEXT, + "createdAt" DATETIME NOT NULL, + "theme" TEXT, + "removed" BOOLEAN NOT NULL DEFAULT false, + "userId" TEXT NOT NULL, + CONSTRAINT "Wishlist_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); +INSERT INTO "new_Wishlist" ("createdAt", "id", "theme", "title", "userId") SELECT "createdAt", "id", "theme", "title", "userId" FROM "Wishlist"; +DROP TABLE "Wishlist"; +ALTER TABLE "new_Wishlist" RENAME TO "Wishlist"; +PRAGMA foreign_key_check; +PRAGMA foreign_keys=ON; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 4f1f760..9c5b2b0 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -64,6 +64,7 @@ model Group { title String createdAt DateTime theme String? + removed Boolean @default(false) wishlist Wishlist[] createdBy User @relation(name: "OwnedGroups", fields: [userId], references: [id], onDelete: Cascade) userId String @@ -75,6 +76,7 @@ model Wishlist { title String? createdAt DateTime theme String? + removed Boolean @default(false) wishlistItem WishlistItem[] userId String user User @relation(fields: [userId], references: [id], onDelete: Cascade) @@ -86,6 +88,7 @@ model WishlistItem { title String url String? createdAt DateTime + removed Boolean @default(false) userId String? boughtBy User? @relation(fields: [userId], references: [id]) wishlistId String diff --git a/src/lib/groupDraw/index.ts b/src/lib/groupDraw/index.ts index 5680a01..6646006 100644 --- a/src/lib/groupDraw/index.ts +++ b/src/lib/groupDraw/index.ts @@ -13,6 +13,7 @@ export const groupDraw = async (groupId: string, formData: FormData) => { const group = await prisma.group.findUnique({ where: { id: groupId, + removed: false, }, include: { members: true, diff --git a/src/lib/groups/deleteGroupById.ts b/src/lib/groups/deleteGroupById.ts index e4e90c9..46fd67c 100644 --- a/src/lib/groups/deleteGroupById.ts +++ b/src/lib/groups/deleteGroupById.ts @@ -32,7 +32,8 @@ export const deleteGroupById = async (id: string) => { throw new Error(t("noAccess")) } - await prisma.group.delete({ + await prisma.group.update({ + data: { removed: true }, where: { id }, }) diff --git a/src/lib/groups/followGroupById.ts b/src/lib/groups/followGroupById.ts index 490338a..bb2839c 100644 --- a/src/lib/groups/followGroupById.ts +++ b/src/lib/groups/followGroupById.ts @@ -22,6 +22,7 @@ export const followGroupById = async (id: string) => { const groupToFollow = await prisma.group.findUnique({ where: { id, + removed: false, }, }) diff --git a/src/lib/groups/getGroupById.ts b/src/lib/groups/getGroupById.ts index ba2fabd..ddd94d4 100644 --- a/src/lib/groups/getGroupById.ts +++ b/src/lib/groups/getGroupById.ts @@ -20,7 +20,7 @@ export const getGroupById = cache(async (id: string) => { const data = await prisma.group.findUnique({ select: groupProperties, - where: { id }, + where: { id, removed: false }, }) if (!data) { diff --git a/src/lib/groups/getGroupsForUser.ts b/src/lib/groups/getGroupsForUser.ts index 3837a39..ef7845a 100644 --- a/src/lib/groups/getGroupsForUser.ts +++ b/src/lib/groups/getGroupsForUser.ts @@ -24,6 +24,7 @@ export const getGroupsForUser = cache(async () => { members: { some: { id: userId }, }, + removed: false, }, }) diff --git a/src/lib/groups/leaveGroupById.ts b/src/lib/groups/leaveGroupById.ts index 4e237fb..2927d2a 100644 --- a/src/lib/groups/leaveGroupById.ts +++ b/src/lib/groups/leaveGroupById.ts @@ -22,6 +22,7 @@ export const leaveGroupById = async (id: string) => { const groupToLeave = await prisma.group.findUnique({ where: { id, + removed: false, }, }) diff --git a/src/lib/groups/publicProperties.ts b/src/lib/groups/publicProperties.ts index c262275..0bda0e9 100644 --- a/src/lib/groups/publicProperties.ts +++ b/src/lib/groups/publicProperties.ts @@ -16,6 +16,7 @@ export const groupProperties = { }, wishlist: { select: wishlistProperties, + where: { removed: false }, }, members: true, theme: true, diff --git a/src/lib/groups/updateGroupById.ts b/src/lib/groups/updateGroupById.ts index ff432ee..8e42a3c 100644 --- a/src/lib/groups/updateGroupById.ts +++ b/src/lib/groups/updateGroupById.ts @@ -25,6 +25,7 @@ export const updateGroupById = async (id: string, formData: FormData) => { where: { id, createdBy: { id: userId }, + removed: false, }, }) diff --git a/src/lib/wishlistItems/deleteWishlistItemById.ts b/src/lib/wishlistItems/deleteWishlistItemById.ts index a5fbc0f..9c334cb 100644 --- a/src/lib/wishlistItems/deleteWishlistItemById.ts +++ b/src/lib/wishlistItems/deleteWishlistItemById.ts @@ -24,6 +24,7 @@ export const deleteWishlistItemById = async (id: string) => { wishlist: { userId, }, + removed: false, }, }) @@ -31,7 +32,8 @@ export const deleteWishlistItemById = async (id: string) => { throw new Error(t("item.notFound")) } - await prisma.wishlistItem.delete({ + await prisma.wishlistItem.update({ + data: { removed: true }, where: { id }, }) diff --git a/src/lib/wishlistItems/toggleWishlistItemById.ts b/src/lib/wishlistItems/toggleWishlistItemById.ts index 6b3879f..365c8c9 100644 --- a/src/lib/wishlistItems/toggleWishlistItemById.ts +++ b/src/lib/wishlistItems/toggleWishlistItemById.ts @@ -22,6 +22,7 @@ export const toggleWishlistItemById = async (id: string) => { const wishlistItem = await prisma.wishlistItem.findUnique({ where: { id, + removed: false, }, select: wishlistItemProperties, }) diff --git a/src/lib/wishlistItems/updateWishlistItemById.ts b/src/lib/wishlistItems/updateWishlistItemById.ts index db64ca6..0f42ed7 100644 --- a/src/lib/wishlistItems/updateWishlistItemById.ts +++ b/src/lib/wishlistItems/updateWishlistItemById.ts @@ -30,6 +30,7 @@ export const updateWishlistItemById = async ( wishlist: { userId, }, + removed: false, }, }) diff --git a/src/lib/wishlists/deleteWishlistById.ts b/src/lib/wishlists/deleteWishlistById.ts index 2a9712d..c33cd2c 100644 --- a/src/lib/wishlists/deleteWishlistById.ts +++ b/src/lib/wishlists/deleteWishlistById.ts @@ -22,6 +22,7 @@ export const deleteWishlistById = async (id: string) => { where: { id, userId, + removed: false, }, }) @@ -29,7 +30,10 @@ export const deleteWishlistById = async (id: string) => { throw new Error(t("wishlist.notFound")) } - await prisma.wishlist.delete({ + await prisma.wishlist.update({ + data: { + removed: true, + }, where: { id }, }) diff --git a/src/lib/wishlists/getWishlistById.ts b/src/lib/wishlists/getWishlistById.ts index 8c0441e..226f4ee 100644 --- a/src/lib/wishlists/getWishlistById.ts +++ b/src/lib/wishlists/getWishlistById.ts @@ -20,7 +20,7 @@ export const getWishlistById = cache( const userId = session.user.id const data = await prisma.wishlist.findUnique({ select: wishlistProperties, - where: { id }, + where: { id, removed: false }, }) if (!data) { diff --git a/src/lib/wishlists/getWishlistsForUser.ts b/src/lib/wishlists/getWishlistsForUser.ts index d68f64d..2d151b1 100644 --- a/src/lib/wishlists/getWishlistsForUser.ts +++ b/src/lib/wishlists/getWishlistsForUser.ts @@ -20,7 +20,7 @@ export const getWishlistsForUser = cache(async () => { const data = await prisma.wishlist.findMany({ orderBy: { createdAt: "asc" }, select: wishlistProperties, - where: { userId }, + where: { userId, removed: false }, }) const wishlists = data.map((wishlist) => ({ diff --git a/src/lib/wishlists/publicProperties.ts b/src/lib/wishlists/publicProperties.ts index 59c7095..cde9409 100644 --- a/src/lib/wishlists/publicProperties.ts +++ b/src/lib/wishlists/publicProperties.ts @@ -17,8 +17,10 @@ export const wishlistProperties = { }, groups: { select: { id: true, title: true }, + where: { removed: false }, }, wishlistItem: { select: wishlistItemProperties, + where: { removed: false }, }, } diff --git a/src/lib/wishlists/updateWishlistById.ts b/src/lib/wishlists/updateWishlistById.ts index 9ee35b8..0da3d7a 100644 --- a/src/lib/wishlists/updateWishlistById.ts +++ b/src/lib/wishlists/updateWishlistById.ts @@ -25,6 +25,7 @@ export const updateWishlistById = async (id: string, formData: FormData) => { where: { id, userId, + removed: false, }, })