You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
car_common_app/lib/repositories/subscription_repo.dart

166 lines
5.7 KiB
Dart

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/dependencies.dart';
import 'package:mc_common_app/models/general_models/generic_resp_model.dart';
import 'package:mc_common_app/models/general_models/m_response.dart';
import 'package:mc_common_app/models/provider_branches_models/branch_detail_model.dart';
import 'package:mc_common_app/models/subscriptions_models/branch_user_selection_model.dart';
import 'package:mc_common_app/models/subscriptions_models/subscription_model.dart';
import 'package:mc_common_app/models/user_models/branch_user.dart';
abstract class SubscriptionRepo {
Future<SubscriptionModel> getAllSubscriptions(String? serviceProviderID);
Future<SubscriptionModel> getMySubscriptions(String? serviceProviderID);
Future<SubscriptionModel> getSubscriptionBySP(
String? serviceProviderID, bool isRenew);
Future<MResponse> calculationUpgradePrice(
String? serviceProviderID, String? newSubscription);
Future<MResponse> payForProviderSubscription(Map<String, dynamic> map);
Future<List<BranchSelectionModel>> getSPBranchUser_Get(
Map<String, String> map);
}
class SubscriptionRepoImp extends SubscriptionRepo {
@override
Future<SubscriptionModel> getAllSubscriptions(
String? serviceProviderID) async {
String t = AppState().getUser.data!.accessToken ?? "";
Map<String, String> queryParameters = {};
if (serviceProviderID != null) {
queryParameters = {
"ID": serviceProviderID,
};
}
return await injector.get<ApiClient>().getJsonForObject(
(json) => SubscriptionModel.fromJson(json),
ApiConsts.getAllSubscriptions,
token: t,
queryParameters: queryParameters);
}
@override
Future<SubscriptionModel> getSubscriptionBySP(
String? serviceProviderID, bool isRenew) async {
String t = AppState().getUser.data!.accessToken ?? "";
Map<String, String> queryParameters = {};
if (serviceProviderID != null) {
queryParameters = {
"ProviderID": serviceProviderID,
"IsRenew": isRenew.toString(),
};
}
return await injector.get<ApiClient>().getJsonForObject(
(json) => SubscriptionModel.fromJson(json),
ApiConsts.getSubscriptionBySP,
token: t,
queryParameters: queryParameters,
);
}
@override
Future<MResponse> calculationUpgradePrice(
String? serviceProviderID, String? newSubscription) async {
String t = AppState().getUser.data!.accessToken ?? "";
Map<String, String> queryParameters = {};
if (serviceProviderID != null) {
queryParameters = {
"ServiceProviderID": serviceProviderID,
"NewSubscription": newSubscription!,
};
}
return await injector.get<ApiClient>().getJsonForObject(
(json) => MResponse.fromJson(json),
ApiConsts.calculationUpgradePrice,
token: t,
queryParameters: queryParameters,
);
}
@override
Future<MResponse> payForProviderSubscription(Map<String, dynamic> map) async {
String t = AppState().getUser.data!.accessToken ?? "";
return await injector.get<ApiClient>().postJsonForObject(
(json) => MResponse.fromJson(json),
ApiConsts.payFortOrder_ProviderSubscription_Create,
map,
token: t,
);
}
@override
Future<List<BranchSelectionModel>> getSPBranchUser_Get(
Map<String, String> map) async {
String t = AppState().getUser.data!.accessToken ?? "";
GenericRespModel genericRespModel =
await injector.get<ApiClient>().getJsonForObject(
(json) => GenericRespModel.fromJson(json),
ApiConsts.getSPBranchUser_Get,
token: t,
queryParameters: map,
);
List<BranchSelectionModel> branchList = [];
if (genericRespModel.data != null) {
List<BranchDetailModel> branches = [];
List<BranchUser> branchUsers = [];
// List<BranchUsersDatum>.from(json["data"]!.map((x) => BranchUsersDatum.fromJson(x))
branches = List<BranchDetailModel>.from(genericRespModel.data["branches"]
["data"]!
.map((x) => BranchDetailModel.fromJson(x)));
branchUsers = List<BranchUser>.from(genericRespModel.data["branchUsers"]
["data"]!
.map((x) => BranchUser.fromJson(x)));
for (int i = 0; i < branches.length; i++) {
List<UserSelectionModel> availableUsers = [];
for (int j = 0; j < branchUsers.length; j++) {
if (branches[i].id == branchUsers[j].serviceProviderBranchID) {
availableUsers.add(UserSelectionModel(
userId: branchUsers[j].id,
userName:
("${branchUsers[j].firstName ?? ""} ${branchUsers[j].lastName}"),
isSelected: false));
}
}
branchList.add(BranchSelectionModel(
branchId: branches[i].id ?? 0,
branchName: branches[i].branchName ?? "",
isSelected: false,
usersList: availableUsers));
}
}
return branchList;
}
@override
Future<SubscriptionModel> getMySubscriptions(
String? serviceProviderID) async {
String t = AppState().getUser.data!.accessToken ?? "";
Map<String, String> queryParameters = {};
if (serviceProviderID != null) {
queryParameters = {
"ID": serviceProviderID,
};
}
return await injector.get<ApiClient>().getJsonForObject(
(json) => SubscriptionModel.fromJson(json),
ApiConsts.getAllSubscriptions,
token: t,
queryParameters: queryParameters);
}
}