server 1.0.3

This commit is contained in:
2020-09-19 18:16:50 +02:00
parent 36dd3d8238
commit d7395f5472
8 changed files with 35 additions and 13 deletions

View File

@@ -39,6 +39,8 @@ const dishSchema = mongoose.Schema({
ingredients: {
type: [String],
},
glicemicIndex: String,
kCal: String,
vegan: Boolean,
vegetarian: Boolean,
});

View File

@@ -50,11 +50,10 @@ const restaurantSchema = mongoose.Schema({
},
links: {
facebook: String,
twitter: String,
instagram: String,
www: String,
},
phone: Number,
phone: String,
hidden: Boolean,
subscriptionActive: Boolean,
subscriptionStarted: Date,

View File

@@ -38,7 +38,6 @@ const userSchema = mongoose.Schema({
},
},
restaurants: [mongoose.Types.ObjectId],
trialUsed: Boolean,
});
export default mongoose.model("User", userSchema);

View File

@@ -14,7 +14,7 @@ const uploadStrategy = multer({
}
cb(null, true);
},
limits: { fileSize: 4000000 },
limits: { fileSize: 2000000 },
}).single("menuiImage");
// POST

View File

@@ -12,6 +12,7 @@ import {
handleError,
validateUserToken,
verifyRestaurantAccess,
newError,
} from "../services/services.js";
import Restaurant from "../models/restaurant.js";
@@ -35,7 +36,9 @@ router.post("/", async (req, res) => {
try {
const token = req.headers["x-auth-token"];
const user = validateUserToken(token);
const restaurant = await createRestaurant(req.body);
const restaurant = await createRestaurant(req.body).catch((err) => {
throw newError("Nie udało się zapisać zdjęcia.", 500);
});
await restaurant.save();
await addRestaurantToUser(user, restaurant);
res.sendStatus(201);

View File

@@ -22,7 +22,7 @@ router.post("/login", async (req, res) => {
}
const user = await fetchUser(req.body.email);
await checkPassword(req.body.password, user.password);
const safeUser = prepareSafeUser(user);
const safeUser = await prepareSafeUser(user);
var token = generateAuthToken(safeUser);
res.header("x-auth-token", token).status(202).send(safeUser);
} catch (error) {

View File

@@ -4,6 +4,7 @@ import mongoose from "mongoose";
import Dish from "../models/dish.js";
import User from "../models/users.js";
import Restaurant from "../models/restaurant.js";
import { fetchMultipleRestaurants } from "./databaseServices.js";
export async function createUser(request) {
const password = await hashPass(request.body.password);
@@ -62,7 +63,11 @@ export async function createRestaurant(request, oldRestaurant) {
workingHours: request.workingHours,
description: sanitizer.sanitize.keepUnicode(request.description),
tags: request.tags,
links: request.links,
links: {
facebook: request.facebook,
instagram: request.instagram,
www: request.www,
},
phone: request.phone,
hidden: request.hidden,
});
@@ -95,13 +100,17 @@ export async function createRestaurant(request, oldRestaurant) {
}
}
export function prepareSafeUser(user) {
export async function prepareSafeUser(user) {
const restaurants = await fetchMultipleRestaurants(user.restaurants);
const safeUser = {
firstname: user.firstname,
lastname: user.lastname,
email: user.email,
id: user._id,
restaurants: user.restaurants,
restaurants: restaurants,
NIP: user.billing.NIP,
adress: user.billing.adress,
companyName: user.billing.companyName,
};
return safeUser;
}
@@ -129,6 +138,8 @@ export async function createDish(dish, restaurantId, generateId) {
sesame: dish.allergens.sesame,
},
ingredients: dish.ingredients,
glicemicIndex: dish.glicemicIndex,
kCal: dish.kCal,
vegan: dish.vegan,
vegetarian: dish.vegetarian,
});
@@ -152,6 +163,8 @@ export async function createDish(dish, restaurantId, generateId) {
sesame: dish.allergens.sesame,
},
ingredients: dish.ingredients,
glicemicIndex: dish.glicemicIndex,
kCal: dish.kCal,
vegan: dish.vegan,
vegetarian: dish.vegetarian,
});

View File

@@ -105,15 +105,21 @@ export async function renewSubscription(restaurantId, monthsToAdd) {
}
export async function fetchRestaurant(id) {
let data;
await Restaurant.findById(id, (err, result) => {
data = result;
}).catch((e) => {
const data = await Restaurant.findById(id).catch((e) => {
throw newError("Nie udało się pobrać restauracji.", 500);
});
return data;
}
export async function fetchMultipleRestaurants(idArray) {
let data = [];
for (const id of idArray) {
let response = await fetchRestaurant(id);
data.push(response);
}
return data;
}
export async function fetchAllDishesForRestaurant(restaurant) {
let dishes = [];
for (const dish of restaurant.dishes) {