split services / delete dish / mail init

This commit is contained in:
2020-08-28 18:54:30 +02:00
parent 3674e4ce3c
commit f304463b46
17 changed files with 507 additions and 204 deletions

View File

@@ -1,5 +1,9 @@
import express from "express";
import Restaurant from "../models/restaurant.js";
import { createDish } from "../services/dataPrepServices.js";
import {
removeDish,
addDishToRestaurant,
} from "../services/databaseServices.js";
import * as services from "../services/services.js";
import Dish from "../models/dish.js";
@@ -22,9 +26,25 @@ router.post("/", async (req, res) => {
await services.validateRestaurant(req.body.restaurantId);
const token = req.headers["x-auth-token"];
services.validateUserToken(token);
const dish = services.createDish(req.body.dish, true);
const dish = createDish(req.body.dish, req.body.restaurantId, true);
await dish.save();
await services.addDishToRestaurant(req.body.restaurantId, dish._id);
await addDishToRestaurant(req.body.restaurantId, dish._id);
res.status(201).send(dish._id);
} catch (error) {
services.handleError(error, res);
}
});
// REMOVE DISH
router.delete("/", async (req, res) => {
try {
await services.validateDishId(req.body.dishId);
const token = req.headers["x-auth-token"];
const decodedToken = services.validateUserToken(token);
await services.verifyDishAccess(req.body.dishId, decodedToken);
await removeDish(req.body.dishId);
res.sendStatus(200);
} catch (error) {
services.handleError(error, res);
}
@@ -34,10 +54,10 @@ router.post("/", async (req, res) => {
router.put("/", async (req, res) => {
try {
services.validateDishId(req.body.dishId);
await services.validateDishId(req.body.dishId);
const token = req.headers["x-auth-token"];
services.validateUserToken(token);
const dish = services.createDish(req.body.dish, false);
const dish = createDish(req.body.dish, req.body.restaurantId, false);
await Dish.replaceOne({ _id: req.body.dishId }, dish);
res.sendStatus(200);
} catch (error) {

View File

@@ -1,4 +1,10 @@
import express from "express";
import { createRestaurant } from "../services/dataPrepServices.js";
import {
addRestaurantToUser,
fetchRestaurant,
fetchAllDishesForRestaurant,
} from "../services/databaseServices.js";
import * as services from "../services/services.js";
import Restaurant from "../models/restaurant.js";
@@ -21,9 +27,10 @@ router.get("/", async (req, res) => {
router.post("/", async (req, res) => {
try {
const token = req.headers["x-auth-token"];
services.validateUserToken(token);
const restaurant = services.createRestaurant(req);
const user = services.validateUserToken(token);
const restaurant = createRestaurant(req.body);
await restaurant.save();
await addRestaurantToUser(user, restaurant);
res.sendStatus(201);
} catch (error) {
services.handleError(error, res);
@@ -36,8 +43,8 @@ router.get("/dishes", async (req, res) => {
try {
const query = services.decodeAndSanitize(req.query.restaurantId);
await services.validateRestaurant(query);
let restaurant = await services.fetchRestaurant(query);
let dishes = await services.fetchAllDishesForRestaurant(restaurant);
let restaurant = await fetchRestaurant(query);
let dishes = await fetchAllDishesForRestaurant(restaurant);
res.send(dishes);
} catch (error) {
services.handleError(error, res);

View File

@@ -4,9 +4,14 @@ import * as services from "../services/services.js";
var router = express.Router();
router.post("/", async (req, res) => {
await services.checkEmailTaken("jonasz@bankai.pl").catch((e) => {
services.handleError(e, res);
});
try {
const decodedToken = services.validateUserToken(
req.headers["x-auth-token"]
);
res.send(decodedToken);
} catch (error) {
services.handleError(error, res);
}
});
export default router;

View File

@@ -1,5 +1,19 @@
import express from "express";
import * as services from "../services/services.js";
import { changeUserPass, fetchUser } from "../services/databaseServices.js";
import {
composeNewContact,
createUser,
prepareSafeUser,
} from "../services/dataPrepServices.js";
import {
newError,
handleError,
checkPassword,
generateAuthToken,
checkEmailTaken,
validateUserToken,
hashPass,
} from "../services/services.js";
import * as config from "../config/index.js";
import AgileCRMManager from "agile_crm";
const { CRM_USER, CRM_EMAIL, CRM_KEY } = config;
@@ -11,29 +25,56 @@ var agileAPI = new AgileCRMManager(CRM_USER, CRM_KEY, CRM_EMAIL);
router.post("/login", async (req, res) => {
try {
if (!req.body.password || !req.body.email) {
throw services.newError("No input data", 204);
throw newError("No input data", 204);
}
const user = await services.fetchUser(req.body.email);
await services.checkPassword(req.body.password, user.password);
const safeUser = services.prepareSafeUser(user);
var token = services.generateAuthToken(safeUser);
const user = await fetchUser(req.body.email);
await checkPassword(req.body.password, user.password);
const safeUser = prepareSafeUser(user);
var token = generateAuthToken(safeUser);
res.header("x-auth-token", token).status(202).send(safeUser);
} catch (error) {
services.handleError(error, res);
handleError(error, res);
}
});
// REGISTER
router.post("/register", async (req, res) => {
try {
await services.checkEmailTaken(req.body.email);
const user = await services.createUser(req);
await checkEmailTaken(req.body.email);
const user = await createUser(req);
await user.save();
const contact = services.composeNewContact(user);
const contact = composeNewContact(user);
agileAPI.contactAPI.add(contact, null, null);
res.sendStatus(201);
} catch (e) {
services.handleError(e, res);
handleError(e, res);
}
});
// CHANGE PASSWORD
router.post("/changepass", async (req, res) => {
try {
if (!req.body.password || !req.body.email || !req.body.newPass) {
throw newError("No input data", 204);
}
const token = req.headers["x-auth-token"];
validateUserToken(token);
const user = await fetchUser(req.body.email);
await checkPassword(req.body.password, user.password);
const newPassword = await hashPass(req.body.newPass);
await changeUserPass(user._id, newPassword);
res.status(200).send("Password changed");
} catch (error) {
handleError(error, res);
}
});
// RESET PASSWORD
router.post("/resetpassword", (req, res) => {
try {
//
} catch (error) {
handleError(error, res);
}
});