- Getting Started
- API Documentation
- API Endpoints
- Real-time Communication
- Testing Real-time Communication
- Clone the repository
git clone https://github.com/Babatunde13/task-stream-api.git
- Change the directory to the project folder
cd task-stream-api
- Install dependencies
For the project, I used yarn
as the package manager, you can install it by running the following command if you don't have it installed already
npm install -g yarn
Then install the dependencies by running the following command
yarn install
- Create a
.env
file in the root directory and add the following environment variables, TheDB_URI
should be a MongoDB URI, you can use a local MongoDB instance or a cloud MongoDB service like MongoDB Atlas, by default it ismongodb://localhost:27017/task-stream
. The JWT_SECRET can be any random but strong string which will be used to sign the JWT token.
PORT=3000
DB_URI=mongodb://localhost:27017/task-stream
JWT_SECRET=your_secret_key
To start the application in dev mode, run the following command
yarn run start:dev
To start the application in production mode, run the following command
yarn run build && yarn run start:prod
User
-> Store user information_id
(ObjectId)fullname
(String)email
(String)password
(String)createdAt
(Date)updatedAt
(Date)
Task
-> Store task information_id
(ObjectId)title
(String)description
(String)status
(Enum) # ['open', 'in_progress', 'done'] (default: 'open')owner
(ObjectId)priority
(String)dueDate
(Date)createdAt
(Date)updatedAt
(Date)
AuthController
login
-> uses LoginDtoregister
-> uses RegisterDto
TaskController
createTask
-> uses CreateTaskDtogetTasks
-> uses GetTasksDtogetTask
-> uses GetTaskDtoupdateTask
-> uses UpdateTaskDtoupdateTaskStatus
-> uses UpdateTaskStatusDtodeleteTask
-> uses GetTaskDtogetAuthUserTasks
-> uses GetTasksDtogetUserTask
-> uses GetTaskDto
AuthService
login
: Gets user by email, compares password and generates tokenregister
-> creates a new user and generates a token
TaskService
createTask
-> creates a new task for the authenticated usergetTasks
-> gets all tasks filtered by status and dueDate(tasks whose dueDate is greater than or equal to the given dueDate) and sorts by priority, dueDate and createdAtgetTask
-> gets a task by idupdateTask
-> updates a task by id for the authenticated userupdateTaskStatus
-> updates a task status by id for the authenticated userdeleteTask
-> deletes a task by id for the authenticated usergetAuthUserTasks
-> gets all tasks of the authenticated user filtered by status and dueDate(tasks whose dueDate is greater than or equal to the given dueDate) and sorts by priority, dueDate and createdAtgetUserTasks
-> gets all tasks of the given user filtered by status and dueDate(tasks whose dueDate is greater than or equal to the given dueDate) and sorts by priority, dueDate and createdAt
- Auth Dtos
LoginDto
email
(String)password
(String) # should be at least 8 characters with at least one uppercase letter, one lowercase letter, one number and one special character
RegisterDto
fullname
(String)email
(String)password
(String) # should be at least 8 characters with at least one uppercase letter, one lowercase letter, one number and one special character
- Task Dtos
CreateTaskDto
title
(String)description
(String)priority
(String)dueDate
(DateString)
UpdateTaskDto
title
(String)description
(String)priority
(String)dueDate
(DateString)
UpdateTaskStatusDto
status
(Enum) # ['open', 'in_progress', 'done']
Get Tasks Dto
status
(Enum) # ['open', 'in_progress', 'done']dueDate
(DateString)
AuthGuard
-> checks if the user is authenticated. It uses a JWT token to authenticate the user, and returns a 401 error if the user is not authenticated
HttpExceptionFilter
-> Catches all exceptions and returns a formatted response with status code and message
utils.ts
: Handles utility functions such as hashing and token generation
The API Documentation is available at BaseURL/api/v1/docs i.e http://localhost:3000/api/v1/docs
POST /api/v1/auth/login
POST /api/v1/auth/register
GET /api/v1/tasks/user
- Get all tasks of the authenticated userGET /api/v1/tasks/user/{id}
- Get all tasks of the given userPOST /api/v1/tasks
- Create a new taskGET /api/v1/tasks
- Get all tasksGET /api/v1/tasks/{id}
- Get a task by idPUT /api/v1/tasks/{id}
- Update a task by idPATCH /api/v1/tasks/{id}/status
- Update a task status by idDELETE /api/v1/tasks/{id}
- Delete a task by id
The application uses Socket.io for real-time communication between the server and the client. The client listens for events emitted by the server and updates the UI accordingly.
When a new task is created, updated or deleted, the server emits the following events to all connected clients
task-created
- Emitted when a new task is createdtask-updated
- Emitted when a task is updatedtask-deleted
- Emitted when a task is deleted
To test the real-time communication, There is a simple HTML file in the root directory test.html
which connects to the server using socket.io and listens for the events emitted by the server. You can open this file in your browser and every time a new task is created, updated or deleted, either from the Swagger UI or any other client, the tasks will be displayed in the browser. Aside from this, you can also use Postman to test the API endpoints and socket io implementation.
The Socket IO server also handles authentication, so if you pass a JWT token to the header it will try to authenticate the user silently.
Next Steps:
- Enforce authentication in the socket.io connection, so only authenticated users can connect
- Implement a front-end application using React or Angular to consume the API