Spaces:
Running
Running
| """ | |
| Quick start example for merchant module usage. | |
| Run this after starting the server to create sample merchants. | |
| """ | |
| import asyncio | |
| import httpx | |
| BASE_URL = "http://localhost:8000" | |
| async def create_sample_merchants(): | |
| """Create a complete merchant hierarchy for testing""" | |
| async with httpx.AsyncClient() as client: | |
| # 1. Create ncnf (Root) | |
| print("Creating ncnf...") | |
| ncnf = { | |
| "merchant_type": "ncnf", | |
| "merchant_code": "ncnf-INDIA-001", | |
| "merchant_name": "National Distribution Center India", | |
| "contact": { | |
| "phone": "+919876543210", | |
| "email": "national@distribution.com", | |
| "address_line1": "Plot 123, Industrial Area", | |
| "address_line2": "Phase 1", | |
| "city": "Mumbai", | |
| "state": "Maharashtra", | |
| "pincode": "400001", | |
| "country": "India" | |
| }, | |
| "kyc": { | |
| "gst_number": "27AAPFU0939F1ZV", | |
| "pan_number": "AAPFU0939F", | |
| "bank_account_number": "1234567890123", | |
| "bank_ifsc": "HDFC0001234", | |
| "business_certificate_url": "https://storage.example.com/ncnf_cert.pdf", | |
| "cancelled_cheque_url": "https://storage.example.com/ncnf_cheque.jpg" | |
| }, | |
| "settings": { | |
| "pricing_inherited": False, | |
| "inventory_inherited": False, | |
| "auto_allocate_stock": True, | |
| "allowed_payment_modes": ["PREPAID", "COD"], | |
| "credit_limit": 10000000 | |
| }, | |
| "created_by": "system_admin" | |
| } | |
| response = await client.post(f"{BASE_URL}/merchants", json=ncnf) | |
| if response.status_code == 201: | |
| ncnf_data = response.json() | |
| print(f"β Created ncnf: {ncnf_data['merchant_id']}") | |
| # 2. Create cnf under ncnf | |
| print("\nCreating cnf...") | |
| cnf = { | |
| "merchant_type": "cnf", | |
| "parent_merchant_id": ncnf_data["merchant_id"], | |
| "merchant_code": "cnf-MH-001", | |
| "merchant_name": "Maharashtra cnf", | |
| "contact": { | |
| "phone": "+919876543211", | |
| "email": "maharashtra@cnf.com", | |
| "address_line1": "456 cnf Street", | |
| "city": "Mumbai", | |
| "state": "Maharashtra", | |
| "pincode": "400002", | |
| "country": "India" | |
| }, | |
| "kyc": { | |
| "gst_number": "27BBCDE1234F1Z5", | |
| "pan_number": "BBCDE1234F", | |
| "bank_account_number": "9876543210123", | |
| "bank_ifsc": "ICIC0001234", | |
| "business_certificate_url": "https://storage.example.com/cnf_cert.pdf", | |
| "cancelled_cheque_url": "https://storage.example.com/cnf_cheque.jpg" | |
| }, | |
| "settings": { | |
| "pricing_inherited": True, | |
| "allowed_payment_modes": ["PREPAID", "COD"], | |
| "credit_limit": 5000000 | |
| }, | |
| "created_by": "ncnf_admin" | |
| } | |
| response = await client.post(f"{BASE_URL}/merchants", json=cnf) | |
| if response.status_code == 201: | |
| cnf_data = response.json() | |
| print(f"β Created cnf: {cnf_data['merchant_id']}") | |
| # 3. Create distributor under cnf | |
| print("\nCreating distributor...") | |
| distributor = { | |
| "merchant_type": "distributor", | |
| "parent_merchant_id": cnf_data["merchant_id"], | |
| "merchant_code": "DIST-MUM-001", | |
| "merchant_name": "Mumbai Central distributor", | |
| "contact": { | |
| "phone": "+919876543212", | |
| "email": "mumbai@distributor.com", | |
| "address_line1": "789 Distribution Hub", | |
| "city": "Mumbai", | |
| "state": "Maharashtra", | |
| "pincode": "400003", | |
| "country": "India" | |
| }, | |
| "kyc": { | |
| "gst_number": "27CDEFG5678H1Z2", | |
| "pan_number": "CDEFG5678H", | |
| "bank_account_number": "5555666677778888", | |
| "bank_ifsc": "SBIN0001234", | |
| "business_certificate_url": "https://storage.example.com/dist_cert.pdf", | |
| "cancelled_cheque_url": "https://storage.example.com/dist_cheque.jpg" | |
| }, | |
| "settings": { | |
| "pricing_inherited": True, | |
| "allowed_payment_modes": ["PREPAID", "COD", "PARTIAL"], | |
| "credit_limit": 2000000 | |
| }, | |
| "created_by": "cnf_admin" | |
| } | |
| response = await client.post(f"{BASE_URL}/merchants", json=distributor) | |
| if response.status_code == 201: | |
| dist_data = response.json() | |
| print(f"β Created distributor: {dist_data['merchant_id']}") | |
| # 4. Create retail under distributor | |
| print("\nCreating retail...") | |
| retail = { | |
| "merchant_type": "retail", | |
| "parent_merchant_id": dist_data["merchant_id"], | |
| "merchant_code": "retail-MUM-001", | |
| "merchant_name": "Glamour retail Mumbai", | |
| "contact": { | |
| "phone": "+919876543213", | |
| "email": "contact@glamourretail.com", | |
| "address_line1": "101 Fashion Street", | |
| "address_line2": "Near Mall", | |
| "city": "Mumbai", | |
| "state": "Maharashtra", | |
| "pincode": "400004", | |
| "country": "India" | |
| }, | |
| "geo_location": { | |
| "lat": 19.0760, | |
| "lng": 72.8777 | |
| }, | |
| "kyc": { | |
| "pan_number": "DEFGH6789I", | |
| "business_certificate_url": "https://storage.example.com/retail_cert.pdf" | |
| }, | |
| "settings": { | |
| "pricing_inherited": True, | |
| "allowed_payment_modes": ["PREPAID", "COD"], | |
| "credit_limit": 100000 | |
| }, | |
| "created_by": "dist_admin" | |
| } | |
| response = await client.post(f"{BASE_URL}/merchants", json=retail) | |
| if response.status_code == 201: | |
| retail_data = response.json() | |
| print(f"β Created retail: {retail_data['merchant_id']}") | |
| # Show hierarchy | |
| print("\n" + "="*60) | |
| print("MERCHANT HIERARCHY CREATED SUCCESSFULLY") | |
| print("="*60) | |
| print(f"ncnf: {ncnf_data['merchant_id']}") | |
| print(f" βββ cnf: {cnf_data['merchant_id']}") | |
| print(f" βββ distributor: {dist_data['merchant_id']}") | |
| print(f" βββ retail: {retail_data['merchant_id']}") | |
| print("="*60) | |
| # Test hierarchy endpoint | |
| print("\nFetching hierarchy for retail...") | |
| response = await client.get(f"{BASE_URL}/merchants/{retail_data['merchant_id']}/hierarchy") | |
| if response.status_code == 200: | |
| hierarchy = response.json() | |
| print(f"Hierarchy depth: {hierarchy['depth']}") | |
| for i, merchant in enumerate(hierarchy['hierarchy'], 1): | |
| print(f" {i}. {merchant['merchant_type']}: {merchant['merchant_name']}") | |
| return True | |
| else: | |
| print(f"β Failed to create retail: {response.text}") | |
| else: | |
| print(f"β Failed to create distributor: {response.text}") | |
| else: | |
| print(f"β Failed to create cnf: {response.text}") | |
| else: | |
| print(f"β Failed to create ncnf: {response.text}") | |
| return False | |
| async def list_all_merchants(): | |
| """List all merchants""" | |
| async with httpx.AsyncClient() as client: | |
| print("\n" + "="*60) | |
| print("ALL MERCHANTS") | |
| print("="*60) | |
| response = await client.get(f"{BASE_URL}/merchants?limit=100") | |
| if response.status_code == 200: | |
| merchants = response.json() | |
| for merchant in merchants: | |
| print(f"{merchant['merchant_type']:15} | {merchant['merchant_code']:15} | {merchant['merchant_name']}") | |
| else: | |
| print(f"Error listing merchants: {response.text}") | |
| async def main(): | |
| """Main execution""" | |
| print("SCM Merchant Module - Quick Start") | |
| print("="*60) | |
| print("Ensure the server is running at http://localhost:8000") | |
| print("="*60) | |
| # Test health check | |
| async with httpx.AsyncClient() as client: | |
| try: | |
| response = await client.get(f"{BASE_URL}/health") | |
| if response.status_code == 200: | |
| print("β Server is running\n") | |
| else: | |
| print("β Server health check failed") | |
| return | |
| except Exception as e: | |
| print(f"β Cannot connect to server: {e}") | |
| print("Please start the server with: python main.py") | |
| return | |
| # Create sample data | |
| success = await create_sample_merchants() | |
| if success: | |
| # List all merchants | |
| await list_all_merchants() | |
| print("\n" + "="*60) | |
| print("Quick Start Complete!") | |
| print("="*60) | |
| print("Next steps:") | |
| print(" - Visit http://localhost:8000/docs for API documentation") | |
| print(" - Try updating a merchant status") | |
| print(" - Create more merchants in the hierarchy") | |
| print(" - Test validation with invalid data") | |
| if __name__ == "__main__": | |
| asyncio.run(main()) | |