import 'dart:collection'; import 'dart:convert'; import 'package:workouttest_util/model/customer.dart'; import 'package:workouttest_util/model/customer_property.dart'; import 'package:workouttest_util/model/property.dart'; import 'package:workouttest_util/model/user.dart'; import 'package:workouttest_util/service/api.dart'; import 'package:workouttest_util/model/cache.dart'; import 'package:workouttest_util/util/logging.dart'; import 'package:workouttest_util/util/not_found_exception.dart'; class CustomerApi with Logging { final APIClient _client = APIClient(); Future> getRealCustomers(String param) async { final body = await _client.get("customers/", param); final Iterable json = jsonDecode(body); final List customers = json.map((customer) => Customer.fromJson(customer)).toList(); return customers; } Future saveCustomer(Customer customer) async { customer.dateChange = DateTime.now(); String body = const JsonEncoder().convert(customer.toJson()); log(" ===== saving customer id: ${customer.customerId}:$body"); await _client.post("customers/${customer.customerId}", body); } Future updateFirebaseUid(int customerId, String uid) async { log(" ===== update Firebase uid : $customerId: $uid"); await _client.post("customers/update_firebase_uid/$customerId", uid); } Future deactivateCustomer(int customerId) async { log(" ===== deactivate : $customerId"); await _client.post("customers/deactivate/$customerId", ""); } Future addCustomer(Customer customer) async { customer.dateAdd = DateTime.now(); customer.dateChange = DateTime.now(); String body = const JsonEncoder().convert(customer.toJson()); log(" ===== add new customer: $body"); await _client.post("customers", body); } Future addUser(User user) async { String body = const JsonEncoder().convert(user.toJson()); log(" ===== add new user: $body"); final String responseBody = await _client.post("registration", body); Customer customer; try { int? status = jsonDecode(responseBody)['status']; if (status != null) { String error = jsonDecode(responseBody)['error']; throw Exception(error); } else { customer = Customer.fromJson(jsonDecode(responseBody)); await Cache().afterRegistration(customer); } } on FormatException { if (responseBody == "Customer exists") { throw WorkoutTestException(code: WorkoutTestException.CUSTOMER_EXISTS, message: responseBody); } throw Exception(responseBody); } } Future getUser(User user) async { String body = const JsonEncoder().convert(user.toJson()); log(" ===== login the user: $body"); final String responseBody = await _client.post("login", body); Customer customer; try { customer = Customer.fromJson(jsonDecode(responseBody)); await Cache().afterLogin(customer); } on FormatException { throw Exception(responseBody); } } Future getUserByEmail(String email) async { log(" ===== User getByEmail : $email"); final String responseBody = await _client.get("customers/find_by_email/$email", ""); Customer customer; try { customer = Customer.fromJson(jsonDecode(responseBody)); if (customer.firebaseUid == null) { await updateFirebaseUid(customer.customerId!, Cache().firebaseUid!); } Cache().userLoggedIn = customer; final List? properties = await getActualProperties(customer.customerId!); if (properties != null) { initProperties(properties); } } on FormatException { throw Exception(responseBody); } } Future getCustomer(int customerId) async { String body = ""; log(" ===== get the customer by id: $customerId"); try { final String responseBody = await _client.get("customers/$customerId", body); Customer customer = Customer.fromJson(jsonDecode(responseBody)); log(" --- Customer: ${customer.toJson()}"); Cache().userLoggedIn = customer; final List? properties = await getActualProperties(customerId); //log(" ---- Props: " + properties.toJson().toString()); //await Cache().initCustomer(customerId); if (properties != null) { initProperties(properties); } } on Exception catch (exception) { log("Exception: $exception"); log(" === go to registration "); Cache().logout(); Cache().startPage = "registration"; } } void initProperties(final List? customerProperties) { List? properties = Cache().getProperties(); Customer customer = Cache().userLoggedIn!; customer.properties = LinkedHashMap(); if (properties != null) { // reset Properties for (var property in properties) { CustomerProperty customerProperty = CustomerProperty(propertyId: property.propertyId, customerId: customer.customerId!, dateAdd: DateTime.now(), propertyValue: 0); customer.properties[property.propertyName] = customerProperty; } for (var customerProperty in customerProperties!) { for (var property in properties) { if (customerProperty.propertyId == property.propertyId) { customer.properties[property.propertyName] = customerProperty; } } } } } Future getTrainee(int customerId) async { String body = ""; Customer customer; log(" ===== get Trainee customer by id: $customerId"); try { final String responseBody = await _client.get("customers/$customerId", body); customer = Customer.fromJson(jsonDecode(responseBody)); log(" --- Trainee: ${customer.toJson()}"); } catch (exception) { log("Exception: $exception"); throw Exception(exception); } return customer; } Future> getTrainees(int trainerId) async { List trainees = []; log("Get trainees list"); try { String body = ""; final String responseBody = await _client.get("customers/trainees/$trainerId", body); final Iterable json = jsonDecode(responseBody); trainees = json.map((customer) => Customer.fromJson(customer)).toList(); } catch (exception) { log("Exception: $exception"); throw Exception(exception); } return trainees; } Future> getAllProperties(int customerId) async { final body = await _client.get("customer_property/", customerId.toString()); final Iterable json = jsonDecode(body); final List properties = json.map((property) => CustomerProperty.fromJson(property)).toList(); return properties; } Future?> getActualProperties(int customerId) async { List? properties; try { final body = await _client.get("customer_property/last/", customerId.toString()); final Iterable json = jsonDecode(body); properties = json.map((property) => CustomerProperty.fromJson(property)).toList(); } on Exception catch (ex) { log(ex.toString()); } return properties; } Future addProperty(CustomerProperty property) async { String body = const JsonEncoder().convert(property.toJson()); log(" ===== add new customer property: $body"); CustomerProperty customerProperty; String? responseBody; try { responseBody = await _client.post("customer_property", body); log(" responseBody: $responseBody"); int? status = jsonDecode(responseBody)['status']; if (status != null) { throw Exception(jsonDecode(responseBody)['error']); } else { customerProperty = CustomerProperty.fromJson(jsonDecode(responseBody)); } } on FormatException { throw Exception(responseBody); } on Exception catch (e) { throw Exception(e); } return customerProperty; } Future updateProperty(CustomerProperty property) async { String body = const JsonEncoder().convert(property.toJson()); CustomerProperty? customerProperty; log(" ===== update customer property: $body"); String? responseBody; try { responseBody = await _client.post("customer_property/update/${property.customerPropertyId}", body); log(" responseBody: $responseBody"); int? status = jsonDecode(responseBody)['status']; if (status != null) { throw Exception(jsonDecode(responseBody)['error']); } else { customerProperty = CustomerProperty.fromJson(jsonDecode(responseBody)); } } on FormatException { throw Exception(responseBody); } on Exception catch (e) { throw Exception(e); } return customerProperty; } }