import 'dart:developer'; import 'package:mc_common_app/api/api_client.dart'; import 'package:mc_common_app/classes/app_state.dart'; import 'package:mc_common_app/classes/consts.dart'; import 'package:mc_common_app/config/dependency_injection.dart'; import 'package:mc_common_app/extensions/string_extensions.dart'; import 'package:mc_common_app/models/appointments_models/appointment_list_model.dart'; import 'package:mc_common_app/models/appointments_models/schedule_model.dart'; import 'package:mc_common_app/models/appointments_models/service_schedule_model.dart'; import 'package:mc_common_app/models/general_models/generic_resp_model.dart'; import 'package:mc_common_app/models/provider_branches_models/profile/services.dart'; import 'package:mc_common_app/utils/enums.dart'; abstract class AppointmentRepo { Future> getMyAppointmentsForProvider(Map map); Future> getMyAppointmentsForCustomersByFilters({ List? providerIdsList, List? categoryIdsList, List? serviceIdsList, List? itemIdsList, List? branchIdsList, }); Future updateAppointmentStatus({required int appointmentId, required AppointmentStatusEnum appointmentStatusEnum}); Future updateAppointmentPaymentStatus(Map map); Future getAppointmentSlots(Map map); Future createMergeAppointment(Map map); Future getAllServices(String branchId); Future createSchedule(Map map); Future addServicesInSchedule(Map map); Future updateSchedule(Map map); Future> getSchedules(String branchId); Future updateServicesInSchedule(Map map); Future> mergeServiceIntoAvailableSchedules({ required List serviceItemIdsForHome, required List serviceItemIdsForWorkshop, }); Future createServiceAppointment({required List schedules, required int serviceProviderID}); Future cancelOrRescheduleServiceAppointment({required int serviceAppointmentID, required int serviceSlotID, required int appointmentScheduleAction}); } class AppointmentRepoImp implements AppointmentRepo { ApiClient apiClient = injector.get(); AppState appState = injector.get(); @override Future getAllServices(String branchId) async { Map map = {"ProviderBranchID": branchId}; String t = appState.getUser.data!.accessToken ?? ""; return await apiClient.getJsonForObject((json) => Services.fromJson(json), ApiConsts.getServicesOfBranch, token: t, queryParameters: map); } @override Future createSchedule(Map map) async { String t = appState.getUser.data!.accessToken ?? ""; return await apiClient.postJsonForObject((json) => GenericRespModel.fromJson(json), ApiConsts.createSchedule, map, token: t); } @override Future addServicesInSchedule(Map map) async { String t = appState.getUser.data!.accessToken ?? ""; return await apiClient.postJsonForObject((json) => GenericRespModel.fromJson(json), ApiConsts.createGroup, map, token: t); } @override Future> getSchedules(String branchId) async { Map map = {"ServiceProviderBranchID": branchId}; String t = appState.getUser.data!.accessToken ?? ""; GenericRespModel adsGenericModel = await apiClient.getJsonForObject( (json) => GenericRespModel.fromJson(json), ApiConsts.getSchedule, token: t, queryParameters: map, ); return List.generate(adsGenericModel.data.length, (index) => ScheduleData.fromJson(adsGenericModel.data[index])); } @override Future updateSchedule(Map map) async { String t = appState.getUser.data!.accessToken ?? ""; return await apiClient.postJsonForObject((json) => GenericRespModel.fromJson(json), ApiConsts.updateSchedule, map, token: t); } @override Future updateServicesInSchedule(Map map) async { String t = appState.getUser.data!.accessToken ?? ""; return await apiClient.postJsonForObject((json) => GenericRespModel.fromJson(json), ApiConsts.updateGroup, map, token: t); } @override Future> mergeServiceIntoAvailableSchedules({ required List serviceItemIdsForHome, required List serviceItemIdsForWorkshop, }) async { String t = appState.getUser.data!.accessToken ?? ""; var queryParameters = []; if (serviceItemIdsForWorkshop.isNotEmpty) { queryParameters.add({ "appointmentType": 1, "ServiceItemIDs": serviceItemIdsForWorkshop, }); } if (serviceItemIdsForHome.isNotEmpty) { queryParameters.add({ "appointmentType": 2, "ServiceItemIDs": serviceItemIdsForHome, }); } GenericRespModel adsGenericModel = await apiClient.postJsonForObject( (json) => GenericRespModel.fromJson(json), ApiConsts.getServiceItemAppointmentScheduleSlots, queryParameters, token: t, ); if (adsGenericModel.data == null) { return []; } List serviceAppointmentScheduleModel = List.generate(adsGenericModel.data.length, (index) => ServiceAppointmentScheduleModel.fromJson(adsGenericModel.data[index], isForAppointment: true)); return serviceAppointmentScheduleModel; } @override Future createServiceAppointment({required List schedules, required int serviceProviderID}) async { String t = appState.getUser.data!.accessToken ?? ""; int customerId = appState.getUser.data!.userInfo!.customerId ?? 0; List> mapList = []; for (var schedule in schedules) { List serviceItemIds = []; for (var service in schedule.servicesListInAppointment!) { for (var item in service.serviceItems!) { serviceItemIds.add(item.id!); } } int slotId = 0; int index = schedule.selectedCustomTimeDateSlotModel!.availableSlots!.indexWhere((element) => element.isSelected); if (index == -1) { index = 0; } slotId = schedule.selectedCustomTimeDateSlotModel!.availableSlots![index].slotId; mapList.add({ "serviceSlotID": slotId, "serviceProviderID": serviceProviderID, "customerID": customerId, "serviceItemID": serviceItemIds, "amountCustomerLocation": schedule.totalLocationCharges.toString(), "customerLocLat": (schedule.locationInfoModel!.latitude).toString(), "customerLocLong": (schedule.locationInfoModel!.longitude).toString(), "customerLocAddress": (schedule.locationInfoModel!.address).toString(), }); } log("maplist: ${mapList.toString()}"); GenericRespModel adsGenericModel = await apiClient.postJsonForObject( (json) => GenericRespModel.fromJson(json), ApiConsts.serviceProvidersAppointmentCreate, mapList, token: t, ); return adsGenericModel; } @override Future cancelOrRescheduleServiceAppointment({required int serviceAppointmentID, required int serviceSlotID, required int appointmentScheduleAction}) async { String t = appState.getUser.data!.accessToken ?? ""; final payload = { "serviceAppointmentID": serviceAppointmentID, "serviceSlotID": serviceSlotID, "appointmentScheduleAction": appointmentScheduleAction, }; GenericRespModel adsGenericModel = await apiClient.postJsonForObject( (json) => GenericRespModel.fromJson(json), ApiConsts.serviceProviderAppointmentRescheduleCancelAppointment, payload, token: t, ); return adsGenericModel; } @override Future> getMyAppointmentsForProvider(Map map) async { String t = appState.getUser.data!.accessToken ?? ""; GenericRespModel genericRespModel = await apiClient.getJsonForObject( token: t, (json) => GenericRespModel.fromJson(json), queryParameters: map, ApiConsts.serviceProvidersAppointmentGet, ); List appointmentList = List.generate(genericRespModel.data.length, (index) => AppointmentListModel.fromJson(genericRespModel.data[index])); return appointmentList; } @override Future> getMyAppointmentsForCustomersByFilters({ List? providerIdsList, List? categoryIdsList, List? serviceIdsList, List? itemIdsList, List? branchIdsList, }) async { var params = { "customerID": appState.getUser.data!.userInfo!.customerId.toString(), "ServiceProviderIDs": providerIdsList ?? [], "ProviderBranchIDs": branchIdsList ?? [], "ServiceIDs": serviceIdsList ?? [], "ServiceItemIDs": itemIdsList ?? [], "CategoryIDs": categoryIdsList ?? [], }; GenericRespModel genericRespModel = await apiClient.getJsonForObject( token: appState.getUser.data!.accessToken, (json) => GenericRespModel.fromJson(json), queryParameters: params, ApiConsts.customersAppointmentGetByFilters, ); List appointmentList = List.generate(genericRespModel.data.length, (index) => AppointmentListModel.fromJson(genericRespModel.data[index])); return appointmentList; } @override Future getAppointmentSlots(Map map) async { String t = appState.getUser.data!.accessToken ?? ""; GenericRespModel adsGenericModel = await apiClient.getJsonForObject( (json) => GenericRespModel.fromJson(json), ApiConsts.getAppointmentSlots, token: t, queryParameters: map, ); return adsGenericModel; } @override Future updateAppointmentPaymentStatus(Map map) async { String t = appState.getUser.data!.accessToken ?? ""; return await apiClient.postJsonForObject((json) => GenericRespModel.fromJson(json), ApiConsts.updateAppointmentPaymentStatus, map, token: t); } @override Future updateAppointmentStatus({required int appointmentId, required AppointmentStatusEnum appointmentStatusEnum}) async { String t = appState.getUser.data!.accessToken ?? ""; var params = { "appointmentID": appointmentId.toString(), "appointmentStatusID": appointmentStatusEnum.getIdFromAppointmentStatusEnum().toString(), }; return await apiClient.postJsonForObject((json) => GenericRespModel.fromJson(json), ApiConsts.updateAppointmentStatus, params, token: t); } @override Future createMergeAppointment(Map map) async { String t = appState.getUser.data!.accessToken ?? ""; return await apiClient.postJsonForObject((json) => GenericRespModel.fromJson(json), ApiConsts.createMergeAppointment, map, token: t); } }