Merge branch 'paritosh' of http://git.wdipl.com/Mayank.Mishra/MinglarBackendNestJS into mayank
This commit is contained in:
@@ -529,42 +529,59 @@ export class MinglarService {
|
||||
}
|
||||
|
||||
async getAllCoadminAndAM() {
|
||||
return await this.prisma.user.findMany({
|
||||
where: {
|
||||
roleXid: {
|
||||
in: [ROLE.CO_ADMIN, ROLE.ACCOUNT_MANAGER, ROLE.MINGLAR_ADMIN]
|
||||
},
|
||||
isActive: true,
|
||||
userStatus: USER_STATUS.ACTIVE,
|
||||
// 1. Fetch all required users (Admin, Co-Admin, AM)
|
||||
const users = await this.prisma.user.findMany({
|
||||
where: {
|
||||
roleXid: {
|
||||
in: [
|
||||
ROLE.MINGLAR_ADMIN, // Admin
|
||||
ROLE.CO_ADMIN, // Co-Admin
|
||||
ROLE.ACCOUNT_MANAGER // AM
|
||||
]
|
||||
},
|
||||
|
||||
include: {
|
||||
role: {
|
||||
select: {
|
||||
id: true,
|
||||
roleName: true,
|
||||
},
|
||||
isActive: true,
|
||||
userStatus: USER_STATUS.ACTIVE,
|
||||
},
|
||||
include: {
|
||||
role: {
|
||||
select: {
|
||||
id: true,
|
||||
roleName: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!users.length) return [];
|
||||
|
||||
const userIds = users.map((u) => u.id);
|
||||
|
||||
// 2. Count assigned hosts for ANY user (Admin / Co-Admin / AM)
|
||||
const groupedHosts = await this.prisma.hostHeader.groupBy({
|
||||
by: ["accountManagerXid"],
|
||||
where: {
|
||||
accountManagerXid: { in: userIds }, // assigned user
|
||||
isActive: true,
|
||||
},
|
||||
_count: {
|
||||
id: true,
|
||||
},
|
||||
});
|
||||
|
||||
// 3. Build quick lookup map: userId -> hostCount
|
||||
const hostCountMap: Record<number, number> = {};
|
||||
groupedHosts.forEach((g) => {
|
||||
const uid = Number(g.accountManagerXid);
|
||||
hostCountMap[uid] = g._count.id;
|
||||
});
|
||||
|
||||
// 4. Attach host counts to each user
|
||||
return users.map((user) => ({
|
||||
...user,
|
||||
assignedHostCount: hostCountMap[user.id] ?? 0,
|
||||
}));
|
||||
}
|
||||
|
||||
// (Optional) to return only the accepted invitations
|
||||
// inviteDetails: {
|
||||
// where: {
|
||||
// isMinglarInvitation: true,
|
||||
// is_accepted: true,
|
||||
// invitation_status: MINGLAR_INVITATION_STATUS.ACCEPTED,
|
||||
// isActive: true
|
||||
// },
|
||||
// select: {
|
||||
// id: true,
|
||||
// invitedBy: true,
|
||||
// invited_on: true,
|
||||
// invitation_status: true,
|
||||
// is_accepted: true
|
||||
// }
|
||||
// }
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async assignAMToHost(userId: number, hostXid: number, accountManagerXid: number) {
|
||||
const hostDetails = await this.prisma.hostHeader.findFirst({
|
||||
|
||||
Reference in New Issue
Block a user