0

I'm in the process of building a portfolio, one project I'd like to build is something similar to AirTasker,

I'm currently working on API built in node.js which takes in a JSON object, this JSON object is basically tasks that are being requested and need to be completed by the relevant person it is assigned to at present it's automatically assigned.

This is my test dummy data:

[
  {
      "clientId": 1,
      "requestId": "help move bed",
      "hours": 6
  },
  {
      "clientId": 2,
      "requestId": "pickup parcel",
      "hours": 1
  },
  {
      "clientId": 1,
      "requestId": "cut the grass",
      "hours": 4
  },
  {
      "clientId": 1,
      "requestId": "clean the kitchen",
      "hours": 2
  }
]

now my API is currently structured as follows:

var workers = [
  {"id": 1, "name": "Person 1", "tasks": []},
  {"id": 2, "name": "Person 2", "tasks": []},
  {"id": 3, "name": "Person 3", "tasks": []},
  {"id": 4, "name": "Person 4", "tasks": []},
  {"id": 5, "name": "Person 5", "tasks": []},
  {"id": 6, "name": "Person 6", "tasks": []},
]


 app.post('/api/v1/request', (req, res) => {

  var requestData = req.body;

  // Loop the tasks passed in
  requestData.forEach(request => {


    // Now evenly spread tasks across workers
    workers.forEach(p => {

        workers.tasks.push(request);

    })

      console.log(request);
  });

  res.status(200).send({
    success: 'true',
    message: 'Tasks has been assigned.',
    workers: workers
  })
});

the object workers as the people who will be doing the tasks, the issue I have is when I loop requestData I can't figure out the syntax to assign one task per one worker, at the moment how my code is it'll assign the one task to all workers and continue, the end result will be all workers have all the same tasks.

Can someone assist me with how I go about assigning one unique task per each worker

3 Answers 3

1

const workers = [
  {"id": 1, "name": "Person 1", "tasks": []},
  {"id": 2, "name": "Person 2", "tasks": []},
  {"id": 3, "name": "Person 3", "tasks": []},
  {"id": 4, "name": "Person 4", "tasks": []},
  {"id": 5, "name": "Person 5", "tasks": []},
  {"id": 6, "name": "Person 6", "tasks": []},
]

const dummies = [
  {
      "clientId": 1,
      "requestId": "help move bed",
      "hours": 6
  },
  {
      "clientId": 2,
      "requestId": "pickup parcel",
      "hours": 1
  },
  {
      "clientId": 1,
      "requestId": "cut the grass",
      "hours": 4
  },
  {
      "clientId": 1,
      "requestId": "clean the kitchen",
      "hours": 2
  }
];

workers.forEach(w => {
  w.tasks = dummies.filter(d => d.clientId === w.id);
});

console.log(workers);

Try this simple code.

Sign up to request clarification or add additional context in comments.

1 Comment

works like a charm thank you. Can I restrict adding a new task to a worker when they have a maximum of 8 hours worth of tasks?
1

You should add task to single worker object in foreach loop like this:

workers.forEach(butler => {
    butler.tasks.push(request);
})

Comments

1

You can use the modulus function to help.

var workers = [
      {"id": 1, "name": "Person 1", "tasks": []},
      {"id": 2, "name": "Person 2", "tasks": []},
      {"id": 3, "name": "Person 3", "tasks": []},
      {"id": 4, "name": "Person 4", "tasks": []},
      {"id": 5, "name": "Person 5", "tasks": []},
      {"id": 6, "name": "Person 6", "tasks": []},
    ]

    var tasks = [
    {
        name: 'a',
        hours: 8
    },
    {
        name: 'b',
        hours: 4
    },
    {
        name: 'c',
        hours: 2
    },{
        name: 'd',
        hours: 4
    },
    {
        name: 'e',
        hours: 3
    },
    {
        name: 'f',
        hours: 5
    },
    {
        name: 'g',
        hours: 4
    },
    {
        name: 'a',
        hours: 2
    },

]

    var reducer = (acc, val) => acc + val.hours;
    var length = workers.length;
    var start = 0;
    tasks.forEach(task => {
      if (workers[start%length].tasks.reduce(reducer, 0) < 8) {
        workers[start%length].tasks.push(task);
      }
      start += 1;
    });

    console.log(workers)

Here's a demo:

2 Comments

Looks good Jack, much appreciated. Can I restrict adding a new task to a worker when they have a maximum of 8 hours worth of tasks?
I've updated the code to use a reducer to check. However be careful of an infinite loop occurring if you provide more tasks that your works can handle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.