Updating shopping-card module

master
mahdi 1 week ago
parent 7a46dd79ff
commit 088acc515d
  1. 4
      src/modules/orders/dto/create-order.dto.ts
  2. 2
      src/modules/orders/entities/order.entity.ts
  3. 5
      src/modules/products/products.controller.ts
  4. 4
      src/modules/products/products.module.ts
  5. 8
      src/modules/products/products.service.ts
  6. 2
      src/modules/shopping-cards/entities/shopping-card.entity.ts
  7. 4
      src/modules/shopping-cards/shopping-cards.module.ts
  8. 48
      src/modules/shopping-cards/shopping-cards.service.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[];
}

@ -14,7 +14,7 @@ import { Product } from 'src/modules/products/entities/product.entity';
export class Order extends Model<Order> {
@Column({
type: DataType.DATE,
allowNull: false,
allowNull: true,
defaultValue: new Date(),
})
orderDate: Date;

@ -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();

@ -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 {}

@ -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();
}
}

@ -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<ShoppingCard> {
@Column({
type: DataType.FLOAT,
allowNull: false,
defaultValue: 0,
})
totalPrice: number;

@ -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],
})

@ -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) {

Loading…
Cancel
Save