From 508844dd9d28ad05ac86e12dc2c32c210487563d Mon Sep 17 00:00:00 2001 From: daniel31x13 Date: Wed, 30 Oct 2024 16:47:40 -0400 Subject: [PATCH] bug fixes --- components/ProfileDropdown.tsx | 4 +- components/SettingsSidebar.tsx | 5 +- .../users/userId/deleteUserById.ts | 51 +++++++++++++------ lib/api/verifyCapacity.ts | 10 +++- pages/settings/billing.tsx | 11 ++-- pages/settings/delete.tsx | 4 +- pages/subscribe.tsx | 2 - .../migration.sql | 17 +++++++ prisma/schema.prisma | 8 +-- 9 files changed, 82 insertions(+), 30 deletions(-) create mode 100644 prisma/migrations/20241030200844_createdby_fields_can_be_null/migration.sql diff --git a/components/ProfileDropdown.tsx b/components/ProfileDropdown.tsx index 81e0505..9d1260b 100644 --- a/components/ProfileDropdown.tsx +++ b/components/ProfileDropdown.tsx @@ -6,6 +6,8 @@ import { signOut } from "next-auth/react"; import { useTranslation } from "next-i18next"; import { useUser } from "@/hooks/store/user"; +const stripeEnabled = process.env.NEXT_PUBLIC_STRIPE === "true"; + export default function ProfileDropdown() { const { t } = useTranslation(); const { settings, updateSettings } = useLocalSettingsStore(); @@ -73,7 +75,7 @@ export default function ProfileDropdown() { )} - {!user.parentSubscriptionId && ( + {!user.parentSubscriptionId && stripeEnabled && (
  • - {process.env.NEXT_PUBLIC_STRIPE && ( + {process.env.NEXT_PUBLIC_STRIPE && !user.parentSubscriptionId && (
    { if (!stripeEnabled) { - return false; + const totalLinks = await prisma.link.count({ + where: { + createdBy: { + id: userId, + }, + }, + }); + + return MAX_LINKS_PER_USER - (numberOfImports + totalLinks) < 0; } const user = await prisma.user.findUnique({ diff --git a/pages/settings/billing.tsx b/pages/settings/billing.tsx index 1c561e3..37a938f 100644 --- a/pages/settings/billing.tsx +++ b/pages/settings/billing.tsx @@ -28,12 +28,13 @@ export default function Billing() { const router = useRouter(); const { t } = useTranslation(); - useEffect(() => { - if (!process.env.NEXT_PUBLIC_STRIPE) router.push("/settings/profile"); - }, []); - - const { data: users = [] } = useUsers(); const { data: account } = useUser(); + const { data: users = [] } = useUsers(); + + useEffect(() => { + if (!process.env.NEXT_PUBLIC_STRIPE || account.parentSubscriptionId) + router.push("/settings/account"); + }, []); const [searchQuery, setSearchQuery] = useState(""); const [filteredUsers, setFilteredUsers] = useState(); diff --git a/pages/settings/delete.tsx b/pages/settings/delete.tsx index 0d11b87..0479d8f 100644 --- a/pages/settings/delete.tsx +++ b/pages/settings/delete.tsx @@ -7,6 +7,7 @@ import Link from "next/link"; import Button from "@/components/ui/Button"; import { useTranslation } from "next-i18next"; import getServerSideProps from "@/lib/client/getServerSideProps"; +import { useUser } from "@/hooks/store/user"; export default function Delete() { const [password, setPassword] = useState(""); @@ -15,6 +16,7 @@ export default function Delete() { const [submitLoader, setSubmitLoader] = useState(false); const { data } = useSession(); const { t } = useTranslation(); + const { data: user } = useUser(); const submit = async () => { const body = { @@ -83,7 +85,7 @@ export default function Delete() { />
    - {process.env.NEXT_PUBLIC_STRIPE && ( + {process.env.NEXT_PUBLIC_STRIPE && !user.parentSubscriptionId && (
    {t("optional")} {t("feedback_help")} diff --git a/pages/subscribe.tsx b/pages/subscribe.tsx index 20e9599..4c490f0 100644 --- a/pages/subscribe.tsx +++ b/pages/subscribe.tsx @@ -9,8 +9,6 @@ import getServerSideProps from "@/lib/client/getServerSideProps"; import { Trans, useTranslation } from "next-i18next"; import { useUser } from "@/hooks/store/user"; -const stripeEnabled = process.env.NEXT_PUBLIC_STRIPE === "true"; - export default function Subscribe() { const { t } = useTranslation(); const [submitLoader, setSubmitLoader] = useState(false); diff --git a/prisma/migrations/20241030200844_createdby_fields_can_be_null/migration.sql b/prisma/migrations/20241030200844_createdby_fields_can_be_null/migration.sql new file mode 100644 index 0000000..85c04d9 --- /dev/null +++ b/prisma/migrations/20241030200844_createdby_fields_can_be_null/migration.sql @@ -0,0 +1,17 @@ +-- DropForeignKey +ALTER TABLE "Collection" DROP CONSTRAINT "Collection_createdById_fkey"; + +-- DropForeignKey +ALTER TABLE "Link" DROP CONSTRAINT "Link_createdById_fkey"; + +-- AlterTable +ALTER TABLE "Collection" ALTER COLUMN "createdById" DROP NOT NULL; + +-- AlterTable +ALTER TABLE "Link" ALTER COLUMN "createdById" DROP NOT NULL; + +-- AddForeignKey +ALTER TABLE "Collection" ADD CONSTRAINT "Collection_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Link" ADD CONSTRAINT "Link_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index ee369dd..1341843 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -108,8 +108,8 @@ model Collection { owner User @relation(fields: [ownerId], references: [id]) ownerId Int members UsersAndCollections[] - createdBy User @relation("CreatedCollections", fields: [createdById], references: [id]) - createdById Int + createdBy User? @relation("CreatedCollections", fields: [createdById], references: [id]) + createdById Int? links Link[] createdAt DateTime @default(now()) updatedAt DateTime @default(now()) @updatedAt @@ -138,8 +138,8 @@ model Link { type String @default("url") description String @default("") pinnedBy User[] @relation("PinnedLinks") - createdBy User @relation("CreatedLinks", fields: [createdById], references: [id]) - createdById Int + createdBy User? @relation("CreatedLinks", fields: [createdById], references: [id]) + createdById Int? collection Collection @relation(fields: [collectionId], references: [id]) collectionId Int tags Tag[]