mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 11:07:40 +01:00
refactor(api): dedupe byte-identical interactions routes + close ownership gap
Last turn I claimed "interactions are already shared" while looking at this lane — wrong. /api/people/[id]/interactions and /api/projects/[id]/interactions were byte-identical (`diff` returned no output) and neither verified that the entity belongs to the user before writing. The unverified write was a small hole — for a single-user app today it doesn't matter, but it diverged from the attrs routes that I just standardised on the verify-then-write pattern. Extracted createEntityInteraction() into db/queries/utils.ts: it checks entity ownership, inserts, and returns the row (or null → caller 404s). Both routes now ~25 lines of HTTP shell, matching the attrs-route shape. Net: byte-identical files collapsed into one helper + two thin shells, and a write-without-ownership-check gap closed. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
3 files changed
+33
-35
No files matched your search
@@ -1,8 +1,6 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { db } from "@/db";
|
||||
import { interactions } from "@/db/schema";
|
||||
import { DEFAULT_USER_ID } from "@/lib/constants";
|
||||
import { readIdParam, readJsonBody, z } from "@/lib/api/route-helpers";
|
||||
import { createEntityInteraction } from "@/db/queries/utils";
|
||||
|
||||
const CreateInteractionBody = z.object({
|
||||
channel: z.string().trim().min(1, "channel is required"),
|
||||
@@ -17,23 +15,11 @@ export async function POST(
|
||||
) {
|
||||
const idOrResp = await readIdParam(params);
|
||||
if (idOrResp instanceof NextResponse) return idOrResp;
|
||||
const id = idOrResp;
|
||||
|
||||
const dataOrResp = await readJsonBody(req, CreateInteractionBody);
|
||||
if (dataOrResp instanceof NextResponse) return dataOrResp;
|
||||
const { channel, direction, summary, occurredAt } = dataOrResp;
|
||||
|
||||
const [created] = await db
|
||||
.insert(interactions)
|
||||
.values({
|
||||
userId: DEFAULT_USER_ID,
|
||||
entityId: id,
|
||||
channel,
|
||||
direction,
|
||||
summary: summary || null,
|
||||
occurredAt: occurredAt ? new Date(occurredAt) : new Date(),
|
||||
})
|
||||
.returning();
|
||||
|
||||
const created = await createEntityInteraction(idOrResp, dataOrResp);
|
||||
if (!created) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
return NextResponse.json({ ok: true, interaction: created }, { status: 201 });
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { db } from "@/db";
|
||||
import { interactions } from "@/db/schema";
|
||||
import { DEFAULT_USER_ID } from "@/lib/constants";
|
||||
import { readIdParam, readJsonBody, z } from "@/lib/api/route-helpers";
|
||||
import { createEntityInteraction } from "@/db/queries/utils";
|
||||
|
||||
const CreateInteractionBody = z.object({
|
||||
channel: z.string().trim().min(1, "channel is required"),
|
||||
@@ -17,23 +15,11 @@ export async function POST(
|
||||
) {
|
||||
const idOrResp = await readIdParam(params);
|
||||
if (idOrResp instanceof NextResponse) return idOrResp;
|
||||
const id = idOrResp;
|
||||
|
||||
const dataOrResp = await readJsonBody(req, CreateInteractionBody);
|
||||
if (dataOrResp instanceof NextResponse) return dataOrResp;
|
||||
const { channel, direction, summary, occurredAt } = dataOrResp;
|
||||
|
||||
const [created] = await db
|
||||
.insert(interactions)
|
||||
.values({
|
||||
userId: DEFAULT_USER_ID,
|
||||
entityId: id,
|
||||
channel,
|
||||
direction,
|
||||
summary: summary || null,
|
||||
occurredAt: occurredAt ? new Date(occurredAt) : new Date(),
|
||||
})
|
||||
.returning();
|
||||
|
||||
const created = await createEntityInteraction(idOrResp, dataOrResp);
|
||||
if (!created) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
return NextResponse.json({ ok: true, interaction: created }, { status: 201 });
|
||||
}
|
||||
+27
-1
@@ -1,5 +1,5 @@
|
||||
import { db } from "@/db";
|
||||
import { attributes, entities } from "@/db/schema";
|
||||
import { attributes, entities, interactions, type Interaction } from "@/db/schema";
|
||||
import { and, eq, sql } from "drizzle-orm";
|
||||
import { DEFAULT_USER_ID } from "@/lib/constants";
|
||||
|
||||
@@ -68,3 +68,29 @@ export async function deleteEntityAttribute(entityId: string, key: string): Prom
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/** Verifies the entity belongs to the user before recording an interaction.
|
||||
* Returns the created row, or null if the entity wasn't found (caller should 404). */
|
||||
export async function createEntityInteraction(
|
||||
entityId: string,
|
||||
body: { channel: string; direction: "inbound" | "outbound"; summary?: string; occurredAt?: string },
|
||||
): Promise<Interaction | null> {
|
||||
const [owner] = await db
|
||||
.select({ id: entities.id })
|
||||
.from(entities)
|
||||
.where(and(eq(entities.id, entityId), eq(entities.userId, DEFAULT_USER_ID)));
|
||||
if (!owner) return null;
|
||||
|
||||
const [created] = await db
|
||||
.insert(interactions)
|
||||
.values({
|
||||
userId: DEFAULT_USER_ID,
|
||||
entityId,
|
||||
channel: body.channel,
|
||||
direction: body.direction,
|
||||
summary: body.summary || null,
|
||||
occurredAt: body.occurredAt ? new Date(body.occurredAt) : new Date(),
|
||||
})
|
||||
.returning();
|
||||
return created;
|
||||
}
|
||||
Reference in new issue
Block a user