Refactoring day1

This commit is contained in:
2020-08-20 20:27:14 +02:00
parent 6aceefeb2f
commit b907489a75
481 changed files with 5321 additions and 5616 deletions

View File

@@ -9,16 +9,40 @@ import bcrypt from "bcrypt";
import * as config from "../config/index.js";
const { jwtSecret } = config;
export function validateRestaurant(id, callback) {
if (mongoose.Types.ObjectId.isValid(id)) {
Restaurant.exists({ _id: id }, (err, res) => {
export async function validateRestaurant(id) {
if (!mongoose.Types.ObjectId.isValid(id)) throw "Invalid ID";
let valid = await Restaurant.exists({ _id: id });
if (valid !== true) throw "Restaurant doesn't exist";
return true;
}
export async function fetchRestaurant(id) {
let data;
await Restaurant.findById(id, (err, result) => {
data = result;
}).catch((e) => {
throw "Couldn't fetch restaurant";
});
return data;
}
export async function fetchAllDishesForRestaurant(restaurant) {
let dishes = [];
await restaurant.dishes.forEach((element) => {
Dish.findById(element._id, (err, result) => {
if (err) {
callback(false);
console.log(err);
} else {
callback(res);
dishes.push(result);
console.log(result);
}
});
} else callback(false);
});
return dishes;
}
export async function fetchDish(id) {
foo;
}
export function fetchUser(email, callback) {
@@ -31,6 +55,11 @@ export function fetchUser(email, callback) {
});
}
export function decodeAndSanitize(query) {
if (!query) throw "Nothing to sanitize...";
return sanitizer.sanitize.keepUnicode(decodeURI(query));
}
export function generateAuthToken(user) {
const token = jwt.sign(
{
@@ -200,19 +229,17 @@ export function composeNewContact(request) {
},
],
};
return contact;
}
function toShortDate(date) {
export function toShortDate(date) {
if (!date) return false;
const shortDate =
date.getMonth() + "/" + date.getDay() + "/" + date.getFullYear();
return shortDate;
}
export function saveImage(url) {
const newURL = renameBlob(url);
return newURL;
}