All files / src/repositories role.repository.ts

86.36% Statements 19/22
0% Branches 0/8
50% Functions 1/2
85% Lines 17/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 731x 1x             1x 1x 1x   1x                           19x   19x   19x   19x 19x       19x       19x       19x     19x     19x     19x                                    
import { Getter, inject } from '@loopback/core';
import {
  BelongsToAccessor,
  HasManyRepositoryFactory,
  Options,
  repository,
} from '@loopback/repository';
import { CompanyRepository, RoleRightsRepository, UserRepository } from '.';
import { LogChainDataSource } from '../datasources';
import { Company, Module, Role, RoleRelations, RoleRights, User } from '../models';
import { BaseCrudRepository } from './base-crud.repository.base';
 
export class RoleRepository extends BaseCrudRepository<
  Role,
  typeof Role.prototype.id,
  RoleRelations
> {
  public readonly creator: BelongsToAccessor<User, typeof User.prototype.id>;
  public readonly modifier: BelongsToAccessor<User, typeof User.prototype.id>;
  public readonly users: HasManyRepositoryFactory<User, typeof User.prototype.id>;
  public readonly role_rights: HasManyRepositoryFactory<RoleRights, typeof RoleRights.prototype.id>;
  public readonly company: BelongsToAccessor<Company, typeof Company.prototype.id>;
 
  constructor(
    @inject('datasources.logchain') dataSource: LogChainDataSource,
    @repository.getter('UserRepository')
    protected user_repo_getter: Getter<UserRepository>,
    @repository.getter('RoleRightsRepository')
    protected rr_repo_getter: Getter<RoleRightsRepository>,
    @repository.getter('CompanyRepository')
    protected comp_repo_getter: Getter<CompanyRepository>,
  ) {
    super(Role, dataSource);
    this.role_rights = this.createHasManyRepositoryFactoryFor(
      'role_rights',
      rr_repo_getter,
    );
    this.registerInclusionResolver(
      'role_rights',
      this.role_rights.inclusionResolver,
    );
    this.users = this.createHasManyRepositoryFactoryFor(
      'users',
      user_repo_getter,
    );
    this.registerInclusionResolver('users', this.users.inclusionResolver);
 
    // register creator relation
    this.creator = this.createBelongsToAccessor('creator', user_repo_getter);
 
    // register modifier relation
    this.modifier = this.createBelongsToAccessor('modifier', user_repo_getter);
 
    // register company relation
    this.company = this.createBelongsToAccessor('company', comp_repo_getter);
  }
 
  /**
   * Get permissions of current user.
   * @param id The role id.
   * @param options The options.
   */
  async getPermissions(options?: Options) {
    const sql = `SELECT * FROM get_permissions_by_role($1, $2);`;
    const resl = await this.execute(
      sql,
      [this.current_user?.role_id, this.current_user?.company_id],
      options,
    );
    return resl as Module[];
  }
}