Use existing 'Primary Contact' relationship type via FORM_CONTACT_RELATIONSHIP constant
This commit is contained in:
@@ -20,7 +20,7 @@
|
||||
|
||||
import { NextResponse } from "next/server";
|
||||
import { civi, verifyChecksum } from "@/lib/civicrm";
|
||||
import { ACTIVITY_TYPE_NAME } from "@/config/form";
|
||||
import { ACTIVITY_TYPE_NAME, FORM_CONTACT_RELATIONSHIP } from "@/config/form";
|
||||
|
||||
interface CheckResult {
|
||||
check: string;
|
||||
@@ -114,59 +114,40 @@ async function probeContactGet(): Promise<CheckResult> {
|
||||
|
||||
async function probeRelationshipType(): Promise<CheckResult> {
|
||||
try {
|
||||
const exact = await civi<{ id: number; name_a_b: string; name_b_a: string; label_a_b: string }>(
|
||||
"RelationshipType",
|
||||
"get",
|
||||
{
|
||||
select: ["id", "name_a_b", "name_b_a", "label_a_b"],
|
||||
where: [["name_a_b", "=", "Primary Form Contact of"]],
|
||||
},
|
||||
);
|
||||
if ((exact.values ?? []).length === 1) {
|
||||
const r = exact.values[0];
|
||||
const exact = await civi<{
|
||||
id: number;
|
||||
name_a_b: string;
|
||||
name_b_a: string;
|
||||
label_a_b: string;
|
||||
contact_type_a: string;
|
||||
contact_type_b: string;
|
||||
}>("RelationshipType", "get", {
|
||||
select: ["id", "name_a_b", "name_b_a", "label_a_b", "contact_type_a", "contact_type_b"],
|
||||
where: [["name_a_b", "=", FORM_CONTACT_RELATIONSHIP]],
|
||||
});
|
||||
const rows = exact.values ?? [];
|
||||
if (rows.length === 1) {
|
||||
const r = rows[0];
|
||||
const directionOk = r.contact_type_a === "Individual" && r.contact_type_b === "Organization";
|
||||
return {
|
||||
check: "relationship_type_primary_form_contact_of",
|
||||
ok: true,
|
||||
detail: `Found id=${r.id}, name_a_b="${r.name_a_b}", name_b_a="${r.name_b_a}".`,
|
||||
check: `relationship_type_${FORM_CONTACT_RELATIONSHIP.replace(/\W+/g, "_").toLowerCase()}`,
|
||||
ok: directionOk,
|
||||
detail: directionOk
|
||||
? `Found id=${r.id}, name_a_b="${r.name_a_b}", direction Individual → Organization. ✓`
|
||||
: `Found id=${r.id} but direction is ${r.contact_type_a} → ${r.contact_type_b} (expected Individual → Organization).`,
|
||||
};
|
||||
}
|
||||
|
||||
// Not found by exact name — list close matches so we know what's available.
|
||||
const candidates = await civi<{ id: number; name_a_b: string; label_a_b: string; contact_type_a: string; contact_type_b: string }>(
|
||||
"RelationshipType",
|
||||
"get",
|
||||
{
|
||||
select: ["id", "name_a_b", "label_a_b", "contact_type_a", "contact_type_b"],
|
||||
where: [
|
||||
[
|
||||
"OR",
|
||||
[
|
||||
["name_a_b", "LIKE", "%Form%"],
|
||||
["name_a_b", "LIKE", "%Primary%"],
|
||||
["name_a_b", "LIKE", "%Contact%"],
|
||||
["label_a_b", "LIKE", "%Form%"],
|
||||
["label_a_b", "LIKE", "%Primary%"],
|
||||
],
|
||||
],
|
||||
],
|
||||
limit: 25,
|
||||
},
|
||||
);
|
||||
const closeMatches = (candidates.values ?? [])
|
||||
.map((r) => ` • id=${r.id} name_a_b="${r.name_a_b}" label="${r.label_a_b}" (${r.contact_type_a} ↔ ${r.contact_type_b})`)
|
||||
.join("\n");
|
||||
return {
|
||||
check: "relationship_type_primary_form_contact_of",
|
||||
check: `relationship_type_${FORM_CONTACT_RELATIONSHIP.replace(/\W+/g, "_").toLowerCase()}`,
|
||||
ok: false,
|
||||
detail:
|
||||
"No relationship type with name_a_b='Primary Form Contact of' on this site.\n" +
|
||||
"Existing relationship types matching Form/Primary/Contact:\n" +
|
||||
(closeMatches || " (none)") +
|
||||
"\nFix: create a new RelationshipType (Individual A ↔ Organization B) with name_a_b='Primary Form Contact of', or change the expected name in /api/data and /api/submit to match an existing type.",
|
||||
rows.length === 0
|
||||
? `No relationship type with name_a_b="${FORM_CONTACT_RELATIONSHIP}" found.`
|
||||
: `Multiple relationship types named "${FORM_CONTACT_RELATIONSHIP}" (${rows.length}). Should be exactly one.`,
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
check: "relationship_type_primary_form_contact_of",
|
||||
check: `relationship_type_${FORM_CONTACT_RELATIONSHIP.replace(/\W+/g, "_").toLowerCase()}`,
|
||||
ok: false,
|
||||
detail: e instanceof Error ? e.message : String(e),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user