Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

1.9 KB
Raw Blame
// import { MEILI_INDEX_VERSION } from "@linkwarden/lib/constants";
import { prisma } from "@linkwarden/prisma";
import { GetUserByIdResponse } from "@linkwarden/types/global";
// import getAccessibleCollectionIds from "@/lib/api/getAccessibleCollectionIds";

export default async function getUserById(userId: number) {
  const [
    user,
    // firstUnIndexedLinks,
    oauthAccountCount,
  ] = await Promise.all([
    prisma.user.findUnique({
      where: {
        id: userId,
      },
      include: {
        subscriptions: true,
        parentSubscription: {
          include: {
            user: true,
          },
        },
        dashboardSections: true,
      },
    }),
    // getAccessibleCollectionIds(userId).then(({ accessibleCollectionIds }) =>
    //   prisma.link.findFirst({
    //     where: {
    //       collectionId: { in: accessibleCollectionIds },
    //       OR: [
    //         { indexVersion: null },
    //         { NOT: { indexVersion: MEILI_INDEX_VERSION } },
    //       ],
    //     },
    //   })
    // ),
    prisma.account.count({
      where: {
        userId,
        type: {
          in: ["oauth", "oidc"],
        },
      },
      select: {
        provider: true,
      },
    }),
  ]);

  if (!user) return { response: "User not found.", status: 404 };

  const { password, subscriptions, parentSubscription, ...lessSensitiveInfo } =
    user;

  const data: GetUserByIdResponse = {
    ...lessSensitiveInfo,
    subscription: {
      active: subscriptions?.active ?? false,
      quantity: subscriptions?.quantity ?? 0,
      provider: subscriptions?.provider,
    },
    parentSubscription: {
      active: parentSubscription?.active,
      user: {
        email: parentSubscription?.user.email,
      },
    },
    hasPassword: !!password,
    hasOAuthAccount: oauthAccountCount.provider > 0,
    // hasUnIndexedLinks: !!firstUnIndexedLinks,
  };

  return { response: data, status: 200 };
}