|
|
@ -17,43 +17,36 @@ export class InvoiceService { |
|
|
|
if (!user) { |
|
|
|
if (!user) { |
|
|
|
throw new HttpException("User not found", HttpStatus.NOT_FOUND); |
|
|
|
throw new HttpException("User not found", HttpStatus.NOT_FOUND); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const userCartItems = await this.cartService.getUserCart(userId); |
|
|
|
const userCartItems = await this.cartService.getUserCart(userId); |
|
|
|
if (!userCartItems || userCartItems.cartItems.length === 0) { |
|
|
|
if (!userCartItems || userCartItems.cartItems.length === 0) { |
|
|
|
throw new HttpException("Cart is empty", HttpStatus.BAD_REQUEST); |
|
|
|
throw new HttpException("Cart is empty", HttpStatus.BAD_REQUEST); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const products = userCartItems.cartItems.map(item => { |
|
|
|
const invoices: Invoice[] = [];
|
|
|
|
return { |
|
|
|
|
|
|
|
productId: item.productId, |
|
|
|
for (const cartItem of userCartItems.cartItems) { |
|
|
|
quantity: item.quantity, |
|
|
|
|
|
|
|
price: item.productPrice, |
|
|
|
|
|
|
|
productName: item.productName, |
|
|
|
|
|
|
|
totalPrice: item.totalPrice, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ذخیره کردن فاکتورهای هر محصول و بازگشت آرایه فاکتورها
|
|
|
|
|
|
|
|
const invoices: Invoice[] = []; |
|
|
|
|
|
|
|
for (const product of products) { |
|
|
|
|
|
|
|
const invoice = await this.invoiceModel.create({ |
|
|
|
const invoice = await this.invoiceModel.create({ |
|
|
|
userId, |
|
|
|
userId, |
|
|
|
firstName: user.firstName, |
|
|
|
firstName: user.firstName, |
|
|
|
lastName: user.lastName, |
|
|
|
lastName: user.lastName, |
|
|
|
phoneNumber: user.phoneNumber, |
|
|
|
phoneNumber: user.phoneNumber, |
|
|
|
email: user.email, |
|
|
|
email: user.email, |
|
|
|
totalAmount: userCartItems.totalPrice, |
|
|
|
totalPaymentAmount: userCartItems.totalPrice, |
|
|
|
productId: product.productId, |
|
|
|
productId: cartItem.productId, |
|
|
|
quantity: product.quantity, |
|
|
|
quantity: cartItem.quantity, |
|
|
|
price: product.price, |
|
|
|
price: cartItem.productPrice, |
|
|
|
productName: product.productName, |
|
|
|
totalPrice:(cartItem.quantity*cartItem.productPrice), |
|
|
|
|
|
|
|
productName: cartItem.productName, |
|
|
|
}); |
|
|
|
}); |
|
|
|
invoices.push(invoice); // ذخیره فاکتور برای هر محصول
|
|
|
|
|
|
|
|
|
|
|
|
invoices.push(invoice); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return invoices; |
|
|
|
return invoices; // بازگرداندن آرایهای از فاکتورها
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async getInvoicesByUser(userId: number): Promise<Invoice[]> { |
|
|
|
async getInvoicesByUser(userId: number): Promise<Invoice[]> { |
|
|
|
try { |
|
|
|
try { |
|
|
|
if (!userId) { |
|
|
|
if (!userId) { |
|
|
|