Skip to content

Open-source status page, incident response and uptime monitoring

1.9 KB
Raw Blame
import { Events } from "@openstatus/analytics";
import { enabledFeatures } from "@openstatus/services";
import {
  getTrialDaysLeft,
  getWorkspaceUsage,
  updateWorkspaceName,
} from "@openstatus/services/workspace";
import { z } from "zod";

import { toServiceCtx, toTRPCError } from "../service-adapter";
import { createTRPCRouter, protectedProcedure } from "../trpc";
import { checkTrialEligibility } from "./stripe/trial";

export const workspaceRouter = createTRPCRouter({
  // The authed middleware already resolved and parsed this row; re-selecting
  // it would be the same query. Counts live in `usage` — the shell reads
  // `limits` on every route, the counts only on two surfaces.
  // `trialDaysLeft` is derived here, not stored: it depends on "now".
  // `trial` skips Stripe and costs no read for a non-free
  // workspace; the Checkout mutation re-checks with Stripe.
  get: protectedProcedure.query(async ({ ctx }) => ({
    ...ctx.workspace,
    trialDaysLeft: getTrialDaysLeft(ctx.workspace.trialEndsAt),
    trial: await checkTrialEligibility({
      ctx: toServiceCtx(ctx),
      email: ctx.user.email,
      stripe: false,
    }),
    features: enabledFeatures(ctx.workspace),
  })),

  usage: protectedProcedure.query(async ({ ctx }) => {
    try {
      return await getWorkspaceUsage({ ctx: toServiceCtx(ctx) });
    } catch (err) {
      toTRPCError(err);
    }
  }),

  // `resolveActiveWorkspace` already joined the user's workspaces to pick the
  // active one, so this is the same rows the service would re-query.
  list: protectedProcedure.query(({ ctx }) => ctx.workspaces),

  updateName: protectedProcedure
    .meta({ track: Events.UpdateWorkspace })
    .input(z.object({ name: z.string() }))
    .mutation(async ({ ctx, input }) => {
      try {
        await updateWorkspaceName({
          ctx: toServiceCtx(ctx),
          input: { name: input.name },
        });
      } catch (err) {
        toTRPCError(err);
      }
    }),
});