New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: new-app-install-flow follow up #14699
base: main
Are you sure you want to change the base?
Changes from 1 commit
2585d62
550a1fe
ca0edf1
a4d20e7
905bc23
4e4a8b6
4fba8af
d5b34b8
81347d9
935745f
7a401ee
c01ca48
c7b3bc8
8703354
f220f78
e4cac4a
a687aa9
7e38f86
c1be5a1
f443494
b117710
5dbecb7
9b3fd28
25d4709
dca6096
718e1f7
ab8ad87
a6a97fa
f6eec4e
1bfd59f
7ae9535
b1302b0
b19b27b
b7cd45b
1f7ba48
82c50d6
c811f4d
fdacdbe
433417c
6b59c2c
cde8190
ba6fdfe
c5023cb
0621d29
c349d89
abafcab
fed2cc3
1f0efa3
19c56f8
7194ce5
fad6c70
6364193
1711e49
9b0f360
444c1a3
738dc99
9a5db48
b9362c1
71d579b
3149aca
a422be6
5ea558e
200ff6c
437aefd
9b2e095
e8c5aac
d3a6a30
cef57d3
db41b2e
631860f
3de4df3
1ae0982
74e9f43
fc20b5e
f9302cb
70096c8
60e04a5
912b0da
a28f093
bd6c70a
8891ab8
7377de7
da31d43
c1b798c
fc159bc
8fde02c
fc2f873
b0d82a6
22b0cd7
ebcdb08
ff25dcd
7693077
eda8ba0
1660aa1
ce64678
19c221b
6e5f13b
a54dce1
919bf3c
398e78d
fc6954d
e8c86fe
43893f4
5ce1e00
9ec06c6
8d04e00
af6060d
966084f
bfddf73
897a98a
7dcb655
ebf015a
18e419f
ec40c3c
ef2635b
29e7db5
4eafb0b
7372e6e
32051f3
6099451
e984dad
ce267a8
83cc298
204caed
40553f8
73e90f9
459aa43
ca90605
88c744d
7cb178b
9d62d04
7f6c12d
89fd6e2
04c22d8
b157944
a495e86
c1fa3e7
910c1e8
2b8c7ed
16aed74
534584b
b221748
386d55e
8760f58
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,29 @@ | ||
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 })[]; | ||
export type UserAdminTeams = number[]; | ||
|
||
/** 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({ | ||
export const getUserAdminTeams = async (userId: number): Promise<number[]> => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of querying all teams and then looking weather this team has current user as admin/owner (which is slow because we have to look at all the teams in db) |
||
const user = await prisma.user.findFirst({ | ||
where: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. queried the user using userId got all his teams and then filter all the teams that have the current user as admin/owner |
||
members: { | ||
some: { | ||
userId: userId, | ||
id: userId, | ||
}, | ||
select: { | ||
teams: { | ||
where: { | ||
accepted: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. filter all the teams that have the current user as admin/owner |
||
role: { in: [MembershipRole.ADMIN, MembershipRole.OWNER] }, | ||
}, | ||
select: { id: true }, | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. only select the required fields |
||
}, | ||
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); | ||
} | ||
const teamIds = []; | ||
for (const team of user?.teams || []) { | ||
teamIds.push(team.id); | ||
} | ||
|
||
return teams; | ||
return teamIds; | ||
}; | ||
|
||
export default getUserAdminTeams; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would argue the aim of this PR should be to get rid of this function. The new app install flow handles this in better way. I see that this function is only used for apps so the rest of the web app doesn't rely on it.
Anything that relied on this should be handled in the new app install flow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we can remove it completly
we need this function for -
fetching the credentials for an app for the current logged in user and all his teams LINK, for this we need the list of all the teams the user is admin/owner of
this credentials are used to check weather this app has been added or not (the green badge that says 2 installed)
weather to show the install button or not here for instance i have installed fathom on both my personal acc and the only team i have so we dont show the install button
also getUserAdminTeams is also called in
throwIfNotHaveAdminAccessToTeam
which is a check that prevents non-admin/owner to install an app to a teamso we do need the list of teams the logged in user is admin/owner of
plus now that we are querying it using the primary key, it shouldnt be that bad!!