Skip to content

Commit

Permalink
Merge pull request #117 from canopas/sidhdhi/select-new-team-admin
Browse files Browse the repository at this point in the history
Select new admin for team on user delete
  • Loading branch information
cp-mayank authored Oct 23, 2024
2 parents 5e8ea0f + 2158219 commit 0b2b8b0
Show file tree
Hide file tree
Showing 50 changed files with 911 additions and 320 deletions.
6 changes: 5 additions & 1 deletion data/lib/api/team/team_model.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// ignore_for_file: non_constant_identifier_names

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

import '../../converter/timestamp_json_converter.dart';
import '../../utils/constant/firestore_constant.dart';
import '../user/user_models.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'team_model.freezed.dart';

Expand All @@ -21,6 +22,9 @@ abstract class TeamModel with _$TeamModel {
String? name_initial,
String? profile_img_url,
String? created_by,
@JsonKey(includeToJson: false, includeFromJson: false)
@Default(UserModel(id: ''))
UserModel created_by_user,
DateTime? created_at,
@TimeStampJsonConverter() DateTime? created_time,
@JsonKey(name: FireStoreConst.teamPlayers)
Expand Down
46 changes: 45 additions & 1 deletion data/lib/api/team/team_model.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ mixin _$TeamModel {
String? get name_initial => throw _privateConstructorUsedError;
String? get profile_img_url => throw _privateConstructorUsedError;
String? get created_by => throw _privateConstructorUsedError;
@JsonKey(includeToJson: false, includeFromJson: false)
UserModel get created_by_user => throw _privateConstructorUsedError;
DateTime? get created_at => throw _privateConstructorUsedError;
@TimeStampJsonConverter()
DateTime? get created_time => throw _privateConstructorUsedError;
Expand Down Expand Up @@ -56,9 +58,13 @@ abstract class $TeamModelCopyWith<$Res> {
String? name_initial,
String? profile_img_url,
String? created_by,
@JsonKey(includeToJson: false, includeFromJson: false)
UserModel created_by_user,
DateTime? created_at,
@TimeStampJsonConverter() DateTime? created_time,
@JsonKey(name: FireStoreConst.teamPlayers) List<TeamPlayer> players});

$UserModelCopyWith<$Res> get created_by_user;
}

/// @nodoc
Expand All @@ -83,6 +89,7 @@ class _$TeamModelCopyWithImpl<$Res, $Val extends TeamModel>
Object? name_initial = freezed,
Object? profile_img_url = freezed,
Object? created_by = freezed,
Object? created_by_user = null,
Object? created_at = freezed,
Object? created_time = freezed,
Object? players = null,
Expand Down Expand Up @@ -116,6 +123,10 @@ class _$TeamModelCopyWithImpl<$Res, $Val extends TeamModel>
? _value.created_by
: created_by // ignore: cast_nullable_to_non_nullable
as String?,
created_by_user: null == created_by_user
? _value.created_by_user
: created_by_user // ignore: cast_nullable_to_non_nullable
as UserModel,
created_at: freezed == created_at
? _value.created_at
: created_at // ignore: cast_nullable_to_non_nullable
Expand All @@ -130,6 +141,16 @@ class _$TeamModelCopyWithImpl<$Res, $Val extends TeamModel>
as List<TeamPlayer>,
) as $Val);
}

/// Create a copy of TeamModel
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
$UserModelCopyWith<$Res> get created_by_user {
return $UserModelCopyWith<$Res>(_value.created_by_user, (value) {
return _then(_value.copyWith(created_by_user: value) as $Val);
});
}
}

/// @nodoc
Expand All @@ -148,9 +169,14 @@ abstract class _$$TeamModelImplCopyWith<$Res>
String? name_initial,
String? profile_img_url,
String? created_by,
@JsonKey(includeToJson: false, includeFromJson: false)
UserModel created_by_user,
DateTime? created_at,
@TimeStampJsonConverter() DateTime? created_time,
@JsonKey(name: FireStoreConst.teamPlayers) List<TeamPlayer> players});

@override
$UserModelCopyWith<$Res> get created_by_user;
}

/// @nodoc
Expand All @@ -173,6 +199,7 @@ class __$$TeamModelImplCopyWithImpl<$Res>
Object? name_initial = freezed,
Object? profile_img_url = freezed,
Object? created_by = freezed,
Object? created_by_user = null,
Object? created_at = freezed,
Object? created_time = freezed,
Object? players = null,
Expand Down Expand Up @@ -206,6 +233,10 @@ class __$$TeamModelImplCopyWithImpl<$Res>
? _value.created_by
: created_by // ignore: cast_nullable_to_non_nullable
as String?,
created_by_user: null == created_by_user
? _value.created_by_user
: created_by_user // ignore: cast_nullable_to_non_nullable
as UserModel,
created_at: freezed == created_at
? _value.created_at
: created_at // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -234,6 +265,8 @@ class _$TeamModelImpl implements _TeamModel {
this.name_initial,
this.profile_img_url,
this.created_by,
@JsonKey(includeToJson: false, includeFromJson: false)
this.created_by_user = const UserModel(id: ''),
this.created_at,
@TimeStampJsonConverter() this.created_time,
@JsonKey(name: FireStoreConst.teamPlayers)
Expand All @@ -258,6 +291,9 @@ class _$TeamModelImpl implements _TeamModel {
@override
final String? created_by;
@override
@JsonKey(includeToJson: false, includeFromJson: false)
final UserModel created_by_user;
@override
final DateTime? created_at;
@override
@TimeStampJsonConverter()
Expand All @@ -273,7 +309,7 @@ class _$TeamModelImpl implements _TeamModel {

@override
String toString() {
return 'TeamModel(id: $id, name: $name, name_lowercase: $name_lowercase, city: $city, name_initial: $name_initial, profile_img_url: $profile_img_url, created_by: $created_by, created_at: $created_at, created_time: $created_time, players: $players)';
return 'TeamModel(id: $id, name: $name, name_lowercase: $name_lowercase, city: $city, name_initial: $name_initial, profile_img_url: $profile_img_url, created_by: $created_by, created_by_user: $created_by_user, created_at: $created_at, created_time: $created_time, players: $players)';
}

@override
Expand All @@ -292,6 +328,8 @@ class _$TeamModelImpl implements _TeamModel {
other.profile_img_url == profile_img_url) &&
(identical(other.created_by, created_by) ||
other.created_by == created_by) &&
(identical(other.created_by_user, created_by_user) ||
other.created_by_user == created_by_user) &&
(identical(other.created_at, created_at) ||
other.created_at == created_at) &&
(identical(other.created_time, created_time) ||
Expand All @@ -310,6 +348,7 @@ class _$TeamModelImpl implements _TeamModel {
name_initial,
profile_img_url,
created_by,
created_by_user,
created_at,
created_time,
const DeepCollectionEquality().hash(_players));
Expand Down Expand Up @@ -339,6 +378,8 @@ abstract class _TeamModel implements TeamModel {
final String? name_initial,
final String? profile_img_url,
final String? created_by,
@JsonKey(includeToJson: false, includeFromJson: false)
final UserModel created_by_user,
final DateTime? created_at,
@TimeStampJsonConverter() final DateTime? created_time,
@JsonKey(name: FireStoreConst.teamPlayers)
Expand All @@ -362,6 +403,9 @@ abstract class _TeamModel implements TeamModel {
@override
String? get created_by;
@override
@JsonKey(includeToJson: false, includeFromJson: false)
UserModel get created_by_user;
@override
DateTime? get created_at;
@override
@TimeStampJsonConverter()
Expand Down
39 changes: 33 additions & 6 deletions data/lib/service/match/match_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ class MatchService {
}
}

Future<int> getUserOwnedMatchesCount(String userId) {
return _matchCollection
.where(FireStoreConst.createdBy, isEqualTo: userId)
.count()
.get()
.then((snapshot) {
return snapshot.count ?? 0;
}).catchError((error, stack) => throw AppError.fromError(error, stack));
}

Stream<MatchSetting?> streamMatchSetting(String matchId) {
return _matchSettingDocument(matchId)
.snapshots()
Expand Down Expand Up @@ -499,12 +509,29 @@ class MatchService {
required String matchId,
required RevisedTarget revisedTarget,
}) async {
await _matchCollection.doc(matchId).update(
{
FireStoreConst.revisedTarget: revisedTarget.toJson(),
FireStoreConst.updatedAt: DateTime.now(),
},
);
try {
await _matchCollection.doc(matchId).update(
{
FireStoreConst.revisedTarget: revisedTarget.toJson(),
FireStoreConst.updatedAt: DateTime.now(),
},
);
} catch (error, stack) {
throw AppError.fromError(error, stack);
}
}

Future<void> changeMatchOwner({
required String matchId,
required String ownerId,
}) async {
try {
await _matchCollection.doc(matchId).update(
{FireStoreConst.createdBy: ownerId},
);
} catch (error, stack) {
throw AppError.fromError(error, stack);
}
}

Future<void> deleteMatch(String matchId) async {
Expand Down
Loading

0 comments on commit 0b2b8b0

Please sign in to comment.