el.xwx.moe/lib/api/controllers/users/getUsers.ts

38 lines
839 B
TypeScript
Raw Normal View History

import { prisma } from "@/lib/api/db";
export default async function (
lookupEmail: string,
isSelf: boolean,
userEmail: string
) {
const user = await prisma.user.findUnique({
where: {
2023-05-18 13:02:17 -05:00
email: lookupEmail,
},
});
if (!user) return { response: "User not found.", status: 404 };
if (
!isSelf &&
user?.isPrivate &&
!user.whitelistedUsers.includes(userEmail)
) {
return { response: "This profile is private.", status: 401 };
}
2023-05-20 14:25:00 -05:00
const { password, ...unsensitiveInfo } = user;
2023-05-18 13:02:17 -05:00
const data = isSelf
2023-05-20 14:25:00 -05:00
? // If user is requesting its own data
unsensitiveInfo
2023-05-18 13:02:17 -05:00
: {
// If user is requesting someone elses data
2023-05-20 14:25:00 -05:00
id: unsensitiveInfo.id,
name: unsensitiveInfo.name,
email: unsensitiveInfo.email,
2023-05-18 13:02:17 -05:00
};
2023-05-20 14:25:00 -05:00
return { response: data || null, status: 200 };
}