diff --git a/package.json b/package.json index 55971f1d..dada7409 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "express": "^4.19.2", "express-winston": "^4.2.0", "glob": "^10.3.12", + "jest-mock-req-res": "^1.0.2", "jsonwebtoken": "^9.0.2", "mailgen": "^2.0.28", "multer": "^1.4.5-lts.1", diff --git a/src/chatSetup.ts b/src/chatSetup.ts index d1ac515d..0ee42a05 100644 --- a/src/chatSetup.ts +++ b/src/chatSetup.ts @@ -26,7 +26,6 @@ export const findId = (socket: CustomSocket) => { }; interface Message { - socketId: string; content: string; msgData: string; } @@ -39,14 +38,14 @@ const sentMessage = async (socket: CustomSocket, data: Message, io: Server) => { const senderId = socket.userId; if (senderId) { try { - const { content, socketId } = data; + const { content } = data; + console.log('--------------messages are here,', data); const { firstName } = await getUserNames(socket.userId as string); - const chat = await Chat.create({ senderId, socketId, content }); + const chat = await Chat.create({ senderId, content }); io.emit('returnMessage', { senderId: chat.dataValues.senderId, - socketId: chat.dataValues.socketId, content: chat.dataValues.content, senderName: firstName, readStatus: chat.dataValues.readStatus, diff --git a/src/controllers/cartController.ts b/src/controllers/cartController.ts index 00ac7a1d..a1e16d5d 100644 --- a/src/controllers/cartController.ts +++ b/src/controllers/cartController.ts @@ -9,6 +9,7 @@ import { Size } from '../database/models/Size'; import CartsProducts from '../database/models/cartsProducts'; import { validateFields } from '../validations'; import { Transaction } from 'sequelize'; +import { time } from 'console'; const addCartItem = async (req: Request, res: Response): Promise => { const { productId, sizeId, quantity } = req.body; const { id: userId } = req.user as User; @@ -120,7 +121,6 @@ const updateCartItem = async (req: Request, res: Response): Promise => { }; //getting items of a cart - const getCartItems = async (req: Request, res: Response): Promise => { const { id: userId } = req.user as User; @@ -168,9 +168,11 @@ const getCartItems = async (req: Request, res: Response): Promise => { transaction, }), quantity: item.quantity, + timeStamps: (item as any).createdAt, }; }) ); + if ((await allProducts).length < 1) { res.status(404).json({ ok: false, message: 'No Product in the Cart' }); return; @@ -185,6 +187,7 @@ const getCartItems = async (req: Request, res: Response): Promise => { sellerId, image: images[0], quantity: item.quantity, + createdAt: item.timeStamps, }; }); await transaction.commit(); @@ -225,4 +228,34 @@ const clearCart = async (req: Request, res: Response): Promise => { } }; -export { addCartItem, updateCartItem, getCartItems, clearCart }; +// controller to delete cart item +const deleteCartItem = async (req: Request, res: Response): Promise => { + const { productId, sizeId } = req.body; + const { id: userId } = req.user as User; + + const transaction = await sequelize.transaction(); + try { + const cartItem: Cart | null = await Cart.findOne({ + where: { userId }, + transaction, + }); + + if (cartItem === null) { + res.status(404).json({ ok: false, message: 'Cart not found' }); + return; + } + + await CartsProducts.destroy({ + where: { cartId: cartItem.id, productId, sizeId }, + transaction, + }); + + await transaction.commit(); + res.status(200).json({ ok: true, message: 'Cart item deleted successfully' }); + } catch (error) { + await transaction.rollback(); + logger.error(error); + sendInternalErrorResponse(res, error); + } +}; +export { addCartItem, updateCartItem, getCartItems, clearCart, deleteCartItem }; diff --git a/src/controllers/chatController.ts b/src/controllers/chatController.ts index 29430b20..cc62f927 100644 --- a/src/controllers/chatController.ts +++ b/src/controllers/chatController.ts @@ -17,7 +17,7 @@ export const chats = async (req: Request, res: Response) => { model: User, attributes: ['firstName', 'lastName', 'photoUrl'], }, - attributes: ['id', 'senderId', 'socketId', 'content', 'updatedAt'], + attributes: ['id', 'senderId', 'content', 'updatedAt'], }); return res.status(200).json({ ok: true, chat }); diff --git a/src/controllers/userController.ts b/src/controllers/userController.ts index 047c1fae..1005b7eb 100644 --- a/src/controllers/userController.ts +++ b/src/controllers/userController.ts @@ -242,7 +242,7 @@ export const editUser = async (req: Request, res: Response) => { photoUrl: updatedFields.photoUrl, }; - res.status(201).json({ ok: true, message: userResponse }); + res.status(201).json({ ok: true, data: userResponse }); } catch (error) { logger.error('Error Edit User Role: ', error); sendInternalErrorResponse(res, error); diff --git a/src/database/migrations/20240712054049-remove-socketId-from-chats.js b/src/database/migrations/20240712054049-remove-socketId-from-chats.js new file mode 100644 index 00000000..7ad1f942 --- /dev/null +++ b/src/database/migrations/20240712054049-remove-socketId-from-chats.js @@ -0,0 +1,15 @@ +'use strict'; + +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + async up(queryInterface) { + await queryInterface.removeColumn('Chats', 'socketId'); + }, + + async down(queryInterface, Sequelize) { + await queryInterface.addColumn('Chats', 'socketId', { + type: Sequelize.STRING, + allowNull: true, + }); + }, +}; diff --git a/src/database/models/chat.ts b/src/database/models/chat.ts index d5c77c5d..1c7c1162 100644 --- a/src/database/models/chat.ts +++ b/src/database/models/chat.ts @@ -6,7 +6,6 @@ interface ChatAttr { id: string; senderId: string; content: string; - socketId: string; readStatus?: boolean; createdAt?: Date | null; updatedAt?: Date | null; @@ -18,7 +17,6 @@ class Chat extends Model implements ChatAttr { public id!: string; public senderId!: string; public content!: string; - public socketId!: string; public readStatus!: boolean; public readonly createdAt: Date | undefined; public readonly updatedAt: Date | undefined; @@ -45,10 +43,7 @@ Chat.init( type: DataTypes.STRING, allowNull: false, }, - socketId: { - type: DataTypes.STRING, - allowNull: false, - }, + readStatus: { type: DataTypes.BOOLEAN, allowNull: true, @@ -66,3 +61,4 @@ Chat.init( Chat.belongsTo(User, { foreignKey: 'senderId' }); export default Chat; +// sequelize migration to remove the column socketId from the Chats table diff --git a/src/routes/cartRoute.ts b/src/routes/cartRoute.ts index 92219c0a..590507cb 100644 --- a/src/routes/cartRoute.ts +++ b/src/routes/cartRoute.ts @@ -1,6 +1,6 @@ import express from 'express'; -import { addCartItem, updateCartItem, getCartItems, clearCart } from '../controllers/cartController'; +import { addCartItem, updateCartItem, getCartItems, clearCart, deleteCartItem } from '../controllers/cartController'; import { checkUserRoles, isAuthenticated } from '../middlewares/authMiddlewares'; @@ -12,5 +12,5 @@ cartRouter .post([isAuthenticated, checkUserRoles('buyer')], addCartItem) .delete([isAuthenticated, checkUserRoles('buyer')], clearCart); cartRouter.route('/:id').patch([isAuthenticated, checkUserRoles('buyer')], updateCartItem); - +cartRouter.route('/delete').delete([isAuthenticated, checkUserRoles('buyer')], deleteCartItem); export default cartRouter;