Add sharing to pixel and link detail pages

This commit is contained in:
Mike Cao committed 2026-03-09 01:26:02 -07:00
1 parent c4226bc63d
commit 64bf4c378c
23 files changed
+582 -43

No files matched your search

@@ -0,0 +1,73 @@
import { z } from 'zod';
import { ENTITY_TYPE } from '@/lib/constants';
import { uuid } from '@/lib/crypto';
import { getRandomChars } from '@/lib/generate';
import { parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams, pagingParams } from '@/lib/schema';
import { canUpdateLink, canViewLink } from '@/permissions';
import { createShare, getSharesByEntityId } from '@/queries/prisma';
export async function GET(
request: Request,
{ params }: { params: Promise<{ linkId: string }> },
) {
const schema = z.object({
...filterParams,
...pagingParams,
});
const { auth, query, error } = await parseRequest(request, schema);
if (error) {
return error();
}
const { linkId } = await params;
const { page, pageSize, search } = query;
if (!(await canViewLink(auth, linkId))) {
return unauthorized();
}
const data = await getSharesByEntityId(linkId, {
page,
pageSize,
search,
});
return json(data);
}
export async function POST(
request: Request,
{ params }: { params: Promise<{ linkId: string }> },
) {
const schema = z.object({
name: z.string().max(200),
});
const { auth, body, error } = await parseRequest(request, schema);
if (error) {
return error();
}
const { linkId } = await params;
const { name } = body;
if (!(await canUpdateLink(auth, linkId))) {
return unauthorized();
}
const share = await createShare({
id: uuid(),
entityId: linkId,
shareType: ENTITY_TYPE.link,
name,
slug: getRandomChars(16),
parameters: {},
});
return json(share);
}
@@ -0,0 +1,73 @@
import { z } from 'zod';
import { ENTITY_TYPE } from '@/lib/constants';
import { uuid } from '@/lib/crypto';
import { getRandomChars } from '@/lib/generate';
import { parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { filterParams, pagingParams } from '@/lib/schema';
import { canUpdatePixel, canViewPixel } from '@/permissions';
import { createShare, getSharesByEntityId } from '@/queries/prisma';
export async function GET(
request: Request,
{ params }: { params: Promise<{ pixelId: string }> },
) {
const schema = z.object({
...filterParams,
...pagingParams,
});
const { auth, query, error } = await parseRequest(request, schema);
if (error) {
return error();
}
const { pixelId } = await params;
const { page, pageSize, search } = query;
if (!(await canViewPixel(auth, pixelId))) {
return unauthorized();
}
const data = await getSharesByEntityId(pixelId, {
page,
pageSize,
search,
});
return json(data);
}
export async function POST(
request: Request,
{ params }: { params: Promise<{ pixelId: string }> },
) {
const schema = z.object({
name: z.string().max(200),
});
const { auth, body, error } = await parseRequest(request, schema);
if (error) {
return error();
}
const { pixelId } = await params;
const { name } = body;
if (!(await canUpdatePixel(auth, pixelId))) {
return unauthorized();
}
const share = await createShare({
id: uuid(),
entityId: pixelId,
shareType: ENTITY_TYPE.pixel,
name,
slug: getRandomChars(16),
parameters: {},
});
return json(share);
}
+38 -14
View File
@@ -6,17 +6,17 @@ import prisma from '@/lib/prisma';
import redis from '@/lib/redis';
import { json, notFound } from '@/lib/response';
import type { WhiteLabel } from '@/lib/types';
import { getBoard, getShareByCode, getWebsite } from '@/queries/prisma';
import { getBoard, getLink, getPixel, getShareByCode, getWebsite } from '@/queries/prisma';
async function getAccountId(website: { userId?: string; teamId?: string }): Promise<string | null> {
if (website.userId) {
return website.userId;
async function getAccountId(entity: { userId?: string; teamId?: string }): Promise<string | null> {
if (entity.userId) {
return entity.userId;
}
if (website.teamId) {
if (entity.teamId) {
const teamOwner = await prisma.client.teamUser.findFirst({
where: {
teamId: website.teamId,
teamId: entity.teamId,
role: ROLES.teamOwner,
},
select: {
@@ -73,22 +73,46 @@ export async function GET(_request: Request, { params }: { params: Promise<{ slu
return json(data);
}
const website = await getWebsite(share.entityId);
if (!website) {
return notFound();
}
let entity: { userId?: string; teamId?: string } | null = null;
const data: Record<string, any> = {
shareId: share.id,
shareType: share.shareType,
websiteId: share.entityId,
parameters: share.parameters,
};
if (share.shareType === ENTITY_TYPE.website) {
entity = await getWebsite(share.entityId);
if (!entity) {
return notFound();
}
data.websiteId = share.entityId;
} else if (share.shareType === ENTITY_TYPE.pixel) {
entity = await getPixel(share.entityId);
if (!entity) {
return notFound();
}
data.websiteId = share.entityId;
data.pixelId = share.entityId;
} else if (share.shareType === ENTITY_TYPE.link) {
entity = await getLink(share.entityId);
if (!entity) {
return notFound();
}
data.websiteId = share.entityId;
data.linkId = share.entityId;
} else {
return notFound();
}
data.token = createToken(data, secret());
const accountId = await getAccountId(website);
const accountId = await getAccountId(entity);
if (accountId) {
const whiteLabel = await getWhiteLabel(accountId);