feat: create customer

This commit is contained in:
efrilm
2025-08-03 16:13:33 +07:00
parent 52bedee66d
commit c87276a0ea
11 changed files with 1151 additions and 15 deletions
@@ -45,4 +45,57 @@ class CustomerRemoteDataSource {
return const Left('Unexpected error occurred');
}
}
Future<Either<String, bool>> createCustomer({
required String name,
required String phone,
required String address,
required String email,
required bool isActive,
}) async {
try {
final authData = await AuthLocalDataSource().getAuthData();
final url = '${Variables.baseUrl}/api/v1/customers';
Map<String, dynamic> data = {
'name': name,
'is_active': isActive,
};
if (phone.isNotEmpty) {
data['phone'] = phone;
}
if (address.isNotEmpty) {
data['address'] = address;
}
if (email.isNotEmpty) {
data['email'] = email;
}
final response = await dio.post(
url,
data: data,
options: Options(
headers: {
'Authorization': 'Bearer ${authData.token}',
'Accept': 'application/json',
},
),
);
if (response.statusCode == 200 || response.statusCode == 201) {
return const Right(true);
} else {
return const Left('Failed to create customer ');
}
} on DioException catch (e) {
log("Dio error: ${e.message}");
return Left(e.response?.data['message'] ?? 'Gagal membuat pelanggan');
} catch (e) {
log("Unexpected error: $e");
return const Left('Unexpected error occurred');
}
}
}