fix: refactored few handler and repository (#25567)
* update * update * refactor
This commit is contained in:
1 parent
e4a6b97017
commit
ead0691ea3
7 files changed
+146
-45
No files matched your search
@@ -333,7 +333,9 @@ export class OrganizationRepository {
|
||||
accepted: membership?.accepted,
|
||||
},
|
||||
...membership?.team,
|
||||
metadata,
|
||||
metadata: {
|
||||
requestedSlug: metadata?.requestedSlug ?? null,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -308,9 +308,16 @@ export class TeamRepository {
|
||||
slug: true,
|
||||
logoUrl: true,
|
||||
isOrganization: true,
|
||||
metadata: true,
|
||||
inviteTokens: true,
|
||||
parent: true,
|
||||
parent: {
|
||||
select: {
|
||||
id: true,
|
||||
slug: true,
|
||||
logoUrl: true,
|
||||
name: true,
|
||||
isOrganization: true,
|
||||
},
|
||||
},
|
||||
parentId: true,
|
||||
},
|
||||
},
|
||||
@@ -323,14 +330,21 @@ export class TeamRepository {
|
||||
if (includeOrgs) return true;
|
||||
return !mmship.team.isOrganization;
|
||||
})
|
||||
.map(({ team: { inviteTokens, ...team }, ...membership }) => ({
|
||||
role: membership.role,
|
||||
accepted: membership.accepted,
|
||||
...team,
|
||||
metadata: teamMetadataSchema.parse(team.metadata),
|
||||
/** To prevent breaking we only return non-email attached token here, if we have one */
|
||||
inviteToken: inviteTokens.find((token) => token.identifier === `invite-link-for-teamId-${team.id}`),
|
||||
}));
|
||||
.map(({ team: { inviteTokens, ...team }, ...membership }) => {
|
||||
// Only return inviteToken if user is OWNER or ADMIN
|
||||
const inviteToken =
|
||||
membership.role === "OWNER" || membership.role === "ADMIN"
|
||||
? inviteTokens.find((token) => token.identifier === `invite-link-for-teamId-${team.id}`)
|
||||
: null;
|
||||
|
||||
return {
|
||||
role: membership.role,
|
||||
accepted: membership.accepted,
|
||||
...team,
|
||||
/** To prevent breaking we only return non-email attached token here, if we have one */
|
||||
inviteToken,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -357,9 +371,7 @@ export class TeamRepository {
|
||||
},
|
||||
});
|
||||
|
||||
return memberships
|
||||
.filter((mmship) => !mmship.team.isOrganization)
|
||||
.map((mmship) => mmship.team);
|
||||
return memberships.filter((mmship) => !mmship.team.isOrganization).map((mmship) => mmship.team);
|
||||
}
|
||||
|
||||
async findTeamWithOrganizationSettings(teamId: number) {
|
||||
|
||||
@@ -516,6 +516,45 @@ export class UserRepository {
|
||||
};
|
||||
}
|
||||
|
||||
async enrichUserWithItsProfileExcludingOrgMetadata<
|
||||
T extends {
|
||||
id: number;
|
||||
username: string | null;
|
||||
}
|
||||
>({
|
||||
user,
|
||||
}: {
|
||||
user: T;
|
||||
}): Promise<
|
||||
T & {
|
||||
nonProfileUsername: string | null;
|
||||
profile: UserProfile;
|
||||
}
|
||||
> {
|
||||
const enrichedUser = await this.enrichUserWithItsProfile({ user });
|
||||
|
||||
const { profile } = enrichedUser;
|
||||
|
||||
if (!profile || !profile.organization) {
|
||||
return enrichedUser;
|
||||
}
|
||||
|
||||
// Exclude organization metadata
|
||||
const sanitizedProfile: UserProfile = {
|
||||
...profile,
|
||||
organization: {
|
||||
...profile.organization,
|
||||
metadata: {},
|
||||
organizationSettings: profile.organization.organizationSettings,
|
||||
},
|
||||
};
|
||||
|
||||
return {
|
||||
...enrichedUser,
|
||||
profile: sanitizedProfile,
|
||||
};
|
||||
}
|
||||
|
||||
async enrichUsersWithTheirProfiles<T extends { id: number; username: string | null }>(
|
||||
users: T[]
|
||||
): Promise<
|
||||
@@ -575,6 +614,42 @@ export class UserRepository {
|
||||
});
|
||||
}
|
||||
|
||||
async enrichUsersWithTheirProfileExcludingOrgMetadata<T extends { id: number; username: string | null }>(
|
||||
users: T[]
|
||||
): Promise<
|
||||
Array<
|
||||
T & {
|
||||
nonProfileUsername: string | null;
|
||||
profile: UserProfile;
|
||||
}
|
||||
>
|
||||
> {
|
||||
const enrichedUsers = await this.enrichUsersWithTheirProfiles(users);
|
||||
|
||||
return enrichedUsers.map((enrichedUser) => {
|
||||
const { profile } = enrichedUser;
|
||||
|
||||
if (!profile || !profile.organization) {
|
||||
return enrichedUser;
|
||||
}
|
||||
|
||||
// Exclude organization metadata
|
||||
const sanitizedProfile: UserProfile = {
|
||||
...profile,
|
||||
organization: {
|
||||
...profile.organization,
|
||||
metadata: {},
|
||||
organizationSettings: profile.organization.organizationSettings,
|
||||
},
|
||||
};
|
||||
|
||||
return {
|
||||
...enrichedUser,
|
||||
profile: sanitizedProfile,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
enrichUserWithItsProfileBuiltFromUser<T extends { id: number; username: string | null }>({
|
||||
user,
|
||||
}: {
|
||||
|
||||
+1
-1
@@ -75,7 +75,7 @@ async function getTeamMembers({
|
||||
for (const membership of memberships) {
|
||||
membershipWithUserProfile.push({
|
||||
...membership,
|
||||
user: await userRepo.enrichUserWithItsProfile({
|
||||
user: await userRepo.enrichUserWithItsProfileExcludingOrgMetadata({
|
||||
user: membership.user,
|
||||
}),
|
||||
});
|
||||
|
||||
@@ -104,13 +104,23 @@ export const listOtherTeamMembers = async ({ input }: ListOptions) => {
|
||||
nextCursor = nextItem?.id || null;
|
||||
}
|
||||
|
||||
const users = members.map((membership) => membership.user);
|
||||
const enrichedUsers = await new UserRepository(prisma).enrichUsersWithTheirProfileExcludingOrgMetadata(
|
||||
users
|
||||
);
|
||||
|
||||
const enrichedUserMap = new Map<number, (typeof enrichedUsers)[0]>();
|
||||
enrichedUsers.forEach((enrichedUser) => {
|
||||
enrichedUserMap.set(enrichedUser.id, enrichedUser);
|
||||
});
|
||||
|
||||
const enrichedMemberships = [];
|
||||
for (const membership of members) {
|
||||
const enrichedUser = enrichedUserMap.get(membership.user.id);
|
||||
if (!enrichedUser) continue;
|
||||
enrichedMemberships.push({
|
||||
...membership,
|
||||
user: await new UserRepository(prisma).enrichUserWithItsProfile({
|
||||
user: membership.user,
|
||||
}),
|
||||
user: enrichedUser,
|
||||
});
|
||||
}
|
||||
return {
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
import { Resource, CustomAction } from "@calcom/features/pbac/domain/types/permission-registry";
|
||||
import { getSpecificPermissions } from "@calcom/features/pbac/lib/resource-permissions";
|
||||
import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
|
||||
import { UserRepository } from "@calcom/features/users/repositories/UserRepository";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import type { Prisma } from "@calcom/prisma/client";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
@@ -110,28 +107,21 @@ export const legacyListMembers = async ({ ctx, input }: ListMembersOptions) => {
|
||||
],
|
||||
});
|
||||
|
||||
const enrichedMembers = await Promise.all(
|
||||
memberships.map(async (membership) =>
|
||||
new UserRepository(prisma).enrichUserWithItsProfile({
|
||||
user: {
|
||||
...membership.user,
|
||||
accepted: membership.accepted,
|
||||
membershipId: membership.id,
|
||||
},
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
const usersFetched = enrichedMembers.length;
|
||||
|
||||
let nextCursor: typeof cursor | undefined = undefined;
|
||||
if (usersFetched > limit) {
|
||||
const nextItem = enrichedMembers.pop();
|
||||
nextCursor = nextItem?.membershipId;
|
||||
if (memberships.length > limit) {
|
||||
const nextItem = memberships.pop();
|
||||
nextCursor = nextItem?.id;
|
||||
}
|
||||
|
||||
const members = memberships.map((membership) => ({
|
||||
id: membership.user.id,
|
||||
name: membership.user.name,
|
||||
username: membership.user.username,
|
||||
avatarUrl: membership.user.avatarUrl,
|
||||
}));
|
||||
|
||||
return {
|
||||
members: enrichedMembers,
|
||||
members,
|
||||
nextCursor,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -105,11 +105,23 @@ export const listMembersHandler = async ({ ctx, input }: ListMembersHandlerOptio
|
||||
// PBAC not enabled or error occurred, continue with traditional roles
|
||||
}
|
||||
|
||||
const membersWithApps = await Promise.all(
|
||||
teamMembers.map(async (member) => {
|
||||
const user = await new UserRepository(prisma).enrichUserWithItsProfile({
|
||||
user: member.user,
|
||||
});
|
||||
const users = teamMembers.map((member) => member.user);
|
||||
const enrichedUsers = await new UserRepository(prisma).enrichUsersWithTheirProfileExcludingOrgMetadata(
|
||||
users
|
||||
);
|
||||
|
||||
const enrichedUserMap = new Map<number, (typeof enrichedUsers)[0]>();
|
||||
enrichedUsers.forEach((enrichedUser) => {
|
||||
enrichedUserMap.set(enrichedUser.id, enrichedUser);
|
||||
});
|
||||
|
||||
const membersWithApps = teamMembers
|
||||
.map((member) => {
|
||||
const user = enrichedUserMap.get(member.user.id);
|
||||
if (!user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const { profile, ...restUser } = user;
|
||||
|
||||
// Determine the role to display
|
||||
@@ -140,7 +152,7 @@ export const listMembersHandler = async ({ ctx, input }: ListMembersHandlerOptio
|
||||
: null,
|
||||
};
|
||||
})
|
||||
);
|
||||
.filter((member): member is NonNullable<typeof member> => member !== null);
|
||||
|
||||
return {
|
||||
members: membersWithApps,
|
||||
|
||||
Reference in new issue
Block a user