Pixel/links development. New validations folder. More refactoring.

This commit is contained in:
Mike Cao 2025-08-14 23:48:11 -07:00
parent 88639dfe83
commit 247e14646b
136 changed files with 1395 additions and 516 deletions

64
src/validations/pixel.ts Normal file
View file

@ -0,0 +1,64 @@
import { Auth } from '@/lib/types';
import { getPixel, getTeamUser } from '@/queries';
import { hasPermission } from '@/lib/auth';
import { PERMISSIONS } from '@/lib/constants';
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;
}