feat: implement complex selection flow
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
import { getUserProfile } from '@/modules/user/services/user.service';
|
||||
import type { AppContext } from '@/types/hono';
|
||||
import { getCookie } from 'hono/cookie';
|
||||
|
||||
export function getUserProfileHandler(c: AppContext) {
|
||||
const SELECTED_COMPLEX_COOKIE = 'selected-complex-id';
|
||||
|
||||
export async function getUserProfileHandler(c: AppContext) {
|
||||
const user = c.get('user');
|
||||
const profile = getUserProfile(user);
|
||||
const complexId = getCookie(c, SELECTED_COMPLEX_COOKIE);
|
||||
const profile = await getUserProfile(user, complexId);
|
||||
return c.json(profile);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import type { User } from '@/lib/auth';
|
||||
import { db } from '@/lib/prisma';
|
||||
import type { UserProfile } from '@repo/api-contract';
|
||||
|
||||
function getGravatarUrl(email: string): string {
|
||||
@@ -7,12 +8,29 @@ function getGravatarUrl(email: string): string {
|
||||
return `https://www.gravatar.com/avatar/${hash}?d=identicon`;
|
||||
}
|
||||
|
||||
export function getUserProfile(user: User): UserProfile {
|
||||
export async function getUserProfile(user: User, complexId?: string): Promise<UserProfile> {
|
||||
let role = 'member';
|
||||
|
||||
if (complexId) {
|
||||
const complexUser = await db.complexUser.findUnique({
|
||||
where: {
|
||||
complexId_userId: {
|
||||
complexId,
|
||||
userId: user.id,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (complexUser) {
|
||||
role = complexUser.role.toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
id: user.id,
|
||||
fullName: user.name,
|
||||
email: user.email,
|
||||
role: (user as any).role || 'member',
|
||||
role,
|
||||
avatarUrl: user.image || getGravatarUrl(user.email),
|
||||
createdAt: user.createdAt.toISOString(),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user