courtmitra / apps /api /src /users /users.service.ts
Hetansh Waghela
feat: Full Clerk Google OAuth + user profiles + combined API/Worker deploy
f69e867
Raw
History Blame
1.65 kB
import { Injectable, Inject, NotFoundException } from "@nestjs/common";
import { eq, desc } from "drizzle-orm";
import { users, missions, type Database } from "@courtmitra/db";
import { DB } from "../db/db.module.js";
@Injectable()
export class UsersService {
constructor(@Inject(DB) private readonly db: Database) {}
async getProfile(userId: string) {
const rows = await this.db.select().from(users).where(eq(users.id, userId)).limit(1);
if (!rows[0]) throw new NotFoundException("User not found");
const u = rows[0];
return {
id: u.id,
displayName: u.displayName,
email: u.email,
avatarUrl: u.avatarUrl,
phone: u.phone,
fullName: u.fullName,
preferredLanguage: u.preferredLanguage,
};
}
async updateProfile(userId: string, data: { phone?: string; fullName?: string; preferredLanguage?: string }) {
const [updated] = await this.db
.update(users)
.set({
phone: data.phone,
fullName: data.fullName,
preferredLanguage: data.preferredLanguage,
updatedAt: new Date(),
})
.where(eq(users.id, userId))
.returning();
if (!updated) throw new NotFoundException("User not found");
return this.getProfile(userId);
}
async listUserMissions(userId: string) {
const rows = await this.db
.select()
.from(missions)
.where(eq(missions.userId, userId))
.orderBy(desc(missions.createdAt))
.limit(100);
return rows.map((m) => ({
id: m.id,
title: m.title,
state: m.state,
issueDomain: m.issueDomain,
createdAt: m.createdAt.toISOString(),
}));
}
}