-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.ts
More file actions
53 lines (41 loc) · 1.4 KB
/
user.ts
File metadata and controls
53 lines (41 loc) · 1.4 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
import type { Prisma, User } from "@prisma/client";
import { type PostsCount } from "./post";
export const userDataSelect = {
id: true,
name: true,
username: true,
avatarUrl: true
} satisfies Prisma.UserSelect;
export type UserData = Prisma.UserGetPayload<{ select: typeof userDataSelect }>;
export const getFollowersInfo = (loggedInUserId: string) => {
return {
followers: { where: { followingId: loggedInUserId }, select: { id: true } },
_count: { select: { followers: true } }
} satisfies Prisma.UserSelect;
};
export const getUserDataWithFollowesInfo = (loggedInUserId: string) => {
return { ...userDataSelect, ...getFollowersInfo(loggedInUserId) } satisfies Prisma.UserSelect;
};
export type UserDataWithFollowInfoPayload = Prisma.UserGetPayload<{
select: ReturnType<typeof getUserDataWithFollowesInfo>;
}>;
export interface FollowerInfo {
followers: number;
isFollowedByUser: boolean;
}
export interface FollowingInfo {
following: number;
}
export type UserDataWithFollowInfo = UserData & FollowerInfo;
export type ProfilePageUser = User & FollowerInfo & FollowingInfo & PostsCount;
export interface UserPage {
users: UserDataWithFollowInfo[];
nextCursor: string | number | null;
}
export function formatUserData({ _count, followers, ...user }: UserDataWithFollowInfoPayload): UserDataWithFollowInfo {
return {
...user,
followers: _count.followers,
isFollowedByUser: !!followers.length
};
}