* feat: optimize Prisma queries by replacing findFirst with findUnique where applicable - Replace findFirst/findFirstOrThrow with findUnique/findUniqueOrThrow for queries using unique constraints - Maintain existing functionality and error handling behavior - Focus on queries using primary keys and unique index fields from schema - Revert problematic changes that caused test failures to maintain stability Co-Authored-By: benny@cal.com <benny@cal.com> * revert: exclude API files from Prisma query optimizations per user request - Reverted all 55 API-related files to their original state - Kept all non-API Prisma query optimizations intact - API files include apps/api/v1, apps/api/v2, apps/web/app/api, and packages/app-store/*/api - Non-API optimizations remain for packages/lib, packages/features, apps/web (non-api), etc. Co-Authored-By: benny@cal.com <benny@cal.com> * feat: optimize membership query in attributeUtils to use findUnique with userId_teamId constraint Co-Authored-By: benny@cal.com <benny@cal.com> * revert: exclude test files from Prisma query optimizations per user request Co-Authored-By: benny@cal.com <benny@cal.com> * revert: revert attributeUtils.ts to use findFirst for test compatibility Co-Authored-By: benny@cal.com <benny@cal.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: benny@cal.com <benny@cal.com> Co-authored-by: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com>
102 lines
2.6 KiB
TypeScript
102 lines
2.6 KiB
TypeScript
import type { Prisma } from "@prisma/client";
|
|
import fetch from "node-fetch";
|
|
|
|
import { uploadAvatar } from "@calcom/lib/server/avatar";
|
|
import { resizeBase64Image } from "@calcom/lib/server/resizeBase64Image";
|
|
import prisma from "@calcom/prisma";
|
|
|
|
interface IPrefillAvatar {
|
|
email: string;
|
|
}
|
|
|
|
async function downloadImageDataFromUrl(url: string) {
|
|
try {
|
|
const response = await fetch(url);
|
|
|
|
if (!response.ok) {
|
|
console.log("Error fetching image from: ", url);
|
|
return null;
|
|
}
|
|
|
|
const imageBuffer = await response.buffer();
|
|
const base64Image = `data:image/png;base64,${imageBuffer.toString("base64")}`;
|
|
|
|
return base64Image;
|
|
} catch (error) {
|
|
console.log(error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export const prefillAvatar = async ({ email }: IPrefillAvatar) => {
|
|
const imageUrl = await getImageUrlAvatarAPI(email);
|
|
if (!imageUrl) return;
|
|
|
|
const base64Image = await downloadImageDataFromUrl(imageUrl);
|
|
if (!base64Image) return;
|
|
|
|
const avatar = await resizeBase64Image(base64Image);
|
|
const user = await prisma.user.findUnique({
|
|
where: { email: email },
|
|
});
|
|
|
|
if (!user) {
|
|
return;
|
|
}
|
|
const avatarUrl = await uploadAvatar({ userId: user.id, avatar });
|
|
|
|
const data: Prisma.UserUpdateInput = {};
|
|
data.avatarUrl = avatarUrl;
|
|
|
|
await prisma.user.update({
|
|
where: { email: email },
|
|
data,
|
|
});
|
|
};
|
|
|
|
const getImageUrlAvatarAPI = async (email: string) => {
|
|
if (!process.env.AVATARAPI_USERNAME || !process.env.AVATARAPI_PASSWORD) {
|
|
console.info("No avatar api credentials found");
|
|
return null;
|
|
}
|
|
|
|
const controller = new AbortController();
|
|
const timeout = setTimeout(() => controller.abort(), 10_000);
|
|
|
|
try {
|
|
const response = await fetch("https://avatarapi.com/v2/api.aspx", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "text/plain",
|
|
},
|
|
body: JSON.stringify({
|
|
username: process.env.AVATARAPI_USERNAME,
|
|
password: process.env.AVATARAPI_PASSWORD,
|
|
email,
|
|
}),
|
|
signal: controller.signal,
|
|
});
|
|
|
|
clearTimeout(timeout);
|
|
|
|
const info = await response.json();
|
|
|
|
if (!info.Success) {
|
|
if (info.Error === "Not found") {
|
|
// Expected case: no avatar for this email
|
|
return null;
|
|
}
|
|
console.warn("Avatar API error:", info.Error);
|
|
return null;
|
|
}
|
|
return info.Image as string;
|
|
} catch (error: unknown) {
|
|
if (error instanceof DOMException && error.name === "AbortError") {
|
|
console.warn("Avatar API request timed out");
|
|
} else {
|
|
console.error("Avatar API request failed:", error);
|
|
}
|
|
return null;
|
|
}
|
|
};
|