-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement card and limit keypadid to 5 chars
- Loading branch information
Showing
6 changed files
with
281 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,73 @@ | ||
import { Router } from 'express' | ||
import { ensureAuthenticatedAPI } from '../../functions/ensureAuthenticatedAPI.js' | ||
import User from '../../models/user.js' | ||
var router = Router() | ||
let responseJson | ||
import { Router } from "express"; | ||
import { ensureAuthenticatedAPI } from "../../functions/ensureAuthenticatedAPI.js"; | ||
import User from "../../models/user.js"; | ||
var router = Router(); | ||
let responseJson; | ||
|
||
// GET /api/customerName - accepts customer's keypadId and returns customer's display name | ||
router.get('/', ensureAuthenticatedAPI, function (req, res, _next) { | ||
router.get("/", ensureAuthenticatedAPI, function (req, res, _next) { | ||
// Check if request contains 'customer' parameter | ||
if (!req.query.customer) { | ||
res.status(400) | ||
res.set('Content-Type', 'application/problem+json') | ||
res.status(400); | ||
res.set("Content-Type", "application/problem+json"); | ||
responseJson = { | ||
type: 'https://github.com/houby-studio/small-business-fridge/wiki/API-documentation#customerName', | ||
type: "https://github.com/houby-studio/small-business-fridge/wiki/API-documentation#customerName", | ||
title: "Your request does not contain parameter 'customer'.", | ||
status: 400, | ||
'detail:': | ||
"detail:": | ||
"This function requires parameter 'customer'. More details can be found in documentation https://git.io/JeodS", | ||
'invalid-params': [ | ||
"invalid-params": [ | ||
{ | ||
name: 'customer', | ||
reason: 'must be specified' | ||
} | ||
] | ||
} | ||
res.json(responseJson) | ||
return | ||
name: "customer", | ||
reason: "must be specified", | ||
}, | ||
], | ||
}; | ||
res.json(responseJson); | ||
return; | ||
} | ||
|
||
// Find user in database | ||
const filter = | ||
req.query.customer_id.length < 6 | ||
? { keypadId: req.query.customer_id } | ||
: { card: req.query.customer_id }; | ||
User.findOne({ | ||
keypadId: req.query.customer | ||
...filter, | ||
}) | ||
.then((user) => { | ||
// If database doesn't contain user with supplied keypadId, database returns empty object, which doesn't contain parameter displayName. | ||
res.set('Content-Type', 'application/json') | ||
res.set("Content-Type", "application/json"); | ||
if (!user) { | ||
res.status(404) | ||
res.json('NOT_FOUND') | ||
res.status(404); | ||
res.json("NOT_FOUND"); | ||
} else { | ||
res.status(200) | ||
res.status(200); | ||
const normalized = user.displayName | ||
.normalize('NFD') | ||
.replace(/[\u0300-\u036f]/g, '') | ||
res.json(normalized) | ||
.normalize("NFD") | ||
.replace(/[\u0300-\u036f]/g, ""); | ||
res.json(normalized); | ||
} | ||
}) | ||
.catch((_err) => { | ||
res.status(400) | ||
res.set('Content-Type', 'application/problem+json') | ||
res.status(400); | ||
res.set("Content-Type", "application/problem+json"); | ||
const responseJson = { | ||
type: 'https://github.com/houby-studio/small-business-fridge/wiki/API-documentation#customerName', | ||
type: "https://github.com/houby-studio/small-business-fridge/wiki/API-documentation#customerName", | ||
title: "Your parameter 'customer' is wrong type.", | ||
status: 400, | ||
'detail:': | ||
"detail:": | ||
"Parameter 'customer' must be a 'Number'. More details can be found in documentation https://git.io/JeodS", | ||
'invalid-params': [ | ||
"invalid-params": [ | ||
{ | ||
name: 'customer', | ||
reason: 'must be natural number' | ||
} | ||
] | ||
} | ||
res.json(responseJson) | ||
return | ||
}) | ||
}) | ||
name: "customer", | ||
reason: "must be natural number", | ||
}, | ||
], | ||
}; | ||
res.json(responseJson); | ||
return; | ||
}); | ||
}); | ||
|
||
export default router | ||
export default router; |
Oops, something went wrong.