aboutsummaryrefslogtreecommitdiff
path: root/src/permissions/pixel.ts
blob: 2131874f1d4cb48c0cbaacf48b93c8f856cdb956 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { hasPermission } from '@/lib/auth';
import { PERMISSIONS } from '@/lib/constants';
import type { Auth } from '@/lib/types';
import { getPixel, getTeamUser } from '@/queries/prisma';

export async function canViewPixel({ user }: Auth, pixelId: string) {
  if (user?.isAdmin) {
    return true;
  }

  const pixel = await getPixel(pixelId);

  if (pixel.userId) {
    return user.id === pixel.userId;
  }

  if (pixel.teamId) {
    const teamUser = await getTeamUser(pixel.teamId, user.id);

    return !!teamUser;
  }

  return false;
}

export async function canUpdatePixel({ user }: Auth, pixelId: string) {
  if (user.isAdmin) {
    return true;
  }

  const pixel = await getPixel(pixelId);

  if (pixel.userId) {
    return user.id === pixel.userId;
  }

  if (pixel.teamId) {
    const teamUser = await getTeamUser(pixel.teamId, user.id);

    return teamUser && hasPermission(teamUser.role, PERMISSIONS.websiteUpdate);
  }

  return false;
}

export async function canDeletePixel({ user }: Auth, pixelId: string) {
  if (user.isAdmin) {
    return true;
  }

  const pixel = await getPixel(pixelId);

  if (pixel.userId) {
    return user.id === pixel.userId;
  }

  if (pixel.teamId) {
    const teamUser = await getTeamUser(pixel.teamId, user.id);

    return teamUser && hasPermission(teamUser.role, PERMISSIONS.websiteDelete);
  }

  return false;
}