-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeleteTopicFromUsers.dart
74 lines (62 loc) · 2.58 KB
/
deleteTopicFromUsers.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import 'package:cloud_firestore/cloud_firestore.dart';
Future deleteTopicFromUsers(
List<DocumentReference> votedBy,
List<DocumentReference> favoriteBy,
DocumentReference topicRef,
) async {
final batch = FirebaseFirestore.instance.batch();
// Iter through every user in the list votedBy
for (final reference in votedBy) {
final documentSnapshot = await reference.get();
if (documentSnapshot.exists) {
final data = documentSnapshot.data() as Map<String, dynamic>?;
if (data != null) {
final topicsYea =
List<DocumentReference>.from(data['topics_yea'] as List<dynamic>);
// remove from the user's topics yea the topic selected
if (topicsYea != null && topicsYea.contains(topicRef)) {
topicsYea.remove(topicRef);
batch.update(reference, {'topics_yea': topicsYea});
}
final topicsNay =
List<DocumentReference>.from(data['topics_nay'] as List<dynamic>);
// remove from the user's topics nay the topic selected
if (topicsNay != null && topicsNay.contains(topicRef)) {
topicsNay.remove(topicRef);
batch.update(reference, {'topics_nay': topicsNay});
}
final topicsVoted =
List<DocumentReference>.from(data['topics_voted'] as List<dynamic>);
// remove from the user's topics voted the topic selected
if (topicsVoted != null && topicsVoted.contains(topicRef)) {
topicsVoted.remove(topicRef);
batch.update(reference, {'topics_voted': topicsVoted});
}
final topicsSkipped = List<DocumentReference>.from(
data['topics_skipped'] as List<dynamic>);
// remove from the user's topics skipped the topic selected
if (topicsSkipped != null && topicsSkipped.contains(topicRef)) {
topicsSkipped.remove(topicRef);
batch.update(reference, {'topics_skipped': topicsSkipped});
}
}
}
}
// Iter through every user in the list favoriteBy
for (final reference in favoriteBy) {
final documentSnapshot = await reference.get();
if (documentSnapshot.exists) {
final data = documentSnapshot.data() as Map<String, dynamic>?;
if (data != null) {
final favorites =
List<DocumentReference>.from(data['favorites'] as List<dynamic>);
// remove from the user's favorites the topic selected
if (favorites != null && favorites.contains(topicRef)) {
favorites.remove(topicRef);
batch.update(reference, {'favorites': favorites});
}
}
}
}
await batch.commit();
}