BOB Docs
APIApplicationStore

CustomerService

Key Features

  • Customer Retrieval and Management: Handles fetching customer data based on user, organization, or customer ID.
  • Customer Creation: Supports the creation of customers for anonymous users, authenticated users, and organizations.
  • Default Customer Handling: Manages the concept of default customers, ensuring one default customer per owner (user, anonymous user, or organization).
  • Ownership Changes: Allows transferring ownership of a customer between users or associating them with organizations.
  • Customer Deletion: Safely removes customers while ensuring data consistency.
  • Bulk Retrieval: Retrieves customers for users, organizations, or the entire system with pagination support.

Methods

  1. Customer Retrieval:

    • _get_customer_based_on_id(customer_id): Fetches a customer by ID.
    • get_default_customer(user): Retrieves the default customer for a user or anonymous user.
    • get_user_customer(user_id, customer_id): Fetches a customer belonging to a user or anonymous user.
    • get_organization_customer(organization_id, customer_id): Retrieves a customer associated with an organization.
    • get_all_customers(): Retrieves all customers in the system.
  2. Customer Creation:

    • get_or_create_customer(anonymous_user_id, custom_user_id, organization_id): Fetches or creates a customer for the provided identifiers.
    • create_customer(user): Creates a customer for a given user or anonymous user.
  3. Default Customer Management:

    • set_default_customer(customer_id): Marks a customer as the default and updates other customers accordingly.
  4. Ownership Changes:

    • change_owner(customer_id, new_owner_id): Transfers ownership of a customer to a new user.
    • change_organization(customer_id, organization_id): Associates a customer with a new organization.
  5. Customer Deletion:

    • delete_customer(user_id, is_anonymous, customer_id): Deletes a customer for a specific user or anonymous user.
  6. Bulk Retrieval:

    • get_user_customers(user_id, is_anonymous): Retrieves all customers associated with a user or anonymous user.
    • get_organization_customers(organization_id, page, page_size): Retrieves customers linked to an organization.

Example Workflow:

  1. Create or Fetch a Customer

    customer = await customer_service.get_or_create_customer(anonymous_user_id="anon123")
  2. Change Customer Ownership

    updated_customer = await customer_service.change_owner(customer_id=456, new_owner_id="user789")
  3. Set Default Customer

    default_customer = await customer_service.set_default_customer(customer_id=123)
  4. Retrieve Organization Customers:

    customers, pagination = await customer_service.get_organization_customers(
        organization_id="org123", page=1, page_size=10
    )
  5. Delete a Customer

    await customer_service.delete_customer(user_id="user123", is_anonymous=False, customer_id=789)

Dependencies

  • Repositories Used:
    • CustomerRepository: Handles database operations for customers.

Benefits

  • Seamless Integration: Integrates seamlessly with carts, orders, and payment services.
  • Flexibility: Supports diverse scenarios, including anonymous shopping, organizational billing, and user-centric management.
  • Data Integrity: Ensures consistency and accuracy when creating, updating, or deleting customer records.

The CustomerService provides robust and flexible handling of customer data, empowering the system to cater to a wide range of e-commerce and billing use cases.

On this page