src/user/service/user.service.ts
Properties |
|
Methods |
|
constructor(cognito: AWS.CognitoIdentityServiceProvider, config: ConfigService, userRepository: UserRepository, positiveInfoRepository: PositiveInfoRepository, profileRepository: ProfileRepository)
|
||||||||||||||||||
|
Defined in src/user/service/user.service.ts:27
|
||||||||||||||||||
|
Parameters :
|
| clearPositiveInfo | ||||||
clearPositiveInfo(userId: string)
|
||||||
|
Defined in src/user/service/user.service.ts:38
|
||||||
|
Parameters :
Returns :
any
|
| Async deleteUser | ||||||
deleteUser(user: UserEntity)
|
||||||
|
Defined in src/user/service/user.service.ts:110
|
||||||
|
Parameters :
Returns :
Promise<DeleteResult>
|
| findByDeviceId | ||||||
findByDeviceId(deviceId: string)
|
||||||
|
Defined in src/user/service/user.service.ts:53
|
||||||
|
Parameters :
Returns :
Promise<UserEntity>
|
| findByEmail | ||||||
findByEmail(email: string)
|
||||||
|
Defined in src/user/service/user.service.ts:45
|
||||||
|
Parameters :
Returns :
Promise<UserEntity>
|
| findById | ||||||
findById(id: string)
|
||||||
|
Defined in src/user/service/user.service.ts:49
|
||||||
|
Parameters :
Returns :
Promise<UserEntity>
|
| Async findByIdOrFail | ||||||
findByIdOrFail(id: string)
|
||||||
|
Defined in src/user/service/user.service.ts:57
|
||||||
|
Parameters :
Returns :
Promise<UserEntity>
|
| Async saveUser |
saveUser(id: string, email: string)
|
|
Defined in src/user/service/user.service.ts:67
|
|
Returns :
Promise<UserEntity>
|
| Async updateUserDeviceId |
updateUserDeviceId(userId: string, deviceId: string)
|
|
Defined in src/user/service/user.service.ts:93
|
|
Returns :
Promise<UpdateResult>
|
| Async updateUserEmail |
updateUserEmail(id: string, email: string)
|
|
Defined in src/user/service/user.service.ts:79
|
|
Returns :
Promise<UpdateResult>
|
| Private Readonly logger |
Default value : new Logger(UserService.name)
|
|
Defined in src/user/service/user.service.ts:27
|
import {
Inject,
Injectable,
BadRequestException,
Logger,
} from '@nestjs/common';
import { UpdateResult, DeleteResult } from 'typeorm';
import { UserRepository } from '../repository/user.repository';
import { UserEntity } from '../entity/user.entity';
import {
UPDATE_USER_EMAIL_FAILED,
USER_NOT_FOUND,
SAVE_USER_FAILED,
DELETE_USER_FROM_COGNITO_FAILED,
DELETE_USER_FROM_DB_FAILED,
UPDATE_USER_DEVICE_ID_FAILED,
} from '../../common/error/keys';
import * as AWS from 'aws-sdk';
import { ConfigService } from '@nestjs/config';
import { DICTIONARY } from '../../common/constant/dictionary.constant';
import { PositiveInfoRepository } from '../repository/positive-info.repository';
import { ProfileRepository } from '../repository/profile.repository';
import { Encrypter } from '../../common/helper/encrypter';
@Injectable()
export class UserService {
private readonly logger = new Logger(UserService.name);
constructor(
@Inject(AWS.CognitoIdentityServiceProvider)
private readonly cognito: AWS.CognitoIdentityServiceProvider,
@Inject(DICTIONARY.CONFIG) private readonly config: ConfigService,
@Inject(UserRepository) private readonly userRepository: UserRepository,
private readonly positiveInfoRepository: PositiveInfoRepository,
private readonly profileRepository: ProfileRepository,
) { }
clearPositiveInfo(userId: string) {
return Promise.all([
this.positiveInfoRepository.clearEverythingForUsers([userId]),
this.profileRepository.disableAutomatedEmergencyForUsers([userId]),
]);
}
findByEmail(email: string): Promise<UserEntity> {
return this.userRepository.findByEmail(Encrypter.encrypt(email));
}
findById(id: string): Promise<UserEntity> {
return this.userRepository.findById(id);
}
findByDeviceId(deviceId: string): Promise<UserEntity> {
return this.userRepository.findByDeviceId(deviceId);
}
async findByIdOrFail(id: string): Promise<UserEntity> {
return this.findById(id).then((data) => {
if (!data) {
throw new BadRequestException(USER_NOT_FOUND);
}
return data;
});
}
async saveUser(id: string, email: string): Promise<UserEntity> {
return this.userRepository
.save({
id,
email,
})
.catch((error) => {
this.logger.error(error);
throw new BadRequestException(SAVE_USER_FAILED);
});
}
async updateUserEmail(id: string, email: string): Promise<UpdateResult> {
return this.userRepository
.update(
{
id,
},
{ email }
)
.catch((error) => {
this.logger.error(error);
throw new BadRequestException(UPDATE_USER_EMAIL_FAILED);
});
}
async updateUserDeviceId(
userId: string,
deviceId: string
): Promise<UpdateResult> {
return this.userRepository
.update(
{
id: userId,
},
{ deviceId }
)
.catch((error) => {
this.logger.error(error);
throw new BadRequestException(UPDATE_USER_DEVICE_ID_FAILED);
});
}
async deleteUser(user: UserEntity): Promise<DeleteResult> {
await new Promise((resolve, reject) => {
this.cognito.adminDeleteUser(
{
UserPoolId: this.config.get('authorization.userPoolId'),
Username: user.id,
},
(error, result) => {
if (!error) {
resolve(result);
}
reject(error);
}
);
}).catch((error) => {
this.logger.error(error, JSON.stringify(user));
throw new BadRequestException(DELETE_USER_FROM_COGNITO_FAILED, error);
});
return this.userRepository.delete(user.id).catch((error) => {
this.logger.error(error);
throw new BadRequestException(DELETE_USER_FROM_DB_FAILED, error);
});
}
}