diff --git a/src/workers/dilmahtea-me-exact-account/src/index.ts b/src/workers/dilmahtea-me-exact-account/src/index.ts index b2294c9e..d6f55a24 100644 --- a/src/workers/dilmahtea-me-exact-account/src/index.ts +++ b/src/workers/dilmahtea-me-exact-account/src/index.ts @@ -28,6 +28,11 @@ async function handlePOST(request: Request, env: ENV) { const { userId, Email, Phone, FirstName, LastName, Language, Address } = await request.json(); + if (!userId) { + handlePOST.retry = false; + handlePOST.catchError = true; + } + const Name = `${FirstName} ${LastName}`, CustomerData = { userId, @@ -44,40 +49,28 @@ async function handlePOST(request: Request, env: ENV) { const CustomerFilter = getCustomerFilter(Email || Phone, !!Email); - try { - var Customer = await fetchExactAPI( - "GET", - `/CRM/Accounts?$filter=${getCustomerFilter( - Email || Phone, - !!Email, - )}&$select=ID,Name,Language,Email,Phone,Country,LeadSource,Classification1`, - ).then((Customer) => { - console.log({ - ExistingCustomer: Customer, - }); - - return Customer.feed.entry?.content["m:properties"]; + let Customer = await fetchExactAPI( + "GET", + `/CRM/Accounts?$filter=${getCustomerFilter( + Email || Phone, + !!Email, + )}&$select=ID,Name,Language,Email,Phone,Country,LeadSource,Classification1`, + ).then((Customer) => { + console.log({ + ExistingCustomer: Customer, }); - if (Customer) { - console.log("Exact: Customer exists"); - - await updateCustomer( - auth, - CustomerData, - Customer, - CustomerFilter, - userId, - ); - } else { - Customer = await createCustomer(CustomerData); - - console.log("Exact: Customer created successfully"); - } - } catch (error) { - if (userId) throw error; - - return reply({ success: false, error: error.message }, 500); + return Customer.feed.entry?.content["m:properties"]; + }); + + if (Customer) { + console.log("Exact: Customer exists"); + + await updateCustomer(auth, CustomerData, Customer, CustomerFilter, userId); + } else { + Customer = await createCustomer(CustomerData); + + console.log("Exact: Customer created successfully"); } if (userId) {