This is an express JS API web-service which captures user contributed reports and returns an aggregate report in response.
Each report consists of a market-commodity combination for which prices in the Mandi(Market) are provided in a certain unit (along with their conversion factor to base unit - Kg). You need to combine the reports per market-commodity by calculating the average of the report prices.
These docs describe how to use the api, how to make request and the possible responses
There have two endpoints
- To submit the report and retrive a final report
- To test the api
- Nodejs
- MongoDB
Run the commands
1. npm install
Create a .env
file in the root and provide the mongodb connection url and specific the database in MONGO_URL
variable.
eg :-MONGO_URL=[CONNECTION_URL]
2. node index
The server will start in Port No: 3000
POST /reports
Create a POST
request by sending the details in request body.
- The request will look for an existing report with marketID-cmdtyID-date.
- A user can submit only one report for a commodity in specific market per day
- Save a report
A sample request is given below.
{
"reportDetails": {
"userID": "user-1",
"marketID": "market-1",
"marketName": "Nabeel shop",
"cmdtyID": "cmdty-1",
"marketType": "Mandi",
"cmdtyName": "Potato",
"priceUnit": "Pack",
"convFctr": 50,
"price": 700
}
}
The response is in JSON representation of the resource created or edited.However, if an invalid request is submitted, or some other error occurs, api returns a JSON response in the following format:
{
"status": "success",
"reportID": "612db4e3496545e401c48b71"
}
The status
attribute contains a message 'success' if the request was successful or the message is 'error' if an error occur.
The reportID
contain the Id of the report created, if an error occur, it is message
containing the error message.
GET /reports?reportID=[REPORT_ID]
Create a GET
request by sending the report id in params which we need to get.
- The request will find the report and calculate the mean price then send back response.
The response is in JSON representation of the report.However, if an invalid request is submitted, or some other error occurs, api returns a JSON response in the following format:
{
"_id": "612db4e3496545e401c48b71",
"cmdtyName": "potato",
"cmdtyID": "cmdty-1",
"marketID": "market-1",
"marketName": "Nabeel shop",
"users": [
"user-2",
"user-1",
"user-3"
],
"timestamp": "31/08/2021",
"priceUnit": "Kg",
"price": "15.00"
}
There will be have two type response :
- For a successfull request it will send back the report testDetails
- If the request was invalid it will send back a error message
GET /test?reportID=[REPORT_ID]
Create a GET
request by sending the report id in params which we need to get.
- The request will find the report and verify that the /reports POST and /reports GET API returns the correct response.
The response is in JSON representation of the report.However, if an invalid request is submitted, or some other error occurs, api returns a JSON response in the following format:
{
"createdAt": "31/08/2021",
"data": {
"users": [
"user-2",
"user-1",
"user-3",
"user-4"
],
"pricesAdded": [
14,
15,
16,
14
]
},
"priceSum": 59,
"numOfSubmission": 4,
"finalPrice": 14.75,
"lastSubmission": "2021-08-31T07:18:20.250Z",
"description": "4 users submitted the report and the system converted the actual value to a base value based on the conversion value given by them"
}