Build [v1.5.0] - Box Management & Label Printing
This commit is contained in:
@@ -2,6 +2,7 @@ from fastapi import APIRouter, Depends, HTTPException, status, UploadFile, File,
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import func
|
||||
from typing import List
|
||||
import json
|
||||
from slowapi import Limiter
|
||||
from slowapi.util import get_remote_address
|
||||
from .. import models, schemas, auth
|
||||
@@ -106,10 +107,27 @@ def create_item(
|
||||
db.refresh(db_item)
|
||||
|
||||
# Audit log the creation — [M-02] user_id from token, not from body
|
||||
# Capture full snapshot
|
||||
item_snapshot = {
|
||||
"barcode": db_item.barcode,
|
||||
"name": db_item.name,
|
||||
"category": db_item.category,
|
||||
"type": db_item.type,
|
||||
"part_number": db_item.part_number,
|
||||
"color": db_item.color,
|
||||
"specs": db_item.specs,
|
||||
"box_label": db_item.box_label,
|
||||
"image_url": db_item.image_url
|
||||
}
|
||||
|
||||
audit = models.AuditLog(
|
||||
user_id=current_user.sub,
|
||||
action="CREATE_ITEM",
|
||||
target_item_id=db_item.id,
|
||||
target_item_name=db_item.name,
|
||||
target_item_pn=db_item.part_number,
|
||||
target_item_barcode=db_item.barcode,
|
||||
target_snapshot=json.dumps(item_snapshot),
|
||||
quantity_change=item.quantity
|
||||
)
|
||||
db.add(audit)
|
||||
@@ -148,15 +166,39 @@ def delete_item(
|
||||
if not db_item:
|
||||
raise HTTPException(status_code=404, detail="Item not found")
|
||||
|
||||
# [AUDIT] Log the deletion to disk file via logger.py
|
||||
# [AUDIT] Log the deletion to database and disk
|
||||
from ..logger import log
|
||||
log.warning(f"USER[{current_user.sub}] DELETING ITEM: ID={item_id}, Name={db_item.name}, PN={db_item.part_number}")
|
||||
|
||||
item_snapshot = {
|
||||
"barcode": db_item.barcode,
|
||||
"name": db_item.name,
|
||||
"category": db_item.category,
|
||||
"type": db_item.type,
|
||||
"part_number": db_item.part_number,
|
||||
"color": db_item.color,
|
||||
"specs": db_item.specs,
|
||||
"box_label": db_item.box_label,
|
||||
"image_url": db_item.image_url,
|
||||
"final_quantity": db_item.quantity
|
||||
}
|
||||
|
||||
audit = models.AuditLog(
|
||||
user_id=current_user.sub,
|
||||
action="DELETE_ITEM",
|
||||
target_item_id=item_id,
|
||||
target_item_name=db_item.name,
|
||||
target_item_pn=db_item.part_number,
|
||||
target_item_barcode=db_item.barcode,
|
||||
target_snapshot=json.dumps(item_snapshot),
|
||||
details=f"Final Quantity: {db_item.quantity}"
|
||||
)
|
||||
db.add(audit)
|
||||
|
||||
# [CLEANUP] Delete related InterventionItems to prevent foreign key issues
|
||||
db.query(models.InterventionItem).filter(models.InterventionItem.item_id == item_id).delete()
|
||||
|
||||
# Audit Logs in database are NOT deleted here to preserve history of actions
|
||||
|
||||
db.delete(db_item)
|
||||
db.commit()
|
||||
return {"message": "Item deleted successfully. History logs preserved."}
|
||||
|
||||
Reference in New Issue
Block a user