langugae :dart
framework:flutter
backend:firebase
i have a bundle which is basically multiple courses sold at a discounted price the problem is that i can’t create purchased_course documents using multiple courses ref list in bundles and each course also has its own books and videos subcollection i would love to hear your ideas for this
FFButtonWidget(
onPressed: () async {
await processRazorpayPayment(
context,
amount: int.parse((double var1) {
return var1.toStringAsFixed(2).replaceAll(".", "");
}(() {
if (_model.d1Value == true) {
return widget.bundle!.dPDays1.price;
} else if (_model.d2Value == true) {
return widget.bundle!.dPDays2.price;
} else if (_model.d3Value == true) {
return widget.bundle!.dPDays3.price;
} else if (_model.m1Value == true) {
return widget.bundle!.dPMonths1.price;
} else if (_model.m2Value == true) {
return widget.bundle!.dPMonths2.price;
} else if (_model.m3Value == true) {
return widget.bundle!.dPMonths3.price;
} else if (_model.y1Value == true) {
return widget.bundle!.dPYears1.price;
} else if (_model.y2Value == true) {
return widget.bundle!.dPYears2.price;
} else if (_model.y3Value == true) {
return widget.bundle!.dPYears3.price;
} else {
return null!;
}
}())),
currency: 'INR',
onReceivedResponse: (paymentId) => safeSetState(
() => _model.razorpayPaymentId3Copy = paymentId),
);
if (_model.razorpayPaymentId3Copy != null &&
_model.razorpayPaymentId3Copy != '') {
await currentUserReference!.update({
...mapToFirestore(
{
'Pbundle': FieldValue.arrayUnion(
[widget.bundle?.bundleId]),
},
),
});
/// Handle the purchase of the bundle
Future<void> handleBundlePurchase(
DocumentReference bundleRef,
DocumentReference userRef) async {
// Fetch the bundle document
final bundleSnapshot = await bundleRef.get();
final bundleData =
bundleSnapshot.data() as Map<String, dynamic>;
// Retrieve the list of course references
final List<DocumentReference> courseRefs =
List<DocumentReference>.from(
bundleData['courseref']);
// Iterate over each course reference
for (final courseRef in courseRefs) {
// Fetch the course document
final courseSnapshot = await courseRef.get();
final courseData =
courseSnapshot.data() as Map<String, dynamic>;
// Create a purchased course document for the user
final purchasedCourseRef =
userRef.collection('purchased_courses').doc();
await purchasedCourseRef.set({
'course_name': courseData['course_name'],
'purchase_date': DateTime.now(),
// Add more fields as needed
});
// Add books sub-collection
final booksSubCollection =
courseRef.collection('books');
final booksSnapshot =
await booksSubCollection.get();
for (final book in booksSnapshot.docs) {
await purchasedCourseRef
.collection('books')
.doc(book.id)
.set(book.data());
}
// Add videos sub-collection
final videosSubCollection =
courseRef.collection('videos');
final videosSnapshot =
await videosSubCollection.get();
for (final video in videosSnapshot.docs) {
await purchasedCourseRef
.collection('videos')
.doc(video.id)
.set(video.data());
}
}
}
i have also added bundles record field as an image and here’s purchased_course fields
// Existing fields
String? _name;
String get name => _name ?? '';
DateTime? _createdAt;
DateTime? get createdAt => _createdAt;
String? _astrologer;
String get astrologer => _astrologer ?? '';
DateTime? _expiry;
DateTime? get expiry => _expiry;
String? _courseID;
String get courseID => _courseID ?? '';
String? _description;
String get description => _description ?? '';
String? _coverimg;
String get coverimg => _coverimg ?? '';
bool? _isExpired;
bool get isExpired => _isExpired ?? false;
// Subcollection for books
CollectionReference get books => reference.collection('books');
// Subcollection for videos
CollectionReference get videos => reference.collection('videos');
/// Handle the purchase of the bundle
Future<void> handleBundlePurchase(
DocumentReference bundleRef,
DocumentReference userRef) async {
// Fetch the bundle document
final bundleSnapshot = await bundleRef.get();
final bundleData =
bundleSnapshot.data() as Map<String, dynamic>;
// Retrieve the list of course references
final List<DocumentReference> courseRefs =
List<DocumentReference>.from(
bundleData['courseref']);
// Iterate over each course reference
for (final courseRef in courseRefs) {
// Fetch the course document
final courseSnapshot = await courseRef.get();
final courseData =
courseSnapshot.data() as Map<String, dynamic>;
// Create a purchased course document for the user
final purchasedCourseRef =
userRef.collection('purchased_courses').doc();
await purchasedCourseRef.set({
'course_name': courseData['course_name'],
'purchase_date': DateTime.now(),
// Add more fields as needed
});
// Add books sub-collection
final booksSubCollection =
courseRef.collection('books');
final booksSnapshot =
await booksSubCollection.get();
for (final book in booksSnapshot.docs) {
await purchasedCourseRef
.collection('books')
.doc(book.id)
.set(book.data());
}
// Add videos sub-collection
final videosSubCollection =
courseRef.collection('videos');
final videosSnapshot =
await videosSubCollection.get();
for (final video in videosSnapshot.docs) {
await purchasedCourseRef
.collection('videos')
.doc(video.id)
.set(video.data());
}
}
}