I can't understand one thing.
When creating a user, I need to add several fields in different tables in the database.
so I converted my createUser method to user.service.ts:
async createUser(
userRegisterDto: UserRegisterDto,
): Promise<[UserEntity, UserAuthEntity, UserSalaryEntity]> {
const user = this.userRepository.create(userRegisterDto);
await this.userRepository.save(user);
const userRegisterDtoWithUserEntity = { ...userRegisterDto, user };
const userAuth = this.userAuthRepository.create(
userRegisterDtoWithUserEntity,
);
const userSalary = this.userSalaryRepository.create(
userRegisterDtoWithUserEntity,
);
await Promise.all([
this.userAuthRepository.save(userAuth),
this.userSalaryRepository.save(userSalary),
]);
return [user, userAuth, userSalary];
}
@Post('register')
@HttpCode(HttpStatus.OK)
@ApiOkResponse({
type: [UserDto, UserAuthDto, UserSalaryDto],
description: 'Successfully Registered',
})
async userRegister(
@Body() userRegisterDto: UserRegisterDto
): Promise<[UserDto, UserAuthDto, UserSalaryDto]> {
const [
createdUser,
createdUserAuth,
createdUserSalary,
] = await this.userService.createUser(userRegisterDto);
return [
createdUser.toDto(),
createdUserAuth.toDto(),
createdUserSalary.toDto(),
];
}
Is this written correctly? Can it be written better? In response, I would like to show data from tables that were created.