added totalQuantity in stock page

This commit is contained in:
2025-11-30 17:20:14 +01:00
parent b7bd3be6a3
commit 9b49f5fe11
2 changed files with 39 additions and 11 deletions

View File

@@ -43,9 +43,8 @@
<td>{{ product.caliber }}</td>
<td>{{ product.weight }}</td>
<td>{{ product.duration }}</td>
<td> Quantité totale ??? </td>
<td>{{ product.totalQuantity }}</td>
<td>{{ product.minimalQuantity }}</td>
<td>
<div style="justify-content: center; display: flex">
<nz-icon nzType="edit" nzTheme="outline" class="cursor-pointer" (click)="openEditModal(product)"></nz-icon>

View File

@@ -6,10 +6,14 @@ import {StockForm} from "../stock-form/stock-form";
import {NzDividerComponent} from "ng-zorro-antd/divider";
import {FormsModule} from "@angular/forms";
import {NzCheckboxComponent} from "ng-zorro-antd/checkbox";
import {GetProductDto, ProductsService} from "../../services/api";
import {GetProductDto, ProductsService, WarehouseproductsService} from "../../services/api";
import {NzNotificationService} from "ng-zorro-antd/notification";
import {firstValueFrom} from "rxjs";
interface ProductWithQuantity extends GetProductDto {
totalQuantity?: number;
}
@Component({
selector: 'app-stock-table',
imports: [
@@ -28,8 +32,9 @@ import {firstValueFrom} from "rxjs";
export class StockTable implements OnInit {
private productsService = inject(ProductsService);
private wareHousseProductsService = inject(WarehouseproductsService)
private notificationService = inject(NzNotificationService)
products = signal<GetProductDto[]>([]);
products = signal<ProductWithQuantity[]>([]);
productsLoading = signal<boolean>(false);
updateProduct = viewChild.required<StockForm>('stockForm');
modal = viewChild.required<ModalNav>('modalNav');
@@ -84,19 +89,43 @@ export class StockTable implements OnInit {
await this.fetchProducts();
}
async fetchProducts() {
this.productsLoading.set(true)
async fetchTotalQuantity(product: ProductWithQuantity) {
try {
const products = await firstValueFrom(this.productsService.getAllProductsEndpoint())
this.products.set(products);
const res = await firstValueFrom(
this.wareHousseProductsService.getTotalQuantityEndpoint(product.id)
);
product.totalQuantity = res.totalQuantity;
} catch (e) {
product.totalQuantity = 0;
this.notificationService.error(
'Erreur',
`Impossible de récupérer la quantité pour le produit n°${product.id}`
);
}
}
async fetchProducts() {
this.productsLoading.set(true);
try {
const products = await firstValueFrom(this.productsService.getAllProductsEndpoint());
// transforme chaque produit en ProductWithQuantity
const productsWithQuantity: ProductWithQuantity[] = products.map(p => ({ ...p }));
this.products.set(productsWithQuantity);
// récupérer la quantité pour chaque produit en parallèle
await Promise.all(productsWithQuantity.map(p => this.fetchTotalQuantity(p)));
// déclencher la mise à jour du signal
this.products.set([...productsWithQuantity]);
} catch (e) {
this.notificationService.error(
'Erreur',
'Erreur de communication avec l\'API'
)
);
}
this.productsLoading.set(false)
this.productsLoading.set(false);
}
async delete(productId:number) {