const { v4: uuidv4 } = require('uuid');
async function createContactSafely(contactData) {
const idempotencyKey = uuidv4(); // gere uma vez e armazene
for (let attempt = 0; attempt < 3; attempt++) {
try {
const response = await fetch('https://api.socialsell.ai/v1/contacts', {
method: 'POST',
headers: {
'Authorization': 'Bearer sk_live_...',
'Content-Type': 'application/json',
'Idempotency-Key': idempotencyKey // mesma chave em todas as tentativas
},
body: JSON.stringify(contactData)
});
if (response.ok) return await response.json();
// se falhar com erro não-transiente, parar
if (response.status < 500) throw new Error('Erro não recuperável');
} catch (err) {
if (attempt === 2) throw err;
await new Promise(r => setTimeout(r, 1000 * (attempt + 1)));
}
}
}