2020-09-15 07:13:41 +06:30
|
|
|
import 'dart:async';
|
|
|
|
|
|
2020-09-13 21:49:39 +06:30
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
2020-10-07 02:33:06 +06:30
|
|
|
import 'package:fcs/data/services/services.dart';
|
2024-09-22 16:49:59 +06:30
|
|
|
import 'package:fcs/constants.dart';
|
2020-10-07 02:33:06 +06:30
|
|
|
import 'package:fcs/domain/entities/user.dart';
|
|
|
|
|
import 'package:fcs/pages/main/model/base_model.dart';
|
2020-09-11 16:14:36 +06:30
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
|
|
2024-01-23 17:48:34 +06:30
|
|
|
import '../../../pagination/paginator_listener.dart';
|
|
|
|
|
|
2020-09-11 16:14:36 +06:30
|
|
|
class CustomerModel extends BaseModel {
|
|
|
|
|
final log = Logger('CustomerModel');
|
2024-01-23 17:48:34 +06:30
|
|
|
PaginatorListener<User>? getCustomers;
|
2020-09-11 16:14:36 +06:30
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
logout() async {
|
2024-01-23 17:48:34 +06:30
|
|
|
getCustomers?.close();
|
2020-09-11 16:14:36 +06:30
|
|
|
}
|
|
|
|
|
|
2024-01-24 16:54:08 +06:30
|
|
|
loadPaginationCustomers() {
|
2021-09-10 16:33:52 +06:30
|
|
|
if (user == null && !user!.hasCustomers()) return;
|
2020-09-13 21:49:39 +06:30
|
|
|
|
2024-01-24 16:54:08 +06:30
|
|
|
String path = "/$user_collection";
|
|
|
|
|
Query col = FirebaseFirestore.instance
|
|
|
|
|
.collection(path)
|
|
|
|
|
.where("is_sys_admin", isEqualTo: false);
|
2020-09-13 21:49:39 +06:30
|
|
|
|
2024-01-24 16:54:08 +06:30
|
|
|
Query pageQuery = FirebaseFirestore.instance
|
|
|
|
|
.collection(path)
|
|
|
|
|
.where("is_sys_admin", isEqualTo: false)
|
|
|
|
|
.orderBy("message_time", descending: true);
|
2020-09-15 07:13:41 +06:30
|
|
|
|
2024-01-24 16:54:08 +06:30
|
|
|
getCustomers?.close();
|
|
|
|
|
getCustomers = PaginatorListener<User>(
|
|
|
|
|
col, pageQuery, (data, id) => User.fromMap(data, id),
|
|
|
|
|
rowPerLoad: 30);
|
2020-09-13 21:49:39 +06:30
|
|
|
}
|
2020-09-20 05:34:49 +06:30
|
|
|
|
2025-03-21 18:19:52 +06:30
|
|
|
Future<User?> getUser(String? id) async {
|
|
|
|
|
if (id == null) return null;
|
2020-09-20 05:34:49 +06:30
|
|
|
String path = "/$user_collection";
|
2021-09-10 16:33:52 +06:30
|
|
|
var snap = await FirebaseFirestore.instance.collection(path).doc(id).get();
|
|
|
|
|
return User.fromMap(snap.data() as Map<String, dynamic>, snap.id);
|
2020-09-20 05:34:49 +06:30
|
|
|
}
|
2020-10-17 01:40:24 +06:30
|
|
|
|
2020-10-22 04:14:53 +06:30
|
|
|
Future<List<User>> getInvoiceUsers(String fcsShipmentID) async {
|
|
|
|
|
List<User> users = [];
|
|
|
|
|
try {
|
2021-09-10 16:33:52 +06:30
|
|
|
var snaps = await FirebaseFirestore.instance
|
2020-10-22 04:14:53 +06:30
|
|
|
.collection(
|
|
|
|
|
"/$fcs_shipment_collection/$fcsShipmentID/$user_collection")
|
|
|
|
|
.where("pending_invoice_carton_count", isGreaterThan: 0)
|
2021-09-10 16:33:52 +06:30
|
|
|
.get(GetOptions(source: Source.server));
|
|
|
|
|
users = snaps.docs.map((documentSnapshot) {
|
|
|
|
|
var user = User.fromMap(documentSnapshot.data(), documentSnapshot.id);
|
2020-10-22 04:14:53 +06:30
|
|
|
return user;
|
|
|
|
|
}).toList();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
log.warning("Error!! $e");
|
|
|
|
|
}
|
|
|
|
|
return users;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-17 01:40:24 +06:30
|
|
|
Future<void> enableUser(User user, bool enabled) {
|
2021-09-10 16:33:52 +06:30
|
|
|
return Services.instance.userService.enableUser(user.id ?? "", enabled);
|
2020-10-17 01:40:24 +06:30
|
|
|
}
|
2024-01-24 16:54:08 +06:30
|
|
|
|
|
|
|
|
Future<void> inviteUser(String userName, String phoneNumber) {
|
|
|
|
|
return Services.instance.userService.inviteUser(userName, phoneNumber);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> deleteInvite(String phoneNumber) {
|
|
|
|
|
return Services.instance.userService.deleteInvite(phoneNumber);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> acceptRequest(String userID) {
|
|
|
|
|
return Services.instance.userService.acceptRequest(userID);
|
|
|
|
|
}
|
2025-03-21 18:19:52 +06:30
|
|
|
|
|
|
|
|
Future<User?> getUserByFCSId(String? fcsID) async {
|
|
|
|
|
if (fcsID == null) return null;
|
|
|
|
|
String path = "/$user_collection";
|
|
|
|
|
var snap = await FirebaseFirestore.instance
|
|
|
|
|
.collection(path)
|
|
|
|
|
.where('fcs_id', isEqualTo: fcsID)
|
|
|
|
|
.limit(1)
|
|
|
|
|
.get();
|
|
|
|
|
|
|
|
|
|
if (snap.docs.isNotEmpty) {
|
|
|
|
|
return User.fromMap(snap.docs.first.data(), snap.docs.first.id);
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-11 16:14:36 +06:30
|
|
|
}
|