Skip to content
pimenoffdPublic

About

A FastAPI-based mini-CRM for automated lead distribution. It supports customizable operator weights (competencies) by source, current workload tracking, and asynchronous database support (SQLAlchemy Async + SQLite).

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

Mini-CRM for Lead Distribution

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).

Data Model

The application uses five main database models:

  • Lead: Represents an end-user/customer. A lead is uniquely identified by external_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 a name, an is_active status, and a max_active_interactions limit.
  • Source: Represents a channel where interactions originate (e.g., "Telegram Bot", "Website Chat").
  • Interaction: Represents a single contact from a Lead via a Source. It has a status (open or closed) and is assigned to an Operator.
  • SourceOperatorConfig: A linking table that defines the weight (competence) of a specific Operator for a specific Source. This configuration is used by the distribution algorithm.

Distribution Algorithm

The distribution algorithm is triggered when a new interaction is created via POST /api/interactions/.

  1. Lead Identification: The system first looks for a Lead with the provided lead_external_id. If no lead is found, a new one is created.

  2. 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_active flag must be True.
    • The operator's current number of open interactions must be less than their max_active_interactions limit.
  3. Weighted Distribution: From the pool of available operators, the system performs a weighted random selection using Python's random.choices. The weight from the SourceOperatorConfig is used, ensuring that operators with a higher weight for that source are chosen more frequently.

  4. 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_id field will be null).

  5. Closing Interactions: To free up an operator's capacity, an interaction's status can be changed from open to closed by calling the POST /api/interactions/{interaction_id}/close endpoint.

Setup and Running

  1. Activate Virtual Environment: On macOS/Linux:

    source .venv/bin/activate
  2. Install dependencies:

    pip install -r requirements.txt
  3. 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 at http://127.0.0.1:8000/docs.

API Endpoints

Interactions

  • 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'.

Configuration

  • POST /api/operators/: Creates a new operator.
  • GET /api/operators/: Retrieves a list of all operators.
  • PATCH /api/operators/{id}: Updates an operator's is_active status or max_active_interactions limit.
  • POST /api/sources/: Creates a new source.
  • POST /api/config/link: Links an operator to a source with a specific weight.

Leads & Statistics

  • 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.

Testing

To run the test suite, ensure your virtual environment is activated and execute:

pytest

About

A FastAPI-based mini-CRM for automated lead distribution. It supports customizable operator weights (competencies) by source, current workload tracking, and asynchronous database support (SQLAlchemy Async + SQLite).

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages