diff --git a/app/carnet/page.tsx b/app/carnet/page.tsx index 3195ad1c..e306b5c1 100644 --- a/app/carnet/page.tsx +++ b/app/carnet/page.tsx @@ -393,18 +393,33 @@ export default function CarnetPage() { } const { content } = await response.json(); - const contacts = parseVCardContent(content); + + // Split the content into individual vCards + const vcards = content.split('BEGIN:VCARD').filter(section => section.trim()); // Update or add the contact - const existingIndex = contacts.findIndex(c => c.id === contact.id); - if (existingIndex >= 0) { - contacts[existingIndex] = contact; - } else { - contacts.push(contact); + let updatedVcards: string[] = []; + let contactUpdated = false; + + for (const section of vcards) { + const vcard = parseVCard('BEGIN:VCARD' + section); + if (vcard.uid?.[0]?.value === contact.id) { + // Replace the existing contact + updatedVcards.push(generateVCardContent(contact)); + contactUpdated = true; + } else { + // Keep the existing contact + updatedVcards.push('BEGIN:VCARD' + section); + } } - // Generate new VCF content - const vcfContent = contacts.map(c => generateVCardContent(c)).join('\n'); + // If contact wasn't found, add it as new + if (!contactUpdated) { + updatedVcards.push(generateVCardContent(contact)); + } + + // Join all vCards back together + const vcfContent = updatedVcards.join('\n'); // Save the updated VCF file const saveResponse = await fetch('/api/nextcloud/files', {