refactor: extract server-side CRM personal account lookup

This commit is contained in:
2026-11-06 11:22:42 +00:00
parent b9be188d27
commit 4b552e9cdd
3 changed files with 83 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
import { relayGetData } from "../../pages/api/middleware/relayForwarding";
import { RELAY_POLICY_LOOKUP } from "../../pages/api/middleware/relayPolicyPresets";
export const getCrmPersonalAccount = async (contactId) => {
const queryUrl =
"contacts(" +
contactId +
")?$select=firstname, lastname, emailaddress1, telephone1, pinswg_companyname, address1_line1,address1_line2,address1_city, address1_county,address1_postalcode,pinswg_typeofinvolvement,pinswg_contact_associatedlpa,pinswg_preferredlanguage&$count=true";
const { data } = await relayGetData({
queryUrl,
relayPolicy: RELAY_POLICY_LOOKUP
});
return data;
};
+2
View File
@@ -34,6 +34,8 @@ const loadModule = (relativePath, injected = {}) => {
source += `\nif (typeof ${name} !== "undefined" && !module.exports.default) module.exports.default = ${name};\n`;
});
source +=
'\nif (typeof getCrmPersonalAccount !== "undefined") module.exports.getCrmPersonalAccount = getCrmPersonalAccount;\n';
source +=
'\nif (typeof respondSuccess !== "undefined") module.exports.respondSuccess = respondSuccess;\n';
source +=
@@ -9,6 +9,71 @@ const {
const tests = [];
const test = (name, fn) => tests.push({ name, fn });
test("getCrmPersonalAccount returns the CRM response unchanged", async () => {
const contactId = "726158c6-708d-ec11-aad8-00224800be9c";
const crmResponse = {
contactid: contactId,
firstname: "Test",
lastname: "User",
emailaddress1: "test@example.com"
};
let capturedQueryUrl;
const mod = loadModule("lib/server/getCrmPersonalAccount.js", {
relayGetData: async ({ queryUrl }) => {
capturedQueryUrl = queryUrl;
return { data: crmResponse };
}
});
const result = await mod.getCrmPersonalAccount(contactId);
assert.deepStrictEqual(result, crmResponse);
assert.strictEqual(
capturedQueryUrl,
"contacts(" +
contactId +
")?$select=firstname, lastname, emailaddress1, telephone1, pinswg_companyname, address1_line1,address1_line2,address1_city, address1_county,address1_postalcode,pinswg_typeofinvolvement,pinswg_contact_associatedlpa,pinswg_preferredlanguage&$count=true"
);
});
test("getCrmPersonalAccount preserves the CRM lookup relay policy", async () => {
let capturedRelayPolicy;
const mod = loadModule("lib/server/getCrmPersonalAccount.js", {
relayGetData: async ({ relayPolicy }) => {
capturedRelayPolicy = relayPolicy;
return { data: {} };
}
});
await mod.getCrmPersonalAccount("726158c6-708d-ec11-aad8-00224800be9c");
assert.deepStrictEqual(JSON.parse(JSON.stringify(capturedRelayPolicy)), {
method: "GET",
timeoutMs: 6000,
maxRetries: 1,
retryBaseDelayMs: 100,
retryMaxDelayMs: 500
});
});
test("getCrmPersonalAccount propagates CRM relay failures", async () => {
const relayError = new Error("CRM relay unavailable");
const mod = loadModule("lib/server/getCrmPersonalAccount.js", {
relayGetData: async () => {
throw relayError;
}
});
await assert.rejects(
() => mod.getCrmPersonalAccount("726158c6-708d-ec11-aad8-00224800be9c"),
(error) => error === relayError
);
});
test("getaccounts returns EMAIL_ADDRESS_REQUIRED when emailAddress missing", async () => {
const mod = loadModule("pages/api/endpoint/getaccounts_api.js", {
respondError: respondErrorMock,