flowstate/services/user.service.ts
Chneemann 3db1a18086
All checks were successful
Deploy Flowstate to VPS / deploy (push) Successful in 43s
refactor(member): update member status stat to dynamically display capitalized user role
2026-08-21 04:09:03 +02:00

60 lines
1.9 KiB
TypeScript

/**
* @file services/user.service.ts
* @description Business logic service handling user-related database queries.
*/
import { db } from "@/db";
import { usersTable } from "@/db/schema";
import { eq, sql } from "drizzle-orm";
/**
* Service class for handling user operations and database interactions.
*/
export class UserService {
/**
* Retrieves specific profile information for a user by ID.
*
* @async
* @param {string} userId - The unique identifier of the user.
* @returns {Promise<{ id: string; firstName: string; lastName: string; email: string; role: string; color: string; isOnline: boolean; lastLogin: Date } | null>} The user profile data or null if not found.
*/
static async findProfileById(userId: string) {
const [user] = await db
.select({
id: usersTable.id,
firstName: usersTable.firstName,
lastName: usersTable.lastName,
email: usersTable.email,
role: usersTable.role,
color: usersTable.color,
isOnline: usersTable.isOnline,
lastLogin: usersTable.lastLogin,
})
.from(usersTable)
.where(eq(usersTable.id, userId));
return user || null;
}
/**
* Retrieves a list of all users sorted by their last login date in descending order, placing null values last.
*
* @async
* @returns {Promise<Array<{ id: string; firstName: string; lastName: string; email: string; role: string; color: string; lastLogin: Date; isOnline: boolean }>>} An array of user list items.
*/
static async findAllUsers() {
return await db
.select({
id: usersTable.id,
firstName: usersTable.firstName,
lastName: usersTable.lastName,
email: usersTable.email,
role: usersTable.role,
color: usersTable.color,
lastLogin: usersTable.lastLogin,
isOnline: usersTable.isOnline,
})
.from(usersTable)
.orderBy(sql`${usersTable.lastLogin} DESC NULLS LAST`);
}
}