From cc68fad7b34d0e90a486d40393caa267e0cdded7 Mon Sep 17 00:00:00 2001 From: Kroonk Date: Sat, 23 May 2026 00:36:11 +0200 Subject: [PATCH] fix: parse birthdate to supply day, month and year fields to satisfy Monica CRM validation --- server.js | 84 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 68 insertions(+), 16 deletions(-) diff --git a/server.js b/server.js index 8f399c4..f5335a6 100644 --- a/server.js +++ b/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) {