feat: customer
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import 'dart:developer';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:enaklo_pos/core/network/dio_client.dart';
|
||||
import 'package:enaklo_pos/data/models/response/customer_response_model.dart';
|
||||
import '../../core/constants/variables.dart';
|
||||
import 'auth_local_datasource.dart';
|
||||
|
||||
class CustomerRemoteDataSource {
|
||||
final Dio dio = DioClient.instance;
|
||||
|
||||
Future<Either<String, CustomerResponseModel>> getCustomers({
|
||||
int page = 1,
|
||||
int limit = Variables.defaultLimit,
|
||||
}) async {
|
||||
try {
|
||||
final authData = await AuthLocalDataSource().getAuthData();
|
||||
final url = '${Variables.baseUrl}/api/v1/customers';
|
||||
|
||||
final response = await dio.get(
|
||||
url,
|
||||
queryParameters: {
|
||||
'page': page,
|
||||
'limit': limit,
|
||||
'organization_id': authData.user?.organizationId,
|
||||
},
|
||||
options: Options(
|
||||
headers: {
|
||||
'Authorization': 'Bearer ${authData.token}',
|
||||
'Accept': 'application/json',
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return Right(CustomerResponseModel.fromMap(response.data));
|
||||
} else {
|
||||
return const Left('Failed to get customers');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
log("Dio error: ${e.message}");
|
||||
return Left(e.response?.data['message'] ?? 'Gagal mengambil customer');
|
||||
} catch (e) {
|
||||
log("Unexpected error: $e");
|
||||
return const Left('Unexpected error occurred');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
import 'dart:convert';
|
||||
|
||||
class CustomerResponseModel {
|
||||
final bool? success;
|
||||
final CustomerData? data;
|
||||
final dynamic errors;
|
||||
|
||||
CustomerResponseModel({
|
||||
this.success,
|
||||
this.data,
|
||||
this.errors,
|
||||
});
|
||||
|
||||
factory CustomerResponseModel.fromJson(String str) =>
|
||||
CustomerResponseModel.fromMap(json.decode(str));
|
||||
|
||||
String toJson() => json.encode(toMap());
|
||||
|
||||
factory CustomerResponseModel.fromMap(Map<String, dynamic> json) =>
|
||||
CustomerResponseModel(
|
||||
success: json["success"],
|
||||
data: json["data"] == null ? null : CustomerData.fromMap(json["data"]),
|
||||
errors: json["errors"],
|
||||
);
|
||||
|
||||
Map<String, dynamic> toMap() => {
|
||||
"success": success,
|
||||
"data": data?.toMap(),
|
||||
"errors": errors,
|
||||
};
|
||||
}
|
||||
|
||||
class CustomerData {
|
||||
final List<Customer>? customers;
|
||||
final int? totalCount;
|
||||
final int? page;
|
||||
final int? limit;
|
||||
final int? totalPages;
|
||||
|
||||
CustomerData({
|
||||
this.customers,
|
||||
this.totalCount,
|
||||
this.page,
|
||||
this.limit,
|
||||
this.totalPages,
|
||||
});
|
||||
|
||||
factory CustomerData.fromMap(Map<String, dynamic> json) => CustomerData(
|
||||
customers: json["data"] == null
|
||||
? []
|
||||
: List<Customer>.from(json["data"].map((x) => Customer.fromMap(x))),
|
||||
totalCount: json["total_count"],
|
||||
page: json["page"],
|
||||
limit: json["limit"],
|
||||
totalPages: json["total_pages"],
|
||||
);
|
||||
|
||||
Map<String, dynamic> toMap() => {
|
||||
"data": customers == null
|
||||
? []
|
||||
: List<dynamic>.from(customers!.map((x) => x.toMap())),
|
||||
"total_count": totalCount,
|
||||
"page": page,
|
||||
"limit": limit,
|
||||
"total_pages": totalPages,
|
||||
};
|
||||
}
|
||||
|
||||
class Customer {
|
||||
final String? id;
|
||||
final String? organizationId;
|
||||
final String? name;
|
||||
final bool? isDefault;
|
||||
final bool? isActive;
|
||||
final Map<String, dynamic>? metadata;
|
||||
final DateTime? createdAt;
|
||||
final DateTime? updatedAt;
|
||||
|
||||
Customer({
|
||||
this.id,
|
||||
this.organizationId,
|
||||
this.name,
|
||||
this.isDefault,
|
||||
this.isActive,
|
||||
this.metadata,
|
||||
this.createdAt,
|
||||
this.updatedAt,
|
||||
});
|
||||
|
||||
factory Customer.fromMap(Map<String, dynamic> json) => Customer(
|
||||
id: json["id"],
|
||||
organizationId: json["organization_id"],
|
||||
name: json["name"],
|
||||
isDefault: json["is_default"],
|
||||
isActive: json["is_active"],
|
||||
metadata: json["metadata"] ?? {},
|
||||
createdAt: json["created_at"] == null
|
||||
? null
|
||||
: DateTime.parse(json["created_at"]),
|
||||
updatedAt: json["updated_at"] == null
|
||||
? null
|
||||
: DateTime.parse(json["updated_at"]),
|
||||
);
|
||||
|
||||
Map<String, dynamic> toMap() => {
|
||||
"id": id,
|
||||
"organization_id": organizationId,
|
||||
"name": name,
|
||||
"is_default": isDefault,
|
||||
"is_active": isActive,
|
||||
"metadata": metadata,
|
||||
"created_at": createdAt?.toIso8601String(),
|
||||
"updated_at": updatedAt?.toIso8601String(),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user