Commercio Docs

Customers

Manage your customers with complete contact information, credit limits, payment terms, and order history tracking.

Customer Model

Structure and properties of a Customer

class Customer {
  id: string;                    // Unique UUID identifier
  name: string;                  // Customer name (required)
  address: CustomerAddress;      // Street, city, postalCode, country, state?
  contact: CustomerContact;      // Email (required), phone?
  creditLimit: number;           // Credit limit in cents (default: 0)
  paymentTerms: PaymentTerms;     // NET_15, NET_30, NET_60, DUE_ON_RECEIPT, PREPAID
  customerGroupId: string | null; // Optional customer group ID
  isActive: boolean;             // Active status (default: true)
  createdAt: Date;
  updatedAt: Date;
}

enum PaymentTerms {
  NET_15 = "NET_15",        // Payment due 15 days after invoice
  NET_30 = "NET_30",        // Payment due 30 days after invoice
  NET_60 = "NET_60",        // Payment due 60 days after invoice
  DUE_ON_RECEIPT = "DUE_ON_RECEIPT",  // Payment due immediately
  PREPAID = "PREPAID",      // Payment required before shipment
}

Create Customer

Create a new customer with address, contact, and payment settings

Basic example:

import { createCustomerService, PaymentTerms } from "commercio";

const customerService = createCustomerService();

// Create customer with all fields
const customer = await customerService.createCustomer(
  "John Doe",
  {
    street: "123 Main St",
    city: "Berlin",
    postalCode: "10115",
    country: "Germany",
    state: "Berlin",
  },
  {
    email: "john.doe@example.com",
    phone: "+49 30 12345678",
  },
  {
    creditLimit: 100000, // €1000.00 in cents
    paymentTerms: PaymentTerms.NET_30,
  }
);

console.log(customer.id);              // UUID
console.log(customer.name);            // "John Doe"
console.log(customer.address.city);    // "Berlin"
console.log(customer.contact.email);   // "john.doe@example.com"
console.log(customer.creditLimit);     // 100000
console.log(customer.paymentTerms);    // "NET_30"

Minimal example:

// Create customer with minimal required fields
const customer = await customerService.createCustomer(
  "Jane Smith",
  {
    street: "456 Oak Ave",
    city: "Munich",
    postalCode: "80331",
    country: "Germany",
  },
  {
    email: "jane.smith@example.com",
  }
);

// Defaults:
// creditLimit: 0
// paymentTerms: NET_30
// customerGroupId: null

Customer Operations

Get Customer

Retrieve customers by ID or email

Get by ID:

// Get customer by ID
const customer = await customerService.getCustomerById(customerId);

console.log(customer.name);
console.log(customer.address.city);
console.log(customer.contact.email);
console.log(customer.creditLimit);

Get by email:

// Get customer by email
const customer = await customerService.getCustomerByEmail("john.doe@example.com");

console.log(customer.id);
console.log(customer.name);

Order History

Retrieve and analyze customer order history

Get all orders for a customer:

// Get complete order history
const orderHistory = await customerService.getOrderHistory(customer.id);

console.log(`Customer has ${orderHistory.length} orders`);
orderHistory.forEach((order) => {
  console.log(`Order ${order.id}: €${(order.totalAmount / 100).toFixed(2)} - Status: ${order.status}`);
});

Filter orders by status:

import { OrderStatus } from "commercio";

// Get only completed orders
const completedOrders = await customerService.getOrderHistoryByStatus(
  customer.id,
  OrderStatus.COMPLETED
);

// Get only paid orders
const paidOrders = await customerService.getOrderHistoryByStatus(
  customer.id,
  OrderStatus.PAID
);

// Get all orders (no filter)
const allOrders = await customerService.getOrderHistoryByStatus(customer.id);

Get order statistics:

// Get customer order statistics
const stats = await customerService.getCustomerOrderStatistics(customer.id);

console.log(`Total orders: ${stats.totalOrders}`);
console.log(`Total spent: €${(stats.totalSpent / 100).toFixed(2)}`);
console.log(`Average order value: €${(stats.averageOrderValue / 100).toFixed(2)}`);
console.log(`Orders by status:`, stats.ordersByStatus);
// Output: { CREATED: 2, CONFIRMED: 1, COMPLETED: 5, ... }

Customer Groups

Organize customers into groups with discount percentages

Create customer group:

// Create customer group with discount
const vipGroup = await customerService.createCustomerGroup(
  "VIP Customers",
  "Premium customer tier",
  10 // 10% discount
);

console.log(vipGroup.name);              // "VIP Customers"
console.log(vipGroup.discountPercentage); // 10

Assign customer to group:

// Assign customer to a group
await customerService.updateCustomer(customer.id, {
  customerGroupId: vipGroup.id,
});

// Get all customers in a group
const vipCustomers = await customerService.getCustomersByGroup(vipGroup.id);
console.log(`VIP customers: ${vipCustomers.length}`);

Get customer groups:

// Get all customer groups
const allGroups = await customerService.getAllCustomerGroups();
const activeGroups = await customerService.getAllCustomerGroups(true);

// Get group by name
const group = await customerService.getCustomerGroupByName("VIP Customers");

Complete Example

Full workflow: Creating customers, groups, and tracking orders

import { createServices, PaymentTerms, OrderStatus } from "commercio";

const {
  categoryService,
  productService,
  customerService,
  orderService,
} = createServices();

// 1. Create customer group
const vipGroup = await customerService.createCustomerGroup(
  "VIP Customers",
  "Premium tier with 15% discount",
  15
);

// 2. Create customer
const customer = await customerService.createCustomer(
  "John Doe",
  {
    street: "123 Main St",
    city: "Berlin",
    postalCode: "10115",
    country: "Germany",
  },
  {
    email: "john.doe@example.com",
    phone: "+49 30 12345678",
  },
  {
    creditLimit: 500000, // €5000.00
    paymentTerms: PaymentTerms.NET_30,
    customerGroupId: vipGroup.id,
  }
);

// 3. Create product and order
const category = await categoryService.createCategory("Electronics");
const product = await productService.createProduct(
  "Laptop",
  "SKU-LAPTOP-001",
  category.id
);

const order = await orderService.createOrder(customer.id, [
  { productId: product.id, quantity: 1, unitPrice: 129999 }, // €1299.99
]);

// 4. Get customer order history
const orderHistory = await customerService.getOrderHistory(customer.id);
console.log(`Customer has ${orderHistory.length} orders`);

// 5. Get order statistics
const stats = await customerService.getCustomerOrderStatistics(customer.id);
console.log(`Total spent: €${(stats.totalSpent / 100).toFixed(2)}`);
console.log(`Average order: €${(stats.averageOrderValue / 100).toFixed(2)}`);

// 6. Filter orders by status
const completedOrders = await customerService.getOrderHistoryByStatus(
  customer.id,
  OrderStatus.COMPLETED
);

// 7. Update customer credit limit
await customerService.updateCustomer(customer.id, {
  creditLimit: 1000000, // Increase to €10000.00
});

Best Practices

Important guidelines for working with customers

1. Create Customers Before Orders

Orders require an existing customer. Always create customers first:

// Good: Create customer first
const customer = await customerService.createCustomer(
  "John Doe",
  { street: "123 St", city: "Berlin", postalCode: "10115", country: "Germany" },
  { email: "john@example.com" }
);
const order = await orderService.createOrder(customer.id, [
  { productId: product.id, quantity: 1, unitPrice: 10000 },
]);

// Bad: This will fail
const order = await orderService.createOrder(
  "non-existent-customer-id",
  [{ productId: product.id, quantity: 1, unitPrice: 10000 }]
);

2. Unique Email Addresses

Customer emails must be unique. Handle duplicate email errors:

try {
  await customerService.createCustomer(
    "Customer",
    { street: "123 St", city: "Berlin", postalCode: "10115", country: "Germany" },
    { email: "existing@example.com" }
  );
} catch (error) {
  if (error.message.includes("already exists")) {
    // Customer already exists, get it instead
    const existing = await customerService.getCustomerByEmail("existing@example.com");
    return existing;
  }
  throw error;
}

3. Use Customer Groups for Discounts

Organize customers into groups to apply discounts:

// Create groups with different discount levels
const vipGroup = await customerService.createCustomerGroup("VIP", null, 15);
const regularGroup = await customerService.createCustomerGroup("Regular", null, 0);

// Assign customers to groups
await customerService.updateCustomer(customer1.id, {
  customerGroupId: vipGroup.id, // 15% discount
});

await customerService.updateCustomer(customer2.id, {
  customerGroupId: regularGroup.id, // No discount
});

4. Monitor Credit Limits

Track customer credit limits and spending:

const customer = await customerService.getCustomerById(customerId);
const stats = await customerService.getCustomerOrderStatistics(customerId);

// Check if customer is approaching credit limit
const creditUsed = stats.totalSpent;
const creditRemaining = customer.creditLimit - creditUsed;

if (creditRemaining < 10000) {
  console.warn(`Customer approaching credit limit: €${(creditRemaining / 100).toFixed(2)} remaining`);
}

5. Use Order History for Analytics

Analyze customer behavior using order history and statistics:

// Get customer statistics
const stats = await customerService.getCustomerOrderStatistics(customerId);

// Identify best customers
if (stats.totalSpent > 1000000) {
  // Customer has spent more than €10000
  // Consider upgrading to VIP group
}

// Analyze order patterns
const completedOrders = await customerService.getOrderHistoryByStatus(
  customerId,
  OrderStatus.COMPLETED
);
console.log(`Customer has ${completedOrders.length} completed orders`);