Skip to content

PhoneBookUtil

// ==========================
// Example usage on deviceready
// ==========================
document.addEventListener(
"deviceready",
function () {
console.log("Device ready - initializing PhoneBookUtil");
PhoneBookUtil.init();
// Example: you should replace these with your real values
const CURRENT_USER_ID = "user-123"; // e.g. from auth
const CURRENT_SYSTEM_ID = "device-abc"; // e.g. device UUID
// Backend expects: [ { user_id, uuid, system_id, primary_phone_number, secondary_phone_number, name, primary_email } ]
PhoneBookUtil.setUploadHandler(async (batch) => {
const payload = batch.map((item) => {
const contact = item.contact;
const primaryPhone =
contact.phones && contact.phones[0] ? contact.phones[0] : null;
const secondaryPhone =
contact.phones && contact.phones[1] ? contact.phones[1] : null;
const primaryEmail =
contact.emails && contact.emails[0] ? contact.emails[0] : null;
return {
user_id: CURRENT_USER_ID,
uuid: item.id,
system_id: CURRENT_SYSTEM_ID,
primary_phone_number: primaryPhone,
secondary_phone_number: secondaryPhone,
name: contact.name || "",
primary_email: primaryEmail
};
});
if (!payload.length) return;
const res = await fetch(
"https://your-api.com/contacts/insert-update",
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload)
}
);
if (!res.ok) {
throw new Error(
"Insert/Update contacts failed with status " + res.status
);
}
});
// Kick off initial sync + upload (weekly rate-limit applies unless force: true)
PhoneBookUtil.syncAndUpload({ force: true })
.then((result) => {
console.log("Initial contacts sync result:", result);
})
.catch((err) => {
console.error("Contacts sync failed:", err);
});
},
false
);