From 088acc515d729f4ec994b331a96e77a10a3fe69f Mon Sep 17 00:00:00 2001 From: mahdi Date: Sun, 12 Jan 2025 14:18:14 +0330 Subject: [PATCH] Updating shopping-card module --- src/modules/orders/dto/create-order.dto.ts | 4 +- src/modules/orders/entities/order.entity.ts | 2 +- src/modules/products/products.controller.ts | 5 ++ src/modules/products/products.module.ts | 4 ++ src/modules/products/products.service.ts | 8 ++++ .../entities/shopping-card.entity.ts | 2 +- .../shopping-cards/shopping-cards.module.ts | 4 ++ .../shopping-cards/shopping-cards.service.ts | 48 +++++++++++++++---- 8 files changed, 63 insertions(+), 14 deletions(-) diff --git a/src/modules/orders/dto/create-order.dto.ts b/src/modules/orders/dto/create-order.dto.ts index f1c1806..3d61442 100644 --- a/src/modules/orders/dto/create-order.dto.ts +++ b/src/modules/orders/dto/create-order.dto.ts @@ -1,9 +1,9 @@ export class CreateOrderDto { userId: number; - orderDate: Date; + orderDate?: Date; status: number; comments: string; - shippedDate: Date; + shippedDate?: Date; shipperName: string; products: number[]; } diff --git a/src/modules/orders/entities/order.entity.ts b/src/modules/orders/entities/order.entity.ts index 72271b3..665f7d1 100644 --- a/src/modules/orders/entities/order.entity.ts +++ b/src/modules/orders/entities/order.entity.ts @@ -14,7 +14,7 @@ import { Product } from 'src/modules/products/entities/product.entity'; export class Order extends Model { @Column({ type: DataType.DATE, - allowNull: false, + allowNull: true, defaultValue: new Date(), }) orderDate: Date; diff --git a/src/modules/products/products.controller.ts b/src/modules/products/products.controller.ts index e5e403f..c3bf95e 100644 --- a/src/modules/products/products.controller.ts +++ b/src/modules/products/products.controller.ts @@ -22,6 +22,11 @@ export class ProductsController { return this.productsService.create(createProductDto); } + @Post('add/:id/:user') + addProduct(@Param('id') id: number, @Param('user') userId: number) { + return this.productsService.addProduct(id, userId); + } + @Get() findAll() { return this.productsService.findAll(); diff --git a/src/modules/products/products.module.ts b/src/modules/products/products.module.ts index bf78394..3a49d58 100644 --- a/src/modules/products/products.module.ts +++ b/src/modules/products/products.module.ts @@ -6,6 +6,8 @@ import { ShoppingCardsService } from '../shopping-cards/shopping-cards.service'; import { ReceiptsService } from '../receipts/receipts.service'; import { shoppingCardsProviders } from '../shopping-cards/shopping-cards.providers'; import { receiptsProviders } from '../receipts/receipts.providers'; +import { OrdersService } from '../orders/orders.service'; +import { ordersProviders } from '../orders/orders.providers'; @Module({ controllers: [ProductsController], @@ -17,6 +19,8 @@ import { receiptsProviders } from '../receipts/receipts.providers'; ReceiptsService, ...shoppingCardsProviders, ...receiptsProviders, + OrdersService, + ...ordersProviders, ], }) export class ProductsModule {} diff --git a/src/modules/products/products.service.ts b/src/modules/products/products.service.ts index fb8af1e..be4335a 100644 --- a/src/modules/products/products.service.ts +++ b/src/modules/products/products.service.ts @@ -2,12 +2,14 @@ import { Inject, Injectable } from '@nestjs/common'; import { CreateProductDto, UpdateProductDto } from './dto'; import { PRODUCT_REPOSITORY } from 'src/core/constants'; import { Product } from './entities/product.entity'; +import { ShoppingCardsService } from '../shopping-cards/shopping-cards.service'; @Injectable() export class ProductsService { constructor( @Inject(PRODUCT_REPOSITORY) private readonly productRepository: typeof Product, + // private readonly shoppingCardsService: ShoppingCardsService, ) {} async create(createProductDto: CreateProductDto) { @@ -38,4 +40,10 @@ export class ProductsService { return deletedProduct; } + + async addProduct(id: number, userId: number) { + // const [card] = await this.shoppingCardsService.findbyUser(userId); + // card.products.push(id); + // return await card.save(); + } } diff --git a/src/modules/shopping-cards/entities/shopping-card.entity.ts b/src/modules/shopping-cards/entities/shopping-card.entity.ts index 947e7c8..8d25633 100644 --- a/src/modules/shopping-cards/entities/shopping-card.entity.ts +++ b/src/modules/shopping-cards/entities/shopping-card.entity.ts @@ -6,7 +6,6 @@ import { ForeignKey, BelongsTo, } from 'sequelize-typescript'; -import { Product } from 'src/modules/products/entities/product.entity'; import { User } from 'src/modules/users/entities/user.entity'; @Table({ tableName: 'shopping_cards' }) @@ -14,6 +13,7 @@ export class ShoppingCard extends Model { @Column({ type: DataType.FLOAT, allowNull: false, + defaultValue: 0, }) totalPrice: number; diff --git a/src/modules/shopping-cards/shopping-cards.module.ts b/src/modules/shopping-cards/shopping-cards.module.ts index 9bf9917..83fa711 100644 --- a/src/modules/shopping-cards/shopping-cards.module.ts +++ b/src/modules/shopping-cards/shopping-cards.module.ts @@ -6,6 +6,8 @@ import { ReceiptsService } from '../receipts/receipts.service'; import { receiptsProviders } from '../receipts/receipts.providers'; import { ProductsService } from '../products/products.service'; import { productsProviders } from '../products/products.providers'; +import { OrdersService } from '../orders/orders.service'; +import { ordersProviders } from '../orders/orders.providers'; @Module({ controllers: [ShoppingCardsController], @@ -16,6 +18,8 @@ import { productsProviders } from '../products/products.providers'; ...receiptsProviders, ProductsService, ...productsProviders, + OrdersService, + ...ordersProviders, ], exports: [ShoppingCardsService], }) diff --git a/src/modules/shopping-cards/shopping-cards.service.ts b/src/modules/shopping-cards/shopping-cards.service.ts index 2bd0d97..ca0d02f 100644 --- a/src/modules/shopping-cards/shopping-cards.service.ts +++ b/src/modules/shopping-cards/shopping-cards.service.ts @@ -6,6 +6,8 @@ import { ShoppingCard } from './entities/shopping-card.entity'; import { CreateReceiptDto } from '../receipts/dto'; import { ReceiptsService } from '../receipts/receipts.service'; import { ProductsService } from '../products/products.service'; +import { OrdersService } from '../orders/orders.service'; +import { CreateOrderDto } from '../orders/dto'; @Injectable() export class ShoppingCardsService { @@ -14,9 +16,19 @@ export class ShoppingCardsService { private readonly shoppingCardRepository: typeof ShoppingCard, private readonly receiptService: ReceiptsService, private readonly productsService: ProductsService, + private readonly ordersService: OrdersService, ) {} async create(createShoppingCardDto: CreateShoppingCardDto) { - return await this.shoppingCardRepository.create(createShoppingCardDto); + const card = await this.shoppingCardRepository.create( + createShoppingCardDto, + ); + + for (let p of card.products) { + const [product] = await this.productsService.findOne(p); + card.totalPrice += product.pricePerUnit; + } + + return await card.save(); } async findAll() { @@ -33,7 +45,19 @@ export class ShoppingCardsService { { ...updateShoppingCardDto }, { where: { userId: id }, returning: true }, ); - return { numberOfAffectedRows, updatedCard }; + + const [card] = await this.shoppingCardRepository.findAll({ + where: { userId: id }, + }); + + card.totalPrice = 0; + + for (let p of card.products) { + const [product] = await this.productsService.findOne(p); + card.totalPrice += product.pricePerUnit; + } + + return card.save(); } async remove(id: number) { @@ -43,18 +67,22 @@ export class ShoppingCardsService { async submitPurchase(id: number) { const [card] = await this.findbyUser(id); - let total = 0; - for (let i of card.products) { - const [product] = await this.productsService.findOne(i); - total += product.pricePerUnit; - } - await this.createReceipt({ userId: id, products: card.products, - totalPrice: total, + totalPrice: card.totalPrice, }); - return await this.remove(id); + + await this.remove(id); + + const order: CreateOrderDto = { + userId: id, + status: 1, + comments: 'ship asap', + shipperName: 'meti comp', + products: card.products, + }; + return await this.ordersService.create(order); } private async createReceipt(createReceiptDto: CreateReceiptDto) {