split services / delete dish / mail init
This commit is contained in:
@@ -46,6 +46,7 @@
|
||||
- ### **Dish**
|
||||
|
||||
- ##### **\_id**: _mongoose.Types.ObjectId_
|
||||
- ##### **restaurantId**: _mongoose.Types.ObjectId_
|
||||
- ##### **name**: _String_ (max: 128, required)
|
||||
- ##### **category**: _String_ (max: 64, required)
|
||||
- ##### **price**: _Number_ (required)
|
||||
@@ -87,9 +88,9 @@
|
||||
- #### **POST**
|
||||
Takes in **restaurantId**, **dish** document, and a JWT **token (header)** as parameters and tries to create a new dish document inside a database. Returns **201** on success. Else returns **401** on bad token, or **400** on wrong **restaurantId**.
|
||||
- #### **PUT**
|
||||
|
||||
Takes in **dishId**, **dish** document, and a JWT **token (header)** and tries to update specified document in a database. Returns **304** on success. Else returns **204** on bad document, or **401** on bad token.
|
||||
|
||||
Takes in **dishId**, **restaurantId**, **dish** document, and a JWT **token (header)** and tries to update specified document in a database. Returns **304** on success. Else returns **204** on bad document, or **401** on bad token.
|
||||
- #### **DELETE**
|
||||
Takes in **dishId**, and JWT **token (header)** and tries to remove specified dish from database. If everything goes OK, it returns **200**.
|
||||
<br>
|
||||
|
||||
* ### **/restaurant**
|
||||
|
||||
138
config/mailTemplateReset.js
Normal file
138
config/mailTemplateReset.js
Normal file
@@ -0,0 +1,138 @@
|
||||
export default function makeResetPassMessage(newPass) {
|
||||
return;
|
||||
`<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<link
|
||||
href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap"
|
||||
rel="stylesheet"
|
||||
/>
|
||||
<title>Menui - Resetowanie hasła</title>
|
||||
<style type="text/css">
|
||||
body {
|
||||
font-family: "Montserrat", sans-serif;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
min-width: 100% !important;
|
||||
}
|
||||
.table {
|
||||
margin-top: 26px;
|
||||
max-width: 600px;
|
||||
}
|
||||
.titlebar {
|
||||
text-align: center;
|
||||
}
|
||||
.content {
|
||||
padding-bottom: 20px;
|
||||
color: #ffffff;
|
||||
text-align: center;
|
||||
font-size: 13px;
|
||||
}
|
||||
.content p {
|
||||
margin: 12px;
|
||||
padding-right: 24px;
|
||||
padding-left: 24px;
|
||||
line-height: 1.6;
|
||||
}
|
||||
.logo {
|
||||
margin: 24px;
|
||||
}
|
||||
.footer {
|
||||
font-size: 13px;
|
||||
}
|
||||
hr {
|
||||
border: none;
|
||||
border-top: solid 1px #262626;
|
||||
}
|
||||
.title {
|
||||
font-size: 20px;
|
||||
margin-bottom: 26px;
|
||||
color: #d68000;
|
||||
}
|
||||
.span {
|
||||
color: #262626;
|
||||
padding-top: 10px;
|
||||
padding-bottom: 10px;
|
||||
font-size: 16px;
|
||||
font-weight: 700;
|
||||
background-color: #d68000;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body bgcolor="#262626">
|
||||
<table
|
||||
width="100%"
|
||||
class="table"
|
||||
bgcolor="#454545"
|
||||
border="0"
|
||||
cellpadding="0"
|
||||
cellspacing="0"
|
||||
align="center"
|
||||
>
|
||||
<tr>
|
||||
<td>
|
||||
<table align="center">
|
||||
<tr>
|
||||
<td>
|
||||
<img
|
||||
class="logo"
|
||||
src="../images/logo.svg"
|
||||
width="100"
|
||||
alt="logo"
|
||||
/>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<table width="100%" class="titlebar" bgcolor="#666666">
|
||||
<tr>
|
||||
<td>
|
||||
<h3 class="title">Resetowanie hasła</h3>
|
||||
<hr />
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<table width="100%" class="content" bgcolor="#666666">
|
||||
<tr>
|
||||
<td>
|
||||
<p>
|
||||
Drogi użytkowniku, dostałeś tę wiadomość, ponieważ użyłeś
|
||||
opcji "Nie pamiętam hasła" w aplikacji Menui.
|
||||
</p>
|
||||
<p>
|
||||
Twoje tymczasowe hasło to:
|
||||
<span><h5 class="span">${newPass}</h5></span>
|
||||
</p>
|
||||
<p>
|
||||
Zaloguj się za jego pomocą i ustaw nowe bezpieczne hasło.
|
||||
Jeżeli nie wysyłałeś prośby o zmianę hasła, prosimy zignoruj
|
||||
tę wiadomość.
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<table align="center">
|
||||
<tr>
|
||||
<td>
|
||||
<p class="footer">Pozdrawiamy!</p>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
1
images/logo.svg
Normal file
1
images/logo.svg
Normal file
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 5.2 KiB |
@@ -2,6 +2,7 @@ import mongoose from "mongoose";
|
||||
|
||||
const dishSchema = mongoose.Schema({
|
||||
_id: mongoose.Types.ObjectId,
|
||||
restaurantId: mongoose.Types.ObjectId,
|
||||
name: {
|
||||
type: String,
|
||||
maxlength: 128,
|
||||
@@ -36,7 +37,7 @@ const dishSchema = mongoose.Schema({
|
||||
sesame: Boolean,
|
||||
},
|
||||
ingredients: {
|
||||
type: Array,
|
||||
type: [String],
|
||||
},
|
||||
vegan: Boolean,
|
||||
vegetarian: Boolean,
|
||||
|
||||
@@ -12,6 +12,22 @@ const restaurantSchema = mongoose.Schema({
|
||||
maxlength: 128,
|
||||
required: true,
|
||||
},
|
||||
adress: {
|
||||
type: String,
|
||||
maxlength: 128,
|
||||
required: true,
|
||||
},
|
||||
location: {
|
||||
type: {
|
||||
type: String,
|
||||
enum: ["Point"],
|
||||
required: true,
|
||||
},
|
||||
coordinates: {
|
||||
type: [Number],
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
imgUrl: {
|
||||
type: String,
|
||||
required: true,
|
||||
|
||||
5
package-lock.json
generated
5
package-lock.json
generated
@@ -5985,6 +5985,11 @@
|
||||
"integrity": "sha512-gsO4vjEdQaTusZAEebUWp2a5d7dF5DYoIpDG7WySnk7BuZDW+GPpHXoXXuYawRBr/9t5q54tirPz79kFIWg4dA==",
|
||||
"dev": true
|
||||
},
|
||||
"nodemailer": {
|
||||
"version": "6.4.11",
|
||||
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.4.11.tgz",
|
||||
"integrity": "sha512-BVZBDi+aJV4O38rxsUh164Dk1NCqgh6Cm0rQSb9SK/DHGll/DrCMnycVDD7msJgZCnmVa8ASo8EZzR7jsgTukQ=="
|
||||
},
|
||||
"nodemon": {
|
||||
"version": "2.0.4",
|
||||
"resolved": "https://registry.npmjs.org/nodemon/-/nodemon-2.0.4.tgz",
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"mongoose": "^5.9.22",
|
||||
"multer": "^1.4.2",
|
||||
"nodemailer": "^6.4.11",
|
||||
"nodemon": "^2.0.4",
|
||||
"string-sanitizer": "^1.1.1",
|
||||
"validator": "^13.1.1"
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import azureBlob from "@azure/storage-blob";
|
||||
import getStream from "into-stream";
|
||||
import { newError } from "./services.js";
|
||||
|
||||
// SETUP
|
||||
const containerURL = `https://${process.env.AZURE_STORAGE_ACCOUNT_NAME}.blob.core.windows.net/img/`;
|
||||
@@ -17,14 +18,18 @@ const blobServiceClient = new azureBlob.BlobServiceClient(
|
||||
);
|
||||
|
||||
// CODE
|
||||
export function renameBlob(blobURL) {
|
||||
export async function renameBlob(blobURL) {
|
||||
try {
|
||||
const blobName = blobURL.replace(containerURL, "");
|
||||
const containerClient = blobServiceClient.getContainerClient(container);
|
||||
const tempBlob = containerClient.getBlobClient(blobName);
|
||||
const newBlob = containerClient.getBlobClient(removePrefix(blobName));
|
||||
|
||||
newBlob.syncCopyFromURL(tempBlob.url);
|
||||
await newBlob.syncCopyFromURL(tempBlob.url);
|
||||
return newBlob.url;
|
||||
} catch (e) {
|
||||
throw newError("Unable to save image", 500);
|
||||
}
|
||||
}
|
||||
|
||||
export async function uploadBlob(request, resp) {
|
||||
@@ -50,6 +55,7 @@ export async function uploadBlob(request, resp) {
|
||||
});
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
throw newError("Unable to save image", 500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
138
services/dataPrepServices.js
Normal file
138
services/dataPrepServices.js
Normal file
@@ -0,0 +1,138 @@
|
||||
import { hashPass, newError, saveImage } from "./services.js";
|
||||
import sanitizer from "string-sanitizer";
|
||||
import mongoose from "mongoose";
|
||||
import Dish from "../models/dish.js";
|
||||
import User from "../models/users.js";
|
||||
import Restaurant from "../models/restaurant.js";
|
||||
|
||||
export function composeNewContact(request) {
|
||||
const contact = {
|
||||
lead_score: "100",
|
||||
tags: ["newUser"],
|
||||
properties: [
|
||||
{
|
||||
type: "SYSTEM",
|
||||
name: "first_name",
|
||||
value: request.firstname,
|
||||
},
|
||||
{
|
||||
type: "SYSTEM",
|
||||
name: "last_name",
|
||||
value: request.lastname,
|
||||
},
|
||||
{
|
||||
type: "SYSTEM",
|
||||
name: "email",
|
||||
subtype: "work",
|
||||
value: request.email,
|
||||
},
|
||||
{
|
||||
type: "CUSTOM",
|
||||
name: "UserID",
|
||||
value: request._id,
|
||||
},
|
||||
],
|
||||
};
|
||||
return contact;
|
||||
}
|
||||
|
||||
export async function createUser(request) {
|
||||
const password = await hashPass(request.body.password);
|
||||
const user = new User({
|
||||
_id: new mongoose.Types.ObjectId(),
|
||||
email: request.body.email,
|
||||
password: password,
|
||||
firstname: request.body.firstname,
|
||||
lastname: request.body.lastname,
|
||||
});
|
||||
return user;
|
||||
}
|
||||
|
||||
export function createRestaurant(request) {
|
||||
try {
|
||||
const restaurant = new Restaurant({
|
||||
_id: new mongoose.Types.ObjectId(),
|
||||
name: sanitizer.sanitize.keepUnicode(request.name),
|
||||
city: sanitizer.sanitize.keepUnicode(request.city),
|
||||
adress: sanitizer.sanitize.keepUnicode(request.adress),
|
||||
location: request.location,
|
||||
imgUrl: saveImage(request.imgURL),
|
||||
workingHours: request.workingHours,
|
||||
description: sanitizer.sanitize.keepUnicode(request.description),
|
||||
tags: request.tags,
|
||||
links: request.links,
|
||||
phone: request.phone,
|
||||
hidden: request.hidden,
|
||||
});
|
||||
return restaurant;
|
||||
} catch (error) {
|
||||
throw newError("Invalid input data", 206);
|
||||
}
|
||||
}
|
||||
|
||||
export function prepareSafeUser(user) {
|
||||
const safeUser = {
|
||||
firstname: user.firstname,
|
||||
lastname: user.lastname,
|
||||
email: user.email,
|
||||
id: user._id,
|
||||
restaurants: user.restaurants,
|
||||
};
|
||||
return safeUser;
|
||||
}
|
||||
|
||||
export async function createDish(dish, restaurantId, generateId) {
|
||||
try {
|
||||
if (generateId) {
|
||||
const img = await saveImage(dish.imgUrl);
|
||||
const newDish = new Dish({
|
||||
_id: new mongoose.Types.ObjectId(),
|
||||
restaurantId: restaurantId,
|
||||
name: sanitizer.sanitize.keepUnicode(dish.name),
|
||||
category: dish.category,
|
||||
price: dish.price,
|
||||
notes: sanitizer.sanitize.keepUnicode(dish.notes),
|
||||
imgUrl: img,
|
||||
weight: dish.weight,
|
||||
allergens: {
|
||||
gluten: dish.allergens.gluten,
|
||||
lactose: dish.allergens.lactose,
|
||||
soy: dish.allergens.soy,
|
||||
eggs: dish.allergens.eggs,
|
||||
seaFood: dish.allergens.seaFood,
|
||||
peanuts: dish.allergens.peanuts,
|
||||
sesame: dish.allergens.sesame,
|
||||
},
|
||||
ingredients: dish.ingredients,
|
||||
vegan: dish.vegan,
|
||||
vegetarian: dish.vegetarian,
|
||||
});
|
||||
return newDish;
|
||||
} else {
|
||||
const newDish = new Dish({
|
||||
restaurantId: restaurantId,
|
||||
name: sanitizer.sanitize.keepUnicode(dish.name),
|
||||
category: dish.category,
|
||||
price: dish.price,
|
||||
notes: sanitizer.sanitize.keepUnicode(dish.notes),
|
||||
imgUrl: dish.imgUrl,
|
||||
weight: dish.weight,
|
||||
allergens: {
|
||||
gluten: dish.allergens.gluten,
|
||||
lactose: dish.allergens.lactose,
|
||||
soy: dish.allergens.soy,
|
||||
eggs: dish.allergens.eggs,
|
||||
seaFood: dish.allergens.seaFood,
|
||||
peanuts: dish.allergens.peanuts,
|
||||
sesame: dish.allergens.sesame,
|
||||
},
|
||||
ingredients: dish.ingredients,
|
||||
vegan: dish.vegan,
|
||||
vegetarian: dish.vegetarian,
|
||||
});
|
||||
return newDish;
|
||||
}
|
||||
} catch (e) {
|
||||
throw newError("Cannot create dish", 500);
|
||||
}
|
||||
}
|
||||
73
services/databaseServices.js
Normal file
73
services/databaseServices.js
Normal file
@@ -0,0 +1,73 @@
|
||||
import Restaurant from "../models/restaurant.js";
|
||||
import Dish from "../models/dish.js";
|
||||
import User from "../models/users.js";
|
||||
import mongoose from "mongoose";
|
||||
import sanitizer from "string-sanitizer";
|
||||
import { newError } from "./services.js";
|
||||
|
||||
export async function changeUserPass(userId, newPass) {
|
||||
User.findByIdAndUpdate(userId, { $set: { password: newPass } }).catch((e) => {
|
||||
throw newError("Cannot change password", 500);
|
||||
});
|
||||
}
|
||||
|
||||
export async function removeDish(dishId) {
|
||||
const deletedDoc = await Dish.findByIdAndDelete(dishId).catch((e) => {
|
||||
throw newError("Unable to delete Dish", 500);
|
||||
});
|
||||
await Restaurant.findByIdAndUpdate(deletedDoc.restaurantId, {
|
||||
$pull: { dishes: dishId },
|
||||
}).catch((error) => {
|
||||
throw newError("Unable to remove Dish from restaurant", 500);
|
||||
});
|
||||
}
|
||||
|
||||
export async function addDishToRestaurant(restaurantId, dishId) {
|
||||
await Restaurant.updateOne(
|
||||
{ _id: restaurantId },
|
||||
{ $push: { dishes: dishId } }
|
||||
).catch((error) => {
|
||||
throw newError("Couldn't add dish to restaurant", 500);
|
||||
});
|
||||
}
|
||||
|
||||
export async function addRestaurantToUser(user, restaurant) {
|
||||
await User.findByIdAndUpdate(user.id, {
|
||||
$push: { restaurants: restaurant._id },
|
||||
}).catch((e) => {
|
||||
throw newError("Couldn't add restaurant to user", 500);
|
||||
});
|
||||
}
|
||||
|
||||
export async function fetchRestaurant(id) {
|
||||
let data;
|
||||
await Restaurant.findById(id, (err, result) => {
|
||||
data = result;
|
||||
}).catch((e) => {
|
||||
throw newError("Couldn't fetch restaurant", 500);
|
||||
});
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function fetchAllDishesForRestaurant(restaurant) {
|
||||
let dishes = [];
|
||||
for (const dish of restaurant.dishes) {
|
||||
let res = await fetchDish(dish._id);
|
||||
if (res !== null) dishes.push(res);
|
||||
}
|
||||
return dishes;
|
||||
}
|
||||
|
||||
export async function fetchDish(id) {
|
||||
let data = await Dish.findById(id).catch((e) => {
|
||||
throw newError(`Couldn't fetch ${id}`, 404);
|
||||
});
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function fetchUser(email) {
|
||||
if (!email) throw newError("No input", 204);
|
||||
const user = await User.findOne({ email: email });
|
||||
if (!user) throw newError("No such user...", 404);
|
||||
return user;
|
||||
}
|
||||
2
services/mailServices.js
Normal file
2
services/mailServices.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import nodemailer from "nodemailer";
|
||||
import makeResetPassMessage from "../config/mailTemplateReset";
|
||||
@@ -1,4 +1,5 @@
|
||||
import Restaurant from "../models/restaurant.js";
|
||||
import {} from "./dataPrepServices.js";
|
||||
import Dish from "../models/dish.js";
|
||||
import User from "../models/users.js";
|
||||
import mongoose from "mongoose";
|
||||
@@ -18,7 +19,8 @@ export function newError(message, status) {
|
||||
}
|
||||
|
||||
export function handleError(error, responseObject) {
|
||||
if (!error.message) {
|
||||
if (!error.status) {
|
||||
console.log(error);
|
||||
responseObject.sendStatus(500);
|
||||
} else {
|
||||
responseObject.status(error.status).send(error.message);
|
||||
@@ -32,39 +34,6 @@ export async function validateRestaurant(id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function fetchRestaurant(id) {
|
||||
let data;
|
||||
await Restaurant.findById(id, (err, result) => {
|
||||
data = result;
|
||||
}).catch((e) => {
|
||||
throw newError("Couldn't fetch restaurant", 500);
|
||||
});
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function fetchAllDishesForRestaurant(restaurant) {
|
||||
let dishes = [];
|
||||
for (const dish of restaurant.dishes) {
|
||||
let res = await fetchDish(dish._id);
|
||||
if (res !== null) dishes.push(res);
|
||||
}
|
||||
return dishes;
|
||||
}
|
||||
|
||||
export async function fetchDish(id) {
|
||||
let data = await Dish.findById(id).catch((e) => {
|
||||
throw newError(`Couldn't fetch ${id}`, 404);
|
||||
});
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function fetchUser(email) {
|
||||
if (!email) throw newError("No input", 204);
|
||||
const user = await User.findOne({ email: email });
|
||||
if (!user) throw newError("No such user...", 404);
|
||||
return user;
|
||||
}
|
||||
|
||||
export function decodeAndSanitize(query) {
|
||||
if (!query) throw newError("Nothing to sanitize...", 204);
|
||||
return sanitizer.sanitize.keepUnicode(decodeURI(query));
|
||||
@@ -75,23 +44,13 @@ export async function checkPassword(password, hash) {
|
||||
if (!result) throw newError("Wrong password :(", 401);
|
||||
}
|
||||
|
||||
export function prepareSafeUser(user) {
|
||||
const safeUser = {
|
||||
firstname: user.firstname,
|
||||
lastname: user.lastname,
|
||||
email: user.email,
|
||||
id: user._id,
|
||||
};
|
||||
return safeUser;
|
||||
}
|
||||
|
||||
export function generateAuthToken(user) {
|
||||
const token = jwt.sign(
|
||||
{
|
||||
email: user.email,
|
||||
firstname: user.firstname,
|
||||
lastname: user.lastname,
|
||||
id: user._id,
|
||||
id: user.id,
|
||||
restaurants: user.restaurants,
|
||||
},
|
||||
jwtSecret,
|
||||
@@ -113,9 +72,10 @@ export function validateUserToken(token) {
|
||||
if (!token) throw newError("Invalid user token", 401);
|
||||
const verified = jwt.verify(token, jwtSecret, { ignoreExpiration: false });
|
||||
if (!verified) throw newError("Invalid user token", 401);
|
||||
return verified;
|
||||
}
|
||||
|
||||
export function validateDishId(id) {
|
||||
export async function validateDishId(id) {
|
||||
if (!mongoose.Types.ObjectId.isValid(id)) {
|
||||
throw newError("Invalid ID", 400);
|
||||
}
|
||||
@@ -123,95 +83,16 @@ export function validateDishId(id) {
|
||||
if (!dishDoesExist) throw newError("Dish doesn't exist", 404);
|
||||
}
|
||||
|
||||
export async function addDishToRestaurant(restaurantId, dishId) {
|
||||
await Restaurant.updateOne(
|
||||
{ _id: restaurantId },
|
||||
{ $push: { dishes: dishId } }
|
||||
).catch((error) => {
|
||||
throw newError("Couldn't add dish to restaurant", 500);
|
||||
});
|
||||
}
|
||||
|
||||
export function createDish(dish, generateId) {
|
||||
// TEST THIS ONE!!!!!
|
||||
if (generateId) {
|
||||
const newDish = new Dish({
|
||||
_id: new mongoose.Types.ObjectId(),
|
||||
name: sanitizer.sanitize.keepUnicode(dish.name),
|
||||
category: dish.category,
|
||||
price: dish.price,
|
||||
notes: sanitizer.sanitize.keepUnicode(dish.notes),
|
||||
imgUrl: dish.imgUrl,
|
||||
weight: dish.weight,
|
||||
allergens: {
|
||||
gluten: dish.allergens.gluten,
|
||||
lactose: dish.allergens.lactose,
|
||||
soy: dish.allergens.soy,
|
||||
eggs: dish.allergens.eggs,
|
||||
seaFood: dish.allergens.seaFood,
|
||||
peanuts: dish.allergens.peanuts,
|
||||
sesame: dish.allergens.sesame,
|
||||
},
|
||||
ingredients: dish.ingredients,
|
||||
vegan: dish.vegan,
|
||||
vegetarian: dish.vegetarian,
|
||||
});
|
||||
return newDish;
|
||||
} else {
|
||||
const newDish = new Dish({
|
||||
name: sanitizer.sanitize.keepUnicode(dish.name),
|
||||
category: dish.category,
|
||||
price: dish.price,
|
||||
notes: sanitizer.sanitize.keepUnicode(dish.notes),
|
||||
imgUrl: dish.imgUrl,
|
||||
weight: dish.weight,
|
||||
allergens: {
|
||||
gluten: dish.allergens.gluten,
|
||||
lactose: dish.allergens.lactose,
|
||||
soy: dish.allergens.soy,
|
||||
eggs: dish.allergens.eggs,
|
||||
seaFood: dish.allergens.seaFood,
|
||||
peanuts: dish.allergens.peanuts,
|
||||
sesame: dish.allergens.sesame,
|
||||
},
|
||||
ingredients: dish.ingredients,
|
||||
vegan: dish.vegan,
|
||||
vegetarian: dish.vegetarian,
|
||||
});
|
||||
return newDish;
|
||||
export async function verifyDishAccess(dishId, decodedToken) {
|
||||
const fetch = await User.findById(decodedToken.id, "restaurants");
|
||||
const restaurants = fetch.restaurants;
|
||||
const restaurantId = await Dish.findById(dishId, "restaurantId").catch(
|
||||
(error) => {
|
||||
throw newError("Couldn't fetch Dish", 404);
|
||||
}
|
||||
}
|
||||
|
||||
export function createRestaurant(request) {
|
||||
try {
|
||||
const restaurant = new Restaurant({
|
||||
_id: new mongoose.Types.ObjectId(),
|
||||
name: sanitizer.sanitize.keepUnicode(request.body.name),
|
||||
city: sanitizer.sanitize.keepUnicode(request.body.city),
|
||||
imgUrl: services.saveImage(request.body.imgURL),
|
||||
workingHours: request.body.workingHours,
|
||||
description: sanitizer.sanitize.keepUnicode(request.body.description),
|
||||
tags: request.body.tags,
|
||||
links: request.body.links,
|
||||
phone: request.body.phone,
|
||||
hidden: request.body.hidden,
|
||||
});
|
||||
return restaurant;
|
||||
} catch (error) {
|
||||
throw newError("Invalid input data", 206);
|
||||
}
|
||||
}
|
||||
|
||||
export async function createUser(request) {
|
||||
const password = await hashPass(request.body.password);
|
||||
const user = new User({
|
||||
_id: new mongoose.Types.ObjectId(),
|
||||
email: request.body.email,
|
||||
password: password,
|
||||
firstname: request.body.firstname,
|
||||
lastname: request.body.lastname,
|
||||
});
|
||||
return user;
|
||||
);
|
||||
const valid = restaurants.includes(restaurantId.restaurantId);
|
||||
if (!valid) throw newError("You don't have access to this Dish.", 401);
|
||||
}
|
||||
|
||||
export function yearFromNowDate() {
|
||||
@@ -230,7 +111,6 @@ export function halfYearFromNowDate() {
|
||||
date.setDate(date.getDate() + days);
|
||||
return date;
|
||||
};
|
||||
|
||||
var nowDate = new Date();
|
||||
var resultDate = nowDate.addDays(183);
|
||||
return toShortDate(resultDate);
|
||||
@@ -254,38 +134,6 @@ export function dueDateBasedOnSubscription(subscriptionActive) {
|
||||
}
|
||||
}
|
||||
|
||||
export function composeNewContact(request) {
|
||||
const dateNow = new Date();
|
||||
const contact = {
|
||||
lead_score: "100",
|
||||
tags: ["newUser"],
|
||||
properties: [
|
||||
{
|
||||
type: "SYSTEM",
|
||||
name: "first_name",
|
||||
value: request.firstname,
|
||||
},
|
||||
{
|
||||
type: "SYSTEM",
|
||||
name: "last_name",
|
||||
value: request.lastname,
|
||||
},
|
||||
{
|
||||
type: "SYSTEM",
|
||||
name: "email",
|
||||
subtype: "work",
|
||||
value: request.email,
|
||||
},
|
||||
{
|
||||
type: "CUSTOM",
|
||||
name: "UserID",
|
||||
value: request._id,
|
||||
},
|
||||
],
|
||||
};
|
||||
return contact;
|
||||
}
|
||||
|
||||
export function toShortDate(date) {
|
||||
if (!date) return false;
|
||||
const shortDate =
|
||||
@@ -293,7 +141,7 @@ export function toShortDate(date) {
|
||||
return shortDate;
|
||||
}
|
||||
|
||||
export function saveImage(url) {
|
||||
const newURL = renameBlob(url);
|
||||
export async function saveImage(url) {
|
||||
const newURL = await renameBlob(url);
|
||||
return newURL;
|
||||
}
|
||||
|
||||
0
services/subscriptionServices.js
Normal file
0
services/subscriptionServices.js
Normal file
Reference in New Issue
Block a user