fix: parse birthdate to supply day, month and year fields to satisfy Monica CRM validation
All checks were successful
Build & Push Friends Image to Gitea Registry / build-and-push (push) Successful in 10s
All checks were successful
Build & Push Friends Image to Gitea Registry / build-and-push (push) Successful in 10s
This commit is contained in:
84
server.js
84
server.js
@@ -259,6 +259,40 @@ export async function monicaFetch(apiPath, options = {}) {
|
||||
return payload;
|
||||
}
|
||||
|
||||
export function parseBirthdate(birthdateStr) {
|
||||
if (!birthdateStr) {
|
||||
return {
|
||||
is_birthdate_known: false,
|
||||
day: null,
|
||||
month: null,
|
||||
year: null
|
||||
};
|
||||
}
|
||||
|
||||
const parts = birthdateStr.split("-");
|
||||
if (parts.length === 3) {
|
||||
const yearVal = parseInt(parts[0], 10);
|
||||
const monthVal = parseInt(parts[1], 10);
|
||||
const dayVal = parseInt(parts[2], 10);
|
||||
|
||||
if (!isNaN(dayVal) && !isNaN(monthVal)) {
|
||||
return {
|
||||
is_birthdate_known: true,
|
||||
day: dayVal,
|
||||
month: monthVal,
|
||||
year: (!isNaN(yearVal) && yearVal > 0) ? yearVal : null
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
is_birthdate_known: false,
|
||||
day: null,
|
||||
month: null,
|
||||
year: null
|
||||
};
|
||||
}
|
||||
|
||||
export async function writeToMonica(entry) {
|
||||
const results = [];
|
||||
|
||||
@@ -275,16 +309,25 @@ export async function writeToMonica(entry) {
|
||||
if (entry.contact?.is_new && entry.contact?.first_name) {
|
||||
try {
|
||||
console.log(`[CRM] Auto-creating new contact: "${entry.contact.first_name} ${entry.contact.last_name || ''}"`);
|
||||
const bday = parseBirthdate(entry.contact.birthdate);
|
||||
const postBody = {
|
||||
first_name: entry.contact.first_name,
|
||||
last_name: entry.contact.last_name || "",
|
||||
description: "Automatisch angelegter Kontakt via Friends",
|
||||
is_birthdate_known: bday.is_birthdate_known,
|
||||
is_deceased: false,
|
||||
is_deceased_date_known: false
|
||||
};
|
||||
if (bday.is_birthdate_known) {
|
||||
postBody.day = bday.day;
|
||||
postBody.month = bday.month;
|
||||
if (bday.year !== null) {
|
||||
postBody.year = bday.year;
|
||||
}
|
||||
}
|
||||
const newContactPayload = await monicaFetch("/api/contacts", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
first_name: entry.contact.first_name,
|
||||
last_name: entry.contact.last_name || "",
|
||||
description: "Automatisch angelegter Kontakt via Friends",
|
||||
is_birthdate_known: !!entry.contact.birthdate,
|
||||
is_deceased: false,
|
||||
is_deceased_date_known: false
|
||||
}),
|
||||
body: JSON.stringify(postBody),
|
||||
});
|
||||
const newContact = newContactPayload.data || {};
|
||||
contactId = Number(newContact.id);
|
||||
@@ -312,16 +355,25 @@ export async function writeToMonica(entry) {
|
||||
const contactDetails = await monicaFetch(`/api/contacts/${contactId}`);
|
||||
const existingData = contactDetails.data || {};
|
||||
|
||||
const bday = parseBirthdate(entry.contact.birthdate);
|
||||
const putBody = {
|
||||
first_name: existingData.first_name || entry.contact.first_name || "Unbekannt",
|
||||
last_name: existingData.last_name || entry.contact.last_name || "",
|
||||
is_birthdate_known: bday.is_birthdate_known,
|
||||
is_deceased: existingData.is_deceased ?? false,
|
||||
is_deceased_date_known: existingData.is_deceased_date_known ?? false
|
||||
};
|
||||
if (bday.is_birthdate_known) {
|
||||
putBody.day = bday.day;
|
||||
putBody.month = bday.month;
|
||||
if (bday.year !== null) {
|
||||
putBody.year = bday.year;
|
||||
}
|
||||
}
|
||||
|
||||
await monicaFetch(`/api/contacts/${contactId}`, {
|
||||
method: "PUT",
|
||||
body: JSON.stringify({
|
||||
first_name: existingData.first_name || entry.contact.first_name || "Unbekannt",
|
||||
last_name: existingData.last_name || entry.contact.last_name || "",
|
||||
birthdate: entry.contact.birthdate,
|
||||
is_birthdate_known: !!entry.contact.birthdate,
|
||||
is_deceased: existingData.is_deceased ?? false,
|
||||
is_deceased_date_known: existingData.is_deceased_date_known ?? false
|
||||
}),
|
||||
body: JSON.stringify(putBody),
|
||||
});
|
||||
results.push({ type: "contact_birthdate", ok: true, id: contactId });
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user