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
-
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.
-
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.
-
Default Customer Management:
set_default_customer(customer_id)
: Marks a customer as the default and updates other customers accordingly.
-
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.
-
Customer Deletion:
delete_customer(user_id, is_anonymous, customer_id)
: Deletes a customer for a specific user or anonymous user.
-
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:
-
Create or Fetch a Customer
-
Change Customer Ownership
-
Set Default Customer
-
Retrieve Organization Customers:
-
Delete a Customer
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.