-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.ts
164 lines (136 loc) · 3.51 KB
/
user.ts
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"use server"
import { db } from "@/lib/db";
import { User, Post, Coment } from '@prisma/client';
import { auth } from "../auth";
export type UserWithPostsAndComments = User & {
posts: (Post & {
coments: Coment[];
})[];
};
export const getUserByEmail = async (email : string) => {
try {
const user = db.user.findUnique({
where : {
email
}
})
return user;
}catch(error){
return null
}
}
export const getUserById = async (id : number) : Promise<UserWithPostsAndComments | null> => {
try {
const user: UserWithPostsAndComments | null = await db.user.findUnique({
where: {
id,
},
include: {
posts: {
include: {
coments: true, // Inclui os comentários dos posts
},
},
_count : {
select : {
following : true,
followers : true,
posts : true,
}
}
},
});
return user;
}catch(error){
return null
}
}
export const getUserByUsername = async (username : string) : Promise<UserWithPostsAndComments | null> => {
try {
const user: UserWithPostsAndComments | null = await db.user.findUnique({
where: {
username
},
include: {
posts: {
include: {
coments: true,
},
},
followers : true,
following : true,
_count : {
select : {
following : true,
followers : true,
posts : true,
}
}
},
});
return user;
}catch(error){
return null
}
}
export const getUsers = async (limit ?: number) => {
const session = await auth();
const authorId = session?.id;
const user = await getUserById(parseInt(authorId));
if (!user) {
throw new Error('Usuário não encontrado.');
}
const userId = user.id
try {
const recentsUsers = await db.user.findMany({
where: {
id: {
not: userId,
},
},
include : {
followers : true,
following : true,
},
take: limit ? limit : undefined,
orderBy: {
id: 'asc',
},
});
return recentsUsers;
}catch(error){
return null
}
}
export const getUsersNotFollowing = async (userId: number) => {
try {
// Busca todos os usuários que o usuário atual está seguindo
const followedUsers = await db.following.findMany({
where: {
followerId: Number(userId)
},
select: {
followingId: true // Pegamos o ID dos usuários seguidos
}
})
// Coletando os IDs dos usuários seguidos
const followedUserIds = followedUsers.map(follow => follow.followingId)
// Busca todos os usuários que não estão na lista de seguidos
const usersNotFollowing = await db.user.findMany({
where: {
id: {
not: Number(userId),
},
NOT: {
id: {
in: followedUserIds
}
}
}
})
return usersNotFollowing
} catch (error) {
console.error('Erro ao buscar usuários não seguidos:', error)
throw new Error('Erro ao buscar usuários não seguidos')
}
}