From 0b55301cdf7b938a5e8eda8486ca7fa5a51db09d Mon Sep 17 00:00:00 2001 From: sidhdhi canopas Date: Thu, 10 Oct 2024 12:41:51 +0530 Subject: [PATCH] select new admin --- khelo/functions/src/index.js | 14 +++++++- khelo/functions/src/team/team_repository.js | 39 +++++++++++++++++++++ khelo/functions/src/team/team_service.js | 29 ++++++++++++++- khelo/functions/src/user/user_repository.js | 21 +++++++++++ 4 files changed, 101 insertions(+), 2 deletions(-) diff --git a/khelo/functions/src/index.js b/khelo/functions/src/index.js index dc5570fa..f0613fd7 100644 --- a/khelo/functions/src/index.js +++ b/khelo/functions/src/index.js @@ -30,7 +30,7 @@ const userRepository = new user_repository.UserRepository(db); const teamRepository = new team_repository.TeamRepository(db); const notificationService = new notification_service.NotificationService(userRepository); -const teamService = new team_service.TeamService(userRepository, notificationService); +const teamService = new team_service.TeamService(userRepository, teamRepository, notificationService); const matchService = new match_service.MatchService(userRepository, teamRepository, notificationService); const authService = new auth_service.AuthService(userRepository); @@ -66,6 +66,18 @@ exports.teamPlayerChangeObserver = (0, firestore_2.onDocumentUpdated)({region: R await teamService.notifyOnAddedToTeam(oldTeam, newTeam); }); +exports.userDeleteObserver = (0, firestore_2.onDocumentDeleted)({region: REGION, document: "users/{userId}"}, async (event) => { + const snapshot = event.data; + if (!snapshot) { + (0, logger.error)("No data associated with the event"); + return; + } + const user = snapshot.data(); + if (user && user.id) { + await teamService.selectNewTeamAdminIfNeeded(user.id); + } +}); + exports.fiveMinuteCron = (0, scheduler.onSchedule)({timeZone: exports.TIMEZONE, schedule: "*/5 * * * *", region: REGION}, async () => { await matchRepository.processUpcomingMatches(); }); diff --git a/khelo/functions/src/team/team_repository.js b/khelo/functions/src/team/team_repository.js index 8759cee3..59b6754b 100644 --- a/khelo/functions/src/team/team_repository.js +++ b/khelo/functions/src/team/team_repository.js @@ -1,6 +1,7 @@ "use strict"; Object.defineProperty(exports, "__esModule", {value: true}); exports.TeamRepository = void 0; +const firestore_1 = require("firebase-admin/firestore"); class TeamRepository { constructor(db) { @@ -19,5 +20,43 @@ class TeamRepository { return []; } } + + async getTeamsCreatedByUserId(userId) { + const teamRef = this.teamRef().where("created_by", "==", userId); + try { + const teamDoc = await teamRef.get(); + return teamDoc.docs.map((doc) => doc.data()); + } catch (e) { + console.error("TeamRepository: Error getting teams created by user id:", e); + return []; + } + } + + async changePlayerRoleToAdmin(teamId, playerId) { + const teamRef = this.teamRef().doc(teamId); + const batch = this.db.batch(); + try { + const elementToRemove= {"id": playerId, "role": "player"}; + const elementToAdd = {"id": playerId, "role": "admin"}; + batch.update(teamRef, { + team_players: firestore_1.FieldValue.arrayRemove(elementToRemove), + }); + batch.update(teamRef, { + team_players: firestore_1.FieldValue.arrayUnion(elementToAdd), + }); + await batch.commit(); + } catch (e) { + console.error("TeamRepository: Error while changing player role to admin:", e); + } + } + + async markNoAdminTrueInTeam(teamId) { + const teamRef = this.teamRef().doc(teamId); + try { + await teamRef.update({no_admin: true}); + } catch (e) { + console.error("TeamRepository: Error while marking no admin as true in team:", e); + } + } } exports.TeamRepository=TeamRepository; diff --git a/khelo/functions/src/team/team_service.js b/khelo/functions/src/team/team_service.js index 01c0553c..fc0ed1ad 100644 --- a/khelo/functions/src/team/team_service.js +++ b/khelo/functions/src/team/team_service.js @@ -4,8 +4,9 @@ exports.TeamService = void 0; const user_models = require("../user/user_models"); class TeamService { - constructor(userRepository, notificationService) { + constructor(userRepository, teamRepository, notificationService) { this.userRepository = userRepository; + this.teamRepository = teamRepository; this.notificationService = notificationService; } async notifyOnAddedToTeam(oldTeam, newTeam) { @@ -28,5 +29,31 @@ class TeamService { await this.notificationService.sendNotification(playersToNotify, title, body, {team_id: teamId, type: "added_to_team"}); } } + + async selectNewTeamAdminIfNeeded(userId) { + const teams = await this.teamRepository.getTeamsCreatedByUserId(userId); + + for (const team of teams) { + const adminIds = team.team_players.filter((player) => player.role == "admin" && player.id != team.created_by).map((e) => e.id); + console.log("TeamService: admins:", adminIds); + + const activeAdmin = await this.userRepository.getAnyActiveUserFromIds(adminIds); + + if (adminIds.length == 0 || activeAdmin === null) { + const playerIds = team.team_players.filter((player) => player.role == "player" && player.id != team.created_by).map((e) => e.id); + console.log("TeamService: players:", playerIds); + if (playerIds.length == 0) { + await this.teamRepository.markNoAdminTrueInTeam(team.id); + } else { + const playerAsAdmin = await this.userRepository.getAnyActiveUserFromIds(playerIds); + if (playerAsAdmin === null) { + await this.teamRepository.markNoAdminTrueInTeam(team.id); + } else { + await this.teamRepository.changePlayerRoleToAdmin(team.id, playerAsAdmin.id); + } + } + } + } + } } exports.TeamService = TeamService; diff --git a/khelo/functions/src/user/user_repository.js b/khelo/functions/src/user/user_repository.js index 1498a758..2b720220 100644 --- a/khelo/functions/src/user/user_repository.js +++ b/khelo/functions/src/user/user_repository.js @@ -62,6 +62,27 @@ class UserRepository { return []; } } + + async getAnyActiveUserFromIds(additionalAdminIds) { + if (additionalAdminIds.length == 0) { + return null; + } + const userRef = this.userRef(); + try { + const querySnapshot = await userRef + .where("id", "in", additionalAdminIds) + .limit(1) + .get(); + if (!querySnapshot.empty) { + return querySnapshot.docs[0].data(); + } else { + return null; + } + } catch (e) { + console.error("UserRepository: Error getting any active user from ids:", e); + return null; + } + } } const firebaseAuthMiddleware = (userRepository) => { return async (req, res, next) => {