LoanService class handles all loan-related business logic, including validation, creation, approval, and returns.
Methods
can_request_laptop
Validates whether a user can request a laptop based on their current loans.The ID of the user requesting the laptop
Returns a tuple containing:
bool: True if user can request, False otherwisestr: Message explaining the result
- Users cannot have more than one active computer loan
- Checks for loans in states: ‘pendiente’, ‘activo’, ‘atrasado’
- Only applies to items in ‘computo’ category
"Ya tienes un equipo pendiente, en uso o atrasado."- User has an active computer loan"Ok"- User can proceed with request
can_request_accessory
Validates whether a user can request an accessory based on their current loans.The ID of the user requesting the accessory
Returns a tuple containing:
bool: True if user can request, False otherwisestr: Message explaining the result
- Users are limited to 2 simultaneous accessories
- Excludes ‘computo’ and ‘libro’ categories
- Checks for loans in states: ‘pendiente’, ‘activo’, ‘atrasado’
"Has alcanzado el límite de 2 accesorios simultáneos."- User has reached the limit"Ok"- User can proceed with request
create_loan
Creates a new loan record for a specific item instance.The ID of the user requesting the loan
The ID of the specific item instance being loaned
The environment where the loan will be used (e.g., ‘sala’, ‘externo’)
Number of days for the loan period
Returns the newly created Loan object with status ‘pendiente’
- Does NOT commit the transaction - the controller must handle
db.session.commit() - Sets initial status to ‘pendiente’ (pending approval)
- Automatically calculates due_date based on days parameter
- Creates the loan tied to a physical item instance
app/services/loan_service.py:30
approve_loan
Approves a pending loan and activates it.The ID of the loan to approve
Returns a tuple containing:
bool: True if approved successfully, False otherwisestr: Success or error message
- Only loans with status ‘pendiente’ can be approved
- Sets status to ‘activo’
- Records approval_date as current UTC time
- Commits the transaction automatically
- Item instance status should already be ‘prestado’ from reservation
"Préstamo no válido o ya procesado."- Loan doesn’t exist or is not pending"Préstamo aprobado con éxito."- Approval successful
app/services/loan_service.py:49
return_loan
Processes the return of a loaned item.The ID of the loan to return
Returns a tuple containing:
bool: True if returned successfully, False otherwisestr: Success or error message
- Only loans with status ‘activo’ or ‘atrasado’ can be returned
- Calculates and records final penalty if loan is overdue
- Sets status to ‘devuelto’
- Records return_date as current UTC time
- Releases the item instance back to ‘disponible’ status
- Commits the transaction automatically
loan.is_overdue property and loan.penalty_fee calculation.
Error Messages:
"Préstamo no válido o no está activo."- Loan doesn’t exist or wrong status"Artículo devuelto exitosamente al inventario."- Return successful
app/services/loan_service.py:63
check_overdue_loans
Scans for active loans past their due date and marks them as overdue.Returns the number of loans marked as overdue
- Queries all loans with status ‘activo’ where due_date < current time
- Updates each to status ‘atrasado’
- Commits changes if any loans were updated
- Typically run as a scheduled background task
app/services/loan_service.py:83