Commit abe971fe authored by Dmitrij Tkačenko's avatar Dmitrij Tkačenko
Browse files

Check profile pin

parent 569830fc
......@@ -38,12 +38,12 @@ Response body will always look like this:
1. Get a whole home object
- type: GET
- route: **/**
- returns: Home object
- returns: object with state and home object
2. Get consumption of a whole home, based on given date range
- type: GET
- route: **/consumption**
- returns: total consumption number
- returns: object with state and total consumption number
- request body: date range object (format: YYYY-MM-DD)
```
// example of an date range object
......@@ -55,7 +55,21 @@ Response body will always look like this:
3. Get home profiles
- type: GET
- route: **/profiles**
- returns: object with profiles
- returns: object with state and object with profiles
- request body: none
4. Check profile pin
- type: POST
- route: **/check-profile**
- returns: object with state and message
- request body: object with profile id and input pin
```
// example of an request object
{
"id": "profile_1",
"pin": "1111"
}
```
**In progress...Other endpoints are coming.**
......@@ -4,8 +4,7 @@ const Utils = require('../libs/Utils')
module.exports = {
getProfiles: function(req, res) {
const home = JSON.parse(fs.readFileSync(process.env.DEV_DB, 'utf-8'))
const profiles = home.profiles
const profiles = Utils.getProfiles()
try {
Response.success(
res,
......@@ -14,5 +13,23 @@ module.exports = {
} catch (error) {
Response.error(res, error.message)
}
},
checkProfile: function(req, res) {
const profiles = Utils.getProfiles()
const profileId = req.body.id
const profilePin = req.body.pin
const incomingProfile = profiles[profileId]
console.log(incomingProfile)
if (incomingProfile) {
if(incomingProfile.pin === profilePin) {
Response.success(res, "Profile pin matched!")
} else {
Response.error(res, "Profile pin missmatch!", 403)
}
} else {
Response.error(res, "Profile with this id doesn't exist", 400)
}
}
}
......@@ -17,6 +17,9 @@ app.route('/consumption').get(HomeController.consumption)
// get profiles
app.route('/profiles').get(ProfileController.getProfiles)
// check profile pin
app.route('/check-profile').post(ProfileController.checkProfile)
app.get('*', (req, res) => {
Response.error(res, "Api endpoint doesn't exist.", 404)
......
const fs = require('fs')
module.exports = {
computeConsumption: function(data = [], numberOfDays) {
let consumption = 0;
......@@ -9,5 +10,11 @@ module.exports = {
} else {
return null;
}
},
getProfiles: function() {
const home = JSON.parse(fs.readFileSync(process.env.DEV_DB, 'utf-8'))
const profiles = home.profiles
return profiles
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment