import { ORPCError } from "@orpc/server"; import type { Actor, ForgetUserFacts, UpdateUserProfile, UserProfileState, } from "@rakazo/contracts"; import type { PrismaClient } from "@rakazo/db"; import { forgetUserFacts, getUserProfileState, InvalidUserFactError, UserFactNotFoundError, updateUserProfile, } from "@rakazo/db"; function toRpcError(error: unknown): unknown { if (error instanceof UserFactNotFoundError) { return new ORPCError("NOT_FOUND", { message: error.message }); } if (error instanceof InvalidUserFactError) { return new ORPCError("BAD_REQUEST", { message: error.message }); } return error; } export function getProfile(prisma: PrismaClient, actor: Actor): Promise { return getUserProfileState(prisma, actor.userId); } export async function updateProfile( prisma: PrismaClient, actor: Actor, input: UpdateUserProfile, ): Promise { try { await updateUserProfile(prisma, actor.userId, input); } catch (error) { throw toRpcError(error); } return getUserProfileState(prisma, actor.userId); } /** Unknown ids are ignored, so a caller cannot probe another user's facts. */ export async function forgetProfile( prisma: PrismaClient, actor: Actor, input: ForgetUserFacts, ): Promise { await forgetUserFacts( prisma, actor.userId, input.factIds ? { factIds: input.factIds } : input.category ? { category: input.category } : { all: true }, ); return getUserProfileState(prisma, actor.userId); }