-
Notifications
You must be signed in to change notification settings - Fork 18
/
UserService.ts
82 lines (65 loc) · 2.77 KB
/
UserService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import type { RequestContext } from '@lokalise/fastify-extras'
import type { User } from '@prisma/client'
import type { Loader } from 'layered-loader'
import { EntityNotFoundError } from '../../../infrastructure/errors/publicErrors.js'
import type { UserRepository } from '../repositories/UserRepository.js'
import type {
CREATE_USER_BODY_SCHEMA_TYPE,
UPDATE_USER_BODY_SCHEMA_TYPE,
USER_SCHEMA_TYPE,
} from '../schemas/userSchemas.js'
import type { UsersInjectableDependencies } from '../userDiConfig.js'
export type UserDTO = USER_SCHEMA_TYPE
export type UserCreateDTO = CREATE_USER_BODY_SCHEMA_TYPE
export type UserUpdateDTO = UPDATE_USER_BODY_SCHEMA_TYPE
export class UserService {
private readonly userRepository: UserRepository
private readonly userLoader: Loader<User>
constructor({ userRepository, userLoader }: UsersInjectableDependencies) {
this.userRepository = userRepository
this.userLoader = userLoader
}
async createUser(user: UserCreateDTO) {
const newUser = await this.userRepository.createUser({
name: user.name ?? null,
age: user.age ?? null,
email: user.email,
})
await this.userLoader.invalidateCacheFor(newUser.id.toString())
return newUser
}
async getUser(requestContext: RequestContext, userId: string): Promise<User> {
const getUserResult =
this.userLoader.getInMemoryOnly(userId.toString()) ?? (await this.userLoader.get(userId))
if (!getUserResult) {
throw new EntityNotFoundError({ message: 'User not found', details: { id: userId } })
}
requestContext.logger.debug({ userId }, 'Resolved user')
return getUserResult
}
async deleteUser(requestContext: RequestContext, userId: string): Promise<void> {
await this.userRepository.deleteUser(userId)
await this.userLoader.invalidateCacheFor(userId.toString())
requestContext.logger.info({ userId }, 'Deleted user')
}
async updateUser(requestContext: RequestContext, userId: string, updatedData: UserUpdateDTO) {
await this.userRepository.updateUser(userId, updatedData)
await this.userLoader.invalidateCacheFor(userId)
requestContext.logger.info({ userId }, 'Updated user')
}
async getUsers(requestContext: RequestContext, userIds: string[]): Promise<UserDTO[]> {
const users = await this.userRepository.getUsers(userIds)
requestContext.logger.debug({ userIds }, 'Resolved users')
return users
}
async findUserById(requestContext: RequestContext, id: string): Promise<UserDTO | null> {
const getUserResult =
this.userLoader.getInMemoryOnly(id.toString()) ?? (await this.userLoader.get(id))
if (getUserResult) {
requestContext.logger.debug({ id }, 'Resolved user')
return getUserResult
}
requestContext.logger.debug({ id }, 'User does not exist')
return null
}
}