Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

2.3 KB
Raw Blame
import { z } from 'zod';
import { uuid } from '@/lib/crypto';
import { getRandomChars } from '@/lib/generate';
import { fetchAccount } from '@/lib/load';
import redis from '@/lib/redis';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { pagingParams, sortingParams } from '@/lib/schema';
import { getCloudTeamLimit } from '@/lib/subscription';
import { canCreateTeam } from '@/permissions';
import { createTeam, getUserOwnedTeamCount, getUserTeams } from '@/queries/prisma';

export async function GET(request: Request) {
  const schema = z.object({
    ...pagingParams,
    ...sortingParams,
  });

  const { auth, query, error } = await parseRequest(request, schema);

  if (error) {
    return error();
  }

  const filters = await getQueryFilters(query);

  const teams = await getUserTeams(auth.user.id, filters);

  return json(teams);
}

export async function POST(request: Request) {
  const schema = z.object({
    name: z.string().max(50),
    ownerId: z.uuid().optional(),
  });

  const { auth, body, error } = await parseRequest(request, schema);

  if (error) {
    return error();
  }

  if (!(await canCreateTeam(auth))) {
    return unauthorized();
  }

  const { name, ownerId } = body;

  const teamId = uuid();
  const teamOwnerId = ownerId && auth.user.isAdmin ? ownerId : auth.user.id;

  let account = null;

  if (process.env.CLOUD_MODE) {
    account = await fetchAccount(teamOwnerId);

    const teamLimit = getCloudTeamLimit(account);

    if (teamLimit !== null) {
      const count = await getUserOwnedTeamCount(teamOwnerId);

      if (count >= teamLimit) {
        return unauthorized({ message: 'Team limit reached.' });
      }
    }
  }

  const team = await createTeam(
    {
      id: teamId,
      name,
      accessCode: `team_${getRandomChars(16)}`,
    },
    teamOwnerId,
  );

  if (process.env.CLOUD_MODE && redis.enabled && account) {
    await redis.client.set(
      `team:${teamId}`,
      {
        teamOwnerId,
        isPro: account.isPro || false,
        isBusiness: account.isBusiness || false,
        isNoBilling: account.isNoBilling || false,
        hasSubscription: account.hasSubscription || false,
        unlimitedWebsites: account.unlimitedWebsites || false,
      },
      60 * 60 * 24 * 90,
    );
  }

  return json(team);
}