/*
Get the *PUBLIC* profile of a user.
*/

import getPool from "@cocalc/database/pool";
import { Profile } from "./types";

export default async function getProfile(
  account_id: string,
  noCache: boolean = false
): Promise<Profile> {
  const pool = getPool(noCache ? undefined : "long");
  // Do not put anything private in this query!!!!
  const { rows } = await pool.query(
    "SELECT first_name, last_name, profile, name FROM accounts WHERE account_id=$1",
    [account_id]
  );
  if (rows.length == 0) {
    throw Error(`no account with id ${account_id}`);
  }
  return {
    account_id,
    first_name: rows[0].first_name ?? "Anonymous",
    last_name: rows[0].last_name ?? "User",
    image: rows[0].profile?.image,
    color: rows[0].profile?.color,
    name: rows[0].name,
  };
}
