chore: add new structure for the ticket assinge to admins
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import { Injectable, InternalServerErrorException, Logger } from "@nestjs/common";
|
||||
|
||||
import { UserGroup } from "../../users/entities/user-group.entity";
|
||||
import { User } from "../../users/entities/user.entity";
|
||||
import { UsersService } from "../../users/providers/users.service";
|
||||
import { IReferralStrategy } from "../interfaces/referral-strategy";
|
||||
|
||||
@Injectable()
|
||||
export class GroupStrategy implements IReferralStrategy {
|
||||
private readonly logger = new Logger(GroupStrategy.name);
|
||||
|
||||
constructor(private readonly usersService: UsersService) {}
|
||||
|
||||
async assignAdmin(group: UserGroup): Promise<User> {
|
||||
try {
|
||||
const { users: admins } = await this.usersService.getUsersByGroup(group.id);
|
||||
|
||||
if (!admins.length) {
|
||||
this.logger.warn(`No admins found in Group: ${group.name}, falling back to super admin`);
|
||||
//fall back
|
||||
const superAdmin = await this.usersService.getSuperAdmin();
|
||||
if (!superAdmin) throw new InternalServerErrorException("No super admin found as fallback");
|
||||
|
||||
return superAdmin;
|
||||
}
|
||||
|
||||
// return the one with the fewest tickets
|
||||
return admins.reduce((prev, curr) => ((prev.tickets?.length || 0) < (curr.tickets?.length || 0) ? prev : curr));
|
||||
//
|
||||
} catch (error: unknown) {
|
||||
this.logger.error(`Error assigning admin for group ${group.name}: ${error instanceof Error ? error.message : "Unknown error"}`);
|
||||
// If any error occurs, try to get a super admin as last resort
|
||||
const superAdmin = await this.usersService.getSuperAdmin();
|
||||
if (!superAdmin) throw new InternalServerErrorException("No super admin found as fallback");
|
||||
|
||||
return superAdmin;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user