Expose manager session flag
This commit is contained in:
@@ -23,7 +23,8 @@ export const userTypeDefs = `#graphql
|
||||
displayName: String
|
||||
phone: String
|
||||
avatarId: String
|
||||
isAdmin: Boolean
|
||||
isManager: Boolean
|
||||
isAdmin: Boolean @deprecated(reason: "Use isManager")
|
||||
activeTeamId: String
|
||||
activeTeam: UserTeam
|
||||
teams: [UserTeam]
|
||||
@@ -171,7 +172,7 @@ async function getOrCreateProfileByPhone(phone: string) {
|
||||
})
|
||||
}
|
||||
|
||||
async function isAdminUser(userId: number): Promise<boolean> {
|
||||
async function isManagerUser(userId: number): Promise<boolean> {
|
||||
const user = await prisma.user.findUnique({ where: { id: userId } })
|
||||
if (user === null) return false
|
||||
if (user.isStaff || user.isSuperuser) return true
|
||||
@@ -183,6 +184,7 @@ async function isAdminUser(userId: number): Promise<boolean> {
|
||||
|
||||
async function mapProfileUser(profile: Awaited<ReturnType<typeof getOrCreateProfile>>) {
|
||||
const memberships = await prisma.teamMember.findMany({ where: { userId: profile.userId }, include: { team: true } })
|
||||
const isManager = await isManagerUser(profile.userId)
|
||||
return {
|
||||
id: profile.logtoId,
|
||||
firstName: profile.user.firstName,
|
||||
@@ -190,7 +192,8 @@ async function mapProfileUser(profile: Awaited<ReturnType<typeof getOrCreateProf
|
||||
displayName: displayName(profile.user.firstName, profile.user.lastName, profile.phone),
|
||||
phone: profile.phone,
|
||||
avatarId: profile.avatarId,
|
||||
isAdmin: await isAdminUser(profile.userId),
|
||||
isManager,
|
||||
isAdmin: isManager,
|
||||
activeTeamId: profile.activeTeam?.uuid ?? null,
|
||||
activeTeam: profile.activeTeam ? { id: profile.activeTeam.uuid, name: profile.activeTeam.name, teamType: profile.activeTeam.teamType, logtoOrgId: profile.activeTeam.logtoOrgId, createdAt: profile.activeTeam.createdAt.toISOString() } : null,
|
||||
teams: memberships.map(m => ({ id: m.team.uuid, name: m.team.name, teamType: m.team.teamType, logtoOrgId: m.team.logtoOrgId, createdAt: m.team.createdAt.toISOString() })),
|
||||
@@ -238,7 +241,7 @@ export const userResolvers = {
|
||||
managerUsers: async (_: unknown, __: unknown, ctx: AuthContext) => {
|
||||
if (!ctx.userId) throw new GraphQLError('Not authenticated')
|
||||
const profile = await getOrCreateProfile(ctx.userId)
|
||||
if (!(await isAdminUser(profile.userId))) return []
|
||||
if (!(await isManagerUser(profile.userId))) return []
|
||||
const members = await prisma.teamMember.findMany({
|
||||
where: profile.activeTeamId === null ? {} : { teamId: profile.activeTeamId },
|
||||
include: { user: { include: { profile: { include: { activeTeam: true } } } }, team: true },
|
||||
|
||||
Reference in New Issue
Block a user