Skip to content

Commit

Permalink
[finishes #187585696] ordermanagement
Browse files Browse the repository at this point in the history
some changes
ii
[finishes #187354263]crud operations of items to a cart

	new file:   src/controllers/cartController.ts
	new file:   src/database/migrations/20240510102608-create-user-product.js
	new file:   src/database/models/userProduct.ts

	new file:   src/docs/cart.yaml
	new file:   src/routes/cartRoute.ts
	modified:   src/routes/index.ts

    pick a62f333 [finishes #187354263]crud operations of items to a cart

fixing bugs
squashing previous commits

	new file:   .vscode/launch.json
	modified:   package.json
	new file:   src/controllers/cartController.ts
	new file:   src/controllers/orderController.ts
	modified:   src/controllers/productsController.ts
	modified:   src/controllers/roleControllers.ts
	modified:   src/dat

abase/migrations/20240413203456-create-role.js
	new file:   src/database/migrations/20240510102608-create-user-product.js
	new file:   src/database/migrations/20240513164738-cart-model.js
	new file:   src/database/migrations/20240514160800-orders.js
	new file:   src/database/migrations/20240514174638-cart-product.js
	modified:   src/database/models/Product.ts
	new file:   src/database/models/cart.ts
	new file:   src/database/models/order.ts
	modified:   src/database/models/role.ts
	modified:   src/database/seeders/20240427082911-create-default-role.js
:i`	modified:   src/database/seeders/20240429200629-add-seller-role.js
	new file:   src/docs/cart.yaml
	modified:   src/middlewares/authMiddlewares.ts
	new file:   src/routes/cartRoute.ts
	modified:   src/routes/index.ts
	new file:   src/routes/orderRoute.ts
	modified:   src/routes/productRoutes.ts
	modified:   src/validations/index.ts

	new file:   .vscode/launch.json
	modified:   package.json
	new file:   src/controllers/cartController.ts
	new file:   src/controllers/orderController.ts
	modified:   src/controllers/productsController.ts
	modified:   src/controllers/roleControllers.ts
	new file:   src/database/migrations/20240510102608-create-user-product.js
	new file:   src/database/migrations/20240513164738-cart-model.js
	new file:   src/database/migrations/20240514160800-orders.js
	new file:   src/database/migrations/20240514174638-cart-product.js
	modified:   src/database/models/Product.ts
	new file:   src/database/models/cart.ts
	modified:   src/database/models/order.ts
	modified:   src/database/models/role.ts
	new file:   src/database/seeders/20240429200629-add-seller-role.js
	new file:   src/docs/cart.yaml
	new file:   src/docs/orders.yaml
	modified:   src/middlewares/authMiddlewares.ts
	new file:   src/routes/cartRoute.ts
	modified:   src/routes/index.ts
	new file:   src/routes/orderRoute.ts	modified:   src/routes/productRoutes.ts
	modified:   src/validations/index.ts
  • Loading branch information
JeanIrad committed May 19, 2024
1 parent becf88b commit 8dbdc8e
Show file tree
Hide file tree
Showing 31 changed files with 1,215 additions and 30 deletions.
20 changes: 20 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/src/controllers/orderController.ts",
"outFiles": [
"${workspaceFolder}/**/*.js"
]
}
]
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
"migrate": "npx sequelize-cli db:migrate",
"migrate:undo": "npx sequelize-cli db:migrate:undo",
"migrate:undo:all": "npx sequelize-cli db:migrate:undo:all",
"migrate:undo:all": "npx sequelize-cli db:migrate:undo:all",
"seed": "npx sequelize-cli db:seed:all",
"seed:undo": "npx sequelize-cli db:seed:undo:all"
"seed:undo": "npx sequelize-cli db:seed:undo",
"seed:undo:all": "npx sequelize-cli db:seed:undo:all"
},
"keywords": [],
"author": "",
Expand Down Expand Up @@ -101,4 +103,4 @@
"eslint --fix"
]
}
}
}
225 changes: 225 additions & 0 deletions src/controllers/cartController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
import { Request, Response } from 'express';
import { Cart } from '../database/models/cart';
import { Product, ProductAttributes } from '../database/models/Product';
import User from '../database/models/user';
import { sendInternalErrorResponse, validateFields } from '../validations';
import sequelize from '../database/models';
import logger from '../logs/config';
import { Size } from '../database/models/Size';

const addCartItem = async (req: Request, res: Response): Promise<void> => {
const { productId, sizeId } = req.body;
const { id: userId } = req.user as User;
try {
// Validate request body
const missingFields = validateFields(req, ['productId']);
if (missingFields.length > 0) {
res.status(400).json({
ok: false,
message: `Following required fields are missing: ${missingFields.join(', ')}`,
});
return;
}

// Start transaction
const transaction = await sequelize.transaction();

try {
// Check if the product exists
const product = await Product.findByPk(productId, {
include: {
model: Size,
as: 'Sizes',
attributes: ['quantity'],
},
transaction,
});
if (!product) {
res.status(404).json({ ok: false, message: 'Product was not found in stock!' });
return;
}

// Check if the item already exists in the cart
const cartItem: any = await Cart.findOne({
include: [
{
model: User,
as: 'user',
attributes: ['id', 'firstName', 'lastName', 'email', 'phoneNumber', 'gender'],
},
{
model: Product,
as: 'products',
through: { attributes: ['quantity'] },
},
],
where: { userId },
transaction: transaction,
});

if (cartItem) {
// Check if the product being added is already in the cart
const existingProduct = cartItem.products.find((product: ProductAttributes) => product.id === productId);
if (existingProduct) {
const [currentCartSize] = await sequelize.query('SELECT quantity from "CartsProducts" where "productId" =?', {
replacements: [productId],
});
const quantity = (currentCartSize[0] as any).quantity;
const stockSize = (product as any).Sizes[0].quantity;
if (quantity < stockSize) {
// If the product already exists in the cart, increment the quantity in the join table
await sequelize.query(
'UPDATE "CartsProducts" SET quantity = quantity + 1 WHERE "cartId" =? AND "productId" =?',
{
replacements: [cartItem.id, productId],
}
);
} else {
throw new Error('No more same products in stock');
return;
}
} else {
// If the product doesn't exist in the cart, add it to the cart
await cartItem.addProducts(product, {
through: { quantity: 1 },
transaction: transaction,
});
}
} else {
// If the cart is empty, create a new cart item
const newCartItem: any = await Cart.create({ userId }, { transaction });
await newCartItem.addProducts(product, {
through: { quantity: 1 },
transaction,
});
}

// Commit the transaction
await transaction.commit();
res.status(200).json({ ok: true, message: 'Product added to cart successfully' });
} catch (error) {
// Rollback the transaction if an error occurs
await transaction.rollback();
throw error;
}
} catch (error) {
// Handle any unexpected errors
console.error(error);
// res.status(500).json({ ok: false, message: 'An unexpected error occurred' });
sendInternalErrorResponse(res, error);
}
};

//updating an item

const updateCartItem = async (req: Request, res: Response): Promise<void> => {
const { productId, quantity } = req.body;
const { id: userId } = req.user as User;

const transaction = await sequelize.transaction();
try {
const cartItem: any = await Cart.findOne({
where: { userId },
transaction,
});

if (!cartItem) {
res.status(404).json({ ok: false, message: 'Cart not found' });
return;
}
if (quantity >= 1) {
await sequelize.query('UPDATE "CartsProducts" SET quantity =? WHERE "cartId" =? AND "productId" =?', {
replacements: [quantity, cartItem.id, productId],
transaction,
});

await transaction.commit();
res.status(200).json({ ok: true, message: 'Cart updated successfully' });
} else {
throw new Error('Quantity cannot be less than 1');
}
} catch (error) {
await transaction.rollback();
logger.error(error);
sendInternalErrorResponse(res, error);
}
};

//getting items of a cart

const getCartItems = async (req: Request, res: Response): Promise<void> => {
const { id: userId } = req.user as User;

const transaction = await sequelize.transaction();
try {
const cart: any = await Cart.findOne({
where: { userId },
include: [
{
model: Product,
as: 'products',
through: { attributes: ['quantity'] },
include: {
model: Size,

Check failure on line 163 in src/controllers/cartController.ts

View workflow job for this annotation

GitHub Actions / Build

No overload matches this call.
as: 'Sizes',
attributes: ['quantity'],
},
},
],
transaction,
});

if (!cart) {
res.status(404).json({ ok: false, message: 'Cart not found' });
return;
}
const [productIds] = await sequelize.query(
'SELECT "productId" from "CartsProducts" where "cartId" =? AND "userId" =?',
{ replacements: [cart.id, userId], transaction }
);
const products = cart.products.map((product: any) => {
return {
id: product.id,
name: product.name,
description: product.description,
quantity: product.CartsProducts,
image: product.images[0],
sellerId: product.sellerId,
createdAt: product.createdAt,
};
});
res.status(200).json({ cart: products });
} catch (error) {
await transaction.rollback();
throw error;
}
};

//clear cart

const clearCart = async (req: Request, res: Response): Promise<void> => {
const { id: userId } = req.user as User;

const transaction = await sequelize.transaction();
try {
const cart = await Cart.findOne({
where: { userId },
transaction,
});

if (!cart) {
res.status(404).json({ ok: false, message: 'Cart not found' });
return;
}

await sequelize.query('DELETE FROM "CartsProducts" WHERE "cartId" =?', { replacements: [cart.id], transaction });
await transaction.commit();
res.status(200).json({ ok: true, message: 'Cart cleared successfully' });
} catch (error) {
await transaction.rollback();
logger.error(error);
sendInternalErrorResponse(res, error);
}
};

export { addCartItem, updateCartItem, getCartItems, clearCart };
11 changes: 11 additions & 0 deletions src/controllers/categoriesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,14 @@ export const createCategory = async (req: Request, res: Response) => {
res.status(500).json({ error: 'Failed to create category' });
}
};
export const getAllCategories = async (req: Request, res: Response) => {
try {
const categories = await Category.findAll();
res.status(200).json({ ok: true, data: categories });
} catch (error) {
if (error instanceof Error) {
logger.error(error.message);
}
res.status(500).json({ error: 'Failed to fetch categories' });
}
};
Loading

0 comments on commit 8dbdc8e

Please sign in to comment.