-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch-profile.ts
38 lines (33 loc) · 960 Bytes
/
fetch-profile.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
import "server-only";
import { getUserId } from "@/features/auth/utils/get-session";
import type { ProfileSchema } from "@/features/profile/schemas/profile-schema";
import prisma from "@/prisma";
// everyone can access
export async function getProfile(username: string) {
return await prisma.users.findUniqueOrThrow({
where: { username },
select: { Profile: true },
});
}
// SELF
export async function getSelfProfile() {
const userId = await getUserId();
const profile = await prisma.profiles.findUnique({
where: { userId },
select: { name: true, bio: true, avatarUrl: true },
});
if (!profile) return undefined;
return {
name: profile.name,
bio: profile.bio ?? undefined,
avatarUrl: profile.avatarUrl ?? undefined,
};
}
export async function upsertSelfProfile(data: ProfileSchema) {
const userId = await getUserId();
return await prisma.profiles.upsert({
where: { userId },
update: data,
create: { userId, ...data },
});
}