chore: fix product
This commit is contained in:
@@ -152,7 +152,7 @@ func (db *Database) createTables() error {
|
|||||||
`CREATE TABLE IF NOT EXISTS product_prices (
|
`CREATE TABLE IF NOT EXISTS product_prices (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
product_id INTEGER NOT NULL REFERENCES products(id) ON DELETE CASCADE,
|
product_id INTEGER NOT NULL REFERENCES products(id) ON DELETE CASCADE,
|
||||||
quantity INTEGER NOT NULL,
|
quantity NUMERIC(10,3) NOT NULL,
|
||||||
price NUMERIC(10,2) NOT NULL,
|
price NUMERIC(10,2) NOT NULL,
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
UNIQUE(product_id, quantity)
|
UNIQUE(product_id, quantity)
|
||||||
@@ -211,7 +211,7 @@ func (db *Database) createTables() error {
|
|||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
username VARCHAR(255) NOT NULL,
|
username VARCHAR(255) NOT NULL,
|
||||||
product_id INTEGER NOT NULL REFERENCES products(id) ON DELETE CASCADE,
|
product_id INTEGER NOT NULL REFERENCES products(id) ON DELETE CASCADE,
|
||||||
quantity INTEGER NOT NULL,
|
quantity NUMERIC(10,3) NOT NULL,
|
||||||
price NUMERIC(10,2) NOT NULL,
|
price NUMERIC(10,2) NOT NULL,
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
);`,
|
);`,
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ func (db *Database) CreateProduct(product interface{}) error {
|
|||||||
log.Printf("❌ [DB CreateProduct] Erreur insertion prix[%d]: %v", i, err)
|
log.Printf("❌ [DB CreateProduct] Erreur insertion prix[%d]: %v", i, err)
|
||||||
return fmt.Errorf("erreur insertion prix: %v", err)
|
return fmt.Errorf("erreur insertion prix: %v", err)
|
||||||
}
|
}
|
||||||
log.Printf("✅ [DB CreateProduct] Prix[%d] inséré: quantity=%d, price=%.2f",
|
log.Printf("✅ [DB CreateProduct] Prix[%d] inséré: quantity=%g, price=%.2f",
|
||||||
i, price.Quantity, price.Price)
|
i, price.Quantity, price.Price)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ func (db *Database) GetProductPrices(productID int) ([]models.ProductPrice, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateProductPrice ajoute un nouveau prix pour un produit
|
// CreateProductPrice ajoute un nouveau prix pour un produit
|
||||||
func (db *Database) CreateProductPrice(productID, quantity int, price float64) error {
|
func (db *Database) CreateProductPrice(productID int, quantity float64, price float64) error {
|
||||||
query := `INSERT INTO product_prices (product_id, quantity, price)
|
query := `INSERT INTO product_prices (product_id, quantity, price)
|
||||||
VALUES ($1, $2, $3)`
|
VALUES ($1, $2, $3)`
|
||||||
_, err := db.Exec(query, productID, quantity, price)
|
_, err := db.Exec(query, productID, quantity, price)
|
||||||
@@ -49,7 +49,7 @@ func (db *Database) CreateProductPrice(productID, quantity int, price float64) e
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateProductPrice met à jour un prix
|
// UpdateProductPrice met à jour un prix
|
||||||
func (db *Database) UpdateProductPrice(priceID, quantity int, price float64) error {
|
func (db *Database) UpdateProductPrice(priceID int, quantity float64, price float64) error {
|
||||||
query := `UPDATE product_prices SET quantity = $1, price = $2 WHERE id = $3`
|
query := `UPDATE product_prices SET quantity = $1, price = $2 WHERE id = $3`
|
||||||
result, err := db.Exec(query, quantity, price, priceID)
|
result, err := db.Exec(query, quantity, price, priceID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ func validateStock(stock float64) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func validatePrice(quantity int, price float64) error {
|
func validatePrice(quantity float64, price float64) error {
|
||||||
if quantity <= 0 {
|
if quantity <= 0 {
|
||||||
return fmt.Errorf("quantité doit être > 0")
|
return fmt.Errorf("quantité doit être > 0")
|
||||||
}
|
}
|
||||||
@@ -258,7 +258,7 @@ func CreateProduct(c *gin.Context) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
quantity, err := strconv.Atoi(quantityStr)
|
quantity, err := strconv.ParseFloat(quantityStr, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Quantité invalide"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Quantité invalide"})
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -17,19 +17,19 @@ type Product struct {
|
|||||||
type ProductPrice struct {
|
type ProductPrice struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
ProductID int `json:"product_id"`
|
ProductID int `json:"product_id"`
|
||||||
Quantity int `json:"quantity" binding:"required"`
|
Quantity float64 `json:"quantity" binding:"required"`
|
||||||
Price float64 `json:"price" binding:"required"`
|
Price float64 `json:"price" binding:"required"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type StockInfo struct {
|
type StockInfo struct {
|
||||||
ProductID int `json:"product_id"`
|
ProductID int `json:"product_id"`
|
||||||
ProductName string `json:"product_name"`
|
ProductName string `json:"product_name"`
|
||||||
Category string `json:"category"`
|
Category string `json:"category"`
|
||||||
Quantity int `json:"quantity"`
|
Quantity float64 `json:"quantity"`
|
||||||
Reserved int `json:"reserved"`
|
Reserved float64 `json:"reserved"`
|
||||||
Available int `json:"available"`
|
Available float64 `json:"available"`
|
||||||
LastUpdated string `json:"last_updated"`
|
LastUpdated string `json:"last_updated"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================
|
// ============================================
|
||||||
|
|||||||
@@ -76,7 +76,11 @@ export const getMyDeliveries = async (): Promise<{
|
|||||||
}> => {
|
}> => {
|
||||||
try {
|
try {
|
||||||
const { data } = await apiClient.get(`${API}/deliveries`);
|
const { data } = await apiClient.get(`${API}/deliveries`);
|
||||||
return { success: true, deliveries: data.deliveries || [] };
|
// Le backend inclut déjà items et client_info dans chaque livraison
|
||||||
|
const deliveries: DeliveryItem[] = (data.deliveries || []).map(
|
||||||
|
(d: any) => ({ ...d, items: d.items || [] }),
|
||||||
|
);
|
||||||
|
return { success: true, deliveries };
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
@@ -94,11 +98,17 @@ export const getDeliveryDetails = async (
|
|||||||
}> => {
|
}> => {
|
||||||
try {
|
try {
|
||||||
const { data } = await apiClient.get(`${API}/deliveries/${deliveryId}`);
|
const { data } = await apiClient.get(`${API}/deliveries/${deliveryId}`);
|
||||||
|
// Le backend retourne { delivery: { id, status, adresse, items, ... }, success: true }
|
||||||
|
// Les items sont dans data.delivery.items, pas dans data.client_info
|
||||||
|
const deliveryObj = data.delivery || {};
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
delivery: {
|
delivery: {
|
||||||
delivery: data.delivery,
|
delivery: {
|
||||||
client_info: data.client_info,
|
...deliveryObj,
|
||||||
|
items: deliveryObj.items || [],
|
||||||
|
},
|
||||||
|
client_info: deliveryObj.client_info || data.client_info || {},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
|||||||
@@ -112,6 +112,12 @@ export interface QueueInfo {
|
|||||||
commands: any[];
|
commands: any[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface DeliveryItemProduct {
|
||||||
|
produit: string;
|
||||||
|
quantite: number;
|
||||||
|
prix: number;
|
||||||
|
}
|
||||||
|
|
||||||
export interface DeliveryItem {
|
export interface DeliveryItem {
|
||||||
id: number;
|
id: number;
|
||||||
status: string;
|
status: string;
|
||||||
@@ -120,6 +126,8 @@ export interface DeliveryItem {
|
|||||||
created_at: string;
|
created_at: string;
|
||||||
updated_at: string;
|
updated_at: string;
|
||||||
eta?: string;
|
eta?: string;
|
||||||
|
items?: DeliveryItemProduct[];
|
||||||
|
items_count?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ClientInfo {
|
export interface ClientInfo {
|
||||||
|
|||||||
@@ -970,7 +970,7 @@ export default function ProductsScreen() {
|
|||||||
v,
|
v,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
keyboardType="numeric"
|
keyboardType="decimal-pad"
|
||||||
placeholderTextColor={
|
placeholderTextColor={
|
||||||
colors.textMuted
|
colors.textMuted
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -228,7 +228,7 @@ export default function DashboardScreen() {
|
|||||||
: client?.username || undefined,
|
: client?.username || undefined,
|
||||||
clientPhone: client?.telephone || undefined,
|
clientPhone: client?.telephone || undefined,
|
||||||
items:
|
items:
|
||||||
(detail.delivery as any)?.items ||
|
detail.delivery?.items ||
|
||||||
(d as any).items ||
|
(d as any).items ||
|
||||||
[],
|
[],
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user