Here’s the code:
import { axios } from "@pipedream/platform";
export default {
props: {
google_tasks: {
type: "app",
app: "google_tasks",
},
task_list_id: {
type: "string",
label: "Task List ID",
default: "Mk1hZng3dVdLeEJ6ZVZqUg", // Replace with your default task list ID
},
notion_due_date: {
type: "string",
label: "Notion Due Date",
},
notion_page_url: {
type: "string",
label: "Notes",
},
notion_task_title: {
type: "string",
label: "Notion Task Title",
},
},
async run({ steps, $ }) {
try {
console.log("Starting the component...");
// Log input values for debugging
console.log("Notion Task Title:", this.notion_task_title);
console.log("Notion Due Date:", this.notion_due_date);
console.log("Notion Page URL:", this.notion_page_url);
const taskDueDate = new Date(this.notion_due_date);
if (isNaN(taskDueDate.getTime())) {
console.error("Invalid date value:", this.notion_due_date);
} else {
const isoDueDate = taskDueDate.toISOString();
console.log("ISO Due Date:", isoDueDate);
// Debugging: Log HTTP request to Google Tasks API
console.log("HTTP Request to Google Tasks API:", {
method: "GET",
url: `https://tasks.googleapis.com/tasks/v1/lists/${this.task_list_id}/tasks`,
headers: {
Authorization: `Bearer ${this.google_tasks.$auth.oauth_access_token}`,
},
});
const response = await axios(this, {
method: "GET",
url: `https://tasks.googleapis.com/tasks/v1/lists/${this.task_list_id}/tasks`,
headers: {
Authorization: `Bearer ${this.google_tasks.$auth.oauth_access_token}`,
},
});
// Check if the response exists
if (response && response.data) {
console.log("Google Tasks Response:", response.data);
const tasksArray = response.data.items;
// Debugging: Log the tasks obtained from Google Tasks
console.log("Tasks from Google Tasks:", tasksArray);
// Check if there is an existing task with a matching title
const partialNotionTaskTitle = this.notion_task_title.substring(0, 6); // Change 6 to the desired length
const matchingTask = tasksArray.find((task) => {
const taskTitle = task.title || "";
return taskTitle.startsWith(partialNotionTaskTitle);
});
if (matchingTask) {
// Task already exists, update it
const taskId = matchingTask.id;
// Check if the task in Notion is marked as completed
const notionTaskCompleted = steps.trigger.event.properties.done.checkbox;
// Debugging: Log the Notion completion status
console.log("Notion Task Completed:", notionTaskCompleted);
// Update the task in Google Tasks based on Notion completion status
const googleTaskStatus = notionTaskCompleted ? "completed" : "needsAction";
try {
const response = await axios(this, {
method: "PATCH",
url: `https://tasks.googleapis.com/tasks/v1/lists/${this.task_list_id}/tasks/${taskId}`,
headers: {
Authorization: `Bearer ${this.google_tasks.$auth.oauth_access_token}`,
"Content-Type": "application/json",
},
data: {
due: isoDueDate,
notes: this.notion_page_url,
status: googleTaskStatus,
},
});
console.log("Update response:", response.data);
} catch (error) {
console.error("Error updating task:", taskId, error.response ? error.response.data : error);
}
} else {
console.log("Matching task not found in Google Tasks. Creating a new task...");
// Create a new task in Google Tasks
const newTask = {
title: this.notion_task_title,
due: isoDueDate,
notes: this.notion_page_url,
};
const createResponse = await axios(this, {
method: "POST",
url: `https://tasks.googleapis.com/tasks/v1/lists/${this.task_list_id}/tasks`,
headers: {
Authorization: `Bearer ${this.google_tasks.$auth.oauth_access_token}`,
"Content-Type": "application/json",
},
data: newTask,
});
console.log("New task created:", createResponse.data);
}
} else {
console.log("Google Tasks Response is empty.");
}
}
console.log("Completed the component successfully.");
} catch (error) {
console.error("Error in the component:", error);
}
},
};