mirror of
https://github.com/Sea-Haven-Industries/google-user-sync.git
synced 2026-05-18 20:20:17 +00:00
Preserve existing custom fields on PATCH and prefer primary org/phone
This commit is contained in:
parent
04cfb41b9a
commit
d6720061e1
1 changed files with 25 additions and 16 deletions
|
|
@ -139,19 +139,19 @@ async function listGoogleUsers(accessToken, domain, orgUnits) {
|
|||
function extractUserFields(googleUser) {
|
||||
const email = googleUser.primaryEmail;
|
||||
|
||||
// Job title lives in organizations[].title
|
||||
let jobTitle = "";
|
||||
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 = "";
|
||||
if (googleUser.phones && googleUser.phones.length > 0) {
|
||||
const workPhone = googleUser.phones.find((p) => p.type === "work");
|
||||
const primaryPhone = googleUser.phones.find((p) => p.primary) || googleUser.phones[0];
|
||||
phone = workPhone
|
||||
? workPhone.value
|
||||
: googleUser.phones[0].value || "";
|
||||
: primaryPhone.value || "";
|
||||
}
|
||||
|
||||
return { email, jobTitle, phone };
|
||||
|
|
@ -163,23 +163,32 @@ function extractUserFields(googleUser) {
|
|||
|
||||
async function updateFrontTeammate(frontToken, email, customFields) {
|
||||
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",
|
||||
headers: {
|
||||
Authorization: `Bearer ${frontToken}`,
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/json",
|
||||
},
|
||||
body: JSON.stringify({ custom_fields: customFields }),
|
||||
headers,
|
||||
body: JSON.stringify({ custom_fields: merged }),
|
||||
});
|
||||
|
||||
// 204 = success, 404 = user not in Front (skip), anything else = error
|
||||
if (res.status === 204) return { status: "updated" };
|
||||
if (res.status === 404) return { status: "not_in_front" };
|
||||
if (patchRes.status === 204) return { status: "updated" };
|
||||
|
||||
const text = await res.text();
|
||||
return { status: "error", code: res.status, message: text };
|
||||
const text = await patchRes.text();
|
||||
return { status: "error", code: patchRes.status, message: text };
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue