2.9 KiB
2.9 KiB
CRM Step 02 — Backend: Customers CRUD
Context
Read .claude/crm-build-plan.md first for full data models, conventions, and IMPORTANT NOTES.
Step 01 must be complete (backend/crm/ module exists).
Task
Add Customers models, service, and router to backend/crm/.
What to build
1. Add to backend/crm/models.py
Contact entry:
ContactTypeenum — email, phone, whatsapp, otherCustomerContact— type (ContactType), label (str, e.g. "personal"/"church"), value (str), primary (bool default False)
Note entry:
CustomerNote— text (str), by (str), at (str ISO datetime)
Owned items (3 tiers):
OwnedItemTypeenum — console_device, product, freetextOwnedItem:- type: OwnedItemType
- For console_device: device_id (Optional[str]), label (Optional[str])
- For product: product_id (Optional[str]), product_name (Optional[str]), quantity (Optional[int]), serial_numbers (Optional[List[str]])
- For freetext: description (Optional[str]), serial_number (Optional[str]), notes (Optional[str])
Location:
CustomerLocation— city (Optional[str]), country (Optional[str]), region (Optional[str])
Customer models:
CustomerCreate— name (str), organization (Optional[str]), contacts (List[CustomerContact] default []), notes (List[CustomerNote] default []), location (Optional[CustomerLocation]), language (str default "el"), tags (List[str] default []), owned_items (List[OwnedItem] default []), linked_user_ids (List[str] default []), nextcloud_folder (Optional[str])CustomerUpdate— all fields OptionalCustomerInDB— extends CustomerCreate + id, created_at, updated_atCustomerListResponse— customers: List[CustomerInDB], total: int
2. Add to backend/crm/service.py
Firestore collection: crm_customers
Functions:
list_customers(search=None, tag=None) -> List[CustomerInDB]- search matches against name, organization, and any contact value
get_customer(customer_id) -> CustomerInDB— 404 if not foundcreate_customer(data: CustomerCreate) -> CustomerInDBupdate_customer(customer_id, data: CustomerUpdate) -> CustomerInDBdelete_customer(customer_id) -> None
3. Add to backend/crm/router.py
Add a second router or extend existing file with prefix /api/crm/customers:
GET /— list_customers (query: search, tag)GET /{customer_id}— get_customerPOST /— create_customerPUT /{customer_id}— update_customerDELETE /{customer_id}— delete_customer
Register this router in backend/main.py alongside the products router.
Notes
- OwnedItem is a flexible struct — store all fields, service doesn't validate which fields are relevant per type (frontend handles that)
- linked_user_ids are Firebase Auth UIDs (strings) — no validation needed here, just store them
- Search in list_customers: do client-side filter after fetching all (small dataset)