2
0
Files
cal/calcom/packages/lib/getAvatarUrl.ts
2024-08-09 00:39:27 +02:00

22 lines
689 B
TypeScript

import { z } from "zod";
import { AVATAR_FALLBACK, CAL_URL } from "@calcom/lib/constants";
import type { User } from "@calcom/prisma/client";
/**
* Gives an organization aware avatar url for a user
* It ensures that the wrong avatar isn't fetched by ensuring that organizationId is always passed
* It should always return a fully formed url
*/
export const getUserAvatarUrl = (user: Pick<User, "avatarUrl"> | undefined) => {
if (user?.avatarUrl) {
const isAbsoluteUrl = z.string().url().safeParse(user.avatarUrl).success;
if (isAbsoluteUrl) {
return user.avatarUrl;
} else {
return CAL_URL + user.avatarUrl;
}
}
return CAL_URL + AVATAR_FALLBACK;
};