Adding IF logic within a let body when using Asana API body parameter

I am utilizing Zapier and the Asana API to create a task within Zapier’s Javascript (uses Node 10) code action. Below is my current code, which currently works as expected.

However, I’d like to update it, to add logic that will allow me to change some of the data objects if the Start Date and End Date are the same.

// this is wrapped in an `async` function
// you can use await throughout the function

// get personName
var personName = inputData.personName;
// set dateStart
const dateStart = inputData.dateStart;
// convert the format of MM/DD/YYYY to YYYY-MM-DD for Asana's use
var dateStartFormatted = new Date(dateStart).toISOString().split('T')[0];
// set dateEnd
const dateEnd = inputData.dateEnd;
// convert the format of MM/DD/YYYY to YYYY-MM-DD for Asana's use
var dateEndFormatted = new Date(dateEnd).toISOString().split('T')[0];
// get dateDuration
var dateDuration = inputData.dateDuration;
// get taskURL from the custom fields above
var theURL = inputData.eventLink;
// encode the URL
var theEventLinkFinal = encodeURI(theURL);
// get dateStartPretty
var dateStartPretty = inputData.dateStartPretty;
// get dateEndPretty
var dateEndPretty = inputData.dateEndPretty;
// get projectSection
var projectSection = inputData.projectSection;

// sets custom dropdown field of Team Member to value of OFF
// sets the start date and end date
// sets the task Name based on person chosen in Google Form
// sets task description (notes) with date range, number of days, and link to Google Calendar event
// sets Asana project to "Timeline" and section of the person chosen in the Google Form
// adds Admin and HR users as followers on the task
let body = {
  "data": {
    "custom_fields": {
      "917734345465519": "917734345465549"
    },
    "start_on": dateStartFormatted,
    "due_on": dateEndFormatted,
    "name": personName + " Off",
    "notes": "Date range: " + dateStartPretty + " – " + dateEndPretty + "nnTotal number of days: " + dateDuration + "nnCalendar link: " + theEventLinkFinal,
    "memberships": [ 
      {
        "project": "893079009430519",
        "section": projectSection
      }
    ],
    "followers": [
      "670933960116792",
      "7747475052050"
    ],
    "workspace": "301669572129"
  }
};
// create the task based on the body and data
const res = await fetch('https://app.asana.com/api/1.0/tasks', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json', 
    'Authorization': 'Bearer 0/XXXXXXXX'
  },
  body: JSON.stringify(body)
});
const data = await res.json();
output = {data: data};

The goal is to update the above code, so:

IF dateStartFormatted === dateEndFormatted … THEN swap out these two lines of data:

"start_on": dateStartFormatted,
"due_on": dateEndFormatted,

And replace with this line of data:

"due_at": dateStartFormatted,

I tried doing if/else statements within the let body, but to no avail.

Answer

The data variable should just be a normal javascript object so you should be able to dynamically assign the properties based on the condition:

let body = {
  "data": {
    "custom_fields": {
      "917734345465519": "917734345465549"
    },
    "name": personName + " Off",
    "notes": "Date range: " + dateStartPretty + " – " + dateEndPretty + "nnTotal number of days: " + dateDuration + "nnCalendar link: " + theEventLinkFinal,
    "memberships": [ 
      {
        "project": "893079009430519",
        "section": projectSection
      }
    ],
    "followers": [
      "670933960116792",
      "7747475052050"
    ],
    "workspace": "301669572129"
  }
};

// New: Check for dates to see what property to set for due dates
if (dateStartFormatted === dateEndFormatted) {
    body.data.due_at = dateStartFormatted;
} else {
    body.data.start_on = dateStartFormatted;
    body.data.due_on = dateEndFormatted;
}

// create the task based on the body and data
const res = await fetch('https://app.asana.com/api/1.0/tasks', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json', 
    'Authorization': 'Bearer 0/XXXXXXXX'
  },
  body: JSON.stringify(body)
});

Just make sure to not include the start_on and due_on in the base body object.