Cleanup
This commit is contained in:
@@ -9,7 +9,7 @@ const { deleteImage } = require("./oceanServices.js");
|
||||
async function createUser(request) {
|
||||
const password = await hashPass(request.body.password);
|
||||
let user;
|
||||
if(request.body.isRestaurant === true){
|
||||
if (request.body.isRestaurant === true) {
|
||||
user = new User({
|
||||
_id: new mongoose.Types.ObjectId(),
|
||||
email: request.body.email,
|
||||
@@ -81,7 +81,7 @@ async function createRestaurant(request, oldRestaurant) {
|
||||
links: request.links,
|
||||
phone: request.phone,
|
||||
hidden: request.hidden,
|
||||
indexed: new Date()
|
||||
indexed: new Date(),
|
||||
});
|
||||
return restaurant;
|
||||
} else {
|
||||
@@ -109,7 +109,7 @@ async function createRestaurant(request, oldRestaurant) {
|
||||
phone: request.phone,
|
||||
hidden: request.hidden,
|
||||
indexed: request.indexed,
|
||||
ratings: request.ratings
|
||||
ratings: request.ratings,
|
||||
});
|
||||
return restaurant;
|
||||
}
|
||||
@@ -182,43 +182,7 @@ async function createDish(dish, restaurantId, oldDish) {
|
||||
}
|
||||
}
|
||||
|
||||
function appendDishToLunchSet(lunchMenu, setName, dishId, quantity) {
|
||||
console.log("append called")
|
||||
const result = lunchMenu.map((lunchSet) => {
|
||||
if (lunchSet.lunchSetName === setName) {
|
||||
let updatedSet = lunchSet;
|
||||
let dishToAdd = {
|
||||
dishId: dishId,
|
||||
quantity: quantity
|
||||
}
|
||||
updatedSet.lunchSetDishes.push(dishToAdd);
|
||||
return updatedSet;
|
||||
} else {
|
||||
return lunchSet;
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
function removeDishFromLunchSet(lunchMenu, setName, dishId) {
|
||||
const result = lunchMenu.map((lunchSet) => {
|
||||
if (lunchSet.lunchSetName === setName) {
|
||||
let updatedSet = lunchSet;
|
||||
const index = updatedSet.lunchSetDishes.findIndex(dish => dish.dishId === dishId);
|
||||
if (index > -1) {
|
||||
updatedSet.lunchSetDishes.splice(index, 1);
|
||||
}
|
||||
return updatedSet;
|
||||
} else {
|
||||
return lunchSet;
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
exports.createUser = createUser;
|
||||
exports.createRestaurant = createRestaurant;
|
||||
exports.prepareSafeUser = prepareSafeUser;
|
||||
exports.createDish = createDish;
|
||||
exports.appendDishToLunchSet = appendDishToLunchSet;
|
||||
exports.removeDishFromLunchSet = removeDishFromLunchSet;
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
const Restaurant = require("../models/restaurant.js");
|
||||
const Dish = require("../models/dish.js");
|
||||
const User = require("../models/users.js");
|
||||
const Report = require("../models/reports.js")
|
||||
const Report = require("../models/reports.js");
|
||||
const { deleteImage } = require("./oceanServices.js");
|
||||
const { newError } = require("./services.js");
|
||||
|
||||
async function changeUserPass(userId, newPass) {
|
||||
User.findByIdAndUpdate(userId, { $set: { password: newPass } }).catch((e) => {
|
||||
throw newError("Zmiana hasła nie powiodła się.", 500);
|
||||
});
|
||||
}
|
||||
// REMOVE DISH
|
||||
|
||||
async function removeDish(dishId) {
|
||||
const deletedDoc = await Dish.findByIdAndDelete(dishId).catch((e) => {
|
||||
@@ -23,6 +19,8 @@ async function removeDish(dishId) {
|
||||
});
|
||||
}
|
||||
|
||||
// REMOVE RESTAURANT
|
||||
|
||||
async function removeRestaurant(restaurantId, userId) {
|
||||
const deletedDoc = await Restaurant.findByIdAndDelete(restaurantId).catch(
|
||||
(e) => {
|
||||
@@ -46,6 +44,8 @@ async function removeRestaurant(restaurantId, userId) {
|
||||
});
|
||||
}
|
||||
|
||||
// ADD DISH TO RESTAURANT
|
||||
|
||||
async function addDishToRestaurant(restaurantId, dishId) {
|
||||
await Restaurant.updateOne(
|
||||
{ _id: restaurantId },
|
||||
@@ -55,6 +55,8 @@ async function addDishToRestaurant(restaurantId, dishId) {
|
||||
});
|
||||
}
|
||||
|
||||
// ADD RESTAURANT
|
||||
|
||||
async function addRestaurantToUser(user, restaurant) {
|
||||
await User.findByIdAndUpdate(user.id, {
|
||||
$push: { restaurants: restaurant._id },
|
||||
@@ -63,6 +65,8 @@ async function addRestaurantToUser(user, restaurant) {
|
||||
});
|
||||
}
|
||||
|
||||
// CHECK IF CATEGORY EXISTS
|
||||
|
||||
async function checkIfCategoryExists(restaurant, category) {
|
||||
const categories = restaurant.categories;
|
||||
if (categories.includes(category)) {
|
||||
@@ -70,27 +74,7 @@ async function checkIfCategoryExists(restaurant, category) {
|
||||
}
|
||||
}
|
||||
|
||||
async function checkIfSetAlreadyInLunchMenu(restaurant, setName) {
|
||||
const lunchMenu = restaurant.lunchMenu;
|
||||
for (lunchSet of lunchMenu) {
|
||||
if (lunchSet.lunchSetName === setName) {
|
||||
throw newError("Nazwa zestawu jest zajęta", 409);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
async function checkIfAlreadyInSet(restaurant, setName, dishId) {
|
||||
const lunchMenu = restaurant.lunchMenu;
|
||||
for (const lunchSet of lunchMenu) {
|
||||
if (lunchSet.lunchSetName === setName) {
|
||||
const dishes = lunchSet.lunchSetDishes;
|
||||
if (dishes.includes(dishId)) {
|
||||
throw newError("Danie jest już w podanym zestawie", 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// ADD OR REMOVE CATEGORY
|
||||
|
||||
async function changeCategory(restaurantId, categoryName, action) {
|
||||
if (action === "add") {
|
||||
@@ -114,6 +98,8 @@ async function changeCategory(restaurantId, categoryName, action) {
|
||||
}
|
||||
}
|
||||
|
||||
// SET DISH VISIBILITY
|
||||
|
||||
async function setDishVisibility(dishId, visible) {
|
||||
await Dish.findByIdAndUpdate(dishId, { $set: { hidden: !visible } }).catch(
|
||||
(e) => {
|
||||
@@ -122,99 +108,7 @@ async function setDishVisibility(dishId, visible) {
|
||||
);
|
||||
}
|
||||
|
||||
async function changeLunchMenuSet(restaurantId, action, lunchSet) {
|
||||
if (action === "add") {
|
||||
const restaurant = await Restaurant.findById(restaurantId).catch((err) => {
|
||||
throw newError("Nie udało się pobrać restauracji.", 404);
|
||||
});
|
||||
await checkIfSetAlreadyInLunchMenu(restaurant, lunchSet.lunchSetName);
|
||||
await Restaurant.findByIdAndUpdate(restaurantId, {
|
||||
$push: { lunchMenu: lunchSet },
|
||||
}).catch((e) => {
|
||||
throw newError("Nie udało się dodać zestawu do lunch menu.", 500);
|
||||
});
|
||||
} else if (action === "delete") {
|
||||
await Restaurant.findByIdAndUpdate(restaurantId, {
|
||||
$pull: { lunchMenu: lunchSet },
|
||||
}).catch((e) => {
|
||||
throw newError("Nie udało się usunąć zestawu.", 500);
|
||||
});
|
||||
} else {
|
||||
throw newError("Nie sprecyzowano akcji", 500);
|
||||
}
|
||||
}
|
||||
|
||||
function appendDishToLunchSet(lunchMenu, setName, dishId, quantity) {
|
||||
const result = lunchMenu.map((lunchSet) => {
|
||||
if (lunchSet.lunchSetName === setName) {
|
||||
let updatedSet = lunchSet;
|
||||
let dishToAdd = {
|
||||
dishId: dishId,
|
||||
quantity: quantity
|
||||
}
|
||||
updatedSet.lunchSetDishes.push(dishToAdd);
|
||||
return updatedSet;
|
||||
} else {
|
||||
return lunchSet;
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
function removeDishFromLunchSet(lunchMenu, setName, dishId) {
|
||||
console.log("remove called")
|
||||
const result = lunchMenu.map((lunchSet) => {
|
||||
if (lunchSet.lunchSetName === setName) {
|
||||
let updatedSet = lunchSet;
|
||||
const index = updatedSet.lunchSetDishes.findIndex((dish) => {
|
||||
return dish.dishId.toString() === dishId.toString();
|
||||
});
|
||||
if (index > -1) {
|
||||
updatedSet.lunchSetDishes.splice(index, 1);
|
||||
}
|
||||
return updatedSet;
|
||||
} else {
|
||||
return lunchSet;
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
async function changeLunchMenu(restaurantId, setName, dishId, quantity, action) {
|
||||
if (action === "add") {
|
||||
const restaurant = await Restaurant.findById(restaurantId).catch((err) => {
|
||||
throw newError("Nie udało się pobrać restauracji.", 404);
|
||||
});
|
||||
await checkIfAlreadyInSet(restaurant, setName, dishId);
|
||||
const updatedLunchMenu = appendDishToLunchSet(
|
||||
restaurant.lunchMenu,
|
||||
setName,
|
||||
dishId,
|
||||
quantity
|
||||
);
|
||||
await Restaurant.findByIdAndUpdate(restaurantId, {
|
||||
$set: { lunchMenu: updatedLunchMenu },
|
||||
}).catch((e) => {
|
||||
throw newError("Nie udało się dodać dania do lunch menu.", 500);
|
||||
});
|
||||
} else if (action === "delete") {
|
||||
const restaurant = await Restaurant.findById(restaurantId).catch((err) => {
|
||||
throw newError("Nie udało się pobrać restauracji.", 404);
|
||||
});
|
||||
const updatedLunchMenu = removeDishFromLunchSet(
|
||||
restaurant.lunchMenu,
|
||||
setName,
|
||||
dishId
|
||||
);
|
||||
await Restaurant.findByIdAndUpdate(restaurantId, {
|
||||
$set: { lunchMenu: updatedLunchMenu },
|
||||
}).catch((e) => {
|
||||
throw newError("Nie udało się usunąć dania.", 500);
|
||||
});
|
||||
} else {
|
||||
throw newError("Nie sprecyzowano akcji", 500);
|
||||
}
|
||||
}
|
||||
// FETCH RESTAURANT
|
||||
|
||||
async function fetchRestaurant(id) {
|
||||
const data = await Restaurant.findById(id).catch((e) => {
|
||||
@@ -223,17 +117,23 @@ async function fetchRestaurant(id) {
|
||||
return data;
|
||||
}
|
||||
|
||||
// FETCH MULTIPLE RESTAURANTS
|
||||
|
||||
async function fetchMultipleRestaurants(idArray) {
|
||||
let data = await Restaurant.find().where('_id').in(idArray).exec();
|
||||
let data = await Restaurant.find().where("_id").in(idArray).exec();
|
||||
return data;
|
||||
}
|
||||
|
||||
// FETCH DISHES FOR RESTAURANT
|
||||
|
||||
async function fetchAllDishesForRestaurant(restaurant) {
|
||||
const idList = restaurant.dishes;
|
||||
const dishes = await Dish.find({ '_id': { $in: idList } });
|
||||
const dishes = await Dish.find({ _id: { $in: idList } });
|
||||
return dishes;
|
||||
}
|
||||
|
||||
// FETCH SINGLE DISH
|
||||
|
||||
async function fetchDish(id) {
|
||||
let data = await Dish.findById(id).catch((e) => {
|
||||
throw newError(`Nie udało się pobrać ${id}`, 404);
|
||||
@@ -241,6 +141,8 @@ async function fetchDish(id) {
|
||||
return data;
|
||||
}
|
||||
|
||||
// FETCH USER BY EMAIL
|
||||
|
||||
async function fetchUser(email) {
|
||||
if (!email) throw newError("Brak danych", 204);
|
||||
const user = await User.findOne({ email: email });
|
||||
@@ -248,35 +150,42 @@ async function fetchUser(email) {
|
||||
return user;
|
||||
}
|
||||
|
||||
// SET RESTAURANT VISIBLE
|
||||
|
||||
async function setRestaurantVisibility(restaurantId, visible) {
|
||||
await Restaurant.findByIdAndUpdate(restaurantId, { $set: { hidden: !visible } }).catch(
|
||||
(e) => {
|
||||
throw newError("Nie udało się zmienić dania.", 500);
|
||||
}
|
||||
);
|
||||
await Restaurant.findByIdAndUpdate(restaurantId, {
|
||||
$set: { hidden: !visible },
|
||||
}).catch((e) => {
|
||||
throw newError("Nie udało się zmienić dania.", 500);
|
||||
});
|
||||
}
|
||||
|
||||
async function fetchAllAdminData(){
|
||||
const restaurants = await Restaurant.find({}, "_id name city adress subscriptionActive subscriptionDue phone dishes");
|
||||
// FETCH ALL DATA
|
||||
|
||||
async function fetchAllAdminData() {
|
||||
const restaurants = await Restaurant.find(
|
||||
{},
|
||||
"_id name city adress subscriptionActive subscriptionDue phone dishes"
|
||||
);
|
||||
const reports = await Report.find({});
|
||||
const users = await User.find({}, "_id email firstname lastname login billing isRestaurant restaurants trialUsed photos");
|
||||
const users = await User.find(
|
||||
{},
|
||||
"_id email firstname lastname login billing isRestaurant restaurants trialUsed photos"
|
||||
);
|
||||
const result = {
|
||||
restaurants: restaurants,
|
||||
reports: reports,
|
||||
users: users
|
||||
}
|
||||
users: users,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
exports.changeUserPass = changeUserPass;
|
||||
exports.removeDish = removeDish;
|
||||
exports.removeRestaurant = removeRestaurant;
|
||||
exports.addDishToRestaurant = addDishToRestaurant;
|
||||
exports.addRestaurantToUser = addRestaurantToUser;
|
||||
exports.changeCategory = changeCategory;
|
||||
exports.setDishVisibility = setDishVisibility;
|
||||
exports.changeLunchMenuSet = changeLunchMenuSet;
|
||||
exports.changeLunchMenu = changeLunchMenu;
|
||||
exports.fetchRestaurant = fetchRestaurant;
|
||||
exports.fetchMultipleRestaurants = fetchMultipleRestaurants;
|
||||
exports.fetchAllDishesForRestaurant = fetchAllDishesForRestaurant;
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
const nodemailer = require("nodemailer");
|
||||
const path = require("path");
|
||||
const { MAIL_PASS } = require("../config/index.js");
|
||||
const makeResetPassMessage = require("../config/mailTemplateReset.js");
|
||||
const {
|
||||
newError,
|
||||
generatePasswordResetLink,
|
||||
} = require("../services/services.js");
|
||||
const { newError } = require("../services/services.js");
|
||||
|
||||
const images = path.resolve("images");
|
||||
|
||||
@@ -35,18 +31,3 @@ async function sendMail(reciever, subject, textMessage, htmlMessage) {
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
async function resetPassword(email) {
|
||||
const resetLink = generatePasswordResetLink(email);
|
||||
const message = makeResetPassMessage(resetLink);
|
||||
await sendMail(
|
||||
email,
|
||||
"Menui - Resetowanie hasła",
|
||||
message.text,
|
||||
message.html
|
||||
).catch((err) => {
|
||||
throw newError("Nieznany błąd podczas resetu hasła", 500);
|
||||
});
|
||||
}
|
||||
|
||||
exports.resetPassword = resetPassword;
|
||||
|
||||
@@ -4,10 +4,18 @@ const User = require("../models/users.js");
|
||||
const mongoose = require("mongoose");
|
||||
const sanitizer = require("string-sanitizer");
|
||||
const { renameBlob } = require("./oceanServices.js");
|
||||
const jwt = require("jsonwebtoken");
|
||||
const bcrypt = require("bcryptjs");
|
||||
const crypto = require("crypto")
|
||||
const { jwtSecret, publicKey } = require("../config/index.js");
|
||||
|
||||
// AUTH
|
||||
|
||||
function authenticate() {
|
||||
auth({
|
||||
audience: "https://menui.pl/api",
|
||||
issuerBaseURL: "https://bankaisoftware.eu.auth0.com/",
|
||||
tokenSigningAlg: "RS256",
|
||||
});
|
||||
}
|
||||
|
||||
// NEW ERROR
|
||||
|
||||
function newError(message, status) {
|
||||
const error = {
|
||||
@@ -17,6 +25,8 @@ function newError(message, status) {
|
||||
return error;
|
||||
}
|
||||
|
||||
// HANDLE ERROR
|
||||
|
||||
function handleError(error, responseObject) {
|
||||
if (!error.status) {
|
||||
console.log(error);
|
||||
@@ -26,6 +36,8 @@ function handleError(error, responseObject) {
|
||||
}
|
||||
}
|
||||
|
||||
// VALIDATE RESTAURANT ID
|
||||
|
||||
async function validateRestaurant(id) {
|
||||
if (!mongoose.Types.ObjectId.isValid(id))
|
||||
throw newError("Nieprawidłowy ID", 204);
|
||||
@@ -34,87 +46,14 @@ async function validateRestaurant(id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// DECODE AND SANITIZE URL
|
||||
|
||||
function decodeAndSanitize(query) {
|
||||
if (!query) throw newError("Brak danych.", 204);
|
||||
return sanitizer.sanitize.keepUnicode(decodeURI(query));
|
||||
}
|
||||
|
||||
async function checkPassword(password, hash) {
|
||||
const result = await bcrypt.compare(password, hash);
|
||||
if (!result) throw newError("Hasło nieprawidłowe", 403);
|
||||
}
|
||||
|
||||
function generateAuthToken(user) {
|
||||
const token = jwt.sign(
|
||||
{
|
||||
email: user.email,
|
||||
id: user.id,
|
||||
},
|
||||
jwtSecret,
|
||||
{ expiresIn: "15m" }
|
||||
);
|
||||
return token;
|
||||
}
|
||||
|
||||
function generateRefreshToken(user) {
|
||||
const token = jwt.sign({
|
||||
email: user.email,
|
||||
id: user.id,
|
||||
}, jwtSecret, {
|
||||
expiresIn: "1h"
|
||||
});
|
||||
return token;
|
||||
}
|
||||
|
||||
function generatePasswordResetToken(email) {
|
||||
const token = jwt.sign(
|
||||
{
|
||||
email: email,
|
||||
},
|
||||
jwtSecret,
|
||||
{ expiresIn: "30m" }
|
||||
);
|
||||
return token;
|
||||
}
|
||||
|
||||
// SET FRONTEND URL HERE
|
||||
|
||||
function generatePasswordResetLink(email) {
|
||||
const token = generatePasswordResetToken(email);
|
||||
const link = `https://www.menui.pl/resetpassword?token=${token}`;
|
||||
return link;
|
||||
}
|
||||
|
||||
async function checkEmailTaken(email) {
|
||||
if (!email) throw newError("Brak adresu email", 204);
|
||||
await User.exists({ email: email }).then((res) => {
|
||||
if (res) {
|
||||
throw newError("Adres email zajęty", 409);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function validateUserToken(token) {
|
||||
if (!token) throw newError("Brak dostępu", 401);
|
||||
try {
|
||||
const verified = jwt.verify(token, jwtSecret, { ignoreExpiration: false });
|
||||
if (!verified) throw newError("Brak dostępu", 401);
|
||||
return verified;
|
||||
} catch (error) {
|
||||
throw newError("Brak dostępu", 401);
|
||||
}
|
||||
}
|
||||
|
||||
function validateRefreshToken(token) {
|
||||
if (!token) throw newError("Brak dostępu", 401);
|
||||
try {
|
||||
const verified = jwt.verify(token, jwtSecret, { ignoreExpiration: false });
|
||||
if (!verified) throw newError("Brak dostępu", 401);
|
||||
return verified;
|
||||
} catch (error) {
|
||||
throw newError("Brak dostępu", 401);
|
||||
}
|
||||
}
|
||||
// VALIDATE DISH ID
|
||||
|
||||
async function validateDishId(id) {
|
||||
if (!mongoose.Types.ObjectId.isValid(id)) {
|
||||
@@ -124,6 +63,8 @@ async function validateDishId(id) {
|
||||
if (!dishDoesExist) throw newError("Te danie nie istnieje w bazie.", 404);
|
||||
}
|
||||
|
||||
// VERIFY DISH ACCESS
|
||||
|
||||
async function verifyDishAccess(dishId, decodedToken) {
|
||||
const fetch = await User.findById(decodedToken.id, "restaurants").catch(
|
||||
(error) => {
|
||||
@@ -138,6 +79,8 @@ async function verifyDishAccess(dishId, decodedToken) {
|
||||
if (!valid) throw newError("Nie masz dostępu do tego dania.", 401);
|
||||
}
|
||||
|
||||
// VERIFY RESTAURANT ACCESS
|
||||
|
||||
async function verifyRestaurantAccess(restaurantId, decodedToken) {
|
||||
const fetch = await User.findById(decodedToken.id, "restaurants").catch(
|
||||
(error) => {
|
||||
@@ -149,6 +92,8 @@ async function verifyRestaurantAccess(restaurantId, decodedToken) {
|
||||
if (!valid) throw newError("Nie masz dostępu do tej restauracji.", 401);
|
||||
}
|
||||
|
||||
// YEAR FROM NOW DATE
|
||||
|
||||
function yearFromNowDate() {
|
||||
Date.prototype.addDays = function (days) {
|
||||
var date = new Date(this.valueOf());
|
||||
@@ -159,38 +104,18 @@ function yearFromNowDate() {
|
||||
return date.addDays(365);
|
||||
}
|
||||
|
||||
async function hashPass(pass) {
|
||||
if (pass.length < 6) {
|
||||
throw newError("Hasło za krótkie.", 400);
|
||||
}
|
||||
try {
|
||||
const salt = await bcrypt.genSalt(10);
|
||||
const hash = await bcrypt.hash(pass, salt);
|
||||
return hash;
|
||||
} catch (error) {
|
||||
throw newError("Błąd", 500);
|
||||
}
|
||||
}
|
||||
|
||||
async function saveImage(url) {
|
||||
const newURL = await renameBlob(url);
|
||||
return newURL;
|
||||
}
|
||||
|
||||
exports.authenticate = authenticate;
|
||||
exports.newError = newError;
|
||||
exports.handleError = handleError;
|
||||
exports.validateRestaurant = validateRestaurant;
|
||||
exports.decodeAndSanitize = decodeAndSanitize;
|
||||
exports.checkPassword = checkPassword;
|
||||
exports.generateAuthToken = generateAuthToken;
|
||||
exports.generatePasswordResetLink = generatePasswordResetLink;
|
||||
exports.checkEmailTaken = checkEmailTaken;
|
||||
exports.validateUserToken = validateUserToken;
|
||||
exports.validateDishId = validateDishId;
|
||||
exports.verifyDishAccess = verifyDishAccess;
|
||||
exports.verifyRestaurantAccess = verifyRestaurantAccess;
|
||||
exports.yearFromNowDate = yearFromNowDate;
|
||||
exports.hashPass = hashPass;
|
||||
exports.saveImage = saveImage;
|
||||
exports.generateRefreshToken = generateRefreshToken;
|
||||
exports.validateRefreshToken = validateRefreshToken;
|
||||
|
||||
@@ -1,74 +1,79 @@
|
||||
const validator = require('validator');
|
||||
const validator = require("validator");
|
||||
const { newError } = require("./services.js");
|
||||
|
||||
const validateLogin = function(requestBody){
|
||||
const email = validator.isEmail(requestBody.email) && validator.isLength(requestBody.email, { max: 64 })
|
||||
const password = validator.isLength(requestBody.password, { max: 64 });
|
||||
if(!email || !password){
|
||||
throw newError("Dane logowania nieprawidłowe :/", 403);
|
||||
const validateRegister = function (requestBody) {
|
||||
if (requestBody.isRestaurant) {
|
||||
const email =
|
||||
validator.isEmail(requestBody.email) &&
|
||||
validator.isLength(requestBody.email, { max: 64 });
|
||||
const password = validator.isLength(requestBody.password, {
|
||||
min: 6,
|
||||
max: 64,
|
||||
});
|
||||
const firstname = validator.isLength(requestBody.firstname, {
|
||||
min: 1,
|
||||
max: 24,
|
||||
});
|
||||
const lastname = validator.isLength(requestBody.lastname, {
|
||||
min: 1,
|
||||
max: 24,
|
||||
});
|
||||
const NIP = validator.isLength(requestBody.NIP, { min: 10, max: 20 });
|
||||
const adress = validator.isLength(requestBody.adress, { min: 2, max: 64 });
|
||||
const companyName = validator.isLength(requestBody.companyName, {
|
||||
min: 2,
|
||||
max: 64,
|
||||
});
|
||||
if (
|
||||
!email ||
|
||||
!password ||
|
||||
!firstname ||
|
||||
!lastname ||
|
||||
!NIP ||
|
||||
!adress ||
|
||||
!companyName
|
||||
) {
|
||||
throw newError("Dane nieprawidłowe", 400);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const email =
|
||||
validator.isEmail(requestBody.email) &&
|
||||
validator.isLength(requestBody.email, { max: 64 });
|
||||
const password = validator.isLength(requestBody.password, {
|
||||
min: 6,
|
||||
max: 64,
|
||||
});
|
||||
const login = validator.isLength(requestBody.login, { min: 2, max: 64 });
|
||||
if (!email || !password || !login) {
|
||||
throw newError("Dane nieprawidłowe", 400);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const validatePassword = function(pass){
|
||||
const password = validator.isLength(pass, { min: 2, max: 64 });
|
||||
}
|
||||
const validateSearch = function (string) {
|
||||
const decodedString = decodeURI(string);
|
||||
const valid = validator.isLength(decodedString, { max: 64 });
|
||||
if (!valid) {
|
||||
throw newError("Niepoprawne zapytanie", 400);
|
||||
}
|
||||
};
|
||||
|
||||
const validateRegister = function(requestBody){
|
||||
if(requestBody.isRestaurant){
|
||||
const email = validator.isEmail(requestBody.email) && validator.isLength(requestBody.email, { max: 64 })
|
||||
const password = validator.isLength(requestBody.password, { min:6, max:64 });
|
||||
const firstname = validator.isLength(requestBody.firstname, { min:1, max:24 });
|
||||
const lastname = validator.isLength(requestBody.lastname, { min:1, max:24 });
|
||||
const NIP = validator.isLength(requestBody.NIP, { min:10, max:20 });
|
||||
const adress = validator.isLength(requestBody.adress, { min:2, max:64 });
|
||||
const companyName = validator.isLength(requestBody.companyName, { min:2, max:64 });
|
||||
if(!email || !password || !firstname || !lastname || !NIP || !adress || !companyName) {
|
||||
throw newError("Dane nieprawidłowe", 400)
|
||||
}
|
||||
} else {
|
||||
const email = validator.isEmail(requestBody.email) && validator.isLength(requestBody.email, { max: 64 })
|
||||
const password = validator.isLength(requestBody.password, { min:6, max:64 });
|
||||
const login = validator.isLength(requestBody.login, { min:2, max:64 });
|
||||
if(!email || !password || !login) {
|
||||
throw newError("Dane nieprawidłowe", 400)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const validateSearch = function(string){
|
||||
const decodedString = decodeURI(string);
|
||||
const valid = validator.isLength(decodedString, { max: 64 })
|
||||
if(!valid){
|
||||
throw newError("Niepoprawne zapytanie", 400)
|
||||
}
|
||||
}
|
||||
|
||||
const validateRestaurantData = function(requestBody){
|
||||
const name = validator.isLength(requestBody.name, { max: 64 })
|
||||
const city = validator.isLength(requestBody.city, { max: 64 })
|
||||
const adress = validator.isLength(requestBody.adress, { max: 64 })
|
||||
const type = validator.isLength(requestBody.type, { max: 64 })
|
||||
let description = true;
|
||||
if(requestBody.description){
|
||||
description = validator.isLength(requestBody.description, { max: 400 })
|
||||
}
|
||||
if(!name || !city || !adress || !type || !description){
|
||||
throw newError("Dane nieprawidłowe", 400)
|
||||
}
|
||||
}
|
||||
|
||||
const validateLunchSet = function(set){
|
||||
const name = validator.isLength(set.lunchSetName, { min: 2, max: 64 })
|
||||
if(!name){
|
||||
throw newError("Nieprawidłowe dane", 400)
|
||||
}
|
||||
}
|
||||
const validateRestaurantData = function (requestBody) {
|
||||
const name = validator.isLength(requestBody.name, { max: 64 });
|
||||
const city = validator.isLength(requestBody.city, { max: 64 });
|
||||
const adress = validator.isLength(requestBody.adress, { max: 64 });
|
||||
const type = validator.isLength(requestBody.type, { max: 64 });
|
||||
let description = true;
|
||||
if (requestBody.description) {
|
||||
description = validator.isLength(requestBody.description, { max: 400 });
|
||||
}
|
||||
if (!name || !city || !adress || !type || !description) {
|
||||
throw newError("Dane nieprawidłowe", 400);
|
||||
}
|
||||
};
|
||||
|
||||
// EXPORTS
|
||||
|
||||
exports.validateLogin = validateLogin;
|
||||
exports.validateRegister = validateRegister;
|
||||
exports.validatePassword = validatePassword;
|
||||
exports.validateSearch = validateSearch;
|
||||
exports.validateRestaurantData = validateRestaurantData;
|
||||
exports.validateLunchSet = validateLunchSet;
|
||||
Reference in New Issue
Block a user