Catalogue ID Generation Update - Complete β
Summary
Successfully updated the catalogue ID generation from random UUIDs to merchant-level sequential IDs.
Changes Made
1. New Method: generate_merchant_catalogue_id()
Location: app/models/catalogue_models.py
async def generate_merchant_catalogue_id(merchant_id: str) -> str:
"""
Generate a simple, merchant-level catalogue ID.
Format: MERCHANT_PREFIX-SEQUENTIAL_NUMBER
"""
# Get count of existing catalogues for this merchant
count = await db['catalogues'].count_documents({"merchant_id": merchant_id})
# Generate sequential ID with merchant prefix
merchant_prefix = merchant_id[:6].upper()
sequential_number = str(count + 1).zfill(5)
catalogue_id = f"{merchant_prefix}-{sequential_number}"
return catalogue_id
2. Updated Method: create_catalogue_item()
Now uses the new ID generation:
merchant_id = data.get("merchant_id")
if merchant_id:
catalogue_id = await CatalogueModel.generate_merchant_catalogue_id(merchant_id)
else:
catalogue_id = str(uuid.uuid4()) # Fallback
ID Format
Before
catalogue_id: "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
After
catalogue_id: "MERCHA-00001"
Format: {MERCHANT_PREFIX}-{SEQUENTIAL_NUMBER}
- Merchant prefix: First 6 characters of merchant_id (uppercase)
- Sequential number: 5-digit zero-padded number
Examples
| Merchant ID | Existing Count | New Catalogue ID |
|---|---|---|
| merchant123 | 0 | MERCHA-00001 |
| merchant123 | 5 | MERCHA-00006 |
| store456 | 99 | STORE4-00100 |
| abc | 0 | ABC-00001 |
Test Results
Unit Tests: β ALL PASSED
Test 1: Merchant with 5 existing catalogues β PASSED
Test 2: New merchant with 0 catalogues β PASSED
Test 3: Short merchant ID (< 6 chars) β PASSED
Test 4: Merchant with 999 catalogues β PASSED
Test 5: Error handling - UUID fallback β PASSED
Test 6: Integration with create_catalogue_item β PASSED
Test file: test_catalogue_id_generation.py
Benefits
- Human-readable: Easy to reference and communicate
- Merchant-scoped: Each merchant has independent sequence
- Sequential: Predictable numbering for inventory management
- Safe fallback: Automatically uses UUID if generation fails
- Backward compatible: No breaking changes to existing code
Error Handling
- If database query fails β Falls back to UUID
- If merchant_id is missing β Uses UUID
- Logs all generation attempts for debugging
Next Steps
To test in your environment:
- Review the test results in
test_catalogue_id_generation.py - Check the manual test guide in
test_catalogue_create_manual.md - Create a new catalogue item via API to see the new ID format
Files Modified
- β
app/models/catalogue_models.py- Added new method and updated create logic
Files Created
- β
test_catalogue_id_generation.py- Comprehensive unit tests - β
test_catalogue_create_manual.md- Manual testing guide - β
CATALOGUE_ID_UPDATE.md- This summary document