File

lib/common/user/user-service-basic.service.ts

Description

Provides methods to interact with the UserEntity repository.

Extends

UserService

Index

Methods

Constructor

constructor(userRep: Repository<UserEntity>)
Parameters :
Name Type Optional
userRep Repository<UserEntity> No

Methods

Async create
create(user: User)
Inherited from UserService
Defined in UserService:84

Create a new user.

Parameters :
Name Type Optional Description
user User No
  • The user object to create.

The created user.

Async findById
findById(id: string)
Inherited from UserService
Defined in UserService:55

Find a user by their ID.

Parameters :
Name Type Optional Description
id string No
  • The user's ID.

The user found or undefined if not found.

Async findByLogin
findByLogin(login: string)
Inherited from UserService
Defined in UserService:42

Find a user by their login.

Parameters :
Name Type Optional Description
login string No
  • The user's login.

The user found or undefined if not found.

Async removeById
removeById(id: string)
Inherited from UserService
Defined in UserService:96

Remove a user by their ID.

Parameters :
Name Type Optional Description
id string No
  • The user's ID.

The removed user.

Async updateById
updateById(id: string, user: User)
Inherited from UserService
Defined in UserService:69

Update a user by their ID.

Parameters :
Name Type Optional Description
id string No
  • The user's ID.
user User No
  • The user object with updated properties.

The updated user.

import { Injectable, NotFoundException } from "@nestjs/common";
import { User, UserService } from "./user.types";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { UserEntity } from "./entity/user.entity";
import { USER_RELATIONS } from "./user.constants";

/**
 * Provides methods to interact with the UserEntity repository.
 */
@Injectable()
export class BasicUserService extends UserService {
  constructor(
    @InjectRepository(UserEntity)
    private readonly userRep: Repository<UserEntity>,
  ) {
    super();
  }

  /**
   * Find a user by their login.
   * @async
   * @param {string} login - The user's login.
   * @returns {Promise<UserEntity | undefined>} The user found or undefined if not found.
   */
  async findByLogin(login: string): Promise<UserEntity> {
    return await this.userRep.findOne({
      where: { login, active: true },
      relations: USER_RELATIONS,
    });
  }

  /**
   * Find a user by their ID.
   * @async
   * @param {string} id - The user's ID.
   * @returns {Promise<UserEntity | undefined>} The user found or undefined if not found.
   */
  async findById(id: string): Promise<UserEntity> {
    return await this.userRep.findOne({
      where: { id },
      relations: USER_RELATIONS,
    });
  }

  /**
   * Update a user by their ID.
   * @async
   * @param {string} id - The user's ID.
   * @param {User} user - The user object with updated properties.
   * @returns {Promise<UserEntity>} The updated user.
   */
  async updateById(id: string, user: User): Promise<UserEntity> {
    user.id = id;
    if (user.login) {
      delete user.login;
    }
    await this.userRep.save(user);
    return await this.findById(id);
  }

  /**
   * Create a new user.
   * @async
   * @param {User} user - The user object to create.
   * @returns {Promise<UserEntity>} The created user.
   */
  async create(user: User): Promise<UserEntity> {
    const newUser = this.userRep.create(user);
    return await this.userRep.save(newUser);
  }

  /**
   * Remove a user by their ID.
   * @async
   * @param {string} id - The user's ID.
   * @returns {Promise<UserEntity>} The removed user.
   * @throws {NotFoundException} If the user with the specified ID is not found.
   */
  async removeById(id: string): Promise<UserEntity> {
    const user = await this.findById(id);
    if (!user) {
      throw new NotFoundException(`User with ID ${id} not found`);
    }
    await this.userRep.remove(user);
    return user;
  }
}

results matching ""

    No results matching ""