Skip to content

Commit

Permalink
catch error instead of retrying in exact account worker if called fro…
Browse files Browse the repository at this point in the history
…m stripe webhook worker
  • Loading branch information
RafidMuhymin committed Jan 15, 2024
1 parent 6e89566 commit f2b4fdd
Showing 1 changed file with 25 additions and 32 deletions.
57 changes: 25 additions & 32 deletions src/workers/dilmahtea-me-exact-account/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ async function handlePOST(request: Request, env: ENV) {
const { userId, Email, Phone, FirstName, LastName, Language, Address } =
await request.json<Body>();

if (!userId) {
handlePOST.retry = false;
handlePOST.catchError = true;
}

const Name = `${FirstName} ${LastName}`,
CustomerData = {
userId,
Expand All @@ -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) {
Expand Down

0 comments on commit f2b4fdd

Please sign in to comment.