import { Injectable } from '@nestjs/common' import { InjectRepository } from '@nestjs/typeorm' import { Repository } from 'typeorm' import { UserEntity } from '../../entity/user.entity' @Injectable() export class UserService { constructor( @InjectRepository(UserEntity) private readonly userRepository: Repository, ) { } async isSuperuser(user: UserEntity): Promise { return user.roles.includes('superuser') } async find(start: number, limit: number, sort: any = {}): Promise<[UserEntity[], number]> { const qb = this.userRepository.createQueryBuilder('u') // TODO readd sort qb.skip(start) qb.take(limit) return await qb.getManyAndCount() } async findById(id: string): Promise { const user = await this.userRepository.findOne(id); if (!user) { throw new Error('no user found') } return user } async findByUsername(username: string): Promise { const user = await this.userRepository.findOne({ username, }) if (!user) { throw new Error('no user found') } return user } async findByEmail(email: string): Promise { const user = await this.userRepository.findOne({ email, }) if (!user) { throw new Error('no user found') } return user } }