Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

1.7 KB
Raw Blame
import { prisma } from "@/lib/prisma";
import { FolderType, WorkspaceRole } from "@prisma/client";
import { FOLDERS_MAX_PAGE_SIZE } from "../zod/schemas/folders";

export const getFolders = async ({
  workspaceId,
  userId,
  search,
  type,
  pageSize = FOLDERS_MAX_PAGE_SIZE,
  page = 1,
}: {
  workspaceId: string;
  userId: string;
  search?: string;
  type?: FolderType;
  pageSize?: number;
  page?: number;
}) => {
  const workspaceUser = await prisma.projectUsers.findUnique({
    select: {
      role: true,
    },
    where: {
      userId_projectId: {
        userId,
        projectId: workspaceId,
      },
    },
  });

  return await prisma.folder.findMany({
    where: {
      projectId: workspaceId,
      ...(workspaceUser?.role !== WorkspaceRole.owner
        ? {
            OR: [
              {
                accessLevel: {
                  not: null,
                },
              },
              {
                users: {
                  some: {
                    userId,
                    role: {
                      not: null,
                    },
                  },
                },
              },
            ],
            users: {
              none: {
                userId,
                role: null,
              },
            },
          }
        : {}),
      ...(search && {
        name: {
          contains: search,
        },
      }),
      type,
    },
    select: {
      id: true,
      name: true,
      description: true,
      type: true,
      accessLevel: true,
      createdAt: true,
      updatedAt: true,
    },
    orderBy: {
      createdAt: "asc",
    },
    take: pageSize,
    skip: (page - 1) * pageSize,
  });
};