Files
var-monorepo/packages/database/prisma/json/User.ts
2026-01-30 16:19:00 +01:00

80 lines
1.9 KiB
TypeScript

import { User } from "../../generated/client";
// USer History
interface UserDeletedEvent {
type: "USER_DELETED";
reason: string;
date: string;
by: string;
}
interface UserProfileUpdatedEvent {
type: "USER_PROFILE_UPDATED";
changes: {
field: string;
oldValue: string;
newValue: string;
};
date: string;
by: string;
}
export type UserHistoryEvent = UserDeletedEvent | UserProfileUpdatedEvent;
export interface PublicUser {
firstname: string;
lastname: string;
publicId: string;
badges: string[];
fullName: string;
}
export const DISCORD_ROLES = {
ONLINE_DISPATCHER: "1287399540390891571",
ONLINE_PILOT: "1287399540390891571",
DISPATCHER: "1081247459994501222",
PILOT: "1081247405304975390",
};
export const getPublicUser = (
user: User,
options = {
ignorePrivacy: false,
fullLastName: false,
},
): PublicUser => {
const lastName = user.lastname
.trim()
.split(" ")
.map((part) => `${part[0] || ""}.`)
.join(" ");
if (options.fullLastName) {
return {
firstname: user.firstname,
lastname: user.settingsHideLastname && !options.ignorePrivacy ? "" : user.lastname,
fullName:
`${user.firstname} ${user.settingsHideLastname && !options.ignorePrivacy ? "" : user.lastname}`.trim(),
publicId: user.publicId,
badges: user.badges,
};
}
return {
firstname: user.firstname,
lastname: user.settingsHideLastname && !options.ignorePrivacy ? "" : lastName.trim(), // Only take the first letter of each section of the last name
fullName:
`${user.firstname} ${user.settingsHideLastname && !options.ignorePrivacy ? "" : lastName}`.trim(),
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;
};