41 lines
940 B
TypeScript
41 lines
940 B
TypeScript
import { User } from "../../generated/client";
|
|
|
|
export interface PublicUser {
|
|
firstname: string;
|
|
lastname: string;
|
|
publicId: string;
|
|
badges: string[];
|
|
fullName: string;
|
|
}
|
|
|
|
export const getPublicUser = (
|
|
user: User,
|
|
options = {
|
|
ignorePrivacy: false,
|
|
},
|
|
): PublicUser => {
|
|
return {
|
|
firstname: user.firstname,
|
|
lastname:
|
|
user.settingsHideLastname && !options.ignorePrivacy
|
|
? ""
|
|
: user.lastname
|
|
.split(" ")
|
|
.map((part) => `${part[0]}.`)
|
|
.join(" "), // Only take the first part of the name
|
|
fullName: `${user.firstname} ${user.lastname
|
|
.split(" ")
|
|
.map((part) => `${part[0]}.`)
|
|
.join(" ")}`,
|
|
publicId: user.publicId,
|
|
badges: user.badges,
|
|
};
|
|
};
|
|
|
|
export const asPublicUser = (publicUSerJson: unknown): PublicUser => {
|
|
if (typeof publicUSerJson !== "object" || publicUSerJson === null) {
|
|
throw new Error("Invalid JSON format for PublicUser");
|
|
}
|
|
return publicUSerJson as PublicUser;
|
|
};
|