Skip to content

nguyenmanhtri/togo

 
 

Repository files navigation

Manabie Application Assignment: A simple API for creating daily tasks

Requirements

  • Implement one single API which accepts a todo task and records it.
  • There is a maximum limit of 5 tasks per user that can be added per day.
  • Write integration (functional) tests.
  • Write unit tests.
  • Choose a suitable architecture to make your code simple, organizable, and maintainable
    • express framework is implemented to structure the API.
  • Write a concise README
    • How to run your code locally?
    • A sample “curl” command to call your API
    • How to run your unit tests locally?
    • What do you love about your solution?
      • Express routings are implemented to keep the codes clean and maintainable. Each file serves (at least) a single purpose so that reading the codes become much easier. Other functions are imported and reused, such as the database models' functions, input validation.
      • Middlewares are a big part of making the HTTP request process functions more human-friendly.
      • Node package Joi is used to extract validation errors easier, keeping the server open without crashing it.
    • What else do you want us to know about however you do not have enough time to complete?
      • Write some functional testing for the API.
      • Currently, anyone can create a task without having an existed user_id, so implementing a user identification/authentication can be considered in the future.
      • A task reporter can report at most 5 tasks/day, so an assignee can also receive at most N tasks a day.
      • A UI to validate user input more effectively.
      • Give a user his/her report on the number of tasks assign/assigned in a period of time.

The detailed description of the assignment can be found here

Table of contents

  • NodeJS ^14.17. You can download NodeJS runtime here.

npm install to install all the dependencies.

In your terminal, run:

npm start

Your server is ready at: http://localhost:3000.

curl http://localhost:3000 | json

Output

{
  "message": "API ready"
}

The | json tag is there to make the output more readable.

curl --header "content-type: application/json" --request POST --data "{\"name\": \"frank\", \"email\": \"[email protected]\"}" http://localhost:3000/users | json

Output:

{
    "id": 1,
    "name": "frank",
    "email": "[email protected]",
    "created_at": "2021-12-17 07:21:09"
}

You have successfully created a user with id (auto generated), name and email.

On Windows:

curl --header "content-type: application/json" --request POST --data "{\"title\": \"get grocery\", \"detail\": \"buy eggs and ham\", \"due_at\": \"2021-12-31 23:59:59\", \"reporter_id\": 1}" http://localhost:3000/tasks | json

Output:

{
    "id": 1,
    "title": "get grocery",
    "detail": "buy eggs and ham",
    "due_at": "2021-12-31 23:59:59",
    "created_at": "2021-12-17 07:24:21",
    "reporter_id": 1,
    "assignee_id": null
}
curl http://localhost:3000/tasks/count | json

Output:

{
  "results": [
    {
      "created_date": "2021-12-17",
      "reporter_id": 1,
      "task_count": 1
    }
  ]
}

Maximum number of tasks submitted per day by a user: 5

When you have already created 5 tasks a day, the API responded with:

curl --header "content-type: application/json" --request POST --data "{\"title\": \"6th task\", \"detail\": \"this will not work\", \"due_at\": \"2021-12-31 23:59:59\", \"reporter_id\": 1}" http://localhost:3000/tasks | json

Output:

{
    "success": false,
    "message": "Reached task count limit of 5"
}

Technologies involved:

  • mocha to create test framework
  • chai assertion library
  • supertest to create a mock HTTP request

Make sure you have closed the server before running the test.

npm test

... to run the test script with mocha.

Test results

Test cases

  • GET /tasks
    • View all tasks when there are no data.
    • View all tasks when there is 1 data row.
  • POST /tasks
    • Send POST request with valid body.
    • Send POST request with body missing property. (failed case)
    • Try to create 6 tasks a day. (validation case)
  • GET /users
    • View all users when there are no data.
    • View all users when there is 1 data row.
  • POST /users
    • Send POST request with valid body.
    • Create a user with duplicated email, responded with { "message": "email duplicated" } (validation case)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%