Skip to content

Commit

Permalink
Implement card and limit keypadid to 5 chars
Browse files Browse the repository at this point in the history
  • Loading branch information
megastary committed Mar 10, 2024
1 parent dca618c commit 6447836
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 253 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## ✨ Novinky

- 💳 Nově si může uživatel přidat kartu o minimální délce 6 znaků, která slouží jako bezpečnější způsob ověření
a rovněž slouží pro načtení fyzického čářového nebo QR kódu místo ručního zadávání
- 🔢 Číslo klávesnice nyní může mít maximálně délku 5 znaků, pro delší bezpečnější způsob ověření slouží karta

## 🐞 Opravy chyb

- 🎨 Kategorie se v nabídce zobrazují ihned po vytvoření, není je potřeba ještě dodatečně upravit přes formulář Upravit kategorii
3 changes: 3 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const userSchema = new Schema({
keypadId: {
type: Number,
required: true,
maxlength: 5,
},
admin: {
type: Boolean,
Expand Down Expand Up @@ -64,6 +65,8 @@ const userSchema = new Schema({
},
});

userSchema.index({});

const model = mongoose.model("User", userSchema);

export const schema = model.schema;
Expand Down
86 changes: 45 additions & 41 deletions routes/api/customerName.js
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;
Loading

0 comments on commit 6447836

Please sign in to comment.