@nestjs/common#BadGatewayException TypeScript Examples
The following examples show how to use
@nestjs/common#BadGatewayException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: contact-info.service.ts From nestjs-starter with MIT License | 6 votes |
async createAddress(userId: number, dto: CreateContactAddressDto): Promise<ContactAddress> {
const user = await this.userService.findOne(userId);
if (!user) {
throw new NotFoundException('User with ID provided does not exist.');
}
if (!user.contactInfo) {
throw new BadGatewayException('This user does not have contact information, please add information first.');
}
const contactAddress = this.contactAddressRepository.create({ ...dto, contactInfo: user.contactInfo });
const result = await this.contactAddressRepository.save(contactAddress);
delete result.contactInfo;
delete result.id;
return result;
}
Example #2
Source File: contact-info.service.ts From nestjs-starter with MIT License | 6 votes |
async createPhone(userId: number, dto: CreateContactPhoneDto): Promise<ContactPhone> {
const user = await this.userService.findOne(userId);
if (!user) {
throw new NotFoundException('User with ID provided does not exist.');
}
if (!user.contactInfo) {
throw new BadGatewayException('This user does not have contact information, please add information first.');
}
const contactPhone = this.contactPhoneRepository.create({ ...dto, contactInfo: user.contactInfo });
const result = await this.contactPhoneRepository.save(contactPhone);
delete result.contactInfo;
delete result.id;
return result;
}
Example #3
Source File: users.service.ts From nestjs-starter with MIT License | 6 votes |
/**
* Create users in batch
* @param dto Array of CreateUserDto
*/
async createBatch(dto: CreateUserDto[]): Promise<User[]> {
const users = this.userRepository.create(dto);
const result = await this.userRepository.save(users).catch((err) => {
throw new BadGatewayException('Something happened', err);
});
for (const res of result) {
delete res.password;
}
return result;
}
Example #4
Source File: users.service.ts From nestjs-starter with MIT License | 6 votes |
/**
* Get by username or email
* @param data { username: string, email: string }
*/
async getByUser(data: { username?: string; email?: string }): Promise<User> {
return this.userRepository
.createQueryBuilder('user')
.where(data)
.addSelect('user.password')
.addSelect('user.enabled')
.getOne()
.catch((err) => {
throw new BadGatewayException('Something happened', err);
});
}
Example #5
Source File: users.service.ts From nestjs-starter with MIT License | 6 votes |
/**
* Get user by id
* @param id User id
*/
async getOneById(id: number): Promise<User | null> {
return (
(await this.userRepository.findOne(id).catch((err) => {
throw new BadGatewayException('Something happened', err);
})) || null
);
}
Example #6
Source File: users.service.ts From nestjs-starter with MIT License | 6 votes |
/**
* Get users by ids
* @param ids Get user by array of Ids
*/
async getByIds(ids: number[]): Promise<User[]> {
return await this.userRepository.findByIds(ids).catch((err) => {
throw new BadGatewayException('Something happened', err);
});
}
Example #7
Source File: users.service.ts From nestjs-starter with MIT License | 6 votes |
/**
* Update users in batch
* @param dtos Array of UpdateUserDto
*/
async updateMany(dtos: UpdateUserDto[]): Promise<User[]> {
const updatedUsers = [];
for (const dto of dtos) {
if (dto.id) {
const user = await this.userRepository.findOne(dto.id);
if (!!user) {
delete dto.id; // Deleting this input to avoid issues with entity
const editedUser = Object.assign(user, dto);
updatedUsers.push(editedUser);
}
}
}
const result = await this.userRepository.save<User>(updatedUsers).catch((err) => {
throw new BadGatewayException('Something happened', err);
});
for (const res of result) {
delete res.password;
}
return result;
}
Example #8
Source File: users.service.ts From nestjs-starter with MIT License | 6 votes |
/**
* Soft delete an user by id
* @param id User id
*/
async softDelete(id: number) {
const user = await this.userRepository.findOne(id);
return await this.userRepository.softRemove(user).catch((err) => {
throw new BadGatewayException('Something happened', err);
});
}
Example #9
Source File: users.service.ts From nestjs-starter with MIT License | 6 votes |
/**
* Hard delete an user by id
* @param id User id
*/
async delete(id: number) {
return await this.userRepository.delete(id).catch((err) => {
throw new BadGatewayException('Something happened', err);
});
}
Example #10
Source File: users.service.ts From nestjs-starter with MIT License | 6 votes |
/**
* Hard delete of users in batch by ids
* @param ids Array of user ids
*/
async deleteMany(ids: number[]) {
return await this.userRepository.delete(ids).catch((err) => {
throw new BadGatewayException('Something happened', err);
});
}
Example #11
Source File: users.service.ts From nestjs-starter with MIT License | 6 votes |
/**
* Restore soft-deleted user by id
* @param id User id
*/
async restore(id: number) {
const user = await this.userRepository.findOne({ withDeleted: true });
return await this.userRepository.recover(user).catch((err) => {
throw new BadGatewayException('Something happened', err);
});
}
Example #12
Source File: users.service.ts From nestjs-starter with MIT License | 6 votes |
/**
* Disable an user
* @param id User id
*/
async disable(id: number) {
return await this.userRepository
.createQueryBuilder('user')
.addSelect('user.enabled')
.update()
.set({ enabled: false })
.where('id = :id', { id })
.execute()
.catch((err) => {
throw new BadGatewayException('Something happened', err);
});
}
Example #13
Source File: users.service.ts From nestjs-starter with MIT License | 6 votes |
/**
* Disable users in batch by ids
* @param ids Array of user ids
*/
async disableMany(ids: number[]) {
return await this.userRepository
.createQueryBuilder('user')
.addSelect('user.enabled')
.update()
.set({ enabled: false })
.where('id IN (:...ids)', { ids })
.execute()
.catch((err) => {
throw new BadGatewayException('Something happened', err);
});
}
Example #14
Source File: users.service.ts From nestjs-starter with MIT License | 6 votes |
/**
* Enable user by id
* @param id User id
*/
async enable(id: number) {
return await this.userRepository
.createQueryBuilder('user')
.addSelect('user.enabled')
.update()
.set({ enabled: true })
.where('id = :id', { id })
.execute()
.catch((err) => {
throw new BadGatewayException('Something happened', err);
});
}
Example #15
Source File: users.service.ts From nestjs-starter with MIT License | 6 votes |
/**
* Enable users in batch by ids
* @param ids Array of user ids
*/
async enableMany(ids: number[]) {
return await this.userRepository
.createQueryBuilder('user')
.addSelect('user.enabled')
.update()
.set({ enabled: true })
.where('id IN (:...ids)', { ids })
.execute()
.catch((err) => {
throw new BadGatewayException('Something happened', err);
});
}