Skip to content

Commit

Permalink
residents logic (updated)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kouhakouu committed Dec 24, 2024
1 parent dec791f commit 1d978cb
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 126 deletions.
27 changes: 21 additions & 6 deletions lib/features/admin/data/admin_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,21 @@ class AdminRepository {
throw Exception('Không lấy được UID của người dùng.');
}

// **New Code Start**
// Extract floor and apartmentNumber from queueData
int floor;
int apartmentNumber;
try {
floor = int.parse(queueData['floor']['integerValue']);
apartmentNumber = int.parse(queueData['apartmentNumber']['integerValue']);
} catch (e) {
throw Exception('Dữ liệu floor hoặc apartmentNumber không hợp lệ.');
}

// Generate profileId as "{floor}-{apartmentNumber}"
String profileId = '$floor-$apartmentNumber';
// **New Code End**

// Chuẩn bị dữ liệu cho collection đích và thêm profileId
if (role == 'Cư dân') {
targetData = {
Expand All @@ -111,11 +126,11 @@ class AdminRepository {
'dob': queueData['dob']['stringValue'],
'phone': queueData['phone']['stringValue'],
'id': queueData['id']['stringValue'],
'floor': int.parse(queueData['floor']['integerValue']),
'apartmentNumber': int.parse(queueData['apartmentNumber']['integerValue']),
'floor': floor, // Already parsed as int
'apartmentNumber': apartmentNumber, // Already parsed as int
'email': email,
'status': 'Đã duyệt',
'profileId': uid, // Thêm profileId
'profileId': profileId, // Sử dụng profileId mới
};
} else if (role == 'Khách') {
targetData = {
Expand All @@ -127,7 +142,7 @@ class AdminRepository {
'email': email,
'jobTitle': queueData['jobTitle']['stringValue'],
'status': 'Đã duyệt',
'profileId': uid, // Thêm profileId
'profileId': profileId, // Sử dụng profileId mới
};
} else {
throw Exception('Vai trò không hợp lệ.');
Expand All @@ -148,7 +163,7 @@ class AdminRepository {
throw Exception('Phê duyệt thất bại khi tạo tài liệu trong $targetCollection.');
}

// Tạo profile document trong collection 'profiles' với documentId = profileId (uid)
// Tạo profile document trong collection 'profiles' với documentId = profileId ("{floor}-{apartmentNumber}")
Map<String, dynamic> profileData = {
'householdHead': _encodeField(''), // Initialize as empty string
'occupation': _encodeField(''), // Initialize as empty string
Expand All @@ -160,7 +175,7 @@ class AdminRepository {
'utilities': _encodeField(<String>[]), // Empty array
};

final profileUrl = 'https://firestore.googleapis.com/v1/projects/$projectId/databases/(default)/documents/profiles/$uid?key=$apiKey';
final profileUrl = 'https://firestore.googleapis.com/v1/projects/$projectId/databases/(default)/documents/profiles/$profileId?key=$apiKey';

final profileResponse = await http.patch(
Uri.parse(profileUrl),
Expand Down
Loading

0 comments on commit 1d978cb

Please sign in to comment.