139 lines
4.1 KiB
TypeScript
139 lines
4.1 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { ObjectId } from 'mongodb';
|
|
import { getDb } from '@/lib/mongodb';
|
|
|
|
export async function GET(
|
|
_request: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const { id } = await params;
|
|
|
|
if (!ObjectId.isValid(id)) {
|
|
return NextResponse.json({ error: 'Invalid ID' }, { status: 400 });
|
|
}
|
|
|
|
const db = await getDb();
|
|
const ingredient = await db.collection('ingredients').aggregate([
|
|
{ $match: { _id: new ObjectId(id) } },
|
|
{
|
|
$lookup: {
|
|
from: 'categories',
|
|
localField: 'categoryId',
|
|
foreignField: '_id',
|
|
as: 'categoryDoc',
|
|
},
|
|
},
|
|
{
|
|
$lookup: {
|
|
from: 'subcategories',
|
|
localField: 'subcategoryId',
|
|
foreignField: '_id',
|
|
as: 'subcategoryDoc',
|
|
},
|
|
},
|
|
{
|
|
$lookup: {
|
|
from: 'suppliers',
|
|
localField: 'supplierId',
|
|
foreignField: '_id',
|
|
as: 'supplierDoc',
|
|
},
|
|
},
|
|
{
|
|
$project: {
|
|
code: 1,
|
|
name: 1,
|
|
category: { $arrayElemAt: ['$categoryDoc.name', 0] },
|
|
subcategory: { $arrayElemAt: ['$subcategoryDoc.name', 0] },
|
|
categoryId: 1,
|
|
subcategoryId: 1,
|
|
supplierId: 1,
|
|
quantity: 1,
|
|
unit: 1,
|
|
unitPrice: 1,
|
|
vat: 1,
|
|
supplier: { $arrayElemAt: ['$supplierDoc.name', 0] },
|
|
discountType: 1,
|
|
discountValue: 1,
|
|
applyDiscountToNet: 1,
|
|
minStockLevel: 1,
|
|
storageInstructions: 1,
|
|
shelfLifeDays: 1,
|
|
notes: 1,
|
|
createdAt: 1,
|
|
updatedAt: 1,
|
|
},
|
|
},
|
|
]).next();
|
|
|
|
if (!ingredient) {
|
|
return NextResponse.json({ error: 'Ingredient not found' }, { status: 404 });
|
|
}
|
|
|
|
return NextResponse.json(ingredient);
|
|
}
|
|
|
|
export async function PUT(
|
|
request: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const { id } = await params;
|
|
|
|
if (!ObjectId.isValid(id)) {
|
|
return NextResponse.json({ error: 'Invalid ID' }, { status: 400 });
|
|
}
|
|
|
|
const body = await request.json();
|
|
const update: Record<string, unknown> = { updatedAt: new Date() };
|
|
|
|
if (body.code !== undefined) update.code = body.code;
|
|
if (body.name !== undefined) update.name = body.name;
|
|
if (body.categoryId !== undefined) update.categoryId = new ObjectId(body.categoryId);
|
|
if (body.subcategoryId !== undefined) update.subcategoryId = new ObjectId(body.subcategoryId);
|
|
if (body.quantity !== undefined) update.quantity = body.quantity;
|
|
if (body.unit !== undefined) update.unit = body.unit;
|
|
if (body.unitPrice !== undefined) update.unitPrice = body.unitPrice;
|
|
if (body.vat !== undefined) update.vat = body.vat;
|
|
if (body.supplierId !== undefined) update.supplierId = new ObjectId(body.supplierId);
|
|
if (body.discountType !== undefined) update.discountType = body.discountType;
|
|
if (body.discountValue !== undefined) update.discountValue = body.discountValue;
|
|
if (body.applyDiscountToNet !== undefined) update.applyDiscountToNet = body.applyDiscountToNet;
|
|
if (body.minStockLevel !== undefined) update.minStockLevel = body.minStockLevel;
|
|
if (body.storageInstructions !== undefined) update.storageInstructions = body.storageInstructions;
|
|
if (body.shelfLifeDays !== undefined) update.shelfLifeDays = body.shelfLifeDays;
|
|
if (body.notes !== undefined) update.notes = body.notes;
|
|
|
|
const db = await getDb();
|
|
const result = await db.collection('ingredients').findOneAndUpdate(
|
|
{ _id: new ObjectId(id) },
|
|
{ $set: update },
|
|
{ returnDocument: 'after' }
|
|
);
|
|
|
|
if (!result) {
|
|
return NextResponse.json({ error: 'Ingredient not found' }, { status: 404 });
|
|
}
|
|
|
|
return NextResponse.json(result);
|
|
}
|
|
|
|
export async function DELETE(
|
|
_request: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const { id } = await params;
|
|
|
|
if (!ObjectId.isValid(id)) {
|
|
return NextResponse.json({ error: 'Invalid ID' }, { status: 400 });
|
|
}
|
|
|
|
const db = await getDb();
|
|
const result = await db.collection('ingredients').deleteOne({ _id: new ObjectId(id) });
|
|
|
|
if (result.deletedCount === 0) {
|
|
return NextResponse.json({ error: 'Ingredient not found' }, { status: 404 });
|
|
}
|
|
|
|
return NextResponse.json({ success: true });
|
|
}
|