# API Create Client with User ## ๐Ÿš€ **Overview** API ini memungkinkan Anda untuk membuat client dan admin user dalam satu request. Sangat berguna untuk setup awal sistem atau ketika Anda perlu membuat client baru dengan admin user yang sudah siap. ## ๐Ÿ“‹ **Endpoint** ``` POST /api/clients/with-user ``` ## ๐Ÿ”ง **Request Body** ```json { "client": { "name": "Client Name", "description": "Client description", "clientType": "standalone", "parentClientId": null, "maxUsers": 100, "maxStorage": 1073741824, "settings": "{\"theme\": \"dark\", \"language\": \"en\"}" }, "adminUser": { "username": "admin", "email": "admin@client.com", "fullname": "Admin User", "password": "SecurePassword123!", "phoneNumber": "+6281234567890", "address": "Jakarta, Indonesia", "workType": "fulltime", "genderType": "male", "identityType": "ktp", "identityGroup": "employee", "identityGroupNumber": "EMP001", "identityNumber": "1234567890123456", "dateOfBirth": "1990-01-01", "lastEducation": "Bachelor" } } ``` ## ๐Ÿ“ **Field Descriptions** ### **Client Fields:** - `name` (required): Nama client - `description` (optional): Deskripsi client - `clientType` (required): Tipe client (`standalone`, `parent_client`, `sub_client`) - `parentClientId` (optional): ID parent client (untuk sub_client) - `maxUsers` (optional): Maksimal jumlah user - `maxStorage` (optional): Maksimal storage dalam bytes - `settings` (optional): Settings dalam format JSON string ### **Admin User Fields:** - `username` (required): Username untuk login - `email` (required): Email address - `fullname` (required): Nama lengkap - `password` (required): Password untuk login - `phoneNumber` (optional): Nomor telepon - `address` (optional): Alamat - `workType` (optional): Tipe pekerjaan - `genderType` (optional): Jenis kelamin - `identityType` (optional): Tipe identitas - `identityGroup` (optional): Grup identitas - `identityGroupNumber` (optional): Nomor grup identitas - `identityNumber` (optional): Nomor identitas - `dateOfBirth` (optional): Tanggal lahir (YYYY-MM-DD) - `lastEducation` (optional): Pendidikan terakhir ## ๐Ÿ“ค **Response** ### **Success Response (200):** ```json { "success": true, "messages": ["Client 'Client Name' and admin user 'admin' created successfully"], "data": { "client": { "id": "550e8400-e29b-41d4-a716-446655440000", "name": "Client Name", "description": "Client description", "clientType": "standalone", "parentClientId": null, "maxUsers": 100, "maxStorage": 1073741824, "currentUsers": 1, "currentStorage": null, "settings": "{\"theme\": \"dark\", \"language\": \"en\"}", "createdById": 1, "isActive": true, "createdAt": "2025-01-01T12:00:00Z", "updatedAt": "2025-01-01T12:00:00Z" }, "adminUser": { "id": 123, "username": "admin", "email": "admin@client.com", "fullname": "Admin User", "userLevelId": 1, "userRoleId": 1, "phoneNumber": "+6281234567890", "address": "Jakarta, Indonesia", "workType": "fulltime", "genderType": "male", "identityType": "ktp", "identityGroup": "employee", "identityGroupNumber": "EMP001", "identityNumber": "1234567890123456", "dateOfBirth": "1990-01-01", "lastEducation": "Bachelor", "keycloakId": "550e8400-e29b-41d4-a716-446655440001", "clientId": "550e8400-e29b-41d4-a716-446655440000", "isActive": true, "createdAt": "2025-01-01T12:00:00Z", "updatedAt": "2025-01-01T12:00:00Z" }, "message": "Client 'Client Name' and admin user 'admin' created successfully" } } ``` ### **Error Response (400):** ```json { "success": false, "messages": ["Validation failed: [Username is required, Email is required]"] } ``` ### **Error Response (500):** ```json { "success": false, "messages": ["Failed to create admin user: Keycloak user creation failed"] } ``` ## ๐Ÿงช **Curl Examples** ### **1. Create Standalone Client with Admin User:** ```bash curl -X POST "http://localhost:8080/api/clients/with-user" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_TOKEN" \ -d '{ "client": { "name": "PT. Example Company", "description": "A technology company", "clientType": "standalone", "maxUsers": 50, "maxStorage": 5368709120, "settings": "{\"theme\": \"light\", \"language\": \"id\"}" }, "adminUser": { "username": "admin", "email": "admin@example.com", "fullname": "John Admin", "password": "SecurePass123!", "phoneNumber": "+6281234567890", "address": "Jakarta, Indonesia", "workType": "fulltime", "genderType": "male" } }' ``` ### **2. Create Sub Client with Admin User:** ```bash curl -X POST "http://localhost:8080/api/clients/with-user" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_TOKEN" \ -d '{ "client": { "name": "PT. Example Branch Office", "description": "Branch office of main company", "clientType": "sub_client", "parentClientId": "550e8400-e29b-41d4-a716-446655440000", "maxUsers": 25, "maxStorage": 2684354560 }, "adminUser": { "username": "branch_admin", "email": "branch@example.com", "fullname": "Jane Branch Admin", "password": "BranchPass123!", "phoneNumber": "+6289876543210", "address": "Surabaya, Indonesia" } }' ``` ### **3. Create Parent Client with Admin User:** ```bash curl -X POST "http://localhost:8080/api/clients/with-user" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_TOKEN" \ -d '{ "client": { "name": "PT. Parent Company", "description": "Parent company that can have sub-clients", "clientType": "parent_client", "maxUsers": 200, "maxStorage": 10737418240, "settings": "{\"allowSubClients\": true, \"maxSubClients\": 10}" }, "adminUser": { "username": "parent_admin", "email": "parent@company.com", "fullname": "Parent Admin User", "password": "ParentPass123!", "phoneNumber": "+6281111111111", "address": "Bandung, Indonesia", "workType": "fulltime", "genderType": "female", "identityType": "ktp", "identityNumber": "1234567890123456" } }' ``` ## ๐Ÿ”’ **Authentication** API ini memerlukan authentication token. Token harus disertakan di header: ``` Authorization: Bearer YOUR_JWT_TOKEN ``` ## โš ๏ธ **Important Notes** 1. **User Level & Role**: Admin user akan otomatis diberikan: - `userLevelId`: 1 (Admin level) - `userRoleId`: 1 (Admin role) 2. **Keycloak Integration**: User akan otomatis dibuat di Keycloak dengan `keycloakId` yang di-generate. 3. **Transaction Safety**: Jika pembuatan user gagal, client yang sudah dibuat akan di-rollback (dihapus). 4. **Password Requirements**: Password harus memenuhi policy keamanan Keycloak. 5. **Email Uniqueness**: Email harus unique di seluruh sistem. 6. **Username Uniqueness**: Username harus unique di seluruh sistem. ## ๐Ÿšจ **Error Handling** ### **Common Errors:** 1. **Validation Error (400)**: - Missing required fields - Invalid email format - Invalid client type 2. **Authentication Error (401)**: - Invalid or missing token - Expired token 3. **Server Error (500)**: - Keycloak connection failed - Database error - User creation failed ## ๐Ÿ”„ **Workflow** 1. **Validate Request**: Cek semua field yang required 2. **Create Client**: Buat client di database 3. **Create User in Keycloak**: Buat user di Keycloak 4. **Create User in Database**: Simpan user info di database 5. **Return Response**: Return client dan user info ## ๐Ÿ“Š **Use Cases** 1. **Initial Setup**: Setup client baru dengan admin user 2. **Branch Office**: Buat branch office dengan admin lokal 3. **Subsidiary**: Buat subsidiary company dengan admin sendiri 4. **Multi-tenant**: Setup tenant baru dalam sistem multi-tenant --- **API ini sangat berguna untuk setup awal sistem atau ketika Anda perlu membuat client baru dengan admin user yang sudah siap!** ๐Ÿš€