Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
2 replies
Bafian

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;
  }
}
Was this page helpful?