Skip to content

Commit

Permalink
add delete cloudinary
Browse files Browse the repository at this point in the history
  • Loading branch information
sakshamchhimwal committed Mar 22, 2024
1 parent 53d29e2 commit d78e5ef
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
30 changes: 17 additions & 13 deletions src/controllers/UserControllers/user.suggestions.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { sendNotification } from "../../config/firebaseWeb";
import notificationToken from "../../models/notificationToken";
import usernotifications from "../../models/usernotifications";
import e from "cors";
import { deleteFromCloudinary } from "../../services/uploadToCloudinary";

export const getSuggestions = async (
req: any,
Expand Down Expand Up @@ -65,7 +66,6 @@ export const postSuggestion = async (
}
const messId = currUser.Eating_Mess;
const newSuggestion = req.body;
console.log(req.body);
const addSuggestion = await SuggestionsModel.create({
messId,
userId: currUser._id,
Expand Down Expand Up @@ -136,18 +136,22 @@ export const deleteSuggestion = async (
if (!currUser) {
return next(createHttpError(403, "Unauthorized"));
}

const suggestionId = req.query.suggestionId;
const deletedSuggestion = await SuggestionsModel.findOneAndDelete({
const currSuggestion = await SuggestionsModel.findOne({
_id: suggestionId,
userId: currUser._id,
});

if (deletedSuggestion) {
// console.log(deletedSuggestion);
if (currSuggestion) {
const imageUrl = currSuggestion.image?.split('/');
if (imageUrl != null) {
const imagePublicID = imageUrl[imageUrl?.length - 1].split('.')[0];
const publicID = `Smart_Mess/User_Uploads/${imagePublicID}`;
await deleteFromCloudinary(publicID);
await currSuggestion.deleteOne();
}
res
.status(200)
.send({ message: "Sugestion deleted successfully", deletedSuggestion });
.send({ message: "Sugestion deleted successfully", deletedSuggestion: currSuggestion });
} else {
res.status(404).send({ message: "Suggestion Not Found" });
}
Expand Down Expand Up @@ -303,13 +307,13 @@ export const voteSuggestionComment = async (
const updateType =
req.body.upvote === true
? {
$addToSet: { "children.$.upvotes": currUser._id },
$pull: { "children.$.downvotes": currUser._id },
}
$addToSet: { "children.$.upvotes": currUser._id },
$pull: { "children.$.downvotes": currUser._id },
}
: {
$pull: { "children.$.upvotes": currUser._id },
$addToSet: { "children.$.downvotes": currUser._id },
};
$pull: { "children.$.upvotes": currUser._id },
$addToSet: { "children.$.downvotes": currUser._id },
};
const newVote = await SuggestionsModel.updateOne(
{
_id: suggestionId,
Expand Down
1 change: 1 addition & 0 deletions src/models/suggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ const Suggestions = new Schema({
},
});


export default mongoose.model("suggestions", Suggestions);
12 changes: 11 additions & 1 deletion src/services/uploadToCloudinary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,18 @@ export const uploadToCloudinary = async (
next();
} catch (err) {
const mute = err;
console.log({cloudinaryError:err});
console.log({ cloudinaryError: err });
console.error("Error Occured in Cloudinary Upload");
return next(createHttpError(500));
}
};


export const deleteFromCloudinary = async (publicID: string) => {
cloudinary.uploader.destroy(publicID).then(() => {
return 1;
}).catch((err) => {
console.log(err);
throw Error("cloudinary delete error");
})
}

0 comments on commit d78e5ef

Please sign in to comment.