'Can you guys help me how I can do refactoring code in nestjs
Hey guys I'm trying to refactor the two methods. As you can see in the code below, 'checkKioskUserPhone' and 'sendKioskUserPin' has almost similar logics. The only difference is the below part.
if (isConfirmedAuthCode && isSetPin) {
await this.userService.sendPin(user.id, Builder(AuthorizePhoneDto).phone(user.phone).build());
}
So my approach was to create another method that can combine common parts of the methods. and remove the common part in each method.. Hmm..but can't figure out how exactly.. Can you guys help me?!...
private async commonMethods(user) {
if (!user) {
throw new NotFoundException('User not found');
}
if (user.provider !== Provider.KIOSK) {
throw new ConflictException('You are already joined App.');
}
const isConfirmedAuthCode = user.authCode === 'OK' ? true : false;
const isSetPin = user.pin ? true : false;
if (!isConfirmedAuthCode && !isSetPin) {
await this.userService.authenticatePhone(user.id, Builder(AuthorizePhoneDto).phone(user.phone).build());
}
}
async checkKioskUserPhone(kioskLoginDto: KioskLoginDto): Promise<ResponseDto<UserAuthDto>> {
const user = await this.userService.findOne({ where: { phone: kioskLoginDto.phone } });
if (!user) {
throw new NotFoundException('User not found');
}
if (user.provider !== Provider.KIOSK) {
throw new ConflictException('You are already joined App.');
}
const isConfirmedAuthCode = user.authCode === 'OK' ? true : false;
const isSetPin = user.pin ? true : false;
if (!isConfirmedAuthCode && !isSetPin) {
await this.userService.authenticatePhone(user.id, Builder(AuthorizePhoneDto).phone(user.phone).build());
}
const jwtInfo = await this.createToken(this.removeCredentialField(user));
return Builder<ResponseDto<UserAuthDto>>(ResponseDto)
.result(Builder(UserAuthDto).isConfirmedAuthCode(isConfirmedAuthCode).isSetPin(isSetPin).jwtInfo(jwtInfo).build())
.build();
}
async sendKioskUserPin(kioskLoginDto: KioskLoginDto): Promise<ResponseDto<UserAuthDto>> {
const user = await this.userService.findOne({ where: { phone: kioskLoginDto.phone } });
if (!user) {
throw new NotFoundException('User not found');
}
if (user.provider !== Provider.KIOSK) {
throw new ConflictException('You are already joined App.');
}
const isConfirmedAuthCode = user.authCode === 'OK' ? true : false;
const isSetPin = user.pin ? true : false;
if (!isConfirmedAuthCode && !isSetPin) {
await this.userService.authenticatePhone(user.id, Builder(AuthorizePhoneDto).phone(user.phone).build());
}
if (isConfirmedAuthCode && isSetPin) {
await this.userService.sendPin(user.id, Builder(AuthorizePhoneDto).phone(user.phone).build());
}
const jwtInfo = await this.createToken(this.removeCredentialField(user));
return Builder<ResponseDto<UserAuthDto>>(ResponseDto)
.result(Builder(UserAuthDto).isConfirmedAuthCode(isConfirmedAuthCode).isSetPin(isSetPin).jwtInfo(jwtInfo).build())
.build();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
