Skip to content

Commit

Permalink
Merge pull request #8 from thesamim/master
Browse files Browse the repository at this point in the history
Additional functionality
lucasvtiradentes authored Nov 14, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 835471e + e600e29 commit 300a708
Showing 8 changed files with 808 additions and 90 deletions.
207 changes: 182 additions & 25 deletions examples/dev-example.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,206 @@
import { Tick } from '../src/index'
import dotenv from 'dotenv'
import { ITask } from '../src/types/Task'
dotenv.config()
import { isValid } from '../src/utils/validate'

async function main() {
let allAllTasks: ITask[] = null;
let donePrinted: string[] = [];

export async function testTick() {
try {
const envUSERNAME = process.env.TICK_USERNAME
const envPASSWORD = process.env.TICK_PASSWORD
// const envUSERNAME = process.env.TICK_USERNAME
// const envPASSWORD = process.env.TICK_PASSWORD
const envUSERNAME = "[email protected]"
const envPASSWORD = "Howdy0021!"
if (!envUSERNAME || !envPASSWORD){
throw new Error('parameters username/password were not set in .env file.');
}


const projectID = "65159d5d8f08a002fd167ce8";
const tickSession = new Tick({ username: envUSERNAME, password: envPASSWORD });
const hasLoggedIn = await tickSession.login();
if (!hasLoggedIn) {
throw new Error('Coudnt login with this username/password.');
}
// let myTask: ITask = {
// "projectId": projectID,
// "title": "Trying too many things. #otherNewTest 10/21/2023 -- latest.",
// "tags": ["NewTest"]
// }
// let addResult = await tickSession.addTask(myTask);
// console.log("Add Result : ", addResult);

const userPreferences = await tickSession.getUserSettings()
console.log(Object.keys(userPreferences))

const allAllTasks = await tickSession.getAllTasks();
console.log(allAllTasks.map((item) => item.title));

const tasks = await tickSession.getTasks();
console.log(tasks.map((item) => item.title));
// let newTaskId = "6515b1642b3163961425bb73";
// let newTaskTitle = "";
// console.log("New Id: ", newTaskId, "New Title: ", newTaskTitle);
// let addedTask = await tickSession.getTask(newTaskId, projectID)
// console.log("getTask result: ", addedTask);

const filters = await tickSession.getFilters();
console.log(filters.map((item) => item.name));
// let myTask: ITask = {
// "projectId": projectID,
// "title": addedTask.title + " just add something.",
// // "tags": ["NewTest"],
// "id": newTaskId,
// "status": 3
// }
// let addResult = await tickSession.updateTask(myTask);
// console.log("Update Result : ", addResult);
// const allAllTasks = await tickSession.getAllTasks();
// if (allAllTasks !== undefined && allAllTasks !== null) {
// console.log("==== allAllTasks")
// console.log(allAllTasks.map((item) => item.title));
// console.log("==== items of allAllTasks")
// console.log(allAllTasks.map((item) => item.items))
// console.log("==== content of allAllTasks")
// console.log(allAllTasks.map((item) => item.content))
// }else {
// console.log("==== No allAllTasks")
// }
allAllTasks = await tickSession.getAllTasks();
if (allAllTasks !== undefined && allAllTasks !== null) {
allAllTasks = allAllTasks.filter((currentTask) => currentTask.status == 0);
//make sure top level tasks are first
allAllTasks.sort((a, b) => {
if (isValid(a.parentId) && isValid(b.parentId)) {
return (a.parentId < b.parentId)
} else if (isValid(a.parentId)) {
return 1;
} else {
return -1;
}
});
// allAllTasks.forEach(task => console.log(task.title))

const projectGroups = await tickSession.getProjectGroups();
console.log(projectGroups.map((item) => item.name));

const projects = await tickSession.getProjects();
console.log(projects.map((item) => item.name));

const habits = await tickSession.getHabits();
console.log(habits.map((item) => item.name));

const tags = await tickSession.getTags();
console.log(tags);
console.log(allAllTasks.map((currentTask) => `${currentTask.title}, ${currentTask.sortOrder}`))
printTasks(allAllTasks);
// let oneLevelTasks = allAllTasks.filter((currentTask) => !isAChild(currentTask.parentId));
// let taskWithChildren = allAllTasks.filter((currentTask) => isAChild(currentTask.parentId));
// console.log(`No Parent tasks: \n ${oneLevelTasks.map((currentTask) => currentTask.title)}\n`)
// console.log(`Parent tasks:\n ${taskWithChildren.map((currentTask) => currentTask.title)}\n`)

// allAllTasks.forEach(task => {
// if (task.status == 0) {
// // if (!(isValid(task.parentId))) {
// console.log(`${task.status} -- ${task.title} -- ${task.id} -- ${isAChild(task.parentId) ? 'is a parent' : 'is not a parent'}`);
// // console.log(task);
// // }
// if (isValid(task.childIds)) {
// task.childIds.forEach(child => {
// console.log(`\t ${child}`)
// let childrens = allAllTasks.filter((currentTask) => currentTask.id==child)
// console.log(`====children of ${child}========`)
// console.log(childrens)
// });
// }
// }
// });
}else {
console.log("==== No allAllTasks")
}

// const projects = await tickSession.getProjects();
// if (projects !== undefined && projects !== null) {
// console.log("==== projects")
// // console.log(projects.map((item) => item.name));
// console.log(`Got ${projects.length} projects.`)
// projects.forEach(async project => {
// console.log("Prj nm: ",project.name);
// const sections = await tickSession.getProjectSections(project.id);
// // console.log(`Project: ${project.name} -- ${sections}`);
// if (sections !== undefined && sections !== null && sections.length > 0) {
// sections.forEach(section => {
// console.log(project.name + '--' + section.name);
// })
// } else {
// console.log(project.name + '--' + 'no sections')
// }
// })

// // projects.forEach(project => {
// // console.log(project.name);
// // const sections = await tickSession.getProjectSections(project.id);
// // if (sections !== undefined && sections !== null) {
// // console.log(sections);
// // });
// // }

// } else {
// console.log("==== No projects")
// }
} catch (e: any) {
console.log(e.message);
}
console.log("\n\nDone")
}

function isAChild(idToCheck: string) {
console.log(`input: ${idToCheck}`);
if (!isValid(idToCheck)) {
console.log('is a child.')
return false;
} else {
console.log("is not a child")
return true
}
}

function printTasks(allTheTasks: ITask[]) {
console.log("=== Print Tasks ==");

allTheTasks.forEach(task => {
// if (!isAChild(task.parentId)) {
// console.log(task.title);
// } else {
if (donePrinted.indexOf(task.id) < 1) {
printTaskAndChildren(task, 0);
}
// }
});
}

function printTaskAndChildren(task: ITask, depth: number) {
// console.log(`printTasks+ ${task.title}, ${depth}`)
let childCount = 0;
//There's probably a more js way of doing this
let tabs: string = '';
childCount = childCount + depth;
for (let index = 0; index < childCount; index++) {
tabs += '\t';
};
// console.log(`[${tabs}], ${tabs.length}, ${childCount}`);
if (donePrinted.indexOf(task.id) < 1) {
console.log(`${tabs}title: ${task.title} -- ${task.id} -- parent ${task.parentId} --- project: ${task.projectId}`)
donePrinted.push(task.id);
}
if (isValid(task.childIds))
{
// console.log("-- has children")
let newDepth = depth + 1;
// console.log(`new depth: ${newDepth}`);
task.childIds.forEach(currentChildId => {
let childTask: ITask = allAllTasks.find(currentTask => currentTask.id == currentChildId);
if (isValid(childTask)) {
printTaskAndChildren(childTask, newDepth);
} else {
console.log(`found no child: for ${task.title}`)
}


});
// printTaskAndChildren(---)

}
}






testTick();




main();
243 changes: 243 additions & 0 deletions examples/dev-example.ts.bkup
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
import { Tick } from '../src/index'
import dotenv from 'dotenv'
import { ITask } from '../src/types/Task'
dotenv.config()
import { isValid } from '../src/utils/validate'

let allAllTasks: ITask[] = null;
let donePrinted: string[] = [];

export async function testTick() {
try {
// const envUSERNAME = process.env.TICK_USERNAME
// const envPASSWORD = process.env.TICK_PASSWORD
const envUSERNAME = "thesamim@yahoo.com"
const envPASSWORD = "howdy002"
if (!envUSERNAME || !envPASSWORD){
throw new Error('parameters username/password were not set in .env file.');
}

const tickSession = new Tick({ username: envUSERNAME, password: envPASSWORD });
const hasLoggedIn = await tickSession.login();
if (!hasLoggedIn) {
throw new Error('Coudnt login with this username/password.');
}
// let myTask: ITask = {
// "projectId": "inbox121990985",
// "title": "This is the one I want to add right now. ",
// "tags": ["mod1"]
// }
// const addResult = await tickSession.addTask(myTask, false);
// console.log(addResult);

// const userPreferences = await tickSession.getUserSettings()
// if (userPreferences !== undefined && userPreferences !== null) {
// console.log("==== userPreferences")
// console.log(Object.keys(userPreferences))
// } else {
// console.log("==== No userPreferences")
// }

// const allAllTasks = await tickSession.getAllTasks();
// if (allAllTasks !== undefined && allAllTasks !== null) {
// console.log("==== allAllTasks")
// console.log(allAllTasks.map((item) => item.title));
// console.log("==== items of allAllTasks")
// console.log(allAllTasks.map((item) => item.items))
// console.log("==== content of allAllTasks")
// console.log(allAllTasks.map((item) => item.content))
// }else {
// console.log("==== No allAllTasks")
// }
allAllTasks = await tickSession.getAllTasks();
if (allAllTasks !== undefined && allAllTasks !== null) {
allAllTasks = allAllTasks.filter((currentTask) => currentTask.status == 0);
//make sure top level tasks are first
allAllTasks.sort((a, b) => {
if (isValid(a.parentId) && isValid(b.parentId)) {
return (a.parentId < b.parentId)
} else if (isValid(a.parentId)) {
return 1;
} else {
return -1;
}
});

console.log(allAllTasks.map((currentTask) => `${currentTask.title}, ${currentTask.sortOrder}`))
printTasks(allAllTasks);
// let oneLevelTasks = allAllTasks.filter((currentTask) => !isAChild(currentTask.parentId));
// let taskWithChildren = allAllTasks.filter((currentTask) => isAChild(currentTask.parentId));
// console.log(`No Parent tasks: \n ${oneLevelTasks.map((currentTask) => currentTask.title)}\n`)
// console.log(`Parent tasks:\n ${taskWithChildren.map((currentTask) => currentTask.title)}\n`)

// allAllTasks.forEach(task => {
// if (task.status == 0) {
// // if (!(isValid(task.parentId))) {
// console.log(`${task.status} -- ${task.title} -- ${task.id} -- ${isAChild(task.parentId) ? 'is a parent' : 'is not a parent'}`);
// // console.log(task);
// // }
// if (isValid(task.childIds)) {
// task.childIds.forEach(child => {
// console.log(`\t ${child}`)
// let childrens = allAllTasks.filter((currentTask) => currentTask.id==child)
// console.log(`====children of ${child}========`)
// console.log(childrens)
// });
// }
// }
// });
}else {
console.log("==== No allAllTasks")
}

// const tasks = await tickSession.getTasks();
// if (tasks !== undefined && tasks !== null) {
// console.log("==== tasks")
// console.log(tasks.map((item) => item.title));
// console.log("==== items of tasks")
// console.log(tasks.map((item) => item.items))
// console.log("==== content of tasks")
// console.log(tasks.map((item) => item.content))
// }else {
// console.log("==== No tasks")
// }

// const filters = await tickSession.getFilters();
// if (filters !== undefined && filters !== null) {
// console.log("==== filters")
// console.log(filters.map((item) => item.name));
// }else {
// console.log("==== No filters")
// }

const projectGroups = await tickSession.getProjectGroups();
if (projectGroups !== undefined && projectGroups !== null) {
console.log("==== projectGroups")
console.log(projectGroups.map((item) => item.name));
}else {
console.log("==== No projectGroups")
}

const projects = await tickSession.getProjects();
if (projects !== undefined && projects !== null) {
console.log("==== projects")
// console.log(projects.map((item) => item.name));
projects.forEach(async project => {
// console.log(project.name);
const singleProject = await tickSession.getProject(project.id);
console.log(singleProject);
const sections = await tickSession.getProjectSections(project.id);
// console.log(`Project: ${project.name} -- ${sections}`);
if (sections !== undefined && sections !== null && sections.length > 0) {
sections.forEach(section => {
console.log(project.name + '--' + section.name);
})
} else {
console.log(project.name + '--' + 'no sections')
}
})

// projects.forEach(project => {
// console.log(project.name);
// const sections = await tickSession.getProjectSections(project.id);
// if (sections !== undefined && sections !== null) {
// console.log(sections);
// });
// }




} else {
console.log("==== No projects")
}

const habits = await tickSession.getHabits();
if (habits !== undefined && habits !== null) {
console.log("==== habits")
console.log(habits.map((item) => item.name));
}else {
console.log("==== No habits")
}
const tags = await tickSession.getTags();
if (tags !== undefined && tags !== null) {
console.log("==== tags")
console.log(tags);
}else {
console.log("==== No tags")
}

} catch (e: any) {
console.log(e.message);
}
}

function isAChild(idToCheck: string) {
console.log(`input: ${idToCheck}`);
if (!isValid(idToCheck)) {
console.log('is a child.')
return false;
} else {
console.log("is not a child")
return true
}
}

function printTasks(allTheTasks: ITask[]) {
console.log("=== Print Tasks ==");

allTheTasks.forEach(task => {
// if (!isAChild(task.parentId)) {
// console.log(task.title);
// } else {
if (donePrinted.indexOf(task.id) < 1) {
printTaskAndChildren(task, 0);
}
// }
});
}

function printTaskAndChildren(task: ITask, depth: number) {
// console.log(`printTasks+ ${task.title}, ${depth}`)
let childCount = 0;
//There's probably a more js way of doing this
let tabs: string = '';
childCount = childCount + depth;
for (let index = 0; index < childCount; index++) {
tabs += '\t';
};
// console.log(`[${tabs}], ${tabs.length}, ${childCount}`);
if (donePrinted.indexOf(task.id) < 1) {
console.log(`${tabs}title: ${task.title} -- ${task.id} -- parent ${task.parentId} --- project: ${task.projectId}`)
donePrinted.push(task.id);
}
if (isValid(task.childIds))
{
// console.log("-- has children")
let newDepth = depth + 1;
// console.log(`new depth: ${newDepth}`);
task.childIds.forEach(currentChildId => {
let childTask: ITask = allAllTasks.find(currentTask => currentTask.id == currentChildId);
if (isValid(childTask)) {
printTaskAndChildren(childTask, newDepth);
} else {
console.log(`found no child: for ${task.title}`)
}


});
// printTaskAndChildren(---)

}
}






testTick();




3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ticktick-api-lvt",
"version": "0.0.0-development",
"version": "0.1.0-development",
"description": "📅 a ticktick api wrapper package to be used in node based projects.",
"license": "MIT",
"author": {
@@ -25,6 +25,7 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"prepare": "npm run build",
"start": "node dist/index.js",
"dev_example": "ts-node-dev --respawn --transpile-only ./examples/dev-example.ts",
"dev": "npm run dev_example",
384 changes: 327 additions & 57 deletions src/index.ts

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions src/types/Project.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export { IProject }

interface IProject {
export interface IProject {
id: string
name: string
isOwner: boolean
@@ -22,3 +20,10 @@ interface IProject {
kind: string
timeline: any
}
export interface ISections
{
id: number,
projectId: number,
name: string,
sortOrder: number,
}
19 changes: 16 additions & 3 deletions src/types/Task.ts
Original file line number Diff line number Diff line change
@@ -9,15 +9,15 @@ export interface ITask {
timeZone: string;
isFloating?: boolean;
isAllDay: boolean;
reminder: string;
reminder: string; // we only get a set
reminders: any[];
repeatFirstDate?: string;
repeatFlag: string;
exDate?: any[];
completedTime?: string;
completedUserId?: any;
repeatTaskId?: string;

priority: number;
status: number;
items: any[];
@@ -31,10 +31,23 @@ export interface ITask {
focusSummaries?: any[];
columnId?: string;
kind?: string;

assignee?: any;
isDirty?: boolean;
local?: boolean;
remindTime?: any;
tags?: any[];
childIds: string[];
parentId: string;

}

export interface IUpdate
{
"add": ITask[],
"addAttachments": [],
"delete": ITask[],
"deleteAttachments": [],
"update": ITask[],
"updateAttachments": []
}
7 changes: 6 additions & 1 deletion src/utils/get-api-endpoints.ts
Original file line number Diff line number Diff line change
@@ -9,7 +9,12 @@ const API_ENDPOINTS = {
allHabitsEndPoint: 'habits',
allTagsEndPoint: 'tags',
allTasksEndPoint: 'batch/check/1',
addTaskEndPoint: '/task'
TaskEndPoint: 'task',
updateTaskEndPoint: 'batch/task',
//If this ever existed, it's gone now. use getSections. That's the only project detail anyway.
//getProject: 'project/',
getSections: 'column/project/',
getAllCompletedItems: "project/all/completedInAll/"
};

/*
24 changes: 24 additions & 0 deletions src/utils/validate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

export function isValid(variable: any)
{
// console.log(`${variable} is ${typeof variable}`)
let retValue = false;
if (variable != null && variable !== 'undefined') {
if (Array.isArray(variable) && variable.length > 0) {
retValue = true;
} else {
// console.log(typeof variable)
if (typeof variable == 'string') {
retValue = variable.length > 0;
} else if (typeof variable == 'object') {
// console.log(Object.keys(variable))
retValue = Object.keys(variable).length !== 0;
}
}
retValue = true;
} else {
retValue = false;
}
// console.log(`is valid: ${retValue}`)
return retValue;
}

0 comments on commit 300a708

Please sign in to comment.