server v1.0.5
//delete image //delete restaurant //update restaurant
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
|||||||
removeRestaurant,
|
removeRestaurant,
|
||||||
changeCategory,
|
changeCategory,
|
||||||
changeLunchMenu,
|
changeLunchMenu,
|
||||||
|
fetchUser,
|
||||||
} from "../services/databaseServices.js";
|
} from "../services/databaseServices.js";
|
||||||
import {
|
import {
|
||||||
decodeAndSanitize,
|
decodeAndSanitize,
|
||||||
@@ -15,6 +16,7 @@ import {
|
|||||||
validateUserToken,
|
validateUserToken,
|
||||||
verifyRestaurantAccess,
|
verifyRestaurantAccess,
|
||||||
newError,
|
newError,
|
||||||
|
checkPassword,
|
||||||
} from "../services/services.js";
|
} from "../services/services.js";
|
||||||
import Restaurant from "../models/restaurant.js";
|
import Restaurant from "../models/restaurant.js";
|
||||||
|
|
||||||
@@ -58,7 +60,7 @@ router.put("/", async (req, res) => {
|
|||||||
const oldRestaurant = await fetchRestaurant(req.body.restaurantId);
|
const oldRestaurant = await fetchRestaurant(req.body.restaurantId);
|
||||||
const newRestaurant = await createRestaurant(req.body, oldRestaurant);
|
const newRestaurant = await createRestaurant(req.body, oldRestaurant);
|
||||||
await Restaurant.replaceOne({ _id: req.body.restaurantId }, newRestaurant);
|
await Restaurant.replaceOne({ _id: req.body.restaurantId }, newRestaurant);
|
||||||
res.send("Dane zostały zaktualizowane.");
|
res.send(newRestaurant);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
handleError(error, res);
|
handleError(error, res);
|
||||||
}
|
}
|
||||||
@@ -91,7 +93,8 @@ router.post("/category", async (req, res) => {
|
|||||||
req.body.category,
|
req.body.category,
|
||||||
req.body.action
|
req.body.action
|
||||||
);
|
);
|
||||||
res.send("Kategoria zmieniona pomyślnie");
|
const restaurant = await fetchRestaurant(req.body.restaurantId);
|
||||||
|
res.send(restaurant);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
handleError(error, res);
|
handleError(error, res);
|
||||||
}
|
}
|
||||||
@@ -110,7 +113,8 @@ router.post("/lunch", async (req, res) => {
|
|||||||
req.body.dishId,
|
req.body.dishId,
|
||||||
req.body.action
|
req.body.action
|
||||||
);
|
);
|
||||||
res.send("Lunch menu zmienione pomyślnie.");
|
const restaurant = await fetchRestaurant(req.body.restaurantId);
|
||||||
|
res.send(restaurant);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
handleError(error, res);
|
handleError(error, res);
|
||||||
}
|
}
|
||||||
@@ -120,11 +124,16 @@ router.post("/lunch", async (req, res) => {
|
|||||||
|
|
||||||
router.post("/delete", async (req, res) => {
|
router.post("/delete", async (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
if (!req.body.password) {
|
||||||
|
throw newError("Niepełne dane.", 204);
|
||||||
|
}
|
||||||
const token = req.headers["x-auth-token"];
|
const token = req.headers["x-auth-token"];
|
||||||
const user = validateUserToken(token);
|
validateUserToken(token);
|
||||||
|
const user = await fetchUser(req.body.email);
|
||||||
|
await checkPassword(req.body.password, user.password);
|
||||||
await validateRestaurant(req.body.restaurantId);
|
await validateRestaurant(req.body.restaurantId);
|
||||||
await verifyRestaurantAccess(req.body.restaurantId, user);
|
await verifyRestaurantAccess(req.body.restaurantId, user);
|
||||||
await removeRestaurant(req.body.restaurantId, user.id);
|
await removeRestaurant(req.body.restaurantId, user._id);
|
||||||
res.send("Restauracja została pomyślnie usunięta.");
|
res.send("Restauracja została pomyślnie usunięta.");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
handleError(error, res);
|
handleError(error, res);
|
||||||
|
|||||||
@@ -30,6 +30,19 @@ router.post("/login", async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// REFRESH
|
||||||
|
router.post("/refresh", async (req, res) => {
|
||||||
|
try {
|
||||||
|
const token = req.headers["x-auth-token"];
|
||||||
|
const user = validateUserToken(token);
|
||||||
|
const freshUser = await fetchUser(user.email);
|
||||||
|
const safeUser = await prepareSafeUser(freshUser);
|
||||||
|
res.send(safeUser);
|
||||||
|
} catch (error) {
|
||||||
|
handleError(error, res);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// REGISTER
|
// REGISTER
|
||||||
router.post("/register", async (req, res) => {
|
router.post("/register", async (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -75,3 +75,12 @@ export function setDeleteTempBlobTimer(blobName, containerClient, minutes) {
|
|||||||
blob.delete();
|
blob.delete();
|
||||||
}, 1000 * 60 * minutes);
|
}, 1000 * 60 * 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();
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import Dish from "../models/dish.js";
|
|||||||
import User from "../models/users.js";
|
import User from "../models/users.js";
|
||||||
import Restaurant from "../models/restaurant.js";
|
import Restaurant from "../models/restaurant.js";
|
||||||
import { fetchMultipleRestaurants } from "./databaseServices.js";
|
import { fetchMultipleRestaurants } from "./databaseServices.js";
|
||||||
|
import { deleteImage } from "./azureServices.js";
|
||||||
|
|
||||||
export async function createUser(request) {
|
export async function createUser(request) {
|
||||||
const password = await hashPass(request.body.password);
|
const password = await hashPass(request.body.password);
|
||||||
@@ -25,20 +26,21 @@ export async function createUser(request) {
|
|||||||
|
|
||||||
async function handleImageUpdate(request, previous) {
|
async function handleImageUpdate(request, previous) {
|
||||||
if (!previous) {
|
if (!previous) {
|
||||||
if (!request.imgURL) {
|
if (!request.imgUrl) {
|
||||||
return "empty";
|
return "empty";
|
||||||
} else {
|
} else {
|
||||||
const img = await saveImage(request.imgURL);
|
const img = await saveImage(request.imgUrl);
|
||||||
return img;
|
return img;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (request.imgURL == previous.imgUrl) {
|
if (request.imgUrl == previous.imgUrl) {
|
||||||
return previous.imgUrl;
|
return previous.imgUrl;
|
||||||
} else {
|
} else {
|
||||||
if (!request.imgURL) {
|
if (!request.imgUrl) {
|
||||||
return previous.imgUrl;
|
return previous.imgUrl;
|
||||||
} else {
|
} else {
|
||||||
const img = await saveImage(request.imgURL);
|
const img = await saveImage(request.imgUrl);
|
||||||
|
await deleteImage(previous.imgUrl);
|
||||||
return img;
|
return img;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import Restaurant from "../models/restaurant.js";
|
import Restaurant from "../models/restaurant.js";
|
||||||
import Dish from "../models/dish.js";
|
import Dish from "../models/dish.js";
|
||||||
import User from "../models/users.js";
|
import User from "../models/users.js";
|
||||||
|
import { deleteImage } from "./azureServices.js";
|
||||||
import { newError } from "./services.js";
|
import { newError } from "./services.js";
|
||||||
|
|
||||||
export async function changeUserPass(userId, newPass) {
|
export async function changeUserPass(userId, newPass) {
|
||||||
@@ -26,6 +27,13 @@ export async function removeRestaurant(restaurantId, userId) {
|
|||||||
throw newError("Usunięcie nie powiodło się.", 500);
|
throw newError("Usunięcie nie powiodło się.", 500);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
await deleteImage(deletedDoc.imgUrl);
|
||||||
|
for (dishId of deletedDoc.dishes) {
|
||||||
|
const deletedDish = await Dish.findByIdAndDelete(dishId).catch((e) =>
|
||||||
|
console.log(e)
|
||||||
|
);
|
||||||
|
await deleteImage(deletedDish.imgUrl);
|
||||||
|
}
|
||||||
await User.findByIdAndUpdate(userId, {
|
await User.findByIdAndUpdate(userId, {
|
||||||
$pull: { restaurants: restaurantId },
|
$pull: { restaurants: restaurantId },
|
||||||
}).catch((e) => {
|
}).catch((e) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user