Overview
The Loans API manages the lending and tracking of inventory items. It supports loan creation, status management, automatic stock adjustments, and return processing. All loan operations are automatically associated with the authenticated user via JWT token.Endpoints
Get All Loans
Retrieves all loans across all containers for the authenticated user (Dashboard view).Authentication
The backend automatically filters loans by theuserId extracted from the JWT token.
Response
Array of loan objects
Example Request
Example Response
Get Container Loans
Retrieves all loans for a specific container.Path Parameters
The ID of the container whose loans to retrieve
Response
Array of loan objects (same structure as Get All Loans)
Example Request
Create Loan
Creates a new loan record and automatically decrements the inventory item stock.Path Parameters
The ID of the container containing the inventory item
Request Body
ID of the inventory item to loan
Name of the item being loaned
Number of units to loan
Name of the person borrowing the item
Email address of the borrower
Phone number of the borrower
Date of the loan (YYYY-MM-DD)
Expected return date (YYYY-MM-DD)
Additional notes about the loan
Initial status (typically ‘active’)
Response
The newly created loan object
Example Request
Example Response
Error Responses
Loan created successfully
Bad request - Insufficient stock or invalid inventory item
Unauthorized - Authentication required
Return Loan
Marks a loan as returned and automatically increments the inventory item stock.Path Parameters
The ID of the container
The ID of the loan to mark as returned
Response
The updated loan object with status ‘returned’ and actualReturnDate populated
Example Request
Example Response
Error Responses
Loan returned successfully
Bad request - Loan already returned
Loan not found
Delete Loan
Deletes a loan record. Only the loan creator can delete their own loans.Path Parameters
The ID of the container
The ID of the loan to delete
Response
Returns status code 200 or 204 on successful deletion.Example Request
Error Responses
Loan deleted successfully
Loan deleted successfully (no content)
Forbidden - You can only delete your own loans
Loan not found
Get Loan Statistics
Retrieves statistical summary of loans for a container.Path Parameters
The ID of the container
Response
Total number of loans (all statuses)
Number of currently active loans
Number of active loans past their expected return date
Number of returned loans
Example Request
Example Response
Business Logic Notes
Automatic Stock Management
- Creating a loan automatically decrements the inventory item’s stock by the loan quantity
- Returning a loan (via the return endpoint) automatically increments the stock back
- The backend enforces stock validation - loans cannot exceed available quantity
User Authentication
- The
userIdfield is automatically populated from the JWT token during loan creation - Users cannot specify their own
userIdin the request body - Only loan owners can delete their loans
Status Management
- Loans are created with status ‘active’
- The return endpoint sets status to ‘returned’ and populates
actualReturnDate - Status transitions are enforced server-side
Overdue Detection
- The
isOverduecomputed property returns true if:- Status is ‘active’ AND
expectedReturnDateexists AND- Current date is after
expectedReturnDate
- Use this for UI indicators and alert generation
Voucher IDs
- Each loan has a formatted voucher ID:
V-000001,V-000042, etc. - Generated via
formattedVoucherIdgetter: pads loan ID to 6 digits - Useful for printing and reference
Date Handling
- All dates are stored and transmitted in ISO 8601 format (YYYY-MM-DD)
- The
toJson()method automatically formats dates to local timezone - Client should parse dates using
DateTime.parse()
Data Integrity
- Deleting a loan does not automatically restore inventory stock
- Consider implementing soft deletes for audit trails
- Loan history is valuable for analytics and reporting
