Skip to content

Commit 0fc05f9

Browse files
committed
Refactor item lifecycle and trip APIs to enhance data handling and logging
- Removed 'retired' condition from VALID_CONDITIONS in item_lifecycle.py to streamline item status management. - Added logging for condition changes and acquisition dates in the update function of item.py to improve tracking of item history. - Updated trip creation and update logic to check user subscription status before modifying trip status, ensuring only subscribed users can create or update trips with locations.
1 parent 43fd8fb commit 0fc05f9

4 files changed

Lines changed: 28 additions & 5 deletions

File tree

app/api/item.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import csv
2+
import datetime
23
import logging
34

45
from fastapi import APIRouter, Depends, HTTPException, File, UploadFile
@@ -8,7 +9,7 @@
89
from io import StringIO
910
from sqlalchemy import or_, func
1011

11-
from models.base import User, Item, ItemCategory, Category, Brand, Product, ProductVariant, CatalogProduct
12+
from models.base import User, Item, ItemLog, ItemCategory, Category, Brand, Product, ProductVariant, CatalogProduct
1213
from utils.auth import authenticate
1314
from utils.weight import standardize_weight_unit
1415
from utils.item_category import get_or_create_item_category
@@ -139,10 +140,33 @@ def update(payload: ItemUpdate, user: User = Depends(authenticate)):
139140
if not item:
140141
raise HTTPException(404, "Item not found.")
141142

143+
old_condition = item.condition
144+
old_acquired_date = item.acquired_date
145+
142146
for key, value in fields.items():
143147
setattr(item, key, value)
144148

145149
try:
150+
if payload.condition and payload.condition != old_condition:
151+
log = ItemLog(
152+
item_id=item.id,
153+
user_id=user.id,
154+
event_type="condition_change",
155+
notes=f"Condition changed from {old_condition or 'unset'} to {payload.condition}",
156+
logged_at=datetime.datetime.utcnow(),
157+
)
158+
db.session.add(log)
159+
160+
if payload.acquired_date and not old_acquired_date:
161+
log = ItemLog(
162+
item_id=item.id,
163+
user_id=user.id,
164+
event_type="acquired",
165+
notes=f"Acquired on {payload.acquired_date}",
166+
logged_at=datetime.datetime.utcnow(),
167+
)
168+
db.session.add(log)
169+
146170
db.session.commit()
147171
db.session.refresh(item)
148172
except Exception:

app/api/item_lifecycle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
route = APIRouter(dependencies=[Depends(authenticate)])
1616

17-
VALID_CONDITIONS = {"new", "good", "fair", "worn", "retired"}
17+
VALID_CONDITIONS = {"new", "good", "fair", "worn"}
1818
VALID_STATUSES = {"active", "wishlist", "retired", "sold", "lost"}
1919
VALID_ACQUISITION_TYPES = {"purchased", "gifted", "traded", "diy"}
2020
VALID_RETIRED_REASONS = {"worn_out", "upgraded", "lost", "sold", "gifted"}

app/api/trip.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def create(payload: TripType, user: User = Depends(authenticate)):
130130
except Exception:
131131
raise HTTPException(400, "Unable to create trip.")
132132

133-
if payload.location and payload.location.strip():
133+
if user.is_subscribed and payload.location and payload.location.strip():
134134
new_trip.enrich_status = "pending"
135135
db.session.commit()
136136
db.session.refresh(new_trip)
@@ -165,7 +165,7 @@ def update(payload: TripUpdate, user: User = Depends(authenticate)):
165165
(payload.start_date and payload.start_date != old_start_date) or
166166
(payload.end_date and payload.end_date != old_end_date)
167167
)
168-
if trip.location and (location_changed or dates_changed):
168+
if user.is_subscribed and trip.location and (location_changed or dates_changed):
169169
trip.enrich_status = "pending"
170170

171171
db.session.commit()

app/utils/gear_lifecycle.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"good": 0.25,
3030
"fair": 0.5,
3131
"worn": 0.8,
32-
"retired": 1.0,
3332
}
3433

3534

0 commit comments

Comments
 (0)