refactor: Improve person creation and identification logic with optional fields handling
This commit refactors the person creation and identification logic to handle optional fields more effectively. The `date_of_birth` field in the `PersonCreateRequest` schema is now optional, and the frontend has been updated to trim whitespace from name fields before submission. Additionally, the identification logic has been enhanced to ensure that only non-empty names are considered valid. Documentation has been updated to reflect these changes.
This commit is contained in:
+10
-6
@@ -290,16 +290,20 @@ def identify_face(
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="person_id not found")
|
||||
else:
|
||||
# Validate required fields for creation
|
||||
if not (request.first_name and request.last_name and request.date_of_birth):
|
||||
first_name = (request.first_name or "").strip()
|
||||
last_name = (request.last_name or "").strip()
|
||||
middle_name = request.middle_name.strip() if request.middle_name else None
|
||||
maiden_name = request.maiden_name.strip() if request.maiden_name else None
|
||||
if not (first_name and last_name):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="first_name, last_name and date_of_birth are required to create a person",
|
||||
detail="first_name and last_name are required to create a person",
|
||||
)
|
||||
person = Person(
|
||||
first_name=request.first_name,
|
||||
last_name=request.last_name,
|
||||
middle_name=request.middle_name,
|
||||
maiden_name=request.maiden_name,
|
||||
first_name=first_name,
|
||||
last_name=last_name,
|
||||
middle_name=middle_name,
|
||||
maiden_name=maiden_name,
|
||||
date_of_birth=request.date_of_birth,
|
||||
)
|
||||
db.add(person)
|
||||
|
||||
@@ -304,41 +304,32 @@ def approve_deny_pending_identifications(
|
||||
|
||||
# Check if person already exists (by name and DOB)
|
||||
# Match the unique constraint: first_name, last_name, middle_name, maiden_name, date_of_birth
|
||||
person = None
|
||||
# Build query with proper None handling
|
||||
query = main_db.query(Person).filter(
|
||||
Person.first_name == row.first_name,
|
||||
Person.last_name == row.last_name,
|
||||
)
|
||||
# Handle optional fields - use IS NULL for None values
|
||||
if row.middle_name:
|
||||
query = query.filter(Person.middle_name == row.middle_name)
|
||||
else:
|
||||
query = query.filter(Person.middle_name.is_(None))
|
||||
|
||||
if row.maiden_name:
|
||||
query = query.filter(Person.maiden_name == row.maiden_name)
|
||||
else:
|
||||
query = query.filter(Person.maiden_name.is_(None))
|
||||
|
||||
if row.date_of_birth:
|
||||
# Build query with proper None handling
|
||||
query = main_db.query(Person).filter(
|
||||
Person.first_name == row.first_name,
|
||||
Person.last_name == row.last_name,
|
||||
Person.date_of_birth == row.date_of_birth
|
||||
)
|
||||
# Handle optional fields - use IS NULL for None values
|
||||
if row.middle_name:
|
||||
query = query.filter(Person.middle_name == row.middle_name)
|
||||
else:
|
||||
query = query.filter(Person.middle_name.is_(None))
|
||||
|
||||
if row.maiden_name:
|
||||
query = query.filter(Person.maiden_name == row.maiden_name)
|
||||
else:
|
||||
query = query.filter(Person.maiden_name.is_(None))
|
||||
|
||||
person = query.first()
|
||||
query = query.filter(Person.date_of_birth == row.date_of_birth)
|
||||
else:
|
||||
query = query.filter(Person.date_of_birth.is_(None))
|
||||
|
||||
person = query.first()
|
||||
|
||||
# Create person if doesn't exist
|
||||
created_person = False
|
||||
if not person:
|
||||
if not row.date_of_birth:
|
||||
errors.append(f"Pending identification {decision.id} missing date_of_birth (required for person creation)")
|
||||
auth_db.execute(text("""
|
||||
UPDATE pending_identifications
|
||||
SET status = 'denied', updated_at = :updated_at
|
||||
WHERE id = :id
|
||||
"""), {"id": decision.id, "updated_at": datetime.utcnow()})
|
||||
auth_db.commit()
|
||||
denied_count += 1
|
||||
continue
|
||||
|
||||
person = Person(
|
||||
first_name=row.first_name,
|
||||
last_name=row.last_name,
|
||||
|
||||
@@ -95,11 +95,15 @@ def list_people_with_faces(
|
||||
@router.post("", response_model=PersonResponse, status_code=status.HTTP_201_CREATED)
|
||||
def create_person(request: PersonCreateRequest, db: Session = Depends(get_db)) -> PersonResponse:
|
||||
"""Create a new person."""
|
||||
first_name = request.first_name.strip()
|
||||
last_name = request.last_name.strip()
|
||||
middle_name = request.middle_name.strip() if request.middle_name else None
|
||||
maiden_name = request.maiden_name.strip() if request.maiden_name else None
|
||||
person = Person(
|
||||
first_name=request.first_name,
|
||||
last_name=request.last_name,
|
||||
middle_name=request.middle_name,
|
||||
maiden_name=request.maiden_name,
|
||||
first_name=first_name,
|
||||
last_name=last_name,
|
||||
middle_name=middle_name,
|
||||
maiden_name=maiden_name,
|
||||
date_of_birth=request.date_of_birth,
|
||||
)
|
||||
db.add(person)
|
||||
|
||||
@@ -30,7 +30,7 @@ class PersonCreateRequest(BaseModel):
|
||||
last_name: str = Field(..., min_length=1)
|
||||
middle_name: Optional[str] = None
|
||||
maiden_name: Optional[str] = None
|
||||
date_of_birth: date
|
||||
date_of_birth: Optional[date] = None
|
||||
|
||||
|
||||
class PeopleListResponse(BaseModel):
|
||||
|
||||
Reference in New Issue
Block a user