Skip to content

Commit

Permalink
select new admin
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sidhdhi-p committed Oct 10, 2024
1 parent 4d1c0c7 commit 0b55301
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
14 changes: 13 additions & 1 deletion khelo/functions/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
});
Expand Down
39 changes: 39 additions & 0 deletions khelo/functions/src/team/team_repository.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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;
29 changes: 28 additions & 1 deletion khelo/functions/src/team/team_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
21 changes: 21 additions & 0 deletions khelo/functions/src/user/user_repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 0b55301

Please sign in to comment.