-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[finishes #187585696] ordermanagement
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
Showing
31 changed files
with
1,215 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.