diff --git a/README.md b/README.md index dc99ee2..6476e67 100644 --- a/README.md +++ b/README.md @@ -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**.
* ### **/restaurant** diff --git a/config/mailTemplateReset.js b/config/mailTemplateReset.js new file mode 100644 index 0000000..a4c634b --- /dev/null +++ b/config/mailTemplateReset.js @@ -0,0 +1,138 @@ +export default function makeResetPassMessage(newPass) { + return; + ` + + + + + Menui - Resetowanie hasła + + + + + + + + + + + + + + + + +
+ + + + +
+ +
+
+ + + + +
+

Resetowanie hasła

+
+
+
+ + + + +
+

+ Drogi użytkowniku, dostałeś tę wiadomość, ponieważ użyłeś + opcji "Nie pamiętam hasła" w aplikacji Menui. +

+

+ Twoje tymczasowe hasło to: +

${newPass}
+

+

+ 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ść. +

+
+
+ + + + +
+ +
+
+ + `; +} diff --git a/images/logo.svg b/images/logo.svg new file mode 100644 index 0000000..fc274b4 --- /dev/null +++ b/images/logo.svg @@ -0,0 +1 @@ +logo_full_orange \ No newline at end of file diff --git a/models/dish.js b/models/dish.js index 2e25578..864125f 100644 --- a/models/dish.js +++ b/models/dish.js @@ -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, diff --git a/models/restaurant.js b/models/restaurant.js index 5871c2a..9bdd6c9 100644 --- a/models/restaurant.js +++ b/models/restaurant.js @@ -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, diff --git a/package-lock.json b/package-lock.json index df63582..81af292 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index bdbeee1..f6bff4f 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/routes/routeDish.js b/routes/routeDish.js index 88a53ec..567c54b 100644 --- a/routes/routeDish.js +++ b/routes/routeDish.js @@ -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) { diff --git a/routes/routeRestaurant.js b/routes/routeRestaurant.js index e26f21f..7a8998c 100644 --- a/routes/routeRestaurant.js +++ b/routes/routeRestaurant.js @@ -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); diff --git a/routes/routeTest.js b/routes/routeTest.js index 02000c7..a882860 100644 --- a/routes/routeTest.js +++ b/routes/routeTest.js @@ -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; diff --git a/routes/routeUser.js b/routes/routeUser.js index 31a9f39..ab1d716 100644 --- a/routes/routeUser.js +++ b/routes/routeUser.js @@ -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); } }); diff --git a/services/azureServices.js b/services/azureServices.js index 586a84a..bfb5fe6 100644 --- a/services/azureServices.js +++ b/services/azureServices.js @@ -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) { - const blobName = blobURL.replace(containerURL, ""); - const containerClient = blobServiceClient.getContainerClient(container); - const tempBlob = containerClient.getBlobClient(blobName); - const newBlob = containerClient.getBlobClient(removePrefix(blobName)); +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); - return newBlob.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); } } diff --git a/services/dataPrepServices.js b/services/dataPrepServices.js new file mode 100644 index 0000000..751721a --- /dev/null +++ b/services/dataPrepServices.js @@ -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); + } +} diff --git a/services/databaseServices.js b/services/databaseServices.js new file mode 100644 index 0000000..a61a222 --- /dev/null +++ b/services/databaseServices.js @@ -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; +} diff --git a/services/mailServices.js b/services/mailServices.js new file mode 100644 index 0000000..26db702 --- /dev/null +++ b/services/mailServices.js @@ -0,0 +1,2 @@ +import nodemailer from "nodemailer"; +import makeResetPassMessage from "../config/mailTemplateReset"; diff --git a/services/services.js b/services/services.js index 9e3dd1b..9c746dc 100644 --- a/services/services.js +++ b/services/services.js @@ -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 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; +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); + } + ); + 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; } diff --git a/services/subscriptionServices.js b/services/subscriptionServices.js new file mode 100644 index 0000000..e69de29