-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathuser.ts
More file actions
75 lines (68 loc) · 1.97 KB
/
Copy pathuser.ts
File metadata and controls
75 lines (68 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { pick } from "es-toolkit";
import type {
NotificationSettings,
UserPreferences,
UserRole,
} from "@shared/types";
import { NotificationEventType, UserPreference } from "@shared/types";
import env from "@server/env";
import type { User } from "@server/models";
type Options = {
includeDetails?: boolean;
includeEmail?: boolean;
};
type UserPresentation = {
id: string;
name: string;
avatarUrl: string | null | undefined;
createdAt: Date;
updatedAt: Date;
deletedAt: Date | null;
lastActiveAt: Date | null;
color: string;
role: UserRole;
isSuspended: boolean;
email?: string | null;
language?: string;
preferences?: UserPreferences | null;
notificationSettings?: NotificationSettings;
timezone?: string | null;
invitedBy?: UserPresentation;
};
export default function presentUser(
user: User,
options: Options = {}
): UserPresentation {
const userData: UserPresentation = {
id: user.id,
name: user.name,
avatarUrl: user.avatarUrl,
color: user.color,
role: user.role,
isSuspended: user.isSuspended,
createdAt: user.createdAt,
updatedAt: user.updatedAt,
deletedAt: user.deletedAt,
lastActiveAt: user.lastActiveAt,
timezone: user.timezone,
};
if (options.includeDetails) {
userData.email = user.email;
userData.language = user.language || env.DEFAULT_LANGUAGE;
// Unrecognized keys are omitted so that clients can safely send the object back.
userData.preferences = user.preferences
? pick(user.preferences, Object.values(UserPreference))
: user.preferences;
userData.notificationSettings = user.notificationSettings
? pick(user.notificationSettings, Object.values(NotificationEventType))
: user.notificationSettings;
}
if (options.includeEmail) {
userData.email = user.email;
}
// Only included when the association has been eager-loaded by the caller.
if (user.invitedBy) {
userData.invitedBy = presentUser(user.invitedBy);
}
return userData;
}