2026-03-10 08:17:45 -04:00
|
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
|
import { prisma } from "@/lib/db";
|
2026-04-16 00:51:44 -04:00
|
|
|
import { deleteObject } from "@/lib/storage";
|
2026-04-07 13:23:29 -04:00
|
|
|
import { fireIntegrationEvent } from "@/lib/integrations";
|
|
|
|
|
import { logActivity, diffCardFields } from "@/lib/activity-log";
|
2026-04-11 01:00:11 -04:00
|
|
|
import { getOrCreateUser, RoleError } from "@/lib/auth";
|
2026-03-10 08:17:45 -04:00
|
|
|
|
|
|
|
|
export async function GET(
|
|
|
|
|
_request: NextRequest,
|
|
|
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
|
|
|
) {
|
|
|
|
|
try {
|
|
|
|
|
const { id } = await params;
|
|
|
|
|
const card = await prisma.responseCard.findUnique({
|
|
|
|
|
where: { id },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!card) {
|
|
|
|
|
return NextResponse.json({ error: "Card not found" }, { status: 404 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 13:38:08 -04:00
|
|
|
const frontImageUrl = card.frontImagePath
|
|
|
|
|
? `/api/images/${card.frontImagePath}`
|
|
|
|
|
: null;
|
|
|
|
|
const backImageUrl = card.backImagePath
|
|
|
|
|
? `/api/images/${card.backImagePath}`
|
|
|
|
|
: null;
|
2026-03-10 08:17:45 -04:00
|
|
|
|
|
|
|
|
return NextResponse.json({
|
|
|
|
|
...card,
|
|
|
|
|
frontImageUrl,
|
|
|
|
|
backImageUrl,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("[cards/[id] GET]", error);
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Failed to fetch card" },
|
|
|
|
|
{ status: 500 }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function PUT(
|
|
|
|
|
request: NextRequest,
|
|
|
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
|
|
|
) {
|
|
|
|
|
try {
|
2026-04-11 01:00:11 -04:00
|
|
|
const user = await getOrCreateUser(request.headers);
|
|
|
|
|
|
2026-03-10 08:17:45 -04:00
|
|
|
const { id } = await params;
|
|
|
|
|
const card = await prisma.responseCard.findUnique({
|
|
|
|
|
where: { id },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!card) {
|
|
|
|
|
return NextResponse.json({ error: "Card not found" }, { status: 404 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 01:00:11 -04:00
|
|
|
if (user) {
|
|
|
|
|
if (user.role === "viewer") {
|
|
|
|
|
return NextResponse.json({ error: "Viewers cannot edit cards" }, { status: 403 });
|
|
|
|
|
}
|
|
|
|
|
if (user.role === "reviewer" && card.assignedToId !== user.id) {
|
|
|
|
|
return NextResponse.json({ error: "You can only edit cards assigned to you" }, { status: 403 });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-10 08:17:45 -04:00
|
|
|
const body = await request.json().catch(() => ({}));
|
|
|
|
|
const data: Record<string, unknown> = {};
|
|
|
|
|
|
|
|
|
|
const stringFields = [
|
|
|
|
|
"name",
|
|
|
|
|
"gender",
|
|
|
|
|
"dateOfBirth",
|
|
|
|
|
"maritalStatus",
|
|
|
|
|
"maritalStatusOther",
|
|
|
|
|
"visitType",
|
|
|
|
|
"cellPhone",
|
|
|
|
|
"homePhone",
|
|
|
|
|
"email",
|
|
|
|
|
"address",
|
|
|
|
|
"aptNumber",
|
|
|
|
|
"city",
|
|
|
|
|
"state",
|
|
|
|
|
"zip",
|
|
|
|
|
"prayerRequests",
|
|
|
|
|
"messageTopicsOther",
|
|
|
|
|
"attendanceDuration",
|
|
|
|
|
"campusPreferenceOther",
|
|
|
|
|
"howHeardOther",
|
|
|
|
|
"serviceAttended",
|
2026-04-07 13:50:03 -04:00
|
|
|
"followUp",
|
|
|
|
|
"notes",
|
|
|
|
|
"serviceTime",
|
|
|
|
|
"planningCenter",
|
2026-03-10 08:17:45 -04:00
|
|
|
"ocrStatus",
|
|
|
|
|
"reviewStatus",
|
|
|
|
|
"ocrError",
|
2026-04-11 01:00:11 -04:00
|
|
|
"reviewNotes",
|
2026-03-10 08:17:45 -04:00
|
|
|
];
|
|
|
|
|
for (const field of stringFields) {
|
|
|
|
|
if (body[field] != null) data[field] = String(body[field]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (body.prayerForTeam != null) data.prayerForTeam = Boolean(body.prayerForTeam);
|
|
|
|
|
if (body.prayerConfidential != null) data.prayerConfidential = Boolean(body.prayerConfidential);
|
2026-04-07 13:50:03 -04:00
|
|
|
if (body.iSaidYesBookSent != null) data.iSaidYesBookSent = Boolean(body.iSaidYesBookSent);
|
|
|
|
|
if (body.ftGuestLetterSent != null) data.ftGuestLetterSent = Boolean(body.ftGuestLetterSent);
|
2026-04-09 18:20:24 -04:00
|
|
|
|
2026-04-11 01:00:11 -04:00
|
|
|
for (const dateField of ["firstTimeGuestDate", "salvationDate", "assignedAt", "reviewedAt"] as const) {
|
2026-04-09 18:20:24 -04:00
|
|
|
if (body[dateField] !== undefined) {
|
|
|
|
|
data[dateField] = body[dateField] ? new Date(body[dateField]) : null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-10 08:17:45 -04:00
|
|
|
if (body.ocrConfidence != null) data.ocrConfidence = Number(body.ocrConfidence);
|
|
|
|
|
if (body.messageTopics != null) data.messageTopics = body.messageTopics;
|
|
|
|
|
if (body.nextStep != null) data.nextStep = body.nextStep;
|
|
|
|
|
if (body.campusPreference != null) data.campusPreference = body.campusPreference;
|
|
|
|
|
if (body.howHeard != null) data.howHeard = body.howHeard;
|
|
|
|
|
if (body.rawOcrResponse != null) data.rawOcrResponse = body.rawOcrResponse;
|
|
|
|
|
|
2026-04-11 01:00:11 -04:00
|
|
|
for (const assignField of ["assignedToId", "assignedById", "reviewedById"] as const) {
|
|
|
|
|
if (body[assignField] !== undefined) {
|
|
|
|
|
data[assignField] = body[assignField] || null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (body.reviewStatus === "in_review" && card.reviewStatus === "assigned") {
|
|
|
|
|
data.reviewStatus = "in_review";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (body.reviewStatus === "reviewed" && user) {
|
|
|
|
|
data.reviewedById = user.id;
|
|
|
|
|
data.reviewedAt = new Date();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 13:23:29 -04:00
|
|
|
const oldCard = card as unknown as Record<string, unknown>;
|
|
|
|
|
|
2026-03-10 08:17:45 -04:00
|
|
|
const updated = await prisma.responseCard.update({
|
|
|
|
|
where: { id },
|
|
|
|
|
data: data as Parameters<typeof prisma.responseCard.update>[0]["data"],
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-07 13:23:29 -04:00
|
|
|
const newCard = updated as unknown as Record<string, unknown>;
|
|
|
|
|
const changes = diffCardFields(oldCard, newCard);
|
|
|
|
|
if (changes.length > 0) {
|
2026-04-11 01:00:11 -04:00
|
|
|
logActivity(
|
|
|
|
|
id,
|
|
|
|
|
"manual_edit",
|
|
|
|
|
"user",
|
|
|
|
|
`${changes.length} field(s) updated manually`,
|
|
|
|
|
changes,
|
|
|
|
|
user?.id
|
|
|
|
|
).catch(() => {});
|
2026-04-07 13:23:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const oldStatus = card.reviewStatus;
|
|
|
|
|
const newStatus = updated.reviewStatus;
|
|
|
|
|
if (oldStatus !== newStatus) {
|
|
|
|
|
if (newStatus === "reviewed") {
|
|
|
|
|
fireIntegrationEvent("card_reviewed", id, { oldCard }).catch(() => {});
|
|
|
|
|
} else if (newStatus === "exported") {
|
|
|
|
|
fireIntegrationEvent("card_exported", id, { oldCard }).catch(() => {});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-10 08:17:45 -04:00
|
|
|
return NextResponse.json(updated);
|
|
|
|
|
} catch (error) {
|
2026-04-11 01:00:11 -04:00
|
|
|
if (error instanceof RoleError) {
|
|
|
|
|
return NextResponse.json({ error: error.message }, { status: 403 });
|
|
|
|
|
}
|
2026-03-10 08:17:45 -04:00
|
|
|
console.error("[cards/[id] PUT]", error);
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Failed to update card" },
|
|
|
|
|
{ status: 500 }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function DELETE(
|
2026-04-11 01:00:11 -04:00
|
|
|
request: NextRequest,
|
2026-03-10 08:17:45 -04:00
|
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
|
|
|
) {
|
|
|
|
|
try {
|
2026-04-11 01:00:11 -04:00
|
|
|
const user = await getOrCreateUser(request.headers);
|
|
|
|
|
if (user && user.role !== "admin") {
|
|
|
|
|
return NextResponse.json({ error: "Only admins can delete cards" }, { status: 403 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-10 08:17:45 -04:00
|
|
|
const { id } = await params;
|
|
|
|
|
const card = await prisma.responseCard.findUnique({
|
|
|
|
|
where: { id },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!card) {
|
|
|
|
|
return NextResponse.json({ error: "Card not found" }, { status: 404 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 13:23:29 -04:00
|
|
|
fireIntegrationEvent("card_deleted", id).catch(() => {});
|
|
|
|
|
|
2026-03-10 08:17:45 -04:00
|
|
|
const deletePromises: Promise<void>[] = [];
|
|
|
|
|
if (card.frontImagePath) deletePromises.push(deleteObject(card.frontImagePath));
|
|
|
|
|
if (card.backImagePath) deletePromises.push(deleteObject(card.backImagePath));
|
|
|
|
|
await Promise.allSettled(deletePromises);
|
|
|
|
|
|
|
|
|
|
await prisma.responseCard.delete({
|
|
|
|
|
where: { id },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return NextResponse.json({ success: true });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("[cards/[id] DELETE]", error);
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Failed to delete card" },
|
|
|
|
|
{ status: 500 }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|