Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/app/api/people/save/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ export async function POST(request: Request) {
select: { id: true, fullName: true },
});
if (!person) return badRequest("Person not found.");
const { startupId, searchRunId, searchResultId } = body.data;
const [startup, searchRun, searchResult] = await Promise.all([
startupId ? prisma.startupProfile.findFirst({ where: { id: startupId, userId: user.id }, select: { id: true } }) : null,
searchRunId ? prisma.peopleSearchRun.findFirst({ where: { id: searchRunId, userId: user.id }, select: { id: true } }) : null,
searchResultId
? prisma.peopleSearchResult.findFirst({ where: { id: searchResultId, userId: user.id }, select: { id: true } })
: null,
]);
if (startupId && !startup) return badRequest("Startup not found.");
if (searchRunId && !searchRun) return badRequest("Search run not found.");
if (searchResultId && !searchResult) return badRequest("Search result not found.");
const list =
body.data.listId
? await prisma.savedPeopleList.findFirst({ where: { id: body.data.listId, userId: user.id } })
Expand Down
71 changes: 71 additions & 0 deletions tests/people-save.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { beforeEach, describe, expect, it, vi } from "vitest";

const auth = vi.hoisted(() => ({ requireCurrentUser: vi.fn() }));
const db = vi.hoisted(() => ({ prisma: {} as Record<string, unknown> }));

vi.mock("@/lib/auth/current-user", () => auth);
vi.mock("@/lib/prisma", () => db);

const user = { id: "user-1", email: "partner@example.com", name: "Partner", imageUrl: null, role: "partner" };

/** findFirst that only returns rows owned by user-1, like a userId-scoped query would. */
function ownedBy(ids: string[]) {
return vi.fn(async ({ where }: { where: { id: string; userId: string } }) =>
where.userId === "user-1" && ids.includes(where.id) ? { id: where.id } : null,
);
}

const listUpsert = vi.fn();
const savedUpsert = vi.fn();

beforeEach(() => {
auth.requireCurrentUser.mockReset();
auth.requireCurrentUser.mockResolvedValue(user);
listUpsert.mockReset().mockResolvedValue({ id: "list-1" });
savedUpsert.mockReset().mockResolvedValue({ id: "saved-1" });
for (const key of Object.keys(db.prisma)) delete db.prisma[key];
Object.assign(db.prisma, {
discoveredPerson: { findFirst: vi.fn().mockResolvedValue({ id: "person-1", fullName: "Dana Lee" }) },
startupProfile: { findFirst: ownedBy(["startup-1"]) },
peopleSearchRun: { findFirst: ownedBy(["run-1"]) },
peopleSearchResult: { findFirst: ownedBy(["result-1"]) },
savedPeopleList: { upsert: listUpsert, findFirst: vi.fn() },
savedPerson: { upsert: savedUpsert },
auditEvent: { create: vi.fn().mockResolvedValue({}) },
});
});

async function save(body: Record<string, unknown>) {
const { POST } = await import("@/app/api/people/save/route");
return POST(
new Request("http://localhost/api/people/save", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ personId: "person-1", ...body }),
}),
);
}

describe("people save route", () => {
it.each([
[{ startupId: "other-users-startup" }],
[{ searchRunId: "other-users-run" }],
[{ searchResultId: "other-users-result" }],
])("rejects ids the user does not own: %o", async (body) => {
const response = await save(body);

expect(response.status).toBe(400);
expect(listUpsert).not.toHaveBeenCalled();
expect(savedUpsert).not.toHaveBeenCalled();
});

it("saves the person when every linked id is owned", async () => {
const response = await save({ startupId: "startup-1", searchRunId: "run-1", searchResultId: "result-1" });

expect(response.status).toBe(200);
expect(listUpsert).toHaveBeenCalledWith(expect.objectContaining({ create: expect.objectContaining({ startupId: "startup-1" }) }));
expect(savedUpsert).toHaveBeenCalledWith(
expect.objectContaining({ create: expect.objectContaining({ searchRunId: "run-1", searchResultId: "result-1" }) }),
);
});
});
Loading