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 { export class CreateOrderDto {
userId: number; userId: number;
orderDate: Date; orderDate?: Date;
status: number; status: number;
comments: string; comments: string;
shippedDate: Date; shippedDate?: Date;
shipperName: string; shipperName: string;
products: number[]; products: number[];
} }

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

@ -22,6 +22,11 @@ export class ProductsController {
return this.productsService.create(createProductDto); 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() @Get()
findAll() { findAll() {
return this.productsService.findAll(); return this.productsService.findAll();

@ -6,6 +6,8 @@ import { ShoppingCardsService } from '../shopping-cards/shopping-cards.service';
import { ReceiptsService } from '../receipts/receipts.service'; import { ReceiptsService } from '../receipts/receipts.service';
import { shoppingCardsProviders } from '../shopping-cards/shopping-cards.providers'; import { shoppingCardsProviders } from '../shopping-cards/shopping-cards.providers';
import { receiptsProviders } from '../receipts/receipts.providers'; import { receiptsProviders } from '../receipts/receipts.providers';
import { OrdersService } from '../orders/orders.service';
import { ordersProviders } from '../orders/orders.providers';
@Module({ @Module({
controllers: [ProductsController], controllers: [ProductsController],
@ -17,6 +19,8 @@ import { receiptsProviders } from '../receipts/receipts.providers';
ReceiptsService, ReceiptsService,
...shoppingCardsProviders, ...shoppingCardsProviders,
...receiptsProviders, ...receiptsProviders,
OrdersService,
...ordersProviders,
], ],
}) })
export class ProductsModule {} export class ProductsModule {}

@ -2,12 +2,14 @@ import { Inject, Injectable } from '@nestjs/common';
import { CreateProductDto, UpdateProductDto } from './dto'; import { CreateProductDto, UpdateProductDto } from './dto';
import { PRODUCT_REPOSITORY } from 'src/core/constants'; import { PRODUCT_REPOSITORY } from 'src/core/constants';
import { Product } from './entities/product.entity'; import { Product } from './entities/product.entity';
import { ShoppingCardsService } from '../shopping-cards/shopping-cards.service';
@Injectable() @Injectable()
export class ProductsService { export class ProductsService {
constructor( constructor(
@Inject(PRODUCT_REPOSITORY) @Inject(PRODUCT_REPOSITORY)
private readonly productRepository: typeof Product, private readonly productRepository: typeof Product,
// private readonly shoppingCardsService: ShoppingCardsService,
) {} ) {}
async create(createProductDto: CreateProductDto) { async create(createProductDto: CreateProductDto) {
@ -38,4 +40,10 @@ export class ProductsService {
return deletedProduct; 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, ForeignKey,
BelongsTo, BelongsTo,
} from 'sequelize-typescript'; } from 'sequelize-typescript';
import { Product } from 'src/modules/products/entities/product.entity';
import { User } from 'src/modules/users/entities/user.entity'; import { User } from 'src/modules/users/entities/user.entity';
@Table({ tableName: 'shopping_cards' }) @Table({ tableName: 'shopping_cards' })
@ -14,6 +13,7 @@ export class ShoppingCard extends Model<ShoppingCard> {
@Column({ @Column({
type: DataType.FLOAT, type: DataType.FLOAT,
allowNull: false, allowNull: false,
defaultValue: 0,
}) })
totalPrice: number; totalPrice: number;

@ -6,6 +6,8 @@ import { ReceiptsService } from '../receipts/receipts.service';
import { receiptsProviders } from '../receipts/receipts.providers'; import { receiptsProviders } from '../receipts/receipts.providers';
import { ProductsService } from '../products/products.service'; import { ProductsService } from '../products/products.service';
import { productsProviders } from '../products/products.providers'; import { productsProviders } from '../products/products.providers';
import { OrdersService } from '../orders/orders.service';
import { ordersProviders } from '../orders/orders.providers';
@Module({ @Module({
controllers: [ShoppingCardsController], controllers: [ShoppingCardsController],
@ -16,6 +18,8 @@ import { productsProviders } from '../products/products.providers';
...receiptsProviders, ...receiptsProviders,
ProductsService, ProductsService,
...productsProviders, ...productsProviders,
OrdersService,
...ordersProviders,
], ],
exports: [ShoppingCardsService], exports: [ShoppingCardsService],
}) })

@ -6,6 +6,8 @@ import { ShoppingCard } from './entities/shopping-card.entity';
import { CreateReceiptDto } from '../receipts/dto'; import { CreateReceiptDto } from '../receipts/dto';
import { ReceiptsService } from '../receipts/receipts.service'; import { ReceiptsService } from '../receipts/receipts.service';
import { ProductsService } from '../products/products.service'; import { ProductsService } from '../products/products.service';
import { OrdersService } from '../orders/orders.service';
import { CreateOrderDto } from '../orders/dto';
@Injectable() @Injectable()
export class ShoppingCardsService { export class ShoppingCardsService {
@ -14,9 +16,19 @@ export class ShoppingCardsService {
private readonly shoppingCardRepository: typeof ShoppingCard, private readonly shoppingCardRepository: typeof ShoppingCard,
private readonly receiptService: ReceiptsService, private readonly receiptService: ReceiptsService,
private readonly productsService: ProductsService, private readonly productsService: ProductsService,
private readonly ordersService: OrdersService,
) {} ) {}
async create(createShoppingCardDto: CreateShoppingCardDto) { 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() { async findAll() {
@ -33,7 +45,19 @@ export class ShoppingCardsService {
{ ...updateShoppingCardDto }, { ...updateShoppingCardDto },
{ where: { userId: id }, returning: true }, { 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) { async remove(id: number) {
@ -43,18 +67,22 @@ export class ShoppingCardsService {
async submitPurchase(id: number) { async submitPurchase(id: number) {
const [card] = await this.findbyUser(id); 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({ await this.createReceipt({
userId: id, userId: id,
products: card.products, 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) { private async createReceipt(createReceiptDto: CreateReceiptDto) {

Loading…
Cancel
Save