28 lines
1.0 KiB
TypeScript
28 lines
1.0 KiB
TypeScript
import { UpdateUser, User, UserResponse } from "@/src/entities/models/users/users.model"
|
|
import { IUsersRepository } from "../../repositories/users.repository.interface"
|
|
import { IInstrumentationService } from "../../services/instrumentation.service.interface"
|
|
import { NotFoundError } from "@/src/entities/errors/common"
|
|
|
|
export type IUpdateUserUseCase = ReturnType<typeof updateUserUseCase>
|
|
|
|
const updateUserUseCase = (
|
|
instrumentationService: IInstrumentationService,
|
|
usersRepository: IUsersRepository
|
|
) => async (id: string, input: UpdateUser): Promise<UserResponse> => {
|
|
return await instrumentationService.startSpan({ name: "updateUser Use Case", op: "function" },
|
|
async () => {
|
|
|
|
const existingUser = await usersRepository.getUserById(id)
|
|
|
|
if (!existingUser) {
|
|
throw new NotFoundError("User not found")
|
|
}
|
|
|
|
const updatedUser = await usersRepository.updateUser(id, input)
|
|
|
|
return {
|
|
...updatedUser
|
|
}
|
|
}
|
|
)
|
|
} |