This is a small CRM service designed to automatically distribute incoming leads (interactions) among a set of operators based on their configuration for different sources.
The project is built with Python, FastAPI, and SQLAlchemy (AsyncIO).
The application uses five main database models:
Lead: Represents an end-user/customer. A lead is uniquely identified byexternal_id, ensuring that interactions from the same person across different sources are linked to a single profile.Operator: Represents a support agent. Each operator has aname, anis_activestatus, and amax_active_interactionslimit.Source: Represents a channel where interactions originate (e.g., "Telegram Bot", "Website Chat").Interaction: Represents a single contact from aLeadvia aSource. It has astatus(openorclosed) and is assigned to anOperator.SourceOperatorConfig: A linking table that defines theweight(competence) of a specificOperatorfor a specificSource. This configuration is used by the distribution algorithm.
The distribution algorithm is triggered when a new interaction is created via POST /api/interactions/.
-
Lead Identification: The system first looks for a
Leadwith the providedlead_external_id. If no lead is found, a new one is created. -
Operator Availability: The system fetches all operators configured for the interaction's source. It then filters this list to find who is available, based on two criteria:
- The operator's
is_activeflag must beTrue. - The operator's current number of
openinteractions must be less than theirmax_active_interactionslimit.
- The operator's
-
Weighted Distribution: From the pool of available operators, the system performs a weighted random selection using Python's
random.choices. Theweightfrom theSourceOperatorConfigis used, ensuring that operators with a higher weight for that source are chosen more frequently. -
Handling No Available Operators: If no operators are available for the interaction (either because none are configured, none are active, or all are at their load limit), the interaction is still created but is not assigned to any operator (the
operator_idfield will benull). -
Closing Interactions: To free up an operator's capacity, an interaction's status can be changed from
opentoclosedby calling thePOST /api/interactions/{interaction_id}/closeendpoint.
-
Activate Virtual Environment: On macOS/Linux:
source .venv/bin/activate -
Install dependencies:
pip install -r requirements.txt
-
Run the application:
uvicorn app.main:app
The API will be available at
http://127.0.0.1:8000. You can access the auto-generated documentation athttp://127.0.0.1:8000/docs.
-
POST /api/interactions/: Creates a new interaction. This is the main endpoint that triggers the lead distribution logic. Body: { "lead_external_id": "client_123", "source_id": 1, "message_text": "Helllo" } -
GET /api/interactions/: Retrieves a list of all interactions with their details. -
POST /api/interactions/{id}/close: Marks an interaction as 'closed'.
POST /api/operators/: Creates a new operator.GET /api/operators/: Retrieves a list of all operators.PATCH /api/operators/{id}: Updates an operator'sis_activestatus ormax_active_interactionslimit.POST /api/sources/: Creates a new source.POST /api/config/link: Links an operator to a source with a specificweight.
GET /api/leads/: Retrieves a list of all leads, with their complete history of interactions.GET /api/stats/distribution/: Shows statistics on how interactions have been distributed among operators for each source.
To run the test suite, ensure your virtual environment is activated and execute:
pytest