Files
calendar/packages/features/ee/teams/lib/getUserAdminTeams.ts
T
Alex van AndelandGitHub d25c043046 chore: Remove all avatar/logo references (#14532)
* chore: Remove all avatar/logo references

* Updated user profile to only use avatarUrl

* fix: minor style issue

* chore: Remove redundant includeTeamLogo

* fix: Use right avatar default in event type list

* fix: placeholder avatar team profile, target

* chore: Add logoUrl/avatarUrl to JWT

* fix: Bunch of org avatar issues, fix members list

* Fix logoUrl on org pages

* More type fixes

* Hopefully final type fixes

* Another round of type fixes

* fix: UserForm ts

* fix: Handle as return types, not input types

* fix: Remove profile and add avatarUrl

* fix: notFound as const

* fix: Seeder avatarUrl params

* revert: Migration changes for easier recovery

* fix: Add explicit type to builder as avatar is now set

* Add logoUrl to unpublished entity

* Avatar test out of scope here

* fix: Use the new avatarUrl after save

* chore: Removed getOrgAvatarUrl/getTeamAvatarUrl

* fix: Update sidebar image immediately after change

* Unable to safe unnamed user, so default

* fix: Unpublished page, add organization test

* Add more tests
2024-04-18 21:54:16 -07:00

97 lines
2.1 KiB
TypeScript

import type { Prisma } from "@prisma/client";
import { prisma } from "@calcom/prisma";
import { MembershipRole } from "@calcom/prisma/enums";
export type UserAdminTeams = (Prisma.TeamGetPayload<{
select: {
id: true;
name: true;
logoUrl: true;
credentials?: true;
parent?: {
select: {
id: true;
name: true;
logoUrl: true;
credentials: true;
};
};
};
}> & { isUser?: boolean })[];
/** Get a user's team & orgs they are admins/owners of. Abstracted to a function to call in tRPC endpoint and SSR. */
const getUserAdminTeams = async ({
userId,
getUserInfo,
getParentInfo,
includeCredentials = false,
}: {
userId: number;
getUserInfo?: boolean;
getParentInfo?: boolean;
includeCredentials?: boolean;
}): Promise<UserAdminTeams> => {
const teams = await prisma.team.findMany({
where: {
members: {
some: {
userId: userId,
accepted: true,
role: { in: [MembershipRole.ADMIN, MembershipRole.OWNER] },
},
},
},
select: {
id: true,
name: true,
logoUrl: true,
...(includeCredentials && { credentials: true }),
...(getParentInfo && {
parent: {
select: {
id: true,
name: true,
logoUrl: true,
credentials: true,
},
},
}),
},
// FIXME - OrgNewSchema: Fix this orderBy
// orderBy: {
// orgUsers: { _count: "desc" },
// },
});
if (teams.length && getUserInfo) {
const user = await prisma.user.findUnique({
where: {
id: userId,
},
select: {
id: true,
name: true,
avatarUrl: true,
...(includeCredentials && { credentials: true }),
},
});
if (user) {
const userObject = {
id: user.id,
name: user.name || "me",
logoUrl: user?.avatarUrl, // bit ugly, no?
isUser: true,
credentials: includeCredentials ? user.credentials : [],
parent: null,
};
teams.unshift(userObject);
}
}
return teams;
};
export default getUserAdminTeams;