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