File size: 6,886 Bytes
1ba9d5e
 
 
 
 
 
 
957ee72
229d06d
1ba9d5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229d06d
 
1ba9d5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229d06d
 
1ba9d5e
 
 
 
 
 
 
 
e0cec61
 
 
 
 
1ba9d5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
 * User Queries - Drizzle ORM
 * 
 * All user-related database operations.
 */

import { db } from "@/db";
import { users, profiles, profileSkills, profileMentoringTopics, profileConnectedRepos, userRepositories } from "@/db/schema";
import { eq, and, like, desc, sql } from "drizzle-orm";
import { v4 as uuidv4 } from "uuid";

// =============================================================================
// User CRUD
// =============================================================================

export async function getUserById(id: string) {
    const result = await db.select().from(users).where(eq(users.id, id)).limit(1);
    return result[0] || null;
}

export async function getUserByGithubId(githubId: number) {
    const result = await db.select().from(users).where(eq(users.githubId, githubId)).limit(1);
    return result[0] || null;
}

export async function getUserByUsername(username: string) {
    // Case-insensitive lookup - GitHub usernames are case-insensitive
    const result = await db.select().from(users).where(sql`LOWER(${users.username}) = LOWER(${username})`).limit(1);
    return result[0] || null;
}

export async function createUser(data: {
    githubId: number;
    username: string;
    avatarUrl: string;
    role?: string;
    githubAccessToken?: string;
}) {
    const now = new Date().toISOString();
    const id = uuidv4();

    await db.insert(users).values({
        id,
        githubId: data.githubId,
        username: data.username,
        avatarUrl: data.avatarUrl,
        role: data.role || null,
        githubAccessToken: data.githubAccessToken || null,
        createdAt: now,
        updatedAt: now,
    });

    return { id, ...data, createdAt: now, updatedAt: now };
}

export async function updateUser(id: string, data: Partial<{
    username: string;
    avatarUrl: string;
    role: string;
    githubAccessToken: string;
}>) {
    await db.update(users)
        .set({ ...data, updatedAt: new Date().toISOString() })
        .where(eq(users.id, id));
}

export async function updateUserRole(id: string, role: string) {
    await db.update(users)
        .set({ role, updatedAt: new Date().toISOString() })
        .where(eq(users.id, id));
}

// =============================================================================
// Profile Operations
// =============================================================================

export async function getProfile(userId: string) {
    const profile = await db.select().from(profiles).where(eq(profiles.userId, userId)).limit(1);
    if (!profile[0]) return null;

    const skills = await db.select().from(profileSkills).where(eq(profileSkills.profileId, userId));
    const topics = await db.select().from(profileMentoringTopics).where(eq(profileMentoringTopics.profileId, userId));
    const repos = await db.select().from(profileConnectedRepos).where(eq(profileConnectedRepos.profileId, userId));

    return {
        ...profile[0],
        skills: skills.map(s => s.skill),
        mentoringTopics: topics.map(t => t.topic),
        connectedRepos: repos.map(r => r.repoName),
    };
}

export async function getProfileByUsername(username: string) {
    // Case-insensitive lookup - GitHub usernames are case-insensitive
    const profile = await db.select().from(profiles).where(sql`LOWER(${profiles.username}) = LOWER(${username})`).limit(1);
    if (!profile[0]) return null;

    const userId = profile[0].userId;
    const skills = await db.select().from(profileSkills).where(eq(profileSkills.profileId, userId));
    const topics = await db.select().from(profileMentoringTopics).where(eq(profileMentoringTopics.profileId, userId));

    return {
        ...profile[0],
        // Include snake_case aliases for frontend compatibility
        available_for_mentoring: profile[0].availableForMentoring,
        avatar_url: profile[0].avatarUrl,
        profile_visibility: profile[0].profileVisibility,
        show_email: profile[0].showEmail,
        skills: skills.map(s => s.skill),
        mentoringTopics: topics.map(t => t.topic),
    };
}

export async function createOrUpdateProfile(userId: string, data: {
    username: string;
    avatarUrl?: string;
    bio?: string;
    location?: string;
    website?: string;
    twitter?: string;
    availableForMentoring?: boolean;
    profileVisibility?: string;
    showEmail?: boolean;
    skills?: string[];
    mentoringTopics?: string[];
}) {
    const now = new Date().toISOString();

    // Upsert profile
    await db.insert(profiles).values({
        userId,
        username: data.username,
        avatarUrl: data.avatarUrl || null,
        bio: data.bio || null,
        location: data.location || null,
        website: data.website || null,
        twitter: data.twitter || null,
        availableForMentoring: data.availableForMentoring || false,
        profileVisibility: data.profileVisibility || "public",
        showEmail: data.showEmail || false,
        createdAt: now,
        updatedAt: now,
    }).onConflictDoUpdate({
        target: profiles.userId,
        set: {
            bio: data.bio,
            location: data.location,
            website: data.website,
            twitter: data.twitter,
            availableForMentoring: data.availableForMentoring,
            profileVisibility: data.profileVisibility,
            showEmail: data.showEmail,
            updatedAt: now,
        }
    });

    // Update skills
    if (data.skills) {
        await db.delete(profileSkills).where(eq(profileSkills.profileId, userId));
        for (const skill of data.skills) {
            await db.insert(profileSkills).values({ profileId: userId, skill });
        }
    }

    // Update topics
    if (data.mentoringTopics) {
        await db.delete(profileMentoringTopics).where(eq(profileMentoringTopics.profileId, userId));
        for (const topic of data.mentoringTopics) {
            await db.insert(profileMentoringTopics).values({ profileId: userId, topic });
        }
    }

    return getProfile(userId);
}

// =============================================================================
// User Stats
// =============================================================================

export async function getUserRepositories(userId: string) {
    return db.select().from(userRepositories).where(eq(userRepositories.userId, userId));
}

export async function addUserRepository(userId: string, repoFullName: string) {
    await db.insert(userRepositories).values({
        id: uuidv4(),
        userId,
        repoFullName,
        addedAt: new Date().toISOString(),
    }).onConflictDoNothing();
}

export async function removeUserRepository(userId: string, repoFullName: string) {
    await db.delete(userRepositories).where(
        and(
            eq(userRepositories.userId, userId),
            eq(userRepositories.repoFullName, repoFullName)
        )
    );
}