Use existing 'Primary Contact' relationship type via FORM_CONTACT_RELATIONSHIP constant

This commit is contained in:
Joel Brock
2026-05-09 20:57:29 -07:00
parent e132789a3e
commit 2e9d3855ba
5 changed files with 46 additions and 54 deletions

View File

@@ -2,7 +2,7 @@
* GET /api/data?cid=<cid>&cs=<cs>
*
* Verifies the checksum, resolves the org from the contact via the
* Primary Form Contact relationship, reads the org's Framework Stage
* Primary Contact relationship, reads the org's Framework Stage
* (`Food_Co_op_Organizing.Stage` on the Organization contact), and walks
* past `Check-in (organizing)` activities for per-field most-recent prefill.
*
@@ -19,7 +19,7 @@
import { NextRequest, NextResponse } from "next/server";
import { civi, verifyChecksum } from "@/lib/civicrm";
import { loadPrefill } from "@/lib/prefill";
import { allFields, optionGroupIds, ACTIVITY_TYPE_NAME } from "@/config/form";
import { allFields, optionGroupIds, ACTIVITY_TYPE_NAME, FORM_CONTACT_RELATIONSHIP } from "@/config/form";
import type { FormDataPayload, SelectOption } from "@/types/form";
const STUB_PAYLOAD: FormDataPayload = {
@@ -118,13 +118,13 @@ export async function GET(req: NextRequest) {
);
}
// Resolve the org from the contact's Primary Form Contact relationship.
// Resolve the org from the contact's "Primary Contact" relationship.
// The relationship is Individual (A) → Organization (B); fetch contact_id_b.
const relRes = await civi<{ contact_id_b: number }>("Relationship", "get", {
select: ["contact_id_b"],
where: [
["contact_id_a", "=", Number(cid)],
["relationship_type_id:name", "=", "Primary Form Contact of"],
["relationship_type_id:name_a_b", "=", FORM_CONTACT_RELATIONSHIP],
["is_active", "=", true],
],
limit: 2,
@@ -132,13 +132,13 @@ export async function GET(req: NextRequest) {
const orgs = relRes.values ?? [];
if (orgs.length === 0) {
return NextResponse.json(
{ error: "No active Primary Form Contact relationship found for your contact." },
{ error: `No active "${FORM_CONTACT_RELATIONSHIP}" relationship found for your contact.` },
{ status: 404 },
);
}
if (orgs.length > 1) {
return NextResponse.json(
{ error: "Your contact has multiple active Primary Form Contact relationships; staff must resolve before this link will work." },
{ error: `Your contact has multiple active "${FORM_CONTACT_RELATIONSHIP}" relationships; staff must resolve before this link will work.` },
{ status: 409 },
);
}