createGroup method

Future<String> createGroup(
  1. String userId,
  2. String name, {
  3. bool isPublic = false,
  4. List<String> members = const [],
  5. bool locationSharingEnabled = true,
})

//////////// //////////// Creates a new group in Firestore

Implementation

// II.B - Group CRUD Operations
///////////////
/// Creates a new group in Firestore
Future<String> createGroup(
  String userId,
  String name,
  {
    bool isPublic = false,
    List<String> members = const [],
    bool locationSharingEnabled = true,
  }
) async {
  try {
    // Generate a unique public link code if public
    String? publicLinkCode;
    if (isPublic) {
      publicLinkCode = _generateGroupCode();
    }

    // Ensure creator is included in members
    final List<String> allMembers = List<String>.from(members);
    if (!allMembers.contains(userId)) {
      allMembers.add(userId);
    }

    // Create the group object
    final group = Group(
      name: name,
      createdBy: userId,
      members: allMembers,
      isPublic: isPublic,
      publicLinkCode: publicLinkCode,
      createdAt: Timestamp.now(),
      locationSharingEnabled: locationSharingEnabled,
    );

    // Add to Firestore
    final docRef = await _firestore.collection(_collectionPath).add(group.toMap());

    return docRef.id;
  } catch (e) {
    print('Error creating group: $e');
    rethrow;
  }
}