Preserve existing custom fields on PATCH and prefer primary org/phone

This commit is contained in:
Adam Moussa 2026-05-11 13:44:23 -04:00
parent 04cfb41b9a
commit d6720061e1

View file

@ -139,19 +139,19 @@ async function listGoogleUsers(accessToken, domain, orgUnits) {
function extractUserFields(googleUser) { function extractUserFields(googleUser) {
const email = googleUser.primaryEmail; const email = googleUser.primaryEmail;
// Job title lives in organizations[].title
let jobTitle = ""; let jobTitle = "";
if (googleUser.organizations && googleUser.organizations.length > 0) { if (googleUser.organizations && googleUser.organizations.length > 0) {
jobTitle = googleUser.organizations[0].title || ""; const primaryOrg = googleUser.organizations.find((o) => o.primary) || googleUser.organizations[0];
jobTitle = primaryOrg.title || "";
} }
// Phone lives in phones[].value — prefer "work" type
let phone = ""; let phone = "";
if (googleUser.phones && googleUser.phones.length > 0) { if (googleUser.phones && googleUser.phones.length > 0) {
const workPhone = googleUser.phones.find((p) => p.type === "work"); const workPhone = googleUser.phones.find((p) => p.type === "work");
const primaryPhone = googleUser.phones.find((p) => p.primary) || googleUser.phones[0];
phone = workPhone phone = workPhone
? workPhone.value ? workPhone.value
: googleUser.phones[0].value || ""; : primaryPhone.value || "";
} }
return { email, jobTitle, phone }; return { email, jobTitle, phone };
@ -163,23 +163,32 @@ function extractUserFields(googleUser) {
async function updateFrontTeammate(frontToken, email, customFields) { async function updateFrontTeammate(frontToken, email, customFields) {
const url = `https://api2.frontapp.com/teammates/alt:email:${encodeURIComponent(email)}`; const url = `https://api2.frontapp.com/teammates/alt:email:${encodeURIComponent(email)}`;
const headers = {
Authorization: `Bearer ${frontToken}`,
"Content-Type": "application/json",
Accept: "application/json",
};
const res = await fetch(url, { const getRes = await fetch(url, { headers });
if (getRes.status === 404) return { status: "not_in_front" };
if (!getRes.ok) {
const text = await getRes.text();
return { status: "error", code: getRes.status, message: text };
}
const teammate = await getRes.json();
const merged = { ...teammate.custom_fields, ...customFields };
const patchRes = await fetch(url, {
method: "PATCH", method: "PATCH",
headers: { headers,
Authorization: `Bearer ${frontToken}`, body: JSON.stringify({ custom_fields: merged }),
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({ custom_fields: customFields }),
}); });
// 204 = success, 404 = user not in Front (skip), anything else = error if (patchRes.status === 204) return { status: "updated" };
if (res.status === 204) return { status: "updated" };
if (res.status === 404) return { status: "not_in_front" };
const text = await res.text(); const text = await patchRes.text();
return { status: "error", code: res.status, message: text }; return { status: "error", code: patchRes.status, message: text };
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------