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 ──────────── */