Add website annotations with chart markers and CRUD management
This commit is contained in:
1 parent
eea124bbf8
commit
64e4cc1c3b
24 files changed
+957
-3
No files matched your search
@@ -0,0 +1,85 @@
|
||||
import { parseRequest } from '@/lib/request';
|
||||
import { json, notFound, ok, unauthorized } from '@/lib/response';
|
||||
import { annotationSchema } from '@/lib/schema';
|
||||
import { canUpdateWebsite, canViewSharedWebsiteFilters } from '@/permissions';
|
||||
import { deleteAnnotation, getWebsiteAnnotation, updateAnnotation } from '@/queries/prisma';
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ websiteId: string; annotationId: string }> },
|
||||
) {
|
||||
const { auth, error } = await parseRequest(request);
|
||||
|
||||
if (error) {
|
||||
return error();
|
||||
}
|
||||
|
||||
const { websiteId, annotationId } = await params;
|
||||
|
||||
if (!(await canViewSharedWebsiteFilters(auth, websiteId))) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
const annotation = await getWebsiteAnnotation(websiteId, annotationId);
|
||||
|
||||
if (!annotation) {
|
||||
return notFound();
|
||||
}
|
||||
|
||||
return json(annotation);
|
||||
}
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ websiteId: string; annotationId: string }> },
|
||||
) {
|
||||
const { auth, body, error } = await parseRequest(request, annotationSchema);
|
||||
|
||||
if (error) {
|
||||
return error();
|
||||
}
|
||||
|
||||
const { websiteId, annotationId } = await params;
|
||||
const { date, allDay, note } = body;
|
||||
|
||||
if (!(await canUpdateWebsite(auth, websiteId))) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
const annotation = await getWebsiteAnnotation(websiteId, annotationId);
|
||||
|
||||
if (!annotation) {
|
||||
return notFound();
|
||||
}
|
||||
|
||||
const result = await updateAnnotation(annotationId, { date, allDay, note });
|
||||
|
||||
return json(result);
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ websiteId: string; annotationId: string }> },
|
||||
) {
|
||||
const { auth, error } = await parseRequest(request);
|
||||
|
||||
if (error) {
|
||||
return error();
|
||||
}
|
||||
|
||||
const { websiteId, annotationId } = await params;
|
||||
|
||||
if (!(await canUpdateWebsite(auth, websiteId))) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
const annotation = await getWebsiteAnnotation(websiteId, annotationId);
|
||||
|
||||
if (!annotation) {
|
||||
return notFound();
|
||||
}
|
||||
|
||||
await deleteAnnotation(annotationId);
|
||||
|
||||
return ok();
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
import { beforeEach, expect, test, vi } from 'vitest';
|
||||
import { parseRequest } from '@/lib/request';
|
||||
import { canUpdateWebsite, canViewSharedWebsiteFilters } from '@/permissions';
|
||||
import { createAnnotation, getWebsiteAnnotations } from '@/queries/prisma';
|
||||
import { GET, POST } from './route';
|
||||
|
||||
vi.mock('@/lib/request', () => ({
|
||||
parseRequest: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/permissions', () => ({
|
||||
canUpdateWebsite: vi.fn(),
|
||||
canViewSharedWebsiteFilters: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/queries/prisma', () => ({
|
||||
createAnnotation: vi.fn(),
|
||||
getWebsiteAnnotations: vi.fn(),
|
||||
}));
|
||||
|
||||
const parseRequestMock = vi.mocked(parseRequest);
|
||||
const canUpdateWebsiteMock = vi.mocked(canUpdateWebsite);
|
||||
const canViewSharedWebsiteFiltersMock = vi.mocked(canViewSharedWebsiteFilters);
|
||||
const createAnnotationMock = vi.mocked(createAnnotation);
|
||||
const getWebsiteAnnotationsMock = vi.mocked(getWebsiteAnnotations);
|
||||
|
||||
const params = Promise.resolve({ websiteId: 'website-1' });
|
||||
|
||||
beforeEach(() => {
|
||||
parseRequestMock.mockReset();
|
||||
canUpdateWebsiteMock.mockReset();
|
||||
canViewSharedWebsiteFiltersMock.mockReset();
|
||||
createAnnotationMock.mockReset();
|
||||
getWebsiteAnnotationsMock.mockReset();
|
||||
});
|
||||
|
||||
test('POST validates the note and stores the annotation with the creating user', async () => {
|
||||
const date = new Date('2026-08-01T00:00:00.000Z');
|
||||
|
||||
parseRequestMock.mockResolvedValue({
|
||||
auth: { user: { id: 'user-1' } },
|
||||
body: { date, allDay: false, note: 'Deployed v2' },
|
||||
error: undefined,
|
||||
} as any);
|
||||
canUpdateWebsiteMock.mockResolvedValue(true);
|
||||
createAnnotationMock.mockResolvedValue({ id: 'annotation-1' } as any);
|
||||
|
||||
const response = await POST(
|
||||
new Request('http://localhost/api/websites/website-1/annotations', { method: 'POST' }),
|
||||
{ params },
|
||||
);
|
||||
const schema = parseRequestMock.mock.calls[0][1] as {
|
||||
safeParse: (value: unknown) => { success: boolean; data?: any };
|
||||
};
|
||||
|
||||
expect(schema.safeParse({ date: '2026-08-01', note: '' }).success).toBe(false);
|
||||
expect(schema.safeParse({ date: '2026-08-01', note: 'x'.repeat(501) }).success).toBe(false);
|
||||
expect(schema.safeParse({ date: 'not-a-date', note: 'ok' }).success).toBe(false);
|
||||
|
||||
const parsed = schema.safeParse({ date: '2026-08-01', note: 'ok' });
|
||||
expect(parsed.success).toBe(true);
|
||||
expect(parsed.data.allDay).toBe(true);
|
||||
|
||||
expect(createAnnotationMock).toHaveBeenCalledWith({
|
||||
id: expect.any(String),
|
||||
websiteId: 'website-1',
|
||||
userId: 'user-1',
|
||||
date,
|
||||
allDay: false,
|
||||
note: 'Deployed v2',
|
||||
});
|
||||
expect(response.status).toBe(200);
|
||||
});
|
||||
|
||||
test('POST rejects users without update permission', async () => {
|
||||
parseRequestMock.mockResolvedValue({
|
||||
auth: { user: { id: 'user-1' } },
|
||||
body: { date: new Date(), allDay: true, note: 'nope' },
|
||||
error: undefined,
|
||||
} as any);
|
||||
canUpdateWebsiteMock.mockResolvedValue(false);
|
||||
|
||||
const response = await POST(
|
||||
new Request('http://localhost/api/websites/website-1/annotations', { method: 'POST' }),
|
||||
{ params },
|
||||
);
|
||||
|
||||
expect(createAnnotationMock).not.toHaveBeenCalled();
|
||||
expect(response.status).toBe(401);
|
||||
});
|
||||
|
||||
test('GET passes the date window through to the query only when both bounds are present', async () => {
|
||||
parseRequestMock.mockResolvedValue({
|
||||
auth: { user: { id: 'user-1' } },
|
||||
query: { startAt: 1000, endAt: 2000, search: 'deploy', page: 2, pageSize: 20 },
|
||||
error: undefined,
|
||||
} as any);
|
||||
canViewSharedWebsiteFiltersMock.mockResolvedValue(true);
|
||||
getWebsiteAnnotationsMock.mockResolvedValue({ data: [], count: 0 } as any);
|
||||
|
||||
await GET(new Request('http://localhost/api/websites/website-1/annotations'), { params });
|
||||
|
||||
expect(getWebsiteAnnotationsMock).toHaveBeenCalledWith('website-1', {
|
||||
search: 'deploy',
|
||||
page: 2,
|
||||
pageSize: 20,
|
||||
startDate: new Date(1000),
|
||||
endDate: new Date(2000),
|
||||
});
|
||||
|
||||
parseRequestMock.mockResolvedValue({
|
||||
auth: { user: { id: 'user-1' } },
|
||||
query: { page: 1 },
|
||||
error: undefined,
|
||||
} as any);
|
||||
|
||||
await GET(new Request('http://localhost/api/websites/website-1/annotations'), { params });
|
||||
|
||||
expect(getWebsiteAnnotationsMock).toHaveBeenLastCalledWith('website-1', {
|
||||
search: undefined,
|
||||
page: 1,
|
||||
pageSize: undefined,
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,70 @@
|
||||
import { z } from 'zod';
|
||||
import { uuid } from '@/lib/crypto';
|
||||
import { parseRequest } from '@/lib/request';
|
||||
import { json, unauthorized } from '@/lib/response';
|
||||
import { annotationSchema, pagingParams, searchParams } from '@/lib/schema';
|
||||
import { canUpdateWebsite, canViewSharedWebsiteFilters } from '@/permissions';
|
||||
import { createAnnotation, getWebsiteAnnotations } from '@/queries/prisma';
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ websiteId: string }> },
|
||||
) {
|
||||
const schema = z.object({
|
||||
startAt: z.coerce.number().optional(),
|
||||
endAt: z.coerce.number().optional(),
|
||||
...searchParams,
|
||||
...pagingParams,
|
||||
});
|
||||
|
||||
const { auth, query, error } = await parseRequest(request, schema);
|
||||
|
||||
if (error) {
|
||||
return error();
|
||||
}
|
||||
|
||||
const { websiteId } = await params;
|
||||
const { startAt, endAt, search, page, pageSize } = query;
|
||||
|
||||
if (!(await canViewSharedWebsiteFilters(auth, websiteId))) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
const annotations = await getWebsiteAnnotations(websiteId, {
|
||||
search,
|
||||
page,
|
||||
pageSize,
|
||||
...(startAt && endAt && { startDate: new Date(startAt), endDate: new Date(endAt) }),
|
||||
});
|
||||
|
||||
return json(annotations);
|
||||
}
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ websiteId: string }> },
|
||||
) {
|
||||
const { auth, body, error } = await parseRequest(request, annotationSchema);
|
||||
|
||||
if (error) {
|
||||
return error();
|
||||
}
|
||||
|
||||
const { websiteId } = await params;
|
||||
const { date, allDay, note } = body;
|
||||
|
||||
if (!(await canUpdateWebsite(auth, websiteId))) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
const result = await createAnnotation({
|
||||
id: uuid(),
|
||||
websiteId,
|
||||
userId: auth.user.id,
|
||||
date,
|
||||
allDay,
|
||||
note,
|
||||
});
|
||||
|
||||
return json(result);
|
||||
}
|
||||
Reference in new issue
Block a user