diff --git a/src/controllers/orderController.ts b/src/controllers/orderController.ts index 74026b57..f04466ce 100644 --- a/src/controllers/orderController.ts +++ b/src/controllers/orderController.ts @@ -39,7 +39,7 @@ export const createOrder = async (req: Request, res: Response): Promise => } const [orderItems]: any = await sequelize.query( - 'SELECT "productId", quantity FROM "CartsProducts" WHERE "cartId" = ?', + 'SELECT "productId", "sizeId", quantity FROM "CartsProducts" WHERE "cartId" = ?', { replacements: [cart.id], } @@ -47,27 +47,32 @@ export const createOrder = async (req: Request, res: Response): Promise => const sizes = await Promise.all( orderItems.map(async (item: any) => { - const product = await Product.findOne({ - where: { id: item.productId }, - include: { - model: Size, - as: 'sizes', - attributes: ['price'], - }, + const productSize = await Size.findOne({ + where: { id: item.sizeId }, + attributes: ['id', 'price', 'discount'], }); - if (!product) { - throw new Error(`Product with id ${item.productId} not found`); + if (!productSize) { + throw new Error(`Size with id ${item.sizeId} not found`); } - const size = product.sizes[0]; - const totalSum = size.price * item.quantity; + const discountDecimal = 1 - productSize.discount / 100; + const discountedPrice = productSize.price * discountDecimal; + const sizeQtyPrice = discountedPrice * item.quantity; - return { productId: item.productId, quantity: item.quantity, price: size.price, totalSum }; + return { + productId: item.productId, + sizeId: productSize.id, + quantity: item.quantity, + price: discountedPrice, + sizeQtyPrice, + }; }) ); - const totalPrice = sizes.reduce((prev: number, cur: any) => prev + cur.totalSum, 0); + // combine all products sizes retrieved by flat() + const combinedproductsAndSizes = sizes.flat(); + const totalPrice = combinedproductsAndSizes.reduce((prev: number, curr: any) => prev + curr.sizeQtyPrice, 0); const order = await Order.create({ phone, @@ -81,10 +86,11 @@ export const createOrder = async (req: Request, res: Response): Promise => }); await Promise.all( - sizes.map(async (item: any) => + combinedproductsAndSizes.map(async (item: any) => OrderItems.create({ orderId: order.id, productId: item.productId, + sizeId: item.sizeId, quantity: item.quantity, price: item.price, }) diff --git a/src/controllers/productsController.ts b/src/controllers/productsController.ts index 00fd53b2..a1eb0245 100644 --- a/src/controllers/productsController.ts +++ b/src/controllers/productsController.ts @@ -13,9 +13,7 @@ import User from '../database/models/user'; import { sendEmail } from '../helpers/send-email'; import { destroyImage } from '../helpers/destroyImage'; import { extractImageId } from '../helpers/extractImageId'; -// import Review, { ReviewAttributes } from '../database/models/Review'; import Review, { ReviewAttributes } from '../database/models/Review'; -import Cart from '../database/models/cart'; import Order from '../database/models/order'; import OrderItems from '../database/models/orderItems'; diff --git a/src/database/migrations/20240527203432-add-sizeId-to-orderItems.js b/src/database/migrations/20240527203432-add-sizeId-to-orderItems.js new file mode 100644 index 00000000..6265586f --- /dev/null +++ b/src/database/migrations/20240527203432-add-sizeId-to-orderItems.js @@ -0,0 +1,20 @@ +'use strict'; + +module.exports = { + up: async (queryInterface, Sequelize) => { + await queryInterface.addColumn('orderItems', 'sizeId', { + type: Sequelize.UUID, + allowNull: false, + references: { + model: 'sizes', + key: 'id', + }, + onUpdate: 'CASCADE', + onDelete: 'CASCADE', + }); + }, + + down: async (queryInterface, Sequelize) => { + await queryInterface.removeColumn('orderItems', 'sizeId'); + }, +}; diff --git a/src/database/models/orderItems.ts b/src/database/models/orderItems.ts index 2cafc934..3b3cd22e 100644 --- a/src/database/models/orderItems.ts +++ b/src/database/models/orderItems.ts @@ -2,11 +2,13 @@ import { DataTypes, Model, Optional, UUIDV4 } from 'sequelize'; import sequelize from './index'; import Order from './order'; import { Product } from './Product'; +import { Size } from './Size'; interface OrderItemsAttributes { id: string; orderId: string; productId: string; + sizeId: string; quantity: number; price: number; createdAt?: Date; @@ -19,6 +21,7 @@ class OrderItems extends Model