Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions src/controllers/apps/ecommerce/cart.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export const getCart = async (userId) => {
$subtract: ["$cartTotal", "$coupon.discountValue"],
},
"$cartTotal", // if there is no coupon applied we will set cart total as out final total
,
],
},
},
Expand Down Expand Up @@ -105,11 +104,19 @@ const addItemOrUpdateItemQuantity = asyncHandler(async (req, res) => {
const { productId } = req.params;
const { quantity = 1 } = req.body;

// fetch user cart
const cart = await Cart.findOne({
// fetch user cart or create one if it doesn't exist
let cart = await Cart.findOne({
owner: req.user._id,
});

// If cart doesn't exist, create a new one for the user
if (!cart) {
cart = await Cart.create({
owner: req.user._id,
items: [],
});
}

// See if product that user is adding exist in the db
const product = await Product.findById(productId);

Expand Down Expand Up @@ -173,6 +180,18 @@ const removeItemFromCart = asyncHandler(async (req, res) => {
throw new ApiError(404, "Product does not exist");
}

// Check if user has a cart first
const existingCart = await Cart.findOne({
owner: req.user._id,
});

if (!existingCart) {
throw new ApiError(
400,
"Cart not found. Cannot remove item from non-existent cart"
);
}

const updatedCart = await Cart.findOneAndUpdate(
{
owner: req.user._id,
Expand Down Expand Up @@ -207,6 +226,15 @@ const removeItemFromCart = asyncHandler(async (req, res) => {
});

const clearCart = asyncHandler(async (req, res) => {
// Check if cart exists first
const existingCart = await Cart.findOne({
owner: req.user._id,
});

if (!existingCart) {
throw new ApiError(400, "Cart not found. Cannot clear non-existent cart");
}

await Cart.findOneAndUpdate(
{
owner: req.user._id,
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/kitchen-sink/statuscode.controllers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { asyncHandler } from "../../utils/asyncHandler.js";
import statusCodesJson from "../../json/status-codes.json" assert { type: "json" };
import statusCodesJson from "../../json/status-codes.json" with { type: "json" };
import { ApiError } from "../../utils/ApiError.js";
import { ApiResponse } from "../../utils/ApiResponse.js";

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/public/book.controllers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import booksJson from "../../json/books.json" assert { type: "json" };
import booksJson from "../../json/books.json" with { type: "json" };
import { filterObjectKeys, getPaginatedPayload } from "../../utils/helpers.js";
import { ApiError } from "../../utils/ApiError.js";
import { ApiResponse } from "../../utils/ApiResponse.js";
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/public/cat.controllers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import catsJson from "../../json/cats.json" assert { type: "json" };
import catsJson from "../../json/cats.json" with { type: "json" };
import { filterObjectKeys, getPaginatedPayload } from "../../utils/helpers.js";
import { ApiError } from "../../utils/ApiError.js";
import { ApiResponse } from "../../utils/ApiResponse.js";
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/public/dog.controllers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import dogsJson from "../../json/dogs.json" assert { type: "json" };
import dogsJson from "../../json/dogs.json" with { type: "json" };
import { filterObjectKeys, getPaginatedPayload } from "../../utils/helpers.js";
import { ApiError } from "../../utils/ApiError.js";
import { ApiResponse } from "../../utils/ApiResponse.js";
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/public/meal.controllers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mealsJson from "../../json/meals.json" assert { type: "json" };
import mealsJson from "../../json/meals.json" with { type: "json" };
import { filterObjectKeys, getPaginatedPayload } from "../../utils/helpers.js";
import { ApiError } from "../../utils/ApiError.js";
import { ApiResponse } from "../../utils/ApiResponse.js";
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/public/quote.controllers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import quotesJson from "../../json/quotes.json" assert { type: "json" };
import quotesJson from "../../json/quotes.json" with { type: "json" };
import { filterObjectKeys, getPaginatedPayload } from "../../utils/helpers.js";
import { ApiError } from "../../utils/ApiError.js";
import { ApiResponse } from "../../utils/ApiResponse.js";
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/public/randomjoke.controllers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import randomJokesJson from "../../json/randomjoke.json" assert { type: "json" };
import randomJokesJson from "../../json/randomjoke.json" with { type: "json" };
import { filterObjectKeys, getPaginatedPayload } from "../../utils/helpers.js";
import { ApiError } from "../../utils/ApiError.js";
import { ApiResponse } from "../../utils/ApiResponse.js";
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/public/randomproduct.controllers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import randomProductsJson from "../../json/randomproduct.json" assert { type: "json" };
import randomProductsJson from "../../json/randomproduct.json" with { type: "json" };
import { filterObjectKeys, getPaginatedPayload } from "../../utils/helpers.js";
import { ApiError } from "../../utils/ApiError.js";
import { ApiResponse } from "../../utils/ApiResponse.js";
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/public/randomuser.controllers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import randomUsersJson from "../../json/randomuser.json" assert { type: "json" };
import randomUsersJson from "../../json/randomuser.json" with { type: "json" };
import { filterObjectKeys, getPaginatedPayload } from "../../utils/helpers.js";
import { ApiError } from "../../utils/ApiError.js";
import { ApiResponse } from "../../utils/ApiResponse.js";
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/public/stock.controllers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import nseStocksJson from "../../json/nse-stocks.json" assert { type: "json" };
import nseStocksJson from "../../json/nse-stocks.json" with { type: "json" };
import { filterObjectKeys, getPaginatedPayload } from "../../utils/helpers.js";
import { ApiError } from "../../utils/ApiError.js";
import { ApiResponse } from "../../utils/ApiResponse.js";
Expand Down
10 changes: 5 additions & 5 deletions src/controllers/public/youtube.controllers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { YouTubeFilterEnum, AvailableYouTubeFilters } from "../../constants.js";
import channelJson from "../../json/youtube/channel.json" assert { type: "json" };
import commentsJson from "../../json/youtube/comments.json" assert { type: "json" };
import playlistItemsJson from "../../json/youtube/playlistitems.json" assert { type: "json" };
import playlistsJson from "../../json/youtube/playlists.json" assert { type: "json" };
import videosJson from "../../json/youtube/videos.json" assert { type: "json" };
import channelJson from "../../json/youtube/channel.json" with { type: "json" };
import commentsJson from "../../json/youtube/comments.json" with { type: "json" };
import playlistItemsJson from "../../json/youtube/playlistitems.json" with { type: "json" };
import playlistsJson from "../../json/youtube/playlists.json" with { type: "json" };
import videosJson from "../../json/youtube/videos.json" with { type: "json" };
import { ApiError } from "../../utils/ApiError.js";
import { ApiResponse } from "../../utils/ApiResponse.js";
import { asyncHandler } from "../../utils/asyncHandler.js";
Expand Down