Bafian
Bafian
TTCTheo's Typesafe Cult
Created by Bafian on 11/19/2023 in #questions
is this a bad idea? Conditionally get User data depending on privacy field
it's a service used by trpc, the full select for a User is quite long with some nested types. Want to keep type consistency, just doesn't seem to optimal.
async getPublicUserById(uid: string): Promise<PublicUserType | PrivateUserType | null> {
try {
let user = await prismaClient.user.findFirstOrThrow({
where: { uid },
select: publicUserSelect,
});

if (user.profilePrivacy === ProfilePrivacyEnum.PRIVATE_PROFILE {
const privateUserData = await prismaClient.user.findFirstOrThrow({
where: { uid },
select: privateUserSelect,
});
return privateUserData as PrivateUserType;
}

return user as PublicUserType;
} catch (e) {
console.error(e);
return null;
}
}
async getPublicUserById(uid: string): Promise<PublicUserType | PrivateUserType | null> {
try {
let user = await prismaClient.user.findFirstOrThrow({
where: { uid },
select: publicUserSelect,
});

if (user.profilePrivacy === ProfilePrivacyEnum.PRIVATE_PROFILE {
const privateUserData = await prismaClient.user.findFirstOrThrow({
where: { uid },
select: privateUserSelect,
});
return privateUserData as PrivateUserType;
}

return user as PublicUserType;
} catch (e) {
console.error(e);
return null;
}
}
3 replies