MarvinAPI icon indicating copy to clipboard operation
MarvinAPI copied to clipboard

Get all subprojects/tasks recursively under a project

Open jbriales opened this issue 3 years ago • 2 comments

Hi! I'm getting started with the Marvin API and I'm not sure if what I'm trying to do is easily doable, worth asking for as a request, ...

I'd like to retrieve the tree of all projects and tasks under a certain root project (either both completed and open, or just open, or just complete). I'm a noob with CouchDB so, I wonder if this might be easily doable via the CouchDB API and if you have any pointers for that.

Thanks!

jbriales avatar Nov 15 '20 20:11 jbriales

I don't know of a way to do this with couchdb other than by using all_docs to grab everything and then doing a depth-first search! With pouchdb it would look like this:

import { keyBy, groupBy, map } from "lodash";
const { rows } = db.allDocs();
const docs = map(rows, "doc");
const collections = groupBy(docs, "db");
const categoryDocs = collections.Categories;
const taskDocs = collections.Tasks;

const categoriesByParent = groupBy(categoryDocs, "parentId");
const tasksByParent = groupBy(taskDocs, "parentId");

const result = [];
result.push(...tasksByParent[targetId]); // the ID of the project you're looking for
const queue = [...categoriesByParent[targetId]];
for (let n = 0; n < queue.length; n++) {
  const curr = queue[n];
  result.push(curr);
  const childTasks = tasksByParent[curr._id];
  const childCats = categoriesByParent[curr._id];
  result.push(...tasksByParent[targetId]);
  queue.push(...childCats);
}

It happens from time to time that you get category cycles, so you might want to check for that too. I hope that helps!

amazingmarvin avatar Nov 17 '20 19:11 amazingmarvin

Great thanks! I'm sure that will be very helpful for me and other noobs. I'll try to elaborate from there and get back if I hit any technical challenges for a first-timer.

jbriales avatar Nov 18 '20 20:11 jbriales