added quantity used

This commit is contained in:
StefanoPutelli 2025-07-02 22:29:39 +02:00
parent d2c44f8386
commit a036e33fd4
10 changed files with 189 additions and 32 deletions

View file

@ -6,15 +6,17 @@ export interface IItem extends Document {
description: string;
tags: Types.Array<ITag['_id']>;
quantity: number; // ← nuovo
used: number; // ← nuovo, opzionale
dateAdded: Date;
addedBy: string;
}
const itemSchema = new Schema<IItem>({
name: { type: String, required: true },
description: { type: String, required: true },
description: { type: String, required: false },
tags: [{ type: Schema.Types.ObjectId, ref: 'Tag' }],
quantity: { type: Number, default: 0, min: 0 }, // default 0, mai negativo
used: { type: Number, default: 0 }, // opzionale, default false
dateAdded: { type: Date, default: Date.now },
addedBy: { type: String },
});

View file

@ -1,6 +1,6 @@
import { Router } from 'express';
import Item from '../models/Item.js';
import Tag from '../models/Tag.js';
import Tag from '../models/Tag.js';
import auth from '../middleware/auth.js';
const router = Router();
@ -18,7 +18,7 @@ router.get('/search', async (req, res) => {
tagIds?: string;
};
const regex = new RegExp(query, 'i');
const regex = new RegExp(query, 'i');
const filter: any = {
$and: [
{ $or: [{ name: regex }, { description: regex }] },
@ -31,11 +31,12 @@ router.get('/search', async (req, res) => {
/* ──────────── POST: crea item ──────────── */
router.post('/', auth, async (req, res) => {
const { name, description, tagIds, quantity = 0 } = req.body as {
const { name, description, tagIds, quantity = 0, used = false } = req.body as {
name: string;
description: string;
tagIds: string[];
quantity?: number;
used?: number;
};
const tags = await Tag.find({ _id: { $in: tagIds } });
@ -44,6 +45,7 @@ router.post('/', auth, async (req, res) => {
description,
tags,
quantity,
used,
addedBy: req.user!.email,
});
@ -61,10 +63,10 @@ router.put('/:id', auth, async (req, res) => {
};
const update: any = {};
if (name !== undefined) update.name = name;
if (name !== undefined) update.name = name;
if (description !== undefined) update.description = description;
if (quantity !== undefined) update.quantity = quantity;
if (tagIds) update.tags = await Tag.find({ _id: { $in: tagIds } });
if (quantity !== undefined) update.quantity = quantity;
if (tagIds) update.tags = await Tag.find({ _id: { $in: tagIds } });
const item = await Item.findByIdAndUpdate(id, update, { new: true }).populate('tags');
if (!item) {
@ -84,17 +86,46 @@ router.patch('/:id/quantity', auth, async (req, res) => {
return;
}
const item = await Item.findByIdAndUpdate(
id,
{ quantity },
{ new: true }
).populate('tags');
const item = await Item.findById(id);
if (!item) {
res.status(404).json({ message: 'Item non trovato' });
return;
}
res.json(item);
if (item.used > quantity) {
res.status(400).json({ message: 'La quantità non può essere inferiore alla quantità utilizzata' });
return;
}
item.quantity = quantity;
await item.save();
res.json(await item.populate('tags'));
});
/* ──────────── PATCH: modifica solo la quantità utilizzata ──────────── */
router.patch('/:id/usedquantity', auth, async (req, res) => {
const { id } = req.params;
const { quantity } = req.body as { quantity: number };
if (quantity < 0) {
res.status(400).json({ message: 'La quantità utilizzata non può essere negativa' });
return;
}
const item = await Item.findById(id);
if (!item) {
res.status(404).json({ message: 'Item non trovato' });
return;
}
if (quantity > item.quantity) {
res.status(400).json({ message: 'La quantità utilizzata non può essere maggiore della quantità totale' });
return;
}
item.used = quantity;
await item.save();
res.json(await item.populate('tags'));
});
/* ──────────── DELETE: elimina item ──────────── */

View file

@ -25,7 +25,7 @@ services:
depends_on:
- backend
ports:
- "80:80"
- "2222:80"
volumes:

View file

@ -3,6 +3,7 @@ import { useState } from 'react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Textarea } from '@/components/ui/textarea';
import { Checkbox } from './ui/checkbox';
import { Badge } from '@/components/ui/badge';
import {
Dialog,
@ -28,6 +29,8 @@ export const AddItemDialog = ({ open, onOpenChange, tags, onItemAdded }: AddItem
const [name, setName] = useState('');
const [description, setDescription] = useState('');
const [selectedTagIds, setSelectedTagIds] = useState<string[]>([]);
const [qt, setQt] = useState(1); // Default quantity set to 1
const [used, setUsed] = useState(0); // Assuming this is for used tags, not implemented in the original code
const [isLoading, setIsLoading] = useState(false);
const { user } = useAuth();
@ -51,7 +54,8 @@ export const AddItemDialog = ({ open, onOpenChange, tags, onItemAdded }: AddItem
const newItem = await inventoryService.createItem({
name: name.trim(),
description: description.trim(),
quantity: 1, // Default quantity set to 1
quantity: qt, // Default quantity set to 1
used: used,
tagIds: selectedTagIds
});
@ -119,8 +123,8 @@ export const AddItemDialog = ({ open, onOpenChange, tags, onItemAdded }: AddItem
key={tag._id}
variant="outline"
className={`cursor-pointer transition-all ${selectedTagIds.includes(tag._id)
? 'bg-blue-100 border-blue-300'
: 'hover:bg-gray-100'
? 'bg-blue-100 border-blue-300'
: 'hover:bg-gray-100'
}`}
onClick={() => toggleTag(tag._id)}
>
@ -136,6 +140,34 @@ export const AddItemDialog = ({ open, onOpenChange, tags, onItemAdded }: AddItem
Clicca sui tag per selezionarli
</p>
</div>
<div className="space-y-2">
<label className="text-sm font-medium text-gray-700">Quantità *</label>
<Input
type="number"
value={qt} // Default quantity is 1
onChange={(e) => {
const quantity = Math.max(1, parseInt(e.target.value, 10) || 1);
setQt(quantity);
setUsed(Math.min(used, quantity)); // Ensure used does not exceed quantity
}}
placeholder="Quantità totale"
required
/>
</div>
<div className="space-y-2">
<label className="text-sm font-medium text-gray-700">Quantità Usata</label>
<Input
type="number"
value={used}
onChange={(e) => {
const usedQuantity = Math.max(0, parseInt(e.target.value, 10) || 0);
setUsed(usedQuantity);
setQt(Math.max(qt, usedQuantity)); // Ensure quantity is at least as much as used
}}
placeholder="Quantità usata"
/>
</div>
<DialogFooter>
<Button

View file

@ -120,7 +120,7 @@ export const EditItemDialog = ({
/>
</div>
<div className="space-y-2">
{/* <div className="space-y-2">
<Label htmlFor="quantity">Quantità</Label>
<Input
id="quantity"
@ -130,7 +130,7 @@ export const EditItemDialog = ({
onChange={(e) => setFormData(prev => ({ ...prev, quantity: parseInt(e.target.value) || 0 }))}
required
/>
</div>
</div> */}
<div className="space-y-2">
<Label>Tag</Label>

View file

@ -13,6 +13,7 @@ import { ManageTagsDialog } from '@/components/ManageTagsDialog';
import { DeleteItemDialog } from '@/components/DeleteItemDialog';
import { EditItemDialog } from '@/components/EditItemDialog';
import { QuantityControls } from '@/components/QuantityControls';
import { QuantityUsedControls } from './QuantityUsedControls';
import { useToast } from '@/hooks/use-toast';
export const InventoryDashboard = () => {
@ -304,11 +305,12 @@ export const InventoryDashboard = () => {
<TableRow className="bg-slate-50">
<TableHead className="font-semibold">Nome</TableHead>
<TableHead className="font-semibold">Descrizione</TableHead>
<TableHead className="font-semibold">Quantità</TableHead>
<TableHead className="font-semibold">Tag</TableHead>
<TableHead className="font-semibold">Data Aggiunta</TableHead>
<TableHead className="font-semibold">Aggiunto da</TableHead>
<TableHead className="font-semibold">Azioni</TableHead>
<TableHead className="font-semibold w-[12%]">Quantità</TableHead>
<TableHead className="font-semibold w-[12%]">Utilizzato</TableHead>
<TableHead className="font-semibold w-[12%]">Data Aggiunta</TableHead>
{/* <TableHead className="font-semibold">Aggiunto da</TableHead> */}
<TableHead className="font-semibold w-[10%]">Azioni</TableHead>
</TableRow>
</TableHeader>
<TableBody>
@ -326,12 +328,6 @@ export const InventoryDashboard = () => {
<TableRow key={item._id} className="hover:bg-slate-50 transition-colors">
<TableCell className="font-medium">{item.name}</TableCell>
<TableCell className="text-gray-600">{item.description}</TableCell>
<TableCell>
<QuantityControls
item={item}
onQuantityUpdate={handleQuantityUpdate}
/>
</TableCell>
<TableCell>
<div className="flex flex-wrap gap-1">
{item.tags.map(tag => (
@ -348,8 +344,20 @@ export const InventoryDashboard = () => {
))}
</div>
</TableCell>
<TableCell>
<QuantityControls
item={item}
onQuantityUpdate={handleQuantityUpdate}
/>
</TableCell>
<TableCell>
<QuantityUsedControls
item={item}
onQuantityUpdate={handleQuantityUpdate}
/>
</TableCell>
<TableCell className="text-gray-600">{formatDate(item.dateAdded)}</TableCell>
<TableCell className="text-gray-600">{item.addedBy}</TableCell>
{/* <TableCell className="text-gray-600">{item.addedBy}</TableCell> */}
<TableCell>
<div className="flex gap-1">
<Button

View file

@ -43,7 +43,7 @@ export const QuantityControls = ({ item, onQuantityUpdate }: QuantityControlsPro
variant="outline"
size="sm"
onClick={() => updateQuantity(item.quantity - 1)}
disabled={isUpdating || item.quantity <= 0}
disabled={isUpdating || item.quantity <= 0 || item.used >= item.quantity}
className="h-8 w-8 p-0"
>
<Minus className="w-3 h-3" />

View file

@ -0,0 +1,65 @@
import { useState } from 'react';
import { Button } from '@/components/ui/button';
import { Plus, Minus } from 'lucide-react';
import { InventoryItem } from '@/types/inventory';
import { inventoryService } from '@/services/inventoryService';
import { useToast } from '@/hooks/use-toast';
interface QuantityControlsProps {
item: InventoryItem;
onQuantityUpdate: (updatedItem: InventoryItem) => void;
}
export const QuantityUsedControls = ({ item, onQuantityUpdate }: QuantityControlsProps) => {
const [isUpdating, setIsUpdating] = useState(false);
const { toast } = useToast();
const updateQuantity = async (newQuantity: number) => {
if (newQuantity < 0) return;
setIsUpdating(true);
try {
const updatedItem = await inventoryService.updateUsedItemQuantity(item._id, newQuantity);
onQuantityUpdate(updatedItem);
toast({
title: "Quantità aggiornata",
description: `${item.name}: ${newQuantity} pezzi`,
});
} catch (error) {
toast({
title: "Errore",
description: "Impossibile aggiornare la quantità",
variant: "destructive",
});
} finally {
setIsUpdating(false);
}
};
return (
<div className="flex items-center space-x-2">
<Button
variant="outline"
size="sm"
onClick={() => updateQuantity(item.used - 1)}
disabled={isUpdating || item.used <= 0}
className="h-8 w-8 p-0"
>
<Minus className="w-3 h-3" />
</Button>
<span className="text-sm font-medium min-w-[2rem] text-center">
{item.used}
</span>
<Button
variant="outline"
size="sm"
onClick={() => updateQuantity(item.used + 1)}
disabled={isUpdating || item.used >= item.quantity}
className="h-8 w-8 p-0"
>
<Plus className="w-3 h-3" />
</Button>
</div>
);
};

View file

@ -80,6 +80,23 @@ export const inventoryService = {
return res.json();
},
async updateUsedItemQuantity(itemId: string, quantity: number): Promise<InventoryItem> {
const res = await fetch(`${API_URL}/items/${itemId}/usedquantity`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
...getAuthHeader(),
},
body: JSON.stringify({ quantity }),
});
if (!res.ok) {
const msg = (await res.json())?.message ?? 'Errore aggiornamento quantità usata';
throw new Error(msg);
}
return res.json();
},
async deleteItem(itemId: string): Promise<void> {
const res = await fetch(`${API_URL}/items/${itemId}`, {
method: 'DELETE',

View file

@ -10,6 +10,7 @@ export interface InventoryItem {
name: string;
description: string;
quantity: number;
used: number;
tags: Tag[];
dateAdded: string;
addedBy: string;
@ -19,6 +20,7 @@ export interface CreateItemData {
name: string;
description: string;
quantity: number;
used: number;
tagIds: string[];
}