code: user,get,dbal (1 files)

This commit is contained in:
2025-12-26 01:30:35 +00:00
parent 9ff5a8b5e7
commit f3d95d3dbf

View File

@@ -2,53 +2,55 @@
* @file get-user.ts
* @description Get user operations
*/
import type { User, Result } from '../types';
import type { InMemoryStore } from '../store/in-memory-store';
import type { Result, User } from '../../types'
import type { InMemoryStore } from '../../store/in-memory-store'
import { validateId } from '../../validation/validate-id'
/**
* Get a user by ID
*/
export async function getUser(store: InMemoryStore, id: string): Promise<Result<User>> {
if (!id) {
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'ID required' } };
export const getUser = async (store: InMemoryStore, id: string): Promise<Result<User>> => {
const idErrors = validateId(id)
if (idErrors.length > 0) {
return { success: false, error: { code: 'VALIDATION_ERROR', message: idErrors[0] } }
}
const user = store.users.get(id);
const user = store.users.get(id)
if (!user) {
return { success: false, error: { code: 'NOT_FOUND', message: `User not found: ${id}` } };
return { success: false, error: { code: 'NOT_FOUND', message: `User not found: ${id}` } }
}
return { success: true, data: user };
return { success: true, data: user }
}
/**
* Get a user by email
*/
export async function getUserByEmail(store: InMemoryStore, email: string): Promise<Result<User>> {
export const getUserByEmail = async (store: InMemoryStore, email: string): Promise<Result<User>> => {
if (!email) {
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'Email required' } };
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'Email is required' } }
}
const id = store.userEmails.get(email);
const id = store.usersByEmail.get(email)
if (!id) {
return { success: false, error: { code: 'NOT_FOUND', message: `User not found: ${email}` } };
return { success: false, error: { code: 'NOT_FOUND', message: `User not found: ${email}` } }
}
return getUser(store, id);
return getUser(store, id)
}
/**
* Get a user by username
*/
export async function getUserByUsername(store: InMemoryStore, username: string): Promise<Result<User>> {
export const getUserByUsername = async (store: InMemoryStore, username: string): Promise<Result<User>> => {
if (!username) {
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'Username required' } };
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'Username is required' } }
}
const id = store.userUsernames.get(username);
const id = store.usersByUsername.get(username)
if (!id) {
return { success: false, error: { code: 'NOT_FOUND', message: `User not found: ${username}` } };
return { success: false, error: { code: 'NOT_FOUND', message: `User not found: ${username}` } }
}
return getUser(store, id);
return getUser(store, id)
}