-
Notifications
You must be signed in to change notification settings - Fork 8.4k
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
refactor: loadUsers and move to separate file #15532
Changes from all commits
9ce191c
e9187e0
60cb11a
1c53606
dc533a8
9a110c5
0cf4d48
0bf8401
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 | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,87 @@ | ||||||
import { Prisma } from "@prisma/client"; | ||||||
import type { IncomingMessage } from "http"; | ||||||
|
||||||
import { orgDomainConfig } from "@calcom/features/ee/organizations/lib/orgDomains"; | ||||||
import { HttpError } from "@calcom/lib/http-error"; | ||||||
import logger from "@calcom/lib/logger"; | ||||||
import { UserRepository } from "@calcom/lib/server/repository/user"; | ||||||
import prisma, { userSelect } from "@calcom/prisma"; | ||||||
import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential"; | ||||||
|
||||||
import type { NewBookingEventType } from "../handleNewBooking"; | ||||||
|
||||||
const log = logger.getSubLogger({ prefix: ["[loadUsers]:handleNewBooking "] }); | ||||||
|
||||||
type EventType = Pick<NewBookingEventType, "hosts" | "users" | "id">; | ||||||
|
||||||
export const loadUsers = async (eventType: EventType, dynamicUserList: string[], req: IncomingMessage) => { | ||||||
try { | ||||||
const { currentOrgDomain } = orgDomainConfig(req); | ||||||
|
||||||
return eventType.id | ||||||
? await loadUsersByEventType(eventType) | ||||||
: await loadDynamicUsers(dynamicUserList, currentOrgDomain); | ||||||
} catch (error) { | ||||||
if (error instanceof HttpError || error instanceof Prisma.PrismaClientKnownRequestError) { | ||||||
throw new HttpError({ statusCode: 400, message: error.message }); | ||||||
} | ||||||
throw new HttpError({ statusCode: 500, message: "Unable to load users" }); | ||||||
} | ||||||
}; | ||||||
|
||||||
const loadUsersByEventType = async (eventType: EventType): Promise<NewBookingEventType["users"]> => { | ||||||
const hosts = eventType.hosts || []; | ||||||
const users = hosts.map(({ user, isFixed, priority }) => ({ | ||||||
...user, | ||||||
isFixed, | ||||||
priority, | ||||||
})); | ||||||
return users.length ? users : eventType.users; | ||||||
}; | ||||||
|
||||||
const loadDynamicUsers = async (dynamicUserList: string[], currentOrgDomain: string | null) => { | ||||||
if (!Array.isArray(dynamicUserList) || dynamicUserList.length === 0) { | ||||||
throw new Error("dynamicUserList is not properly defined or empty."); | ||||||
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.
Suggested change
|
||||||
} | ||||||
return findUsersByUsername({ | ||||||
usernameList: dynamicUserList, | ||||||
orgSlug: !!currentOrgDomain ? currentOrgDomain : null, | ||||||
}); | ||||||
}; | ||||||
|
||||||
/** | ||||||
* This method is mostly same as the one in UserRepository but it includes a lot more relations which are specific requirement here | ||||||
* TODO: Figure out how to keep it in UserRepository and use it here | ||||||
*/ | ||||||
export const findUsersByUsername = async ({ | ||||||
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. no change in the code here. 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. It looks like this logic is already in the 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. this function returns few more fields(credentials and metadata) than the static function in UserRepository. |
||||||
usernameList, | ||||||
orgSlug, | ||||||
}: { | ||||||
orgSlug: string | null; | ||||||
usernameList: string[]; | ||||||
}) => { | ||||||
log.debug("findUsersByUsername", { usernameList, orgSlug }); | ||||||
const { where, profiles } = await UserRepository._getWhereClauseForFindingUsersByUsername({ | ||||||
orgSlug, | ||||||
usernameList, | ||||||
}); | ||||||
return ( | ||||||
await prisma.user.findMany({ | ||||||
where, | ||||||
select: { | ||||||
...userSelect.select, | ||||||
credentials: { | ||||||
select: credentialForCalendarServiceSelect, | ||||||
}, | ||||||
metadata: true, | ||||||
}, | ||||||
}) | ||||||
).map((user) => { | ||||||
const profile = profiles?.find((profile) => profile.user.id === user.id) ?? null; | ||||||
return { | ||||||
...user, | ||||||
organizationId: profile?.organizationId ?? null, | ||||||
profile, | ||||||
}; | ||||||
}); | ||||||
}; |
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.