Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
taugk committed Dec 15, 2023
1 parent 5a731a9 commit 9b0ad36
Showing 1 changed file with 57 additions and 51 deletions.
108 changes: 57 additions & 51 deletions routes/postsRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,72 +501,78 @@ router.put(
if (!post) {
return res.status(404).json({ message: "Post not found" });
}
const headerAuth = req.headers["authorization"];
const token = headerAuth && headerAuth.split(" ")[1];
const tokenDecode = req.authData;

if (!token) {
return res.status(401).json({ message: "Unauthorized: Missing token" });
if (!tokenDecode.id) {
return res
.status(401)
.json({ message: "Unauthorized: User not logged in" });
}
if (tokenDecode.id !== post.userId) {
return res.status(403).json({
message:
"Forbidden: User does not have permission to update this post",
});
}

// Ensure that pickupTime is defined in the request body
const {
title,
description,
price,
lat,
lon,
freshness,
categoryId,
isAvailable,
pickupTime,
} = req.body;

if (!pickupTime) {
return res.status(400).json({ message: "pickupTime is required" });
}

let formatDateTime = "";
try {
const tokenDecode = jwt.verify(token, process.env.SECRET_KEY);
console.log("Decoded Token:", tokenDecode);

if (!tokenDecode.id) {
return res
.status(401)
.json({ message: "Unauthorized: User not logged in" });
}
if (tokenDecode.id !== post.userId) {
return res.status(403).json({
message:
"Forbidden: User does not have permission to update this post",
});
}
let formatDateTime = "";
try {
const datetime = moment.unix(pickupTime);
formatDateTime = datetime.format("YYYY-MM-DD HH:mm:ss");
} catch (error) {
console.error("Error converting pickupTime:", error);
}
post.title = title;
post.description = description;
post.price = price;
post.pickupTime = formatDateTime;
post.lat = lat;
post.lon = lon;
post.freshness = freshness;
post.categoryId = categoryId;
post.isAvailable = isAvailable;
if (req.file && req.file.cloudStoragePublicUrl) {
post.imgUrl = req.file.cloudStoragePublicUrl;
}
await post.save();
res.status(200).json({ message: "Post updated successfully", post });
const datetime = moment.unix(pickupTime);
formatDateTime = datetime.format("YYYY-MM-DD HH:mm:ss");
} catch (error) {
console.error("Error updating post", error);
if (invalidatedTokens && invalidatedTokens.has(token)) {
return res
.status(401)
.json({ message: "Unauthorized: Token invalidated" });
}
return res.status(401).json({ message: "Unauthorized: Invalid token" });
console.error("Error converting pickupTime:", error);
return res.status(400).json({ message: "Invalid pickupTime format" });
}

post.title = title;
post.description = description;
post.price = price;
post.pickupTime = formatDateTime;
post.lat = lat;
post.lon = lon;
post.freshness = freshness;
post.categoryId = categoryId;
post.isAvailable = isAvailable;

if (req.file && req.file.cloudStoragePublicUrl) {
post.imgUrl = req.file.cloudStoragePublicUrl;
}

await post.save();
res.status(200).json({ message: "Post updated successfully", post });
} catch (error) {
console.error("Error updating post", error);
res.status(500).json({ message: "Internal server error" });
if (invalidatedTokens && invalidatedTokens.has(token)) {
return res
.status(401)
.json({ message: "Unauthorized: Token invalidated" });
}
return res.status(401).json({ message: "Unauthorized: Invalid token" });
}
}
);

//Delete Routes
router.delete("/deletePost/:id", async (req, res) => {
try {
const headerAuth = req.headers["authorization"];
const token = headerAuth && headerAuth.split(" ")[1];
const tokenDecode = jwt.verify(token, process.env.SECRET_KEY);
console.log("Decoded Token:", tokenDecode);
const tokenDecode = req.authData;
const deletedPost = await Post.destroy({
where: {
id: req.params.id,
Expand Down

0 comments on commit 9b0ad36

Please sign in to comment.