This is a simple Express.js application that provides a REST API for managing employee records and calculating summary statistics. The API is secured using JSON Web Tokens (JWT) for authentication.
- User login and token generation.
- CRUD operations on employee records.
- Calculation of summary statistics over the dataset.
- Middleware for checking the validity of tokens.
- Express.js
- Body-parser
- JSON Web Token (JWT)
- Clone this repository to your local machine.
- Run
npm install
to install all necessary dependencies. - To start the server, run
node index.js
.
Authenticates a user and returns a JWT token.
Request body:
{
"username": "Akshay",
"password": "Akshay@123"
}
Adds a new record to the dataset.
Request headers:
Authorization: Bearer <token>
Request body:
{
"name": "John Doe",
"salary": "5000",
"currency": "USD",
"department": "Marketing",
"sub_department": "Digital Marketing"
}
Deletes a record by index from the dataset.
Request headers:
Authorization: Bearer <token>
URL parameters:
/delete_record/0
Fetches summary statistics for salary over the entire dataset.
Example Response:
{
"mean": 5000,
"min": 5000,
"max": 5000
}
Fetches summary statistics for salaries of records with "on_contract": "true"
.
Example Response:
{
"mean": 5000,
"min": 5000,
"max": 5000
}
Fetches summary statistics for salaries of employees in a specific department.
URL parameters:
/summary_stats/department_salary/Engineering
Example Response:
{
"mean": 5000,
"min": 5000,
"max": 5000
}
Fetches summary statistics for salaries of employees in a specific department and sub-department.
URL parameters:
/summary_stats/department_sub_department_salary/Engineering/Platform
Example Response:
{
"mean": 5000,
"min": 5000,
"max": 5000
}
Please replace <token>
with the actual token received from the /login
endpoint.
The API will return an error response if:
- The request does not include a valid token.
- The payload for adding a record is invalid.
- A record index is invalid or out of range.
- There are no records found for a specific query.
This application includes a test suite that tests all the API endpoints. The tests are written using the Chai assertion library and the Chai HTTP plugin to perform HTTP requests.
- Chai
- Chai HTTP
To run the tests, use the following command:
npm test
The test suite includes the following tests:
- User login and token generation.
- Adding a new record to the dataset.
- Deleting a record by index from the dataset.
- Fetching summary statistics for salary over the entire dataset.
- Fetching summary statistics for salaries of records with
"on_contract": "true"
. - Fetching summary statistics for salaries of employees in a specific department.
- Fetching summary statistics for salaries of employees in a specific department and sub-department.
The tests check the HTTP response status code and the structure and content of the response body to ensure the API is functioning correctly.
If you want to add more tests or improve the existing ones, feel free to submit a pull request.