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

View file

@ -0,0 +1,84 @@
import { z } from 'zod';
import { canUpdateWebsite, canDeleteWebsite, canViewWebsite } from '@/validations';
import { SHARE_ID_REGEX } from '@/lib/constants';
import { parseRequest } from '@/lib/request';
import { ok, json, unauthorized, serverError } from '@/lib/response';
import { deleteWebsite, getWebsite, updateWebsite } from '@/queries';
export async function GET(
request: Request,
{ params }: { params: Promise<{ websiteId: string }> },
) {
const { auth, error } = await parseRequest(request);
if (error) {
return error();
}
const { websiteId } = await params;
if (!(await canViewWebsite(auth, websiteId))) {
return unauthorized();
}
const website = await getWebsite(websiteId);
return json(website);
}
export async function POST(
request: Request,
{ params }: { params: Promise<{ websiteId: string }> },
) {
const schema = z.object({
name: z.string().optional(),
domain: z.string().optional(),
shareId: z.string().regex(SHARE_ID_REGEX).nullable().optional(),
});
const { auth, body, error } = await parseRequest(request, schema);
if (error) {
return error();
}
const { websiteId } = await params;
const { name, domain, shareId } = body;
if (!(await canUpdateWebsite(auth, websiteId))) {
return unauthorized();
}
try {
const website = await updateWebsite(websiteId, { name, domain, shareId });
return Response.json(website);
} catch (e: any) {
if (e.message.includes('Unique constraint') && e.message.includes('share_id')) {
return serverError(new Error('That share ID is already taken.'));
}
return serverError(e);
}
}
export async function DELETE(
request: Request,
{ params }: { params: Promise<{ websiteId: string }> },
) {
const { auth, error } = await parseRequest(request);
if (error) {
return error();
}
const { websiteId } = await params;
if (!(await canDeleteWebsite(auth, websiteId))) {
return unauthorized();
}
await deleteWebsite(websiteId);
return ok();
}

View file

@ -0,0 +1,62 @@
import { z } from 'zod';
import { canCreateTeamWebsite, canCreateWebsite } from '@/validations';
import { json, unauthorized } from '@/lib/response';
import { uuid } from '@/lib/crypto';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { pagingParams, searchParams } from '@/lib/schema';
import { createPixel, getUserLinks } from '@/queries';
export async function GET(request: Request) {
const schema = z.object({
...pagingParams,
...searchParams,
});
const { auth, query, error } = await parseRequest(request, schema);
if (error) {
return error();
}
const filters = await getQueryFilters(query);
const inks = await getUserLinks(auth.user.id, filters);
return json(inks);
}
export async function POST(request: Request) {
const schema = z.object({
name: z.string().max(100),
slug: z.string().max(100),
teamId: z.string().nullable().optional(),
id: z.string().uuid().nullable().optional(),
});
const { auth, body, error } = await parseRequest(request, schema);
if (error) {
return error();
}
const { id, name, slug, teamId } = body;
if ((teamId && !(await canCreateTeamWebsite(auth, teamId))) || !(await canCreateWebsite(auth))) {
return unauthorized();
}
const data: any = {
id: id ?? uuid(),
name,
slug,
teamId,
};
if (!teamId) {
data.userId = auth.user.id;
}
const result = await createPixel(data);
return json(result);
}