Get all milestones for a date interval

Hello there !

I’m attempting to create a “Weekly Milestone Page” using ReactJs and Asana API.

Goal : Get all milestones of the week and display them.

Some details :

  • For each milestone, I aim to retrieve the associate project
  • For each milestone, I want to get the duedate

I’ve tried a method that works, but my request takes excessively long (1 or 2 minutes), and I’m uncertain how to optimize this process.

Is there a method to fetch tasks exclusively within a specific date interval, or something similar?

Thank you in advance for all your responses!

Here is what I am currently doing:

export const getProjectsFromPortfolio = async (portfolioId: string) => {
  console.log("getProjectsFromPortfolio");

  try {
    const response = await asanaApi.get(`/portfolios/${portfolioId}/items`);
    return response.data.data;
  } catch (error) {
    console.error("Error fetching projects from portfolio:", error);
    return [];
  }
};

export const getTaskDetails = async (taskId: string) => {
  try {
    const response = await asanaApi.get(`/tasks/${taskId}`);
    return response.data.data;
  } catch (error) {
    console.error("Error fetching task details:", error);
    return null;
  }
};

export const getMilestonesOfProject = async (projectId: string, projectName: string) => {
  console.log("getMilestonesOfProject");

  try {
    const response = await asanaApi.get(`/projects/${projectId}/tasks`);
    const milestones = response.data.data.filter(
      (task: any) => task.resource_subtype === "milestone"
    );

    // Week dates
    const start = startOfWeek(new Date());
    const end = endOfWeek(new Date());

    const detailedMilestones = await Promise.all(
      milestones.map(async (milestone: any) => {
        const details = await getTaskDetails(milestone.gid);
        const dueDate = parseISO(details.due_on);
        
        // Check duedate is on the week
        if (isWithinInterval(dueDate, { start, end })) {
          return {
            ...milestone,
            projectName: projectName,
            dueDate: details.due_on,
            status: details.completed ? "Completed" : "Pending"
          };
        }
        console.log("delete task");
        
        return null;
      })
    );

    // Filter the null
    return detailedMilestones.filter(milestone => milestone !== null);
  } catch (error) {
    console.error("Error fetching detailed milestones of project:", error);
    return [];
  }
};

export const getAllMilestonesFromPortfolio = async (portfolioId: string) => {
  console.log("getAllMilestonesFromPortfolio");
  
  try {
    const projects = await getProjectsFromPortfolio(portfolioId);
    const milestonesPromises = projects.map((project: any) =>
      getMilestonesOfProject(project.gid, project.name)
    );
    const milestonesArrays = await Promise.all(milestonesPromises);
    console.log("finished");

    return milestonesArrays.flat();
  } catch (error) {
    console.error("Error fetching all milestones:", error);
    return [];
  }
};

I think you want to use the Search API here instead. It will let you get all tasks across your projects and portfolios without having to iterate through each one. Use the resource_subtype filter (along with due_on.after and due_on.before) to return only those items of subtype milestone (so you won’t have to do that filtering on your end).

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.