Skip to content

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

6.5 KB
Raw Blame
import { getSubscriptionState } from '@openpanel/payments/subscription-state';
import { PrismaClient } from './generated/prisma/client';
import { logger } from './logger';

export * from './generated/prisma/client';

const subscriptionStateNeeds = {
  subscriptionStatus: true,
  subscriptionCanceledAt: true,
  subscriptionEndsAt: true,
  subscriptionPauseAtPeriodEnd: true,
} as const;

const getPrismaClient = () => {
  // emit: 'event' keeps the engine from writing prisma:error lines straight
  // to stderr, so they flow through pino to the OTLP pipeline instead.
  const client = new PrismaClient({
    // Prisma's default error format splices file, line and a source snippet
    // into messages that reach API clients verbatim. `minimal` keeps our
    // source layout out of a 500.
    errorFormat: 'minimal',
    log: [
      { emit: 'event', level: 'error' },
      { emit: 'event', level: 'warn' },
    ],
  });

  // $on only exists on the base client — keep it before $extends.
  client.$on('error', (event) => {
    logger.error({ target: event.target }, `prisma: ${event.message}`);
  });
  client.$on('warn', (event) => {
    logger.warn({ target: event.target }, `prisma: ${event.message}`);
  });

  const prisma = client.$extends({
    result: {
      organization: {
        subscriptionState: {
          needs: subscriptionStateNeeds,
          compute(org) {
            return getSubscriptionState(org);
          },
        },
        subscriptionStatus: {
          needs: { subscriptionStatus: true, subscriptionCanceledAt: true },
          compute(org) {
            if (process.env.SELF_HOSTED === 'true') {
              return 'active';
            }

            return org.subscriptionStatus || 'trialing';
          },
        },
        hasSubscription: {
          needs: subscriptionStateNeeds,
          compute(org) {
            const state = getSubscriptionState(org);
            return (
              state === 'active' ||
              state === 'canceling' ||
              state === 'pausing' ||
              state === 'paused' ||
              state === 'past_due' ||
              state === 'unpaid' ||
              state === 'incomplete'
            );
          },
        },
        slug: {
          needs: { id: true },
          compute(org) {
            return org.id;
          },
        },
        subscriptionChartEndDate: {
          needs: {
            subscriptionEndsAt: true,
            subscriptionPeriodEventsCountExceededAt: true,
          },
          compute(org) {
            if (process.env.SELF_HOSTED === 'true') {
              return null;
            }

            if (
              org.subscriptionEndsAt &&
              org.subscriptionPeriodEventsCountExceededAt
            ) {
              return org.subscriptionEndsAt >
                org.subscriptionPeriodEventsCountExceededAt
                ? org.subscriptionPeriodEventsCountExceededAt
                : org.subscriptionEndsAt;
            }

            if (org.subscriptionEndsAt) {
              return org.subscriptionEndsAt;
            }

            return new Date(Date.now() + 1000 * 60 * 60 * 24);
          },
        },
        isActive: {
          needs: subscriptionStateNeeds,
          compute(org) {
            const state = getSubscriptionState(org);
            return state === 'active' || state === 'self_hosted';
          },
        },
        isTrial: {
          needs: subscriptionStateNeeds,
          compute(org) {
            return getSubscriptionState(org) === 'trialing';
          },
        },
        isCanceled: {
          needs: subscriptionStateNeeds,
          compute(org) {
            return getSubscriptionState(org) === 'canceled';
          },
        },
        isWillBeCanceled: {
          needs: subscriptionStateNeeds,
          compute(org) {
            return getSubscriptionState(org) === 'canceling';
          },
        },
        isExpired: {
          needs: subscriptionStateNeeds,
          compute(org) {
            const state = getSubscriptionState(org);
            return state === 'expired' || state === 'trial_expired';
          },
        },
        isExceeded: {
          needs: {
            subscriptionPeriodEventsCount: true,
            subscriptionPeriodEventsLimit: true,
          },
          compute(org) {
            if (process.env.SELF_HOSTED === 'true') {
              return false;
            }

            return (
              org.subscriptionPeriodEventsCount >
              org.subscriptionPeriodEventsLimit
            );
          },
        },
        subscriptionCurrentPeriodStart: {
          needs: { subscriptionStartsAt: true, subscriptionInterval: true },
          compute(org) {
            if (process.env.SELF_HOSTED === 'true') {
              return null;
            }

            if (!org.subscriptionStartsAt) {
              return null;
            }

            if (org.subscriptionInterval === 'year') {
              const startDay = org.subscriptionStartsAt.getUTCDate();
              const now = new Date();
              return new Date(
                Date.UTC(
                  now.getUTCFullYear(),
                  now.getUTCMonth(),
                  startDay,
                  0,
                  0,
                  0,
                  0
                )
              );
            }

            return org.subscriptionStartsAt;
          },
        },
        subscriptionCurrentPeriodEnd: {
          needs: {
            subscriptionStartsAt: true,
            subscriptionEndsAt: true,
            subscriptionInterval: true,
          },
          compute(org) {
            if (process.env.SELF_HOSTED === 'true') {
              return null;
            }

            if (!org.subscriptionStartsAt) {
              return null;
            }

            if (org.subscriptionInterval === 'year') {
              const startDay = org.subscriptionStartsAt.getUTCDate();
              const now = new Date();
              return new Date(
                Date.UTC(
                  now.getUTCFullYear(),
                  now.getUTCMonth() + 1,
                  startDay - 1,
                  0,
                  0,
                  0,
                  0
                )
              );
            }

            return org.subscriptionEndsAt;
          },
        },
      },
    },
  });

  return prisma;
};

const globalForPrisma = globalThis as unknown as {
  prisma: ReturnType<typeof getPrismaClient>;
};

export const db = globalForPrisma.prisma ?? getPrismaClient();

if (process.env.NODE_ENV !== 'production') {
  globalForPrisma.prisma = db;
}