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

@@ -1,10 +1,11 @@
import { removePrefix } from "./azureServices.js";
import azureBlob from "@azure/storage-blob";
jest.mock("@azure/storage-blob", () => {
return jest.fn().mockImplementation(() => {
return { StorageSharedKeyCredential: jest.fn() };
});
return {
StorageSharedKeyCredential: jest.fn(),
newPipeline: jest.fn(),
BlobServiceClient: jest.fn(),
};
});
test("should remove TEMP_ prefix", () => {

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;
}

19
services/services.test.js Normal file
View File

@@ -0,0 +1,19 @@
import { validateRestaurant, toShortDate } from "./services";
jest.mock("@azure/storage-blob", () => {
return {
StorageSharedKeyCredential: jest.fn(),
newPipeline: jest.fn(),
BlobServiceClient: jest.fn(),
};
});
jest.mock("bcrypt", () => {
return {
foo: jest.fn(),
};
});
test("should return false for no date on input", () => {
expect(toShortDate()).toBe(false);
});