Skip to content

Commit

Permalink
Soft delete groups, wishlists and items
Browse files Browse the repository at this point in the history
  • Loading branch information
RobVermeer committed Dec 6, 2023
1 parent 4b24fe7 commit c8618e8
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 6 deletions.
42 changes: 42 additions & 0 deletions prisma/migrations/20231206165259_soft_delete/migration.sql
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/lib/groupDraw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/groups/deleteGroupById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
})

Expand Down
1 change: 1 addition & 0 deletions src/lib/groups/followGroupById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const followGroupById = async (id: string) => {
const groupToFollow = await prisma.group.findUnique({
where: {
id,
removed: false,
},
})

Expand Down
2 changes: 1 addition & 1 deletion src/lib/groups/getGroupById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/lib/groups/getGroupsForUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const getGroupsForUser = cache(async () => {
members: {
some: { id: userId },
},
removed: false,
},
})

Expand Down
1 change: 1 addition & 0 deletions src/lib/groups/leaveGroupById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const leaveGroupById = async (id: string) => {
const groupToLeave = await prisma.group.findUnique({
where: {
id,
removed: false,
},
})

Expand Down
1 change: 1 addition & 0 deletions src/lib/groups/publicProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const groupProperties = {
},
wishlist: {
select: wishlistProperties,
where: { removed: false },
},
members: true,
theme: true,
Expand Down
1 change: 1 addition & 0 deletions src/lib/groups/updateGroupById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const updateGroupById = async (id: string, formData: FormData) => {
where: {
id,
createdBy: { id: userId },
removed: false,
},
})

Expand Down
4 changes: 3 additions & 1 deletion src/lib/wishlistItems/deleteWishlistItemById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ export const deleteWishlistItemById = async (id: string) => {
wishlist: {
userId,
},
removed: false,
},
})

if (!wishlistItemToDelete) {
throw new Error(t("item.notFound"))
}

await prisma.wishlistItem.delete({
await prisma.wishlistItem.update({
data: { removed: true },
where: { id },
})

Expand Down
1 change: 1 addition & 0 deletions src/lib/wishlistItems/toggleWishlistItemById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const toggleWishlistItemById = async (id: string) => {
const wishlistItem = await prisma.wishlistItem.findUnique({
where: {
id,
removed: false,
},
select: wishlistItemProperties,
})
Expand Down
1 change: 1 addition & 0 deletions src/lib/wishlistItems/updateWishlistItemById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const updateWishlistItemById = async (
wishlist: {
userId,
},
removed: false,
},
})

Expand Down
6 changes: 5 additions & 1 deletion src/lib/wishlists/deleteWishlistById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ export const deleteWishlistById = async (id: string) => {
where: {
id,
userId,
removed: false,
},
})

if (!wishlist) {
throw new Error(t("wishlist.notFound"))
}

await prisma.wishlist.delete({
await prisma.wishlist.update({
data: {
removed: true,
},
where: { id },
})

Expand Down
2 changes: 1 addition & 1 deletion src/lib/wishlists/getWishlistById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wishlists/getWishlistsForUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => ({
Expand Down
2 changes: 2 additions & 0 deletions src/lib/wishlists/publicProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ export const wishlistProperties = {
},
groups: {
select: { id: true, title: true },
where: { removed: false },
},
wishlistItem: {
select: wishlistItemProperties,
where: { removed: false },
},
}
1 change: 1 addition & 0 deletions src/lib/wishlists/updateWishlistById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const updateWishlistById = async (id: string, formData: FormData) => {
where: {
id,
userId,
removed: false,
},
})

Expand Down

0 comments on commit c8618e8

Please sign in to comment.