Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

4.4 KB
Raw Blame
import type { Dashboard, Prisma } from '@openpanel/db/src/prisma-client';
import { PrismaError } from 'prisma-error-enum';
import { TRPCNotFoundError } from '../../rpc/errors';
import type { ServiceDeps, Services } from '../../services';
import { getId } from '../../slug-id';
import { getProjectById } from '../project/project.service';

export type IServiceDashboard = Dashboard;
export type IServiceDashboards = Prisma.DashboardGetPayload<{
  include: {
    project: true;
    reports: true;
  };
}>[];

export type DashboardWithProject = Prisma.DashboardGetPayload<{
  include: { project: true };
}>;

export interface DashboardListItem {
  id: string;
  name: string;
  projectId: string;
}

export function getDashboardById(
  deps: ServiceDeps,
  id: string,
  projectId: string
): Promise<DashboardWithProject | null> {
  return deps.db.dashboard.findUnique({
    where: {
      id,
      projectId,
    },
    include: {
      project: true,
    },
  });
}

/** Unscoped lookup for mutation handlers that only receive a dashboard id and
 *  need its `projectId` to run the access check. */
export function getDashboardByIdOrThrow(
  deps: ServiceDeps,
  id: string
): Promise<Dashboard> {
  return deps.db.dashboard.findUniqueOrThrow({ where: { id } });
}

export function getDashboardsByProjectId(
  deps: ServiceDeps,
  projectId: string
): Promise<IServiceDashboards> {
  return deps.db.dashboard.findMany({
    where: {
      projectId,
    },
    include: {
      project: true,
      reports: true,
    },
  });
}

export function listDashboardsCore(
  deps: ServiceDeps,
  input: {
    projectId: string;
    organizationId: string;
  }
): Promise<DashboardListItem[]> {
  return deps.db.dashboard.findMany({
    where: { projectId: input.projectId },
    orderBy: { createdAt: 'desc' },
    select: { id: true, name: true, projectId: true },
  });
}

export async function createDashboard(
  deps: ServiceDeps,
  input: {
    name: string;
    projectId: string;
  }
): Promise<Dashboard> {
  const project = await getProjectById(deps, input.projectId);

  if (!project) {
    throw new TRPCNotFoundError('Project not found');
  }

  return deps.db.dashboard.create({
    data: {
      id: await getId(deps, 'dashboard', input.name),
      projectId: input.projectId,
      organizationId: project.organizationId,
      name: input.name,
    },
  });
}

export function updateDashboard(
  deps: ServiceDeps,
  input: { id: string; name: string }
): Promise<Dashboard> {
  return deps.db.dashboard.update({
    where: {
      id: input.id,
    },
    data: {
      name: input.name,
    },
  });
}

export async function deleteDashboard(
  deps: ServiceDeps,
  input: {
    id: string;
    forceDelete?: boolean;
  }
): Promise<void> {
  try {
    if (input.forceDelete) {
      await deps.db.report.deleteMany({
        where: {
          dashboardId: input.id,
        },
      });
    }
    await deps.db.dashboard.delete({
      where: {
        id: input.id,
      },
    });
  } catch (e) {
    // Below does not work...
    // error instanceof Prisma.PrismaClientKnownRequestError
    if (typeof e === 'object' && e && 'code' in e) {
      const error = e as Prisma.PrismaClientKnownRequestError;
      switch (error.code) {
        case PrismaError.ForeignConstraintViolation:
          throw new Error('Cannot delete dashboard with associated reports');
        default:
          throw new Error('Unknown error deleting dashboard');
      }
    }
  }
}

export function createDashboardService(
  deps: ServiceDeps,
  _services: () => Services
) {
  return {
    getDashboardById: (
      id: string,
      projectId: string
    ): Promise<DashboardWithProject | null> =>
      getDashboardById(deps, id, projectId),
    getDashboardByIdOrThrow: (id: string): Promise<Dashboard> =>
      getDashboardByIdOrThrow(deps, id),
    getDashboardsByProjectId: (
      projectId: string
    ): Promise<IServiceDashboards> => getDashboardsByProjectId(deps, projectId),
    listDashboardsCore: (input: {
      projectId: string;
      organizationId: string;
    }): Promise<DashboardListItem[]> => listDashboardsCore(deps, input),
    createDashboard: (input: {
      name: string;
      projectId: string;
    }): Promise<Dashboard> => createDashboard(deps, input),
    updateDashboard: (input: {
      id: string;
      name: string;
    }): Promise<Dashboard> => updateDashboard(deps, input),
    deleteDashboard: (input: {
      id: string;
      forceDelete?: boolean;
    }): Promise<void> => deleteDashboard(deps, input),
  };
}