File

src/application-profile/application-profile.service.ts

Index

Methods

Constructor

constructor(dataSource: DataSource, profileRepository: Repository, applicationRepository: Repository<Application>)
Parameters :
Name Type Optional
dataSource DataSource No
profileRepository Repository<ApplicationProfile> No
applicationRepository Repository<Application> No

Methods

Async addProfile
addProfile(applicationId: number, body: ApplicationProfileBodyDto)
Parameters :
Name Type Optional
applicationId number No
body ApplicationProfileBodyDto No
Returns : unknown
Async delete
delete(applicationId: number, id: number)
Parameters :
Name Type Optional
applicationId number No
id number No
Returns : any
Async disable
disable(applicationId: number, id: number)
Parameters :
Name Type Optional
applicationId number No
id number No
Returns : Promise<ApplicationProfile>
Async enable
enable(applicationId: number, id: number)
Parameters :
Name Type Optional
applicationId number No
id number No
Returns : Promise<ApplicationProfile>
getProfilesByApplicationId
getProfilesByApplicationId(applicationId: number)
Parameters :
Name Type Optional
applicationId number No
Returns : any
Async profileExists
profileExists(applicationId: number, name: string)
Parameters :
Name Type Optional
applicationId number No
name string No
Returns : unknown
import { Injectable, NotFoundException } from '@nestjs/common';
import { DataSource, Repository, ILike } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { ApplicationProfile } from 'src/entities/application-profile';
import { ApplicationUser } from 'src/entities/application-user';
import { ApplicationProfileBodyDto } from './dto/application-profile.dto';
import { GeneralException } from 'src/util/general.exception';
import { Application } from 'src/entities/application';

@Injectable()
export class ApplicationProfileService {
  constructor(
    private dataSource: DataSource,
    @InjectRepository(ApplicationProfile)
    private profileRepository: Repository<ApplicationProfile>,
    @InjectRepository(Application)
    private applicationRepository: Repository<Application>,
  ) {}

  async addProfile(applicationId: number, body: ApplicationProfileBodyDto) {
    const application = await this.applicationRepository.countBy({ id: applicationId })

    if(!application) {
      throw new NotFoundException({ messageKey: 'application_not_found'})
    }

    const name = body.name.toLowerCase()

    const applicationProfileName = await this.profileExists(applicationId, name)

    if(applicationProfileName) {
      throw new GeneralException('application_profile_already_exists')
    }

    const saveResult = await this.profileRepository.insert({
      applicationId,
      name,
    })
    return saveResult.raw[0].id
  }

  async profileExists(applicationId: number, name: string) {
    return await this.profileRepository.findOneBy({
      applicationId,
      name: ILike(name),
    })
  }

  getProfilesByApplicationId(applicationId: number) {
    return this.profileRepository.find({
      where: {
        applicationId,
      },
      order: {
        name: 'ASC'
      }
    });
  }

  async enable(applicationId: number, id: number): Promise<ApplicationProfile> {
    const profile = await this.profileRepository.findOneBy({ id, applicationId })
    if(!profile) {
      throw new NotFoundException({ messageKey: 'application_profile_not_found'})
    }

    return this.profileRepository.save({
      ...profile,
      enabled: true,
    })
  }

  async disable(applicationId: number, id: number): Promise<ApplicationProfile> {
    const profile = await this.profileRepository.findOneBy({ id, applicationId })
    if(!profile) {
      throw new NotFoundException({ messageKey: 'application_profile_not_found'})
    }

    return this.profileRepository.save({
      ...profile,
      enabled: false,
    })
  }

  async delete(applicationId: number, id: number) {
    const profile = await this.profileRepository.countBy({ applicationId, id })

    if (!profile) {
      throw new NotFoundException({ messageKey: 'application_profile_not_found'})
    }

    const queryRunner = await this.dataSource.createQueryRunner()
    const applicationUser = await queryRunner.manager
      .findOneBy(ApplicationUser, {
        applicationProfileId: id,
      });

    if (applicationUser) {
      throw new GeneralException('application_profile_delete_error_user_linked');
    }

    await this.profileRepository.delete({ applicationId, id });
    await queryRunner.release()
  }
}

results matching ""

    No results matching ""