server v1.0.7

This commit is contained in:
2020-10-01 20:21:01 +02:00
parent 5d6c7d5e3f
commit 63c2fb88e0
6 changed files with 33 additions and 37 deletions

View File

@@ -1,4 +1,5 @@
import azureBlob from "@azure/storage-blob";
import e from "express";
import getStream from "into-stream";
import { newError } from "./services.js";
@@ -77,10 +78,18 @@ export function setDeleteTempBlobTimer(blobName, containerClient, minutes) {
}
export async function deleteImage(url) {
const containerClient = blobServiceClient.getContainerClient(container);
const containerUrl = containerClient.url + "/";
const blobName = url.replace(containerUrl, "");
console.log(`BLOB NAME = ${blobName}`);
const blob = containerClient.getBlobClient(blobName);
await blob.delete();
if (!url || url === "" || url === "empty") {
return;
} else {
try {
const containerClient = blobServiceClient.getContainerClient(container);
const containerUrl = containerClient.url + "/";
const blobName = url.replace(containerUrl, "");
console.log(`BLOB NAME = ${blobName}`);
const blob = containerClient.getBlobClient(blobName);
await blob.delete();
} catch (error) {
console.log(error);
}
}
}

View File

@@ -113,9 +113,9 @@ export async function prepareSafeUser(user) {
return safeUser;
}
export async function createDish(dish, restaurantId, generateId) {
export async function createDish(dish, restaurantId, oldDish) {
try {
if (generateId) {
if (!oldDish) {
const img = await handleImageUpdate(dish);
const newDish = new Dish({
_id: new mongoose.Types.ObjectId(),
@@ -126,15 +126,7 @@ export async function createDish(dish, restaurantId, generateId) {
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,
},
allergens: dish.allergens,
ingredients: dish.ingredients,
glicemicIndex: dish.glicemicIndex,
kCal: dish.kCal,
@@ -143,24 +135,16 @@ export async function createDish(dish, restaurantId, generateId) {
});
return newDish;
} else {
const img = "";
const img = await handleImageUpdate(dish, oldDish);
const newDish = new Dish({
restaurantId: restaurantId,
restaurantId: oldDish.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,
},
allergens: dish.allergens,
ingredients: dish.ingredients,
glicemicIndex: dish.glicemicIndex,
kCal: dish.kCal,

View File

@@ -14,6 +14,7 @@ export async function removeDish(dishId) {
const deletedDoc = await Dish.findByIdAndDelete(dishId).catch((e) => {
throw newError("Usunięcie dania nie powiodło się.", 500);
});
await deleteImage(deletedDoc.imgUrl);
await Restaurant.findByIdAndUpdate(deletedDoc.restaurantId, {
$pull: { dishes: dishId },
}).catch((error) => {

View File

@@ -112,11 +112,9 @@ export async function verifyDishAccess(dishId, decodedToken) {
}
);
const restaurants = fetch.restaurants;
const restaurantId = await Dish.findById(dishId, "restaurantId").catch(
(error) => {
throw newError("Nie znaleziono dania.", 404);
}
);
const restaurantId = await Dish.findById(dishId).catch((error) => {
throw newError("Nie znaleziono dania.", 404);
});
const valid = restaurants.includes(restaurantId.restaurantId);
if (!valid) throw newError("Nie masz dostępu do tego dania.", 401);
}