Merge pull request #237 from johndoe6345789/codex/create-user-operations-in-core/entities

Refactor user operations into separate modules
This commit is contained in:
2025-12-27 18:39:07 +00:00
committed by GitHub
7 changed files with 91 additions and 58 deletions

View File

@@ -1,2 +1,11 @@
export { createUserOperations } from './user'
export type { UserOperations } from './user'
export { createUser } from './user/create'
export { deleteUser } from './user/delete'
export { updateUser } from './user/update'
export {
assertValidUserCreate,
assertValidUserId,
assertValidUserUpdate,
} from './user/validation'

View File

@@ -0,0 +1,20 @@
import type { DBALAdapter } from '../../../../adapters/adapter'
import { DBALError } from '../../../../foundation/errors'
import type { User } from '../../../../foundation/types'
import { assertValidUserCreate } from './validation'
export const createUser = async (
adapter: DBALAdapter,
data: Omit<User, 'id' | 'createdAt' | 'updatedAt'>,
): Promise<User> => {
assertValidUserCreate(data)
try {
return adapter.create('User', data) as Promise<User>
} catch (error) {
if (error instanceof DBALError && error.code === 409) {
throw DBALError.conflict('User with username or email already exists')
}
throw error
}
}

View File

@@ -0,0 +1,13 @@
import type { DBALAdapter } from '../../../../adapters/adapter'
import { DBALError } from '../../../../foundation/errors'
import { assertValidUserId } from './validation'
export const deleteUser = async (adapter: DBALAdapter, id: string): Promise<boolean> => {
assertValidUserId(id)
const result = await adapter.delete('User', id)
if (!result) {
throw DBALError.notFound(`User not found: ${id}`)
}
return result
}

View File

@@ -1,6 +1,8 @@
import type { DBALAdapter } from '../../../../adapters/adapter'
import type { User, ListOptions, ListResult } from '../../../../foundation/types'
import { createUser, deleteUser, updateUser } from './mutations'
import { createUser } from './create'
import { deleteUser } from './delete'
import { updateUser } from './update'
import { createManyUsers, deleteManyUsers, updateManyUsers } from './batch'
import { listUsers, readUser } from './reads'

View File

@@ -1,57 +0,0 @@
import type { DBALAdapter } from '../../../../adapters/adapter'
import type { User } from '../../../../foundation/types'
import { DBALError } from '../../../../foundation/errors'
import { validateUserCreate, validateUserUpdate, validateId } from '../../../../foundation/validation'
export const createUser = async (
adapter: DBALAdapter,
data: Omit<User, 'id' | 'createdAt' | 'updatedAt'>,
): Promise<User> => {
const validationErrors = validateUserCreate(data)
if (validationErrors.length > 0) {
throw DBALError.validationError('Invalid user data', validationErrors.map(error => ({ field: 'user', error })))
}
try {
return adapter.create('User', data) as Promise<User>
} catch (error) {
if (error instanceof DBALError && error.code === 409) {
throw DBALError.conflict('User with username or email already exists')
}
throw error
}
}
export const updateUser = async (adapter: DBALAdapter, id: string, data: Partial<User>): Promise<User> => {
const idErrors = validateId(id)
if (idErrors.length > 0) {
throw DBALError.validationError('Invalid user ID', idErrors.map(error => ({ field: 'id', error })))
}
const validationErrors = validateUserUpdate(data)
if (validationErrors.length > 0) {
throw DBALError.validationError('Invalid user update data', validationErrors.map(error => ({ field: 'user', error })))
}
try {
return adapter.update('User', id, data) as Promise<User>
} catch (error) {
if (error instanceof DBALError && error.code === 409) {
throw DBALError.conflict('Username or email already exists')
}
throw error
}
}
export const deleteUser = async (adapter: DBALAdapter, id: string): Promise<boolean> => {
const validationErrors = validateId(id)
if (validationErrors.length > 0) {
throw DBALError.validationError('Invalid user ID', validationErrors.map(error => ({ field: 'id', error })))
}
const result = await adapter.delete('User', id)
if (!result) {
throw DBALError.notFound(`User not found: ${id}`)
}
return result
}

View File

@@ -0,0 +1,22 @@
import type { DBALAdapter } from '../../../../adapters/adapter'
import { DBALError } from '../../../../foundation/errors'
import type { User } from '../../../../foundation/types'
import { assertValidUserId, assertValidUserUpdate } from './validation'
export const updateUser = async (
adapter: DBALAdapter,
id: string,
data: Partial<User>,
): Promise<User> => {
assertValidUserId(id)
assertValidUserUpdate(data)
try {
return adapter.update('User', id, data) as Promise<User>
} catch (error) {
if (error instanceof DBALError && error.code === 409) {
throw DBALError.conflict('Username or email already exists')
}
throw error
}
}

View File

@@ -0,0 +1,24 @@
import { DBALError } from '../../../../foundation/errors'
import type { User } from '../../../../foundation/types'
import { validateId, validateUserCreate, validateUserUpdate } from '../../../../foundation/validation'
export const assertValidUserId = (id: string): void => {
const validationErrors = validateId(id)
if (validationErrors.length > 0) {
throw DBALError.validationError('Invalid user ID', validationErrors.map(error => ({ field: 'id', error })))
}
}
export const assertValidUserCreate = (data: Omit<User, 'id' | 'createdAt' | 'updatedAt'>): void => {
const validationErrors = validateUserCreate(data)
if (validationErrors.length > 0) {
throw DBALError.validationError('Invalid user data', validationErrors.map(error => ({ field: 'user', error })))
}
}
export const assertValidUserUpdate = (data: Partial<User>): void => {
const validationErrors = validateUserUpdate(data)
if (validationErrors.length > 0) {
throw DBALError.validationError('Invalid user update data', validationErrors.map(error => ({ field: 'user', error })))
}
}